Search in sources :

Example 81 with Claim

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

the class JWTVerifierTest method shouldSuccessfullyVerifyClaimWithPredicate.

@Test
public void shouldSuccessfullyVerifyClaimWithPredicate() {
    String jwt = JWTCreator.init().withClaim("claimName", "claimValue").sign(Algorithm.HMAC256("secret"));
    JWTVerifier verifier = JWTVerifier.init(Algorithm.HMAC256("secret")).withClaim("claimName", (claim, decodedJWT) -> "claimValue".equals(claim.asString())).build();
    DecodedJWT decodedJWT = verifier.verify(jwt);
    assertThat(decodedJWT, is(notNullValue()));
}
Also used : Verification(com.auth0.jwt.interfaces.Verification) com.auth0.jwt.exceptions(com.auth0.jwt.exceptions) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Date(java.util.Date) Assert.assertThrows(org.junit.Assert.assertThrows) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Instant(java.time.Instant) ZoneId(java.time.ZoneId) BiPredicate(java.util.function.BiPredicate) Algorithm(com.auth0.jwt.algorithms.Algorithm) Rule(org.junit.Rule) Duration(java.time.Duration) Clock(java.time.Clock) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Claim(com.auth0.jwt.interfaces.Claim) ExpectedException(org.junit.rules.ExpectedException) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Test(org.junit.Test)

Example 82 with Claim

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

the class JsonNodeClaim method asMap.

@Override
public Map<String, Object> asMap() throws JWTDecodeException {
    if (isMissing() || isNull() || !data.isObject()) {
        return null;
    }
    try {
        TypeReference<Map<String, Object>> mapType = new TypeReference<Map<String, Object>>() {
        };
        JsonParser thisParser = objectReader.treeAsTokens(data);
        return thisParser.readValueAs(mapType);
    } catch (IOException e) {
        throw new JWTDecodeException("Couldn't map the Claim value to Map", e);
    }
}
Also used : JWTDecodeException(com.auth0.jwt.exceptions.JWTDecodeException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException) Map(java.util.Map) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 83 with Claim

use of com.auth0.jwt.Claim in project wikidata-query-rdf by wikimedia.

the class TimeLimitedAccessTokenFactory method decide.

<T> T decide(String token, Supplier<T> good, Supplier<T> bad) {
    if (token == null) {
        return bad.get();
    }
    DecodedJWT decoded;
    try {
        decoded = verifier.verify(token);
    } catch (JWTVerificationException e) {
        return bad.get();
    }
    Claim claim = decoded.getClaim(USERNAME);
    if (claim.isNull()) {
        throw new IllegalStateException(("All valid jwt tokens must have a username claim"));
    }
    if (bannedUsernames.contains(claim.asString())) {
        return bad.get();
    }
    return good.get();
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Claim(com.auth0.jwt.interfaces.Claim)

Example 84 with Claim

use of com.auth0.jwt.Claim in project libresonic by Libresonic.

the class JWTSecurityServiceTest method addJWTToken.

@Test
public void addJWTToken() throws Exception {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(uriString);
    String actualUri = service.addJWTToken(builder).build().toUriString();
    String jwtToken = UriComponentsBuilder.fromUriString(actualUri).build().getQueryParams().getFirst(JWTSecurityService.JWT_PARAM_NAME);
    DecodedJWT verify = verifier.verify(jwtToken);
    Claim claim = verify.getClaim(JWTSecurityService.CLAIM_PATH);
    assertEquals(expectedClaimString, claim.asString());
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Claim(com.auth0.jwt.interfaces.Claim) Test(org.junit.Test)

Example 85 with Claim

use of com.auth0.jwt.Claim in project data-transfer-project by google.

the class JWTTokenManager method getJobIdFromToken.

@Override
public UUID getJobIdFromToken(String token) {
    try {
        DecodedJWT jwt = verifier.verify(token);
        // Token is verified, get claim
        Claim claim = jwt.getClaim(JWTTokenManager.ID_CLAIM_KEY);
        if (claim.isNull()) {
            return null;
        }
        return claim.isNull() ? null : UUID.fromString(claim.asString());
    } catch (JWTVerificationException exception) {
        throw new RuntimeException("Error verifying token: " + token);
    }
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Claim(com.auth0.jwt.interfaces.Claim)

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