Skip to content

Commit 85914ae

Browse files
author
Justin Dahmubed
committed
Very rough draft of README
1 parent 544398e commit 85914ae

1 file changed

Lines changed: 82 additions & 74 deletions

File tree

README.md

Lines changed: 82 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,43 @@ The library implements JWT Verification and Signing using the following algorith
4444
| ES384 | ECDSA384 | ECDSA with curve P-384 and SHA-384 |
4545
| ES512 | ECDSA512 | ECDSA with curve P-521 and SHA-512 |
4646

47-
## Usage
47+
## Supported token profile types
48+
49+
#### Basic Token
50+
51+
- Standard claims: *iss, sub, iat, jti*
52+
- Nonstandard claims: *aud, exp, nbf*
53+
54+
#### Extended Token
55+
- Standard claims: *name, email, picture, iss, sub, iat*
56+
- Nonstandard claims: *aud, exp, nbf*
57+
58+
#### Access Token
59+
- Standard claims: *iss, sub, iat*
60+
- Nonstandard claims: *aud, exp*
61+
62+
#### Facebook Token
63+
- Standard claims: *user_id, app_id, issued_at*
64+
- Nonstandard claims: *expired_at*
65+
66+
#### Google Token
67+
- Standard claims: *name, email, picture, iss, sub, iat*
68+
- Nonstandard claims: *exp, aud*
69+
70+
#### Implicit Access Token
71+
- Standard claims: *iss, sub, iat*
72+
- Nonstandard claims: *aud*
73+
74+
#### Refresh Token
75+
- Standard claims: *refresh_token, access_token*
76+
77+
#### Risc Token
78+
- Standard claims: *jti, iss, sub, iat*
79+
- Nonstandard claims: *aud, nbf, exp*
80+
81+
#### Scoped Access Token
82+
- Standard claims: *iss, sub, iat, scope*
83+
- Nonstandard claims: *aud, exp*
4884

4985
### Pick the Algorithm
5086

@@ -147,23 +183,22 @@ If a Claim couldn't be converted to JSON or the Key used in the signing process
147183

148184
### Verify a Token
149185

150-
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.
186+
You'll first need to create a `Verification` instance by calling `JWT.require()` and passing the `Algorithm` instance. Once you have the `Verification` instance, you can call the corresponding verifier method. For the example of Google,
187+
you would have a `GoogleVerificiation` instance that has inherited from the `Verification` instance in order to call `createVerifierForGoogle()`, and you would pass in the claims that you would want to be verified.
188+
Once you call `build`, you would get back a `JWT` object and with that, you would call `decode()` while passing in the token that was created after signing. You will get back a `DecodedJWT` object, which contains all of the claims, and you can verify
189+
those claims against what's the expected claims by calling `verifyClaims()`.
151190

152191
* Example using `HS256`
153192

154193
```java
155194
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
156-
try {
157-
Algorithm algorithm = Algorithm.HMAC256("secret");
158-
JWTVerifier verifier = JWT.require(algorithm)
159-
.withIssuer("auth0")
160-
.build(); //Reusable verifier instance
161-
DecodedJWT jwt = verifier.verify(token);
162-
} catch (UnsupportedEncodingException exception){
163-
//UTF-8 encoding not supported
164-
} catch (JWTVerificationException exception){
165-
//Invalid signature/claims
166-
}
195+
Algorithm algorithm = Algorithm.HMAC256("secret");
196+
GoogleVerification verification = GoogleJWT.require(algorithm);
197+
JWT verifier = verification.createVerifierForGoogle(PICTURE, EMAIL, asList("accounts.fake.com"), asList("audience"),
198+
NAME, 1, 1).build();
199+
DecodedJWT jwt = verifier.decode(token);
200+
Map<String, Claim> claims = jwt.getClaims();
201+
verifyClaims(claims, exp);
167202
```
168203

