This is the reverse of generating a JWT - here we’re reading claims out of a token the client sent us, not
signing a new one.
The problem
A JWT is made up of three base64url-encoded segments separated by. - a header, a payload and a signature. The
claims you want to use in your response live in the payload segment, so to get at them you need to:
- Read the token out of the request
- Split it into its three segments and take the payload (the second one)
- Base64-decode the payload
- Parse the decoded string as JSON so its claims can be referenced
Example
Suppose an incoming request carries its JWT in anX-JWT-Assertion header, and you want to echo the token’s
sub and a custom apiName claim back in the response body.
Enable templating on the stub’s response and use the following as the body:
How it works
request.headers.[X-JWT-Assertion].[0]reads the first value of theX-JWT-Assertionheader. The square-bracket form is used because the header name contains a hyphen.splitdivides the token string on., giving you the three JWT segments as a list.lookupretrieves a value by key, using the same square-bracket convention asrequest.headers.[key]above to treat the bracketed content as a single literal key rather than a dotted property path. This is what lets you pull out the segment at array index1(the payload), and later themy.org/claims/apinameclaim, even though both contain characters (/, digits-only) that wouldn’t work with plain dot notation.base64withdecode=truedecodes the payload segment back into its raw JSON string.parseJsonturns that JSON string into an object so its claims can be accessed directly.- Claims with names that are valid identifiers, like
sub, can be read with plain dot notation (jwtClaims.sub). valis used throughout to assign each intermediate result to a variable, andtrimstrips the surrounding whitespace left by thevallines from the final output.