Skip to content

Commit c76d073

Browse files
committed
add KeyProvider usage to the readme
1 parent b2eac86 commit c76d073

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,57 @@ The library implements JWT Verification and Signing using the following algorith
4646

4747
## Usage
4848

49+
### Pick the Algorithm
50+
51+
The Algorithm defines how a token is signed and verified. It can be instantiated with the raw value of the secret in the case of HMAC algorithms, or the key pairs or `KeyProvider` in the case of RSA and ECDSA algorithms. Once created, the instance is reusable for token signing and verification operations.
52+
53+
#### Using static secrets or keys:
54+
55+
```java
56+
//HMAC
57+
Algorithm algorithmHS = Algorithm.HMAC256("secret");
58+
59+
//RSA
60+
RSAPublicKey publicKey = //Get the key instance
61+
RSAPrivateKey privateKey = //Get the key instance
62+
Algorithm algorithmRS = Algorithm.RSA256(publicKey, privateKey);
63+
```
64+
65+
#### Using a KeyProvider:
66+
67+
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.
68+
69+
The following snippet uses example classes showing how this would work:
70+
71+
72+
```java
73+
final MyOwnJwkProvider jwkProvider = new MyOwnJwkProvider("{JWKS_FILE_HOST}");
74+
final RSAPrivateKey signingKey = //Get the key instance
75+
final String signingKeyId = //Create an Id for the above key
76+
77+
RSAKeyProvider keyProvider = new RSAKeyProvider() {
78+
@Override
79+
public RSAPublicKey getPublicKey(String keyId) {
80+
//Value might be null if it wasn't defined in the Token's header
81+
Jwk jwk = jwkProvider.get(keyId);
82+
return (RSAPublicKey) jwk.getPublicKey();
83+
}
84+
85+
@Override
86+
public RSAPrivateKey getPrivateKey() {
87+
return signingKey;
88+
}
89+
90+
@Override
91+
public String getSigningKeyId() {
92+
return signingKeyId;
93+
}
94+
};
95+
Algorithm algorithm = Algorithm.RSA256(keyProvider);
96+
//Use the Algorithm to create and verify JWTs.
97+
```
98+
99+
49100
### Create and Sign a Token
50101

51102
You'll first need to create a `JWTCreator` instance by calling `JWT.create()`. Use the builder to define the custom Claims your token needs to have. Finally to get the String token call `sign()` and pass the `Algorithm` instance.

0 commit comments

Comments
 (0)