@Test public void shouldGetNotBefore() throws Exception { Date expectedDate = new Date(1477592 * 1000); Clock clock = mock(Clock.class); when(clock.getToday()).thenReturn(expectedDate); String token = "eyJhbGciOiJIUzI1NiJ9.eyJuYmYiOjE0Nzc1OTJ9.mWYSOPoNXstjKbZkKrqgkwPOQWEx3F3gMm6PMcfuJd8"; JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWT.require(Algorithm.HMAC256("secret")); DecodedJWT jwt = verification .build(clock) .verify(token); assertThat(jwt, is(notNullValue())); assertThat(jwt.getNotBefore(), is(instanceOf(Date.class))); assertThat(jwt.getNotBefore(), is(notNullValue())); assertThat(jwt.getNotBefore(), is(equalTo(expectedDate))); }
/** * Require a specific Audience ("aud") claim. * * @param audience the required Audience value * @return this same Verification instance. */ @Override public Verification withAudience(String... audience) { requireClaim(PublicClaims.AUDIENCE, Arrays.asList(audience)); return this; }
@Test public void shouldOverrideAcceptIssuedAtWhenIgnoreIssuedAtFlagPassedAndSkipTheVerification() throws Exception { Clock clock = mock(Clock.class); when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 1000)); String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0Nzc1OTJ9.0WJky9eLN7kuxLyZlmbcXRL3Wy8hLoNCEk5CCl2M4lo"; JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret")); DecodedJWT jwt = verification.acceptIssuedAt(20).ignoreIssuedAt() .build() .verify(token); assertThat(jwt, is(notNullValue())); }
/** * Require a specific Array Claim to contain at least the given items. * * @param name the Claim's name. * @param items the items the Claim must contain. * @return this same Verification instance. * @throws IllegalArgumentException if the name is null. */ @Override public Verification withArrayClaim(String name, Integer... items) throws IllegalArgumentException { assertNonNull(name); requireClaim(name, items); return this; }
/** * Require a specific Claim value. * * @param name the Claim's name. * @param value the Claim's value. * @return this same Verification instance. * @throws IllegalArgumentException if the name is null. */ @Override public Verification withClaim(String name, Date value) throws IllegalArgumentException { assertNonNull(name); requireClaim(name, value); return this; }
/** * Set a specific leeway window in seconds in which the Not Before ("nbf") Claim will still be valid. * Not Before Date is always verified when the value is present. This method overrides the value set with acceptLeeway * * @param leeway the window in seconds in which the Not Before Claim will still be valid. * @return this same Verification instance. * @throws IllegalArgumentException if leeway is negative. */ @Override public Verification acceptNotBefore(long leeway) throws IllegalArgumentException { assertPositive(leeway); requireClaim(PublicClaims.NOT_BEFORE, leeway); return this; }
/** * Set a specific leeway window in seconds in which the Expires At ("exp") Claim will still be valid. * Expiration Date is always verified when the value is present. This method overrides the value set with acceptLeeway * * @param leeway the window in seconds in which the Expires At Claim will still be valid. * @return this same Verification instance. * @throws IllegalArgumentException if leeway is negative. */ @Override public Verification acceptExpiresAt(long leeway) throws IllegalArgumentException { assertPositive(leeway); requireClaim(PublicClaims.EXPIRES_AT, leeway); return this; }
/** * Require a specific Array Claim to contain at least the given items. * * @param name the Claim's name. * @param items the items the Claim must contain. * @return this same Verification instance. * @throws IllegalArgumentException if the name is null. */ @Override public Verification withArrayClaim(String name, String... items) throws IllegalArgumentException { assertNonNull(name); requireClaim(name, items); return this; }
/** * Require a specific Claim value. * * @param name the Claim's name. * @param value the Claim's value. * @return this same Verification instance. * @throws IllegalArgumentException if the name is null. */ @Override public Verification withClaim(String name, Long value) throws IllegalArgumentException { assertNonNull(name); requireClaim(name, value); return this; }
/** * Require a specific Claim value. * * @param name the Claim's name. * @param value the Claim's value. * @return this same Verification instance. * @throws IllegalArgumentException if the name is null. */ @Override public Verification withClaim(String name, Boolean value) throws IllegalArgumentException { assertNonNull(name); requireClaim(name, value); return this; }
/** * Set a specific leeway window in seconds in which the Issued At ("iat") Claim will still be valid. * Issued At Date is always verified when the value is present. This method overrides the value set with acceptLeeway * * @param leeway the window in seconds in which the Issued At Claim will still be valid. * @return this same Verification instance. * @throws IllegalArgumentException if leeway is negative. */ @Override public Verification acceptIssuedAt(long leeway) throws IllegalArgumentException { assertPositive(leeway); requireClaim(PublicClaims.ISSUED_AT, leeway); return this; }
/** * Define the default window in seconds in which the Not Before, Issued At and Expires At Claims will still be valid. * Setting a specific leeway value on a given Claim will override this value for that Claim. * * @param leeway the window in seconds in which the Not Before, Issued At and Expires At Claims will still be valid. * @return this same Verification instance. * @throws IllegalArgumentException if leeway is negative. */ @Override public Verification acceptLeeway(long leeway) throws IllegalArgumentException { assertPositive(leeway); this.defaultLeeway = leeway; return this; }
/** * Require a specific Claim value. * * @param name the Claim's name. * @param value the Claim's value. * @return this same Verification instance. * @throws IllegalArgumentException if the name is null. */ @Override public Verification withClaim(String name, String value) throws IllegalArgumentException { assertNonNull(name); requireClaim(name, value); return this; }
/** * Require a specific Claim value. * * @param name the Claim's name. * @param value the Claim's value. * @return this same Verification instance. * @throws IllegalArgumentException if the name is null. */ @Override public Verification withClaim(String name, Double value) throws IllegalArgumentException { assertNonNull(name); requireClaim(name, value); return this; }
/** * Require a specific Claim value. * * @param name the Claim's name. * @param value the Claim's value. * @return this same Verification instance. * @throws IllegalArgumentException if the name is null. */ @Override public Verification withClaim(String name, Integer value) throws IllegalArgumentException { assertNonNull(name); requireClaim(name, value); return this; }
/** * Require a specific JWT Id ("jti") claim. * * @param jwtId the required Id value * @return this same Verification instance. */ @Override public Verification withJWTId(String jwtId) { requireClaim(PublicClaims.JWT_ID, jwtId); return this; }
/** * Require a specific Audience ("aud") claim. * * @param audience the required Audience value * @return this same Verification instance. */ @Override public Verification withAudience(String... audience) { requireClaim(PublicClaims.AUDIENCE, Arrays.asList(audience)); return this; }
/** * Creates a new and reusable instance of the JWTVerifier with the configuration already provided. * ONLY FOR TEST PURPOSES. * * @param clock the instance that will handle the current time. * @return a new JWTVerifier instance with a custom Clock. */ public JWTVerifier build(Clock clock) { addLeewayToDateClaims(); return new JWTVerifier(algorithm, claims, clock); }
/** * Creates a new and reusable instance of the JWTVerifier with the configuration already provided. * * @return a new JWTVerifier instance. */ @Override public JWTVerifier build() { return this.build(new ClockImpl()); }
/** * Initialize a JWTVerifier instance using the given Algorithm. * * @param algorithm the Algorithm to use on the JWT verification. * @return a JWTVerifier.Verification instance to configure. * @throws IllegalArgumentException if the provided algorithm is null. */ static Verification init(Algorithm algorithm) throws IllegalArgumentException { return new BaseVerification(algorithm); }