|
7 | 7 | import java.time.Clock;
|
8 | 8 | import java.time.Duration;
|
9 | 9 |
|
| 10 | +/** |
| 11 | + * Verify that a JWT is valid. |
| 12 | + * |
| 13 | + * <pre>{@code |
| 14 | + * |
| 15 | + * String issuer = "https://cognito-idp.<region>.amazonaws.com/<endpoint>" |
| 16 | + * |
| 17 | + * JwtVerifier verifier = |
| 18 | + * JwtVerifier.builder() |
| 19 | + * .issuer(issuer) |
| 20 | + * .build() |
| 21 | + * |
| 22 | + * }</pre> |
| 23 | + */ |
10 | 24 | public interface JwtVerifier {
|
11 | 25 |
|
| 26 | + /** |
| 27 | + * Create and return a builder for JwtVerifier. |
| 28 | + */ |
12 | 29 | static Builder builder() {
|
13 | 30 | return DJwtVerifier.builder();
|
14 | 31 | }
|
15 | 32 |
|
| 33 | + /** |
| 34 | + * Verify that the SignedJwt is valid. |
| 35 | + */ |
16 | 36 | void verify(SignedJwt jwt) throws JwtVerifyException;
|
17 | 37 |
|
| 38 | + /** |
| 39 | + * Parse and verify the accessToken. |
| 40 | + * |
| 41 | + * @param accessToken The raw JWT access token. |
| 42 | + * @return The verified AccessToken. |
| 43 | + * @throws JwtVerifyException When the access token is not valid. |
| 44 | + */ |
18 | 45 | AccessToken verifyAccessToken(String accessToken) throws JwtVerifyException;
|
19 | 46 |
|
| 47 | + /** |
| 48 | + * Builder for JwtVerifier. |
| 49 | + */ |
20 | 50 | interface Builder {
|
21 | 51 |
|
| 52 | + /** |
| 53 | + * Add RSA 256 algorithm signing. |
| 54 | + */ |
22 | 55 | Builder addRS256();
|
23 | 56 |
|
| 57 | + /** |
| 58 | + * Add an algorithm that this verifier will support. |
| 59 | + * |
| 60 | + * @param key The key for the algorithm |
| 61 | + * @param algorithm The algorithm |
| 62 | + */ |
24 | 63 | Builder add(String key, String algorithm);
|
25 | 64 |
|
| 65 | + /** |
| 66 | + * Add a key source. |
| 67 | + */ |
26 | 68 | Builder keySource(JwtKeySource keySource);
|
27 | 69 |
|
| 70 | + /** |
| 71 | + * Add a URI for remote JSON Web Key Set. |
| 72 | + */ |
28 | 73 | Builder jwksUri(String jwksUri);
|
29 | 74 |
|
| 75 | + /** |
| 76 | + * Add a JsonDataMapper to parse the various json payloads. |
| 77 | + * <p> |
| 78 | + * A default is provided when a mapper is not explicitly specified. |
| 79 | + */ |
30 | 80 | Builder jsonMapper(JsonDataMapper mapper);
|
31 | 81 |
|
| 82 | + /** |
| 83 | + * Add the HttpClient to use. Defaults to creating a new HttpClient. |
| 84 | + */ |
32 | 85 | Builder httpClient(HttpClient httpClient);
|
33 | 86 |
|
| 87 | + /** |
| 88 | + * Specify the Issuer. |
| 89 | + * <p> |
| 90 | + * Cognito example: <code>https://cognito-idp.{region}.amazonaws.com/{endpoint}</code> |
| 91 | + */ |
34 | 92 | Builder issuer(String expectedIssuer);
|
35 | 93 |
|
| 94 | + /** |
| 95 | + * Specify the Clock to use. Defaults to <code>Clock.systemDefaultZone()</code> |
| 96 | + */ |
36 | 97 | Builder clock(Clock clock);
|
37 | 98 |
|
| 99 | + /** |
| 100 | + * Specify the Clock skew to use. Defaults to 60 seconds. |
| 101 | + */ |
38 | 102 | Builder clockSkew(Duration clockSkew);
|
39 | 103 |
|
| 104 | + /** |
| 105 | + * Build and return the JwtVerifier. |
| 106 | + */ |
40 | 107 | JwtVerifier build();
|
41 | 108 | }
|
42 | 109 |
|
|
0 commit comments