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已过期");
}
}
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解码异常.");
}
}
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);
}
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);
}
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);
}
Aggregations