Search in sources :

Example 21 with Verification

use of com.auth0.jwt.interfaces.Verification in project jahia by Jahia.

the class JWTConfig method verifyToken.

@Override
public DecodedJWT verifyToken(String token) throws JWTVerificationException, RepositoryException {
    Verification verification = signedVerification();
    addConfigToVerification(verification);
    // Reusable verifier instance
    JWTVerifier verifier = verification.build();
    return verifier.verify(token);
}
Also used : Verification(com.auth0.jwt.interfaces.Verification) JWTVerifier(com.auth0.jwt.JWTVerifier)

Example 22 with Verification

use of com.auth0.jwt.interfaces.Verification in project java-jwt by auth0.

the class JWTTest method shouldGetId.

@Test
public void shouldGetId() {
    String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMjM0NTY3ODkwIn0.m3zgEfVUFOd-CvL3xG5BuOWLzb0zMQZCqiVNQQOPOvA";
    JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWT.require(Algorithm.HMAC256("secret"));
    DecodedJWT jwt = verification.build().verify(token);
    assertThat(jwt, is(notNullValue()));
    assertThat(jwt.getId(), is("1234567890"));
}
Also used : DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Test(org.junit.Test)

Example 23 with Verification

use of com.auth0.jwt.interfaces.Verification in project java-jwt by auth0.

the class JWTTest method shouldGetIssuedAt.

@Test
public void shouldGetIssuedAt() {
    long seconds = 1477592;
    Clock clock = Clock.fixed(Instant.ofEpochSecond(seconds), ZoneId.of("UTC"));
    String token = "eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE0Nzc1OTJ9.5o1CKlLFjKKcddZzoarQ37pq7qZqNPav3sdZ_bsZaD4";
    JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWT.require(Algorithm.HMAC256("secret"));
    DecodedJWT jwt = verification.build(clock).verify(token);
    assertThat(jwt, is(notNullValue()));
    assertThat(jwt.getIssuedAt(), is(equalTo(new Date(seconds * 1000))));
    assertThat(jwt.getIssuedAtAsInstant(), is(equalTo(Instant.ofEpochSecond(seconds))));
}
Also used : Clock(java.time.Clock) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Date(java.util.Date) Test(org.junit.Test)

Example 24 with Verification

use of com.auth0.jwt.interfaces.Verification in project java-jwt by auth0.

the class JWTVerifierTest method shouldOverrideDefaultNotBeforeLeeway.

@Test
public void shouldOverrideDefaultNotBeforeLeeway() {
    Algorithm algorithm = mock(Algorithm.class);
    JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(algorithm);
    JWTVerifier verifier = verification.acceptLeeway(1234L).acceptNotBefore(9999L).build();
    assertThat(verifier.expectedChecks, is(notNullValue()));
    assertThat(verification.getLeewayFor(RegisteredClaims.ISSUED_AT), is(1234L));
    assertThat(verification.getLeewayFor(RegisteredClaims.EXPIRES_AT), is(1234L));
    assertThat(verification.getLeewayFor(RegisteredClaims.NOT_BEFORE), is(9999L));
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) Test(org.junit.Test)

Example 25 with Verification

use of com.auth0.jwt.interfaces.Verification in project java-jwt by auth0.

the class JWTVerifierTest method shouldAllowWithAudienceVerificationToOverrideWithAnyOfAudience.

@Test
public void shouldAllowWithAudienceVerificationToOverrideWithAnyOfAudience() {
    // Token 'aud' = ["Mark", "David", "John"]
    String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiTWFyayIsIkRhdmlkIiwiSm9obiJdfQ.DX5xXiCaYvr54x_iL0LZsJhK7O6HhAdHeDYkgDeb0Rw";
    Verification verification = JWTVerifier.init(Algorithm.HMAC256("secret")).withAnyOfAudience("Jim");
    Exception exception = null;
    try {
        verification.build().verify(token);
    } catch (Exception e) {
        exception = e;
    }
    assertThat(exception, is(notNullValue()));
    assertThat(exception, is(instanceOf(IncorrectClaimException.class)));
    assertThat(exception.getMessage(), is("The Claim 'aud' value doesn't contain the required audience."));
    DecodedJWT jwt = JWTVerifier.init(Algorithm.HMAC256("secret")).withAudience("Mark").build().verify(token);
    assertThat(jwt, is(notNullValue()));
}
Also used : Verification(com.auth0.jwt.interfaces.Verification) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)29 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)28 Algorithm (com.auth0.jwt.algorithms.Algorithm)14 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)11 Date (java.util.Date)11 Verification (com.auth0.jwt.interfaces.Verification)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 JWTVerifier (com.auth0.jwt.JWTVerifier)5 RSAPublicKey (java.security.interfaces.RSAPublicKey)5 Job (com.auth0.json.mgmt.jobs.Job)4 Claim (com.auth0.jwt.interfaces.Claim)4 Clock (com.auth0.jwt.interfaces.Clock)4 List (java.util.List)4 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)4 JWT (com.auth0.jwt.JWT)3 JWTVerifier (com.auth0.jwt.interfaces.JWTVerifier)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 FloodlightModuleException (net.floodlightcontroller.core.module.FloodlightModuleException)3