You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 11e96ad
Browse filesBrowse the repository at this point in the historyBrowse files
By using a `KeyProvider` the library delegates the decision of which key to use in each case to the user. For the verification process, this means that the provider will be asked for a `PublicKey` with a given **Key Id** value. Your provider implementation should have the logic to fetch the right key, for example by parsing a JWKS file from a public domain like [auth0/jwks-rsa-java](https://github.com/auth0/jwks-rsa-java) does. For the signing process, this means that the provider will be asked for a `PrivateKey` and it's associated **Key Id**, so it can set it in the Token's header for future verification in the same way. Check the [IETF draft](https://tools.ietf.org/html/rfc7517) for more information on how to implement this.
67
+
By using a `KeyProvider` you can change in runtime the key used either to verify the token signature or to sign a new token for RSA or ECDSA algorithms. This is achieved by implementing either `RSAKeyProvider` or `ECDSAKeyProvider` methods:
68
+
69
+
-`getPublicKeyById(String kid)`: Its called during token signature verification and it should return the key used to verify the token. If key rotation is being used, e.g. [JWK](https://tools.ietf.org/html/rfc7517) it can fetch the correct rotation key using the id. (Or just return the same key all the time).
70
+
-`getPrivateKey()`: Its called during token signing and it should return the key that will be used to sign the JWT.
71
+
-`getPrivateKeyId()`: Its called during token signing and it should return the id of the key that identifies the one returned by `getPrivateKey()`. This value is preferred over the one set in the `JWTCreator.Builder#withKeyId(String)` method. If you don't need to set a `kid` value avoid instantiating an Algorithm using a `KeyProvider`.
72
+
68
73
69
74
The following snippet uses example classes showing how this would work:
Copy file name to clipboardExpand all lines: lib/src/main/java/com/auth0/jwt/JWTCreator.java
+2-1Lines changed: 2 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,7 @@ public Builder withHeader(Map<String, Object> headerClaims) {
77
77
78
78
/**
79
79
* Add a specific Key Id ("kid") claim to the Header.
80
+
* If the {@link Algorithm} used to sign this token was instantiated with a KeyProvider, the 'kid' value will be taken from that provider and this one will be ignored.
* Getter for the Public Key instance, used to verify the signature.
15
+
* Getter for the Public Key instance with the given Id. Used to verify the signature on the JWT verification stage.
16
16
*
17
17
* @param keyId the Key Id specified in the Token's Header or null if none is available. Provides a hint on which Public Key to use to verify the token's signature.
18
18
* @return the Public Key instance
19
19
*/
20
-
UgetPublicKey(StringkeyId);
20
+
UgetPublicKeyById(StringkeyId);
21
21
22
22
/**
23
-
* Getter for the Private Key instance, used to sign the content.
23
+
* Getter for the Private Key instance. Used to sign the content on the JWT signing stage.
24
24
*
25
25
* @return the Private Key instance
26
26
*/
27
27
RgetPrivateKey();
28
28
29
29
/**
30
-
* Getter for the Id of the Private Key used to sign the tokens. This represents the `kid` claim and will be placed in the Header if no other "Key Id" has been set already.
30
+
* Getter for the Id of the Private Key used to sign the tokens. This represents the `kid` claim and will be placed in the Header.
31
31
*
32
-
* @return the Key Id that identifies the Signing Key or null if it's not specified.
32
+
* @return the Key Id that identifies the Private Key or null if it's not specified.
0 commit comments