Search in sources :

Example 31 with Token

use of com.auth0.json.mgmt.Token in project ed-springboot-learning by QQ986945193.

the class JwtUtil method unsign.

/**
 * 根据加密token,解析返回对象
 */
public static <T> T unsign(String jwt, Class<T> classT) {
    final JWTVerifier verifier = new JWTVerifier(SECRET);
    try {
        final Map<String, Object> claims = verifier.verify(jwt);
        if (claims.containsKey(EXP) && claims.containsKey(PAYLOAD)) {
            long exp = (Long) claims.get(EXP);
            long currentTimeMillis = System.currentTimeMillis();
            if (exp > currentTimeMillis) {
                String json = (String) claims.get(PAYLOAD);
                ObjectMapper objectMapper = new ObjectMapper();
                return objectMapper.readValue(json, classT);
            }
        }
        return null;
    } catch (Exception e) {
        return null;
    }
}
Also used : JWTVerifier(com.auth0.jwt.JWTVerifier) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 32 with Token

use of com.auth0.json.mgmt.Token in project ed-springboot-learning by QQ986945193.

the class JwtUtil method sign.

/**
 * 根据对象加密生成token。
 */
public static <T> String sign(T object, long maxAge) {
    try {
        final JWTSigner signer = new JWTSigner(SECRET);
        final Map<String, Object> claims = new HashMap<String, Object>();
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(object);
        claims.put(PAYLOAD, jsonString);
        claims.put(EXP, System.currentTimeMillis() + maxAge);
        return signer.sign(claims);
    } catch (Exception e) {
        return null;
    }
}
Also used : JWTSigner(com.auth0.jwt.JWTSigner) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 33 with Token

use of com.auth0.json.mgmt.Token in project java-jwt by auth0.

the class ConcurrentVerifyTest method shouldPassECDSA256KVerificationWithJOSESignature.

@Test
public void shouldPassECDSA256KVerificationWithJOSESignature() throws Exception {
    String token = "eyJraWQiOiJteS1rZXktaWQiLCJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJpc3MiOiJhdXRoMCJ9.W-AbsnuQ4vqmPftAyQuF09hn3oGn3tN7VGergxyMbK74yEzDV-mLyC3o3fxXrZxcW5h01DM6BckNag7ZcimPjw";
    ECPublicKey publicKey = (ECPublicKey) readPublicKeyFromFile(PUBLIC_KEY_FILE_256K, "EC");
    ECPrivateKey privateKey = (ECPrivateKey) readPrivateKeyFromFile(PRIVATE_KEY_FILE_256K, "EC");
    Algorithm algorithm = Algorithm.ECDSA256K(publicKey, privateKey);
    JWTVerifier verifier = JWTVerifier.init(algorithm).withIssuer("auth0").build();
    concurrentVerify(verifier, token);
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECPublicKey(java.security.interfaces.ECPublicKey) Algorithm(com.auth0.jwt.algorithms.Algorithm) Test(org.junit.Test)

Example 34 with Token

use of com.auth0.json.mgmt.Token in project java-jwt by auth0.

the class JWTVerifierTest method shouldThrowOnInvalidExpiresAtIfPresent.

@Test
public void shouldThrowOnInvalidExpiresAtIfPresent() throws Exception {
    exception.expect(TokenExpiredException.class);
    exception.expectMessage(startsWith("The Token has expired on"));
    Clock clock = mock(Clock.class);
    when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE + 1000));
    String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0Nzc1OTJ9.isvT0Pqx0yjnZk53mUFSeYFJLDs-Ls9IsNAm86gIdZo";
    JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"));
    verification.build(clock).verify(token);
}
Also used : Clock(com.auth0.jwt.interfaces.Clock) Date(java.util.Date) Test(org.junit.Test)

Example 35 with Token

use of com.auth0.json.mgmt.Token in project java-jwt by auth0.

the class JWTVerifierTest method shouldThrowOnInvalidNotBeforeIfPresent.

@Test
public void shouldThrowOnInvalidNotBeforeIfPresent() throws Exception {
    exception.expect(InvalidClaimException.class);
    exception.expectMessage(startsWith("The Token can't be used before"));
    Clock clock = mock(Clock.class);
    when(clock.getToday()).thenReturn(new Date(DATE_TOKEN_MS_VALUE - 1000));
    String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0Nzc1OTJ9.wq4ZmnSF2VOxcQBxPLfeh1J2Ozy1Tj5iUaERm3FKaw8";
    JWTVerifier.BaseVerification verification = (JWTVerifier.BaseVerification) JWTVerifier.init(Algorithm.HMAC256("secret"));
    verification.build(clock).verify(token);
}
Also used : Clock(com.auth0.jwt.interfaces.Clock) Date(java.util.Date) Test(org.junit.Test)

Aggregations

DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)276 Algorithm (com.auth0.jwt.algorithms.Algorithm)147 Test (org.junit.Test)120 JWTVerifier (com.auth0.jwt.JWTVerifier)97 Date (java.util.Date)78 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)62 IOException (java.io.IOException)59 Claim (com.auth0.jwt.interfaces.Claim)49 HashMap (java.util.HashMap)40 VoidRequest (com.auth0.net.VoidRequest)31 RSAPublicKey (java.security.interfaces.RSAPublicKey)31 Test (org.junit.jupiter.api.Test)30 JWTDecodeException (com.auth0.jwt.exceptions.JWTDecodeException)28 JWTCreator (com.auth0.jwt.JWTCreator)21 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)21 JWT (com.auth0.jwt.JWT)20 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)19 UnsupportedEncodingException (java.io.UnsupportedEncodingException)18 Instant (java.time.Instant)18 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)17