use of com.auth0.jwt.JWTVerifier 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;
}
use of com.auth0.jwt.JWTVerifier in project fizz-gateway-community by wehotel.
the class JwtAuthPluginFilter method verify.
/**
* Verify JWT
*
* @param token
* @param secretKey key for HS256/HS384/HS512
* @param publicKey pub key for RSA or ECDSA
* @return
* @throws Exception
*/
public DecodedJWT verify(String token, String secretKey, String publicKey) {
try {
DecodedJWT jwt = JWT.decode(token);
String alg = jwt.getAlgorithm();
Algorithm algorithm = null;
switch(alg) {
case "HS256":
algorithm = Algorithm.HMAC256(secretKey);
break;
case "HS384":
algorithm = Algorithm.HMAC384(secretKey);
break;
case "HS512":
algorithm = Algorithm.HMAC512(secretKey);
break;
case "RS256":
algorithm = Algorithm.RSA256((RSAPublicKey) PemUtils.readPublicKeyFromString(publicKey, RSA), null);
break;
case "RS384":
algorithm = Algorithm.RSA384((RSAPublicKey) PemUtils.readPublicKeyFromString(publicKey, RSA), null);
break;
case "RS512":
algorithm = Algorithm.RSA512((RSAPublicKey) PemUtils.readPublicKeyFromString(publicKey, RSA), null);
break;
case "ES256":
algorithm = Algorithm.ECDSA256((ECPublicKey) PemUtils.readPublicKeyFromString(publicKey, EC), null);
break;
case "ES256K":
algorithm = Algorithm.ECDSA256K((ECPublicKey) PemUtils.readPublicKeyFromString(publicKey, EC), null);
break;
case "ES384":
algorithm = Algorithm.ECDSA384((ECPublicKey) PemUtils.readPublicKeyFromString(publicKey, EC), null);
break;
case "ES512":
algorithm = Algorithm.ECDSA512((ECPublicKey) PemUtils.readPublicKeyFromString(publicKey, EC), null);
break;
}
if (algorithm == null) {
// Algorithm NOT Supported
log.warn("{} Algorithm NOT Supported", alg);
} else {
JWTVerifier verifier = JWT.require(algorithm).build();
try {
return verifier.verify(token);
} catch (JWTVerificationException e) {
// Verification failed
log.warn("JWT verification failed: {}", e.getMessage());
}
}
} catch (Exception e) {
log.warn("JWT verification exception", e);
}
return null;
}
use of com.auth0.jwt.JWTVerifier in project structr by structr.
the class JWTHelper method validateTokenWithSecret.
private static Map<String, Claim> validateTokenWithSecret(String token, String secret) {
try {
Algorithm alg = Algorithm.HMAC256(secret.getBytes(StandardCharsets.UTF_8));
JWTVerifier verifier = JWT.require(alg).build();
DecodedJWT decodedJWT = verifier.verify(token);
return decodedJWT.getClaims();
} catch (JWTVerificationException e) {
logger.debug("Invalid token", e);
}
return null;
}
use of com.auth0.jwt.JWTVerifier in project structr by structr.
the class JWTHelper method validateTokenWithKeystore.
private static Map<String, Claim> validateTokenWithKeystore(String token, Algorithm alg) {
try {
JWTVerifier verifier = JWT.require(alg).build();
DecodedJWT decodedJWT = verifier.verify(token);
return decodedJWT.getClaims();
} catch (JWTVerificationException e) {
logger.debug("Invalid token", e);
}
return null;
}
use of com.auth0.jwt.JWTVerifier in project structr by structr.
the class JWTHelper method getPrincipalForAccessToken.
public static Principal getPrincipalForAccessToken(final String token, final PropertyKey<String> eMailKey) throws FrameworkException {
final String jwtSecretType = Settings.JWTSecretType.getValue();
Principal user = null;
switch(jwtSecretType) {
default:
case "secret":
user = getUserForAccessTokenWithSecret(token, eMailKey);
break;
case "keypair":
user = getPrincipalForAccessTokenWithKeystore(token, eMailKey);
break;
case "jwks":
final String provider = Settings.JWTSProvider.getValue();
final String issuer = Settings.JWTIssuer.getValue();
if (provider != null) {
try {
DecodedJWT jwt = JWT.decode(token);
final String kid = jwt.getKeyId();
if (kid != null) {
// if no issuer is specified, we can assume that issuer url = provider url.
JwkProvider jwkProvider;
if (!StringUtils.isEmpty(issuer) && !StringUtils.equals("structr", issuer)) {
jwkProvider = new UrlJwkProvider(new URL(provider));
} else {
// loads jwks from .well-known resource of provider
jwkProvider = new UrlJwkProvider(provider);
}
Jwk jwk = jwkProvider.get(kid);
Algorithm algorithm = Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null);
JWTVerifier verifier = JWT.require(algorithm).withIssuer(issuer).build();
jwt = verifier.verify(jwt);
user = getPrincipalForTokenClaims(jwt.getClaims(), eMailKey);
}
} catch (JWTVerificationException ex) {
throw new FrameworkException(422, ex.getMessage());
} catch (Exception ex) {
logger.warn("Error while trying to process JWKS.\n {}", ex.getMessage());
throw new FrameworkException(422, "Error while trying to process JWKS.");
}
}
break;
}
return user;
}
Aggregations