Skip to content

Commit bf7e7a7

Browse files
committed
update readme with algorithm changes
1 parent 4f8f54a commit bf7e7a7

1 file changed

Lines changed: 32 additions & 22 deletions

File tree

README.md

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
A Java implementation of [JSON Web Tokens (draft-ietf-oauth-json-web-token-08)](http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html).
1010

11-
If you're looking for an *Android* version of the JWT Decoder take a look at our [JWTDecode.Android](https://github.com/auth0/JWTDecode.Android) library.
11+
If you're looking for an **Android** version of the JWT Decoder take a look at our [JWTDecode.Android](https://github.com/auth0/JWTDecode.Android) library.
1212

1313
## Installation
1414

@@ -48,15 +48,18 @@ The library implements JWT Verification and Signing using the following algorith
4848

4949
### Create and Sign a Token
5050

51-
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.
51+
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.
5252

5353
* Example using `HS256`
5454

5555
```java
5656
try {
57+
Algorithm algorithm = Algorithm.HMAC256("secret");
5758
String token = JWT.create()
5859
.withIssuer("auth0")
59-
.sign(Algorithm.HMAC256("secret"));
60+
.sign(algorithm);
61+
} catch (UnsupportedEncodingException exception){
62+
//UTF-8 encoding not supported
6063
} catch (JWTCreationException exception){
6164
//Invalid Signing configuration / Couldn't convert Claims.
6265
}
@@ -65,11 +68,13 @@ try {
6568
* Example using `RS256`
6669

6770
```java
68-
PrivateKey key = //Get the key instance
71+
RSAPublicKey publicKey = //Get the key instance
72+
RSAPrivateKey privateKey = //Get the key instance
6973
try {
74+
Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
7075
String token = JWT.create()
7176
.withIssuer("auth0")
72-
.sign(Algorithm.RSA256(key));
77+
.sign(algorithm);
7378
} catch (JWTCreationException exception){
7479
//Invalid Signing configuration / Couldn't convert Claims.
7580
}
@@ -80,17 +85,20 @@ If a Claim couldn't be converted to JSON or the Key used in the signing process
8085

8186
### Verify a Token
8287

83-
You'll first need to create a `JWTVerifier` instance by calling `JWT.require()` and passing the Algorithm instance. If you require the token to have specific Claim values, use the builder to define them. The instance returned by the method `build()` is reusable, so you can define it once and use it to verify different tokens. Finally call `verifier.verify()` passing the token.
88+
You'll first need to create a `JWTVerifier` instance by calling `JWT.require()` and passing the `Algorithm` instance. If you require the token to have specific Claim values, use the builder to define them. The instance returned by the method `build()` is reusable, so you can define it once and use it to verify different tokens. Finally call `verifier.verify()` passing the token.
8489

8590
* Example using `HS256`
8691

8792
```java
8893
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
8994
try {
90-
JWTVerifier verifier = JWT.require(Algorithm.HMAC256("secret"))
95+
Algorithm algorithm = Algorithm.HMAC256("secret");
96+
JWTVerifier verifier = JWT.require(algorithm)
9197
.withIssuer("auth0")
9298
.build(); //Reusable verifier instance
9399
DecodedJWT jwt = verifier.verify(token);
100+
} catch (UnsupportedEncodingException exception){
101+
//UTF-8 encoding not supported
94102
} catch (JWTVerificationException exception){
95103
//Invalid signature/claims
96104
}
@@ -100,9 +108,11 @@ try {
100108

101109
```java
102110
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
103-
PublicKey key = //Get the key instance
111+
RSAPublicKey publicKey = //Get the key instance
112+
RSAPrivateKey privateKey = //Get the key instance
104113
try {
105-
JWTVerifier verifier = JWT.require(Algorithm.RSA256(key))
114+
Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
115+
JWTVerifier verifier = JWT.require(algorithm)
106116
.withIssuer("auth0")
107117
.build(); //Reusable verifier instance
108118
DecodedJWT jwt = verifier.verify(token);
@@ -126,15 +136,15 @@ When verifying a token the time validation occurs automatically, resulting in a
126136
To specify a **leeway window** in which the Token should still be considered valid, use the `acceptLeeway()` method in the `JWTVerifier` builder and pass a positive seconds value. This applies to every item listed above.
127137

128138
```java
129-
JWTVerifier verifier = JWT.require(Algorithm.RSA256(key))
139+
JWTVerifier verifier = JWT.require(algorithm)
130140
.acceptLeeway(1) // 1 sec for nbf, iat and exp
131141
.build();
132142
```
133143

134144
You can also specify a custom value for a given Date claim and override the default one for only that claim.
135145

136146
```java
137-
JWTVerifier verifier = JWT.require(Algorithm.RSA256(key))
147+
JWTVerifier verifier = JWT.require(algorithm)
138148
.acceptLeeway(1) //1 sec for nbf and iat
139149
.acceptExpiresAt(5) //5 secs for exp
140150
.build();
@@ -143,7 +153,7 @@ JWTVerifier verifier = JWT.require(Algorithm.RSA256(key))
143153
If you need to test this behaviour in your lib/app cast the `Verification` instance to a `BaseVerification` to gain visibility of the `verification.build()` method that accepts a custom `Clock`. e.g.:
144154

145155
```java
146-
BaseVerification verification = (BaseVerification) JWT.require(Algorithm.RSA256(key))
156+
BaseVerification verification = (BaseVerification) JWT.require(algorithm)
147157
.acceptLeeway(1)
148158
.acceptExpiresAt(5);
149159
Clock clock = new CustomClock(); //Must implement Clock interface
@@ -211,9 +221,9 @@ When creating a Token with the `JWT.create()` you can specify header Claims by c
211221
```java
212222
Map<String, Object> headerClaims = new HashMap();
213223
headerclaims.put("owner", "auth0");
214-
JWT.create()
215-
.withHeader(headerClaims)
216-
.sign(Algorithm.HMAC256("secret"));
224+
String token = JWT.create()
225+
.withHeader(headerClaims)
226+
.sign(algorithm);
217227
```
218228

219229
> The `alg` and `typ` values will always be included in the Header after the signing process.
@@ -295,20 +305,20 @@ Claim claim = jwt.getClaim("isAdmin");
295305
When creating a Token with the `JWT.create()` you can specify a custom Claim by calling `withClaim()` and passing both the name and the value.
296306

297307
```java
298-
JWT.create()
299-
.withClaim("name", 123)
300-
.withArrayClaim("array", new Integer[]{1, 2, 3})
301-
.sign(Algorithm.HMAC256("secret"));
308+
String token = JWT.create()
309+
.withClaim("name", 123)
310+
.withArrayClaim("array", new Integer[]{1, 2, 3})
311+
.sign(algorithm);
302312
```
303313

304314
You can also verify custom Claims on the `JWT.require()` by calling `withClaim()` and passing both the name and the required value.
305315

306316
```java
307-
JWT.require(Algorithm.HMAC256("secret"))
317+
JWTVerifier verifier = JWT.require(algorithm)
308318
.withClaim("name", 123)
309319
.withArrayClaim("array", 1, 2, 3)
310-
.build()
311-
.verify("my.jwt.token");
320+
.build();
321+
DecodedJWT jwt = verifier.verify("my.jwt.token");
312322
```
313323

314324
> Currently supported classes for custom JWT Claim creation and verification are: Boolean, Integer, Double, String, Date and Arrays of type String and Integer.

0 commit comments

Comments
 (0)