Search in sources :

Example 76 with Claim

use of com.auth0.jwt.Claim in project java-jwt by auth0.

the class JsonNodeClaimTest method shouldGetBooleanValue.

@Test
public void shouldGetBooleanValue() {
    JsonNode value = mapper.valueToTree(true);
    Claim claim = claimFromNode(value);
    assertThat(claim.asBoolean(), is(notNullValue()));
    assertThat(claim.asBoolean(), is(true));
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) Claim(com.auth0.jwt.interfaces.Claim) Test(org.junit.Test)

Example 77 with Claim

use of com.auth0.jwt.Claim in project java-jwt by auth0.

the class JsonNodeClaimTest method shouldGetArrayValueOfCustomClass.

@Test
public void shouldGetArrayValueOfCustomClass() {
    JsonNode value = mapper.valueToTree(new UserPojo[] { new UserPojo("George", 1), new UserPojo("Mark", 2) });
    Claim claim = claimFromNode(value);
    assertThat(claim.asArray(UserPojo.class), is(notNullValue()));
    assertThat(claim.asArray(UserPojo.class), is(arrayContaining(new UserPojo("George", 1), new UserPojo("Mark", 2))));
}
Also used : UserPojo(com.auth0.jwt.UserPojo) JsonNode(com.fasterxml.jackson.databind.JsonNode) Claim(com.auth0.jwt.interfaces.Claim) Test(org.junit.Test)

Example 78 with Claim

use of com.auth0.jwt.Claim in project java-jwt by auth0.

the class JsonNodeClaimTest method shouldReturnBaseClaimWhenParsingNullNode.

@Test
public void shouldReturnBaseClaimWhenParsingNullNode() {
    JsonNode value = NullNode.getInstance();
    Claim claim = claimFromNode(value);
    assertThat(claim, is(notNullValue()));
    assertThat(claim.isNull(), is(true));
    assertThat(claim.isMissing(), is(false));
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) Claim(com.auth0.jwt.interfaces.Claim) Test(org.junit.Test)

Example 79 with Claim

use of com.auth0.jwt.Claim 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)

Example 80 with Claim

use of com.auth0.jwt.Claim in project java-jwt by auth0.

the class JWTVerifierTest method shouldAllowWithAnyOfAudienceVerificationToOverrideWithAudience.

@Test
public void shouldAllowWithAnyOfAudienceVerificationToOverrideWithAudience() {
    // Token 'aud' = ["Mark", "David", "John"]
    String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiTWFyayIsIkRhdmlkIiwiSm9obiJdfQ.DX5xXiCaYvr54x_iL0LZsJhK7O6HhAdHeDYkgDeb0Rw";
    Verification verification = JWTVerifier.init(Algorithm.HMAC256("secret")).withAudience("Mark", "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")).withAnyOfAudience("Mark", "Jim").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

Claim (com.auth0.jwt.interfaces.Claim)110 Test (org.junit.Test)67 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)62 JsonNode (com.fasterxml.jackson.databind.JsonNode)42 Algorithm (com.auth0.jwt.algorithms.Algorithm)24 Date (java.util.Date)24 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)21 RSAPublicKey (java.security.interfaces.RSAPublicKey)21 Test (org.junit.jupiter.api.Test)18 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)17 JWTVerifier (com.auth0.jwt.JWTVerifier)15 JwksTestKeySource (org.sdase.commons.server.auth.service.testsources.JwksTestKeySource)14 JsonObject (com.google.gson.JsonObject)10 HashMap (java.util.HashMap)9 UserPojo (com.auth0.jwt.UserPojo)8 IOException (java.io.IOException)8 Map (java.util.Map)8 TestingProcessManager (io.supertokens.test.TestingProcessManager)7 NullClaim (com.auth0.jwt.impl.NullClaim)5 JWT (com.auth0.jwt.JWT)4