render_macros |
---|
Adds Handlebars helpers for generating JWT, claims and JWKS.
For Maven users:
<dependency>
<groupId>org.wiremock.extensions</groupId>
<artifactId>wiremock-jwt-extension</artifactId>
<version>0.2.0</version>
</dependency>
For Gradle users:
dependencies {
implementation 'org.wiremock.extensions:wiremock-jwt-extension:0.2.0'
}
new WireMockServer(wireMockConfig().extensions(JwtExtensionFactory.class));
wm.stubFor(
get(urlPathEqualTo("/.well-known/jwks.json"))
.willReturn(okJson("{{jwks}}").withTransformers("response-template")));
wm.stubFor(
get(urlPathEqualTo("/oauth/token"))
.willReturn(okJson("{{jwt}}").withTransformers("response-template")));
The jwt
helper has a number of parameters you can use to customise the generated token.
You can customise expiry term either by setting the maxAge
parameter e.g.
{% raw %}
{% endraw %}
or by setting an absolute expiry date e.g.
{% raw %}
{% endraw %}
You can similarly set the nbf
(not before) date:
{% raw %}
{% endraw %}
Standard claims can be set as follows.
Issuer:
{% raw %}
{% endraw %}
Audience:
{% raw %}
{% endraw %}
Subject:
{% raw %}
{% endraw %}
You can also set any custom claim you wish via named parameters e.g.
{% raw %}
{% endraw %}
By setting the alg
parameter, the token can be signed using the public/private key
algorithm:
{% raw %}
{% endraw %}
For clients to be able to validate JWTs, they need to be able to retrieve either the shared secret or the public key, depending on the signing algorithm.
The keys used to sign tokens for a particular mock API can be retrieved via the settings admin API resource. To fetch these via curl, you can do the following:
curl http://localhost:8080/__admin/settings
This will return a JSON document like this, from which you can retrieve the any of the keys:
{
"settings": {
"extended": {
"jwt": {
"hs256Secret": "...",
"rs256PublicKeyId": "...",
"rs256PublicKey": "-----BEGIN RSA PUBLIC KEY-----\n...\n-----END RSA PUBLIC KEY-----\n",
"rs256PrivateKey": "-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----\n"
}
}
}
}
When using RS256
(public/private key) signing, it is common for clients to fetch
the public key for verification via a JSON Web Key Set (JWKS) endpoint. You serve
a JWKS from your mock API simply by adding a stub containing the following response
body (with templating enabled):
{% raw %}
{% endraw %}