Search in sources :

Example 86 with Claim

use of com.auth0.android.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) {
        logger.debug("Error verifying token: {}", 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)

Example 87 with Claim

use of com.auth0.android.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) {
        monitor.debug(() -> "Error verifying token", 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)

Example 88 with Claim

use of com.auth0.android.jwt.Claim in project conWasteJiNing by Yingjie-tian.

the class JwtUtils method verifyToken.

/**
 * 解密jwt
 * @param token
 * @return
 * @throws RuntimeException
 */
public static Map<String, String> verifyToken(String token) throws RuntimeException {
    Algorithm algorithm = null;
    try {
        // 使用HMAC256进行加密
        algorithm = Algorithm.HMAC256(SECRET);
    } catch (IllegalArgumentException | UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
    // 解密
    JWTVerifier verifier = JWT.require(algorithm).withIssuer(ISSUER).build();
    DecodedJWT jwt = verifier.verify(token);
    Map<String, Claim> map = jwt.getClaims();
    Map<String, String> resultMap = new HashMap<>();
    map.forEach((k, v) -> resultMap.put(k, v.asString()));
    return resultMap;
}
Also used : HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Claim(com.auth0.jwt.interfaces.Claim)

Example 89 with Claim

use of com.auth0.android.jwt.Claim in project opencast by opencast.

the class JWTVerifierTest method testVerifySymmetric.

@Test
public void testVerifySymmetric() {
    // Valid JWT + valid claim constraints
    DecodedJWT decodedJWT = JWTVerifier.verify(generator.generateValidSymmetricJWT(), generator.getSecret(), generator.generateValidClaimConstraints());
    assertEquals(generator.getUsername(), decodedJWT.getClaim("username").asString());
    // Valid JWT + invalid claim constraints
    assertThrows(JWTVerificationException.class, () -> JWTVerifier.verify(generator.generateValidSymmetricJWT(), generator.getSecret(), generator.generateInvalidClaimConstraints()));
    // Valid JWT + invalid secret
    assertThrows(JWTVerificationException.class, () -> JWTVerifier.verify(generator.generateValidSymmetricJWT(), "abc", generator.generateValidClaimConstraints()));
    // Invalid JWT
    assertThrows(JWTVerificationException.class, () -> JWTVerifier.verify(generator.generateExpiredSymmetricJWT(), generator.getSecret(), generator.generateValidClaimConstraints()));
}
Also used : DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) Test(org.junit.Test)

Example 90 with Claim

use of com.auth0.android.jwt.Claim in project opencast by opencast.

the class JWTVerifier method verify.

/**
 * Verifies a given JWT string with a secret and given claim constraints.
 *
 * @param token The JWT string.
 * @param secret The secret.
 * @param claimConstraints The claim constraints.
 * @return The decoded and verified JWT.
 * @throws JWTVerificationException If the JWT cannot be verified successfully.
 */
public static DecodedJWT verify(String token, String secret, List<String> claimConstraints) throws JWTVerificationException {
    Assert.notNull(token, "A token must be set");
    Assert.isTrue(StringUtils.isNotBlank(secret), "A secret must be set");
    DecodedJWT jwt = JWT.decode(token);
    return verify(jwt, claimConstraints, AlgorithmBuilder.buildAlgorithm(jwt, secret));
}
Also used : DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

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