169204
* Example using `RS256`
@@ -172,19 +207,18 @@ try {
172207
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
173208
RSAPublicKey publicKey = //Get the key instance
174209
RSAPrivateKey privateKey = //Get the key instance
175-
try {
176-
Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
177-
JWTVerifier verifier = JWT.require(algorithm)
178-
.withIssuer("auth0")
179-
.build(); //Reusable verifier instance
180-
DecodedJWT jwt = verifier.verify(token);
181-
} catch (JWTVerificationException exception){
182-
//Invalid signature/claims
183-
}
184-
```
185210

186-
If the token has an invalid signature or the Claim requirement is not met, a `JWTVerificationException` will raise.
211+
Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey);
212+
GoogleVerification verification = GoogleJWT.require(algorithm);
213+
JWT verifier = verification.createVerifierForGoogle(PICTURE, EMAIL, asList("accounts.fake.com"), asList("audience"),
214+
NAME, 1, 1).build();
215+
DecodedJWT jwt = verifier.decode(token);
216+
Map<String, Claim> claims = jwt.getClaims();
217+
verifyClaims(claims, exp);
218+
```
187219

220+
If the token has a Claim requirement that has not been met, an `InvalidClaimException` will raise.
221+
If the token has an invalid signature, an `AlgorithmMismatchException` will raise.
188222

189223
#### Time Validation
190224

@@ -195,20 +229,20 @@ The JWT token may include DateNumber fields that can be used to validate that:
195229

196230
When verifying a token the time validation occurs automatically, resulting in a `JWTVerificationException` being throw when the values are invalid. If any of the previous fields are missing they won't be considered in this validation.
197231

198-
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.
199-
232+
To specify a **nbf value** in which the Token should still be considered valid, use the `withNbf()` method in the respective `Creator` builder and pass a Date object. This applies to every item listed above.
233+
**NOTE:** `Nbf` and `iat` date values should be in the past, but the `exp` value should be in the future.
200234
```java
201-
JWTVerifier verifier = JWT.require(algorithm)
202-
.acceptLeeway(1) // 1 sec for nbf, iat and exp
235+
Verification verifier = JWT.require(algorithm)
236+
.withNbf(new Date(2016,1,1))
203237
.build();
204238
```
205239

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

208242
```java
209-
JWTVerifier verifier = JWT.require(algorithm)
210-
.acceptLeeway(1) //1 sec for nbf and iat
211-
.acceptExpiresAt(5) //5 secs for exp
243+
Verification verifier = JWT.require(algorithm)
244+
.withNbf(new Date(2016,1,1))
245+
.withExp(new Date(2100,1,1))
212246
.build();
213247
```
214248

@@ -219,18 +253,17 @@ BaseVerification verification = (BaseVerification) JWT.require(algorithm)
219253
.acceptLeeway(1)
220254
.acceptExpiresAt(5);
221255
Clock clock = new CustomClock(); //Must implement Clock interface
222-
JWTVerifier verifier = verification.build(clock);
256+
JWT verifier = verification.build(clock);
223257
```
224258

225259
### Decode a Token
226260

261+
This example is for an Implicit JWT token and can be applied to all the types of tokens:
227262
```java
228-
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJpc3MiOiJhdXRoMCJ9.AbIJTDMFc7yUa5MhvcP03nJPyCPzZtQcGEp-zWfOkEE";
229-
try {
230-
DecodedJWT jwt = JWT.decode(token);
231-
} catch (JWTDecodeException exception){
232-
//Invalid token
233-
}
263+
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOlsic3ViamVjdCJdLCJpc3MiOlsiYWNjb3VudHMuZmFrZS5jb20iXSwiYXVkIjoiYXVkaWVuY2UiLCJpYXQiOi0xMjQ1MjgxNTI3fQ.-eRoMolUy7PnEcpvfs-jTEvP6qagBZ1G_lqp1jY3Nqg";
264+
Verification verification = ImplicitJWT.require(algorithm);
265+
JWT verifier = verification.createVerifierForImplicit(asList("accounts.fake.com"), asList("audience"), 1).build();
266+
DecodedJWT jwt = verifier.decode(token);
234267
```
235268

236269
If the token has an invalid syntax or the header or payload are not JSONs, a `JWTDecodeException` will raise.
@@ -278,12 +311,12 @@ Additional Claims defined in the token's Header can be obtained by calling `getH
278311
Claim claim = jwt.getHeaderClaim("owner");
279312
```
280313

281-
When creating a Token with the `JWT.create()` you can specify header Claims by calling `withHeader()` and passing both the map of claims.
314+
When creating a Token with the `JWTCreator.init()` you can specify header Claims by calling `withHeader()` and passing both the map of claims.
282315

283316
```java
284317
Map<String, Object> headerClaims = new HashMap();
285318
headerClaims.put("owner", "auth0");
286-
String token = JWT.create()
319+
String token = JWTCreator.init()
287320
.withHeader(headerClaims)
288321
.sign(algorithm);
289322
```
@@ -349,9 +382,9 @@ Returns the JWT ID value or null if it's not defined in the Payload.
349382
String id = jwt.getId();
350383
```
351384

352-
#### Private Claims
385+
#### Nonstandard Claims
353386

354-
Additional Claims defined in the token's Payload can be obtained by calling `getClaims()` or `getClaim()` and passing the Claim name. A Claim will always be returned, even if it can't be found. You can check if a Claim's value is null by calling `claim.isNull()`.
387+
Nonstandard Claims defined in the token's Payload can be obtained by calling `getClaims()` or `getClaim()` and passing the Claim name. A Claim will always be returned, even if it can't be found. You can check if a Claim's value is null by calling `claim.isNull()`.
355388

356389
```java
357390
Map<String, Claim> claims = jwt.getClaims(); //Key is the Claim name
@@ -364,24 +397,16 @@ or
364397
Claim claim = jwt.getClaim("isAdmin");
365398
```
366399

367-
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.
400+
When creating an Implicit Token for example with the `ImplicitJwtCreator.build()` you can specify a custom Claim by calling `withNonStandardClaim()` and passing both the name and the value.
368401

369402
```java
370-
String token = JWT.create()
371-
.withClaim("name", 123)
403+
String token = ImplicitJwtCreator.build()
404+
.withNonStandardClaim("nonStandardClaim", 123)
372405
.withArrayClaim("array", new Integer[]{1, 2, 3})
373406
.sign(algorithm);
374407
```
375408

376-
You can also verify custom Claims on the `JWT.require()` by calling `withClaim()` and passing both the name and the required value.
377-
378-
```java
379-
JWTVerifier verifier = JWT.require(algorithm)
380-
.withClaim("name", 123)
381-
.withArrayClaim("array", 1, 2, 3)
382-
.build();
383-
DecodedJWT jwt = verifier.verify("my.jwt.token");
384-
```
409+
**NOTE:** Nonstandard claims do not need to verified.
385410

386411
> Currently supported classes for custom JWT Claim creation and verification are: Boolean, Integer, Double, String, Date and Arrays of type String and Integer.
387412
@@ -407,31 +432,14 @@ To obtain a Claim as a Collection you'll need to provide the **Class Type** of t
407432

408433
If the values can't be converted to the given **Class Type** a `JWTDecodeException` will raise.
409434

410-
411-
412-
## What is Auth0?
413-
414-
Auth0 helps you to:
415-
416-
* Add authentication with [multiple authentication sources](https://docs.auth0.com/identityproviders), either social like **Google, Facebook, Microsoft Account, LinkedIn, GitHub, Twitter, Box, Salesforce, among others**, or enterprise identity systems like **Windows Azure AD, Google Apps, Active Directory, ADFS or any SAML Identity Provider**.
417-
* Add authentication through more traditional **[username/password databases](https://docs.auth0.com/mysql-connection-tutorial)**.
418-
* Add support for **[linking different user accounts](https://docs.auth0.com/link-accounts)** with the same user.
419-
* Support for generating signed [Json Web Tokens](https://docs.auth0.com/jwt) to call your APIs and **flow the user identity** securely.
420-
* Analytics of how, when and where users are logging in.
421-
* Pull data from other sources and add it to the user profile, through [JavaScript rules](https://docs.auth0.com/rules).
422-
423-
## Create a free account in Auth0
424-
425-
1. Go to [Auth0](https://auth0.com) and click Sign Up.
426-
2. Use Google, GitHub or Microsoft Account to login.
427-
428435
## Issue Reporting
429436

430-
If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker. The [Responsible Disclosure Program](https://auth0.com/whitehat) details the procedure for disclosing security issues.
437+
If you have found a bug or if you have a feature request, please report them at this repository issues section. Please do not report security vulnerabilities on the public GitHub issue tracker.
431438

432439
## Author
433440

434-
[Auth0](https://auth0.com/)
441+
Justin Dahmubed\
442+
Application Engineer II @ Google
435443

436444
## License
437445

0 commit comments

Comments
 (0)