Search in sources :

Example 1 with Token

use of com.auth0.json.mgmt.Token in project Taier by DTStack.

the class TokenService method decryption.

public DTToken decryption(String tokenText) {
    Assert.notNull(tokenText, "JWT Token Text can't blank.");
    try {
        /**
         * 验证
         */
        DecodedJWT jwt = JWT.require(Algorithm.HMAC256(JWT_TOKEN)).build().verify(tokenText);
        DTToken token = new DTToken();
        token.setUserName(jwt.getClaim(DTToken.USER_NAME).asString());
        token.setUserId(Long.parseLong(jwt.getClaim(DTToken.USER_ID).asString()));
        if (!jwt.getClaim(DTToken.TENANT_ID).isNull()) {
            token.setTenantId(Long.parseLong(jwt.getClaim(DTToken.TENANT_ID).asString()));
        }
        token.setExpireAt(jwt.getExpiresAt());
        return token;
    } catch (UnsupportedEncodingException e) {
        if (log.isErrorEnabled()) {
            log.error("JWT Token decode Error.", e);
        }
        throw new RdosDefineException("DT Token解码异常.");
    } catch (TokenExpiredException e) {
        if (log.isErrorEnabled()) {
            log.error("JWT Token expire.", e);
        }
        throw new RdosDefineException("DT Token已过期");
    }
}
Also used : TokenExpiredException(com.auth0.jwt.exceptions.TokenExpiredException) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DTToken(com.dtstack.taier.develop.dto.user.DTToken) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 2 with Token

use of com.auth0.json.mgmt.Token in project Taier by DTStack.

the class TokenService method decryptionWithOutExpire.

public DTToken decryptionWithOutExpire(String tokenText) {
    Assert.notNull(tokenText, "JWT Token Text can't blank.");
    try {
        DecodedJWT jwt = JWT.require(Algorithm.HMAC256(JWT_TOKEN)).build().verify(tokenText);
        DTToken token = new DTToken();
        token.setUserName(jwt.getClaim(DTToken.USER_NAME).asString());
        token.setUserId(Long.parseLong(jwt.getClaim(DTToken.USER_ID).asString()));
        if (!jwt.getClaim(DTToken.TENANT_ID).isNull()) {
            token.setTenantId(Long.parseLong(jwt.getClaim(DTToken.TENANT_ID).asString()));
        }
        return token;
    } catch (UnsupportedEncodingException e) {
        throw new RdosDefineException("DT Token解码异常.");
    }
}
Also used : RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DTToken(com.dtstack.taier.develop.dto.user.DTToken) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 3 with Token

use of com.auth0.json.mgmt.Token in project sda-dropwizard-commons by SDA-SE.

the class AuthBuilder method buildToken.

/**
 * @return the signed and encoded token, e.g. {@code eyXXX.eyYYY.ZZZ}
 */
public String buildToken() {
    Algorithm algorithm = Algorithm.RSA256(null, privateKey);
    JWTCreator.Builder builder = JWT.create().withKeyId(keyId).withIssuer(issuer).withSubject(subject);
    claims.keySet().forEach(key -> {
        Object value = claims.get(key);
        if (value instanceof String) {
            builder.withClaim(key, (String) value);
        } else if (value instanceof Long) {
            builder.withClaim(key, (Long) value);
        } else if (value instanceof Integer) {
            builder.withClaim(key, (Integer) value);
        } else if (value instanceof Double) {
            builder.withClaim(key, (Double) value);
        } else if (value instanceof Date) {
            builder.withClaim(key, (Date) value);
        } else if (value instanceof Boolean) {
            builder.withClaim(key, (Boolean) value);
        } else if (value instanceof String[]) {
            builder.withArrayClaim(key, (String[]) value);
        } else if (value instanceof Long[]) {
            builder.withArrayClaim(key, (Long[]) value);
        } else if (value instanceof Integer[]) {
            builder.withArrayClaim(key, (Integer[]) value);
        }
    });
    return builder.sign(algorithm);
}
Also used : JWTCreator(com.auth0.jwt.JWTCreator) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date)

Example 4 with Token

use of com.auth0.json.mgmt.Token in project sda-dropwizard-commons by SDA-SE.

the class AuthBuilderTest method shouldAddIntegerClaim.

@Test
public void shouldAddIntegerClaim() {
    String token = authBuilder.addClaim("testKey", 42).buildToken();
    Claim claim = JWT.decode(token).getClaim("testKey");
    assertThat(claim.asInt()).isEqualTo(42);
}
Also used : Claim(com.auth0.jwt.interfaces.Claim) Test(org.junit.Test)

Example 5 with Token

use of com.auth0.json.mgmt.Token in project sda-dropwizard-commons by SDA-SE.

the class AuthBuilderTest method shouldAddDoubleClaim.

@Test
public void shouldAddDoubleClaim() {
    String token = authBuilder.addClaim("testKey", 3.141D).buildToken();
    Claim claim = JWT.decode(token).getClaim("testKey");
    assertThat(claim.asDouble()).isEqualTo(3.141D);
}
Also used : Claim(com.auth0.jwt.interfaces.Claim) 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