use of com.auth0.jwt.JWTVerifier in project gravitee-management-rest-api by gravitee-io.
the class OAuth2AuthenticationResourceTest method verifyJwtToken.
private void verifyJwtToken(Response response) throws NoSuchAlgorithmException, InvalidKeyException, IOException, SignatureException, JWTVerifyException {
String cookieContent = response.getCookies().get(HttpHeaders.AUTHORIZATION).getValue();
assertThat(cookieContent, StringStartsWith.startsWith("Bearer "));
String jwt = cookieContent.substring(7);
JWTVerifier jwtVerifier = new JWTVerifier("myJWT4Gr4v1t33_S3cr3t");
Map<String, Object> mapJwt = jwtVerifier.verify(jwt);
assertEquals(mapJwt.get("sub"), "janedoe@example.com");
assertEquals(mapJwt.get("firstname"), "Jane");
assertEquals(mapJwt.get("iss"), "gravitee-management-auth");
assertEquals(mapJwt.get("sub"), "janedoe@example.com");
assertEquals(mapJwt.get("email"), "janedoe@example.com");
assertEquals(mapJwt.get("lastname"), "Doe");
}
use of com.auth0.jwt.JWTVerifier in project neubbs by nuitcoder.
the class SecretUtil method decryptUserInfoToken.
/**
* 解密用户信息 Token
* - 解密 JWT 生成的用户信息 Token, 获取 UserDO 对象
*
* @param token 密文 token
* @return UserDO 用户信息对象(包含 id,name,rank,state 属性)
*/
public static UserDO decryptUserInfoToken(String token) {
checkParamNotNull(token);
DecodedJWT decodedJWT;
try {
// decrypt HS256
JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SetConst.JWT_TOKEN_SECRET_KEY)).build();
// decoding Base64
decodedJWT = verifier.verify(token);
} catch (UnsupportedEncodingException | TokenExpiredException e) {
throw new UtilClassException(ApiMessage.UNKNOWN_ERROR).log(LogWarnEnum.UC10);
}
// Get User information(id, name, rank ,state)
UserDO user = new UserDO();
user.setId(decodedJWT.getClaim(ParamConst.ID).asInt());
user.setName(decodedJWT.getClaim(ParamConst.NAME).asString());
user.setRank(decodedJWT.getClaim(ParamConst.RANK).asString());
user.setState(decodedJWT.getClaim(ParamConst.STATE).asInt());
return user;
}
use of com.auth0.jwt.JWTVerifier in project gravitee-management-rest-api by gravitee-io.
the class UserServiceImpl method getDecodedJWT.
private DecodedJWT getDecodedJWT(String token) {
final String jwtSecret = environment.getProperty("jwt.secret");
if (jwtSecret == null || jwtSecret.isEmpty()) {
throw new IllegalStateException("JWT secret is mandatory");
}
Algorithm algorithm = Algorithm.HMAC256(jwtSecret);
JWTVerifier verifier = JWT.require(algorithm).withIssuer(environment.getProperty("jwt.issuer", DEFAULT_JWT_ISSUER)).build();
return verifier.verify(token);
}
use of com.auth0.jwt.JWTVerifier in project gravitee-management-rest-api by gravitee-io.
the class OAuth2AuthenticationResourceTest method verifyJwtToken.
private void verifyJwtToken(Response response) throws NoSuchAlgorithmException, InvalidKeyException, IOException, SignatureException, JWTVerificationException {
Token responseToken = response.readEntity(Token.class);
assertEquals("BEARER", responseToken.getTokenType().name());
String token = responseToken.getToken();
Algorithm algorithm = Algorithm.HMAC256("myJWT4Gr4v1t33_S3cr3t");
JWTVerifier jwtVerifier = JWT.require(algorithm).build();
DecodedJWT jwt = jwtVerifier.verify(token);
assertEquals(jwt.getSubject(), "janedoe@example.com");
assertEquals("Jane", jwt.getClaim("firstname").asString());
assertEquals("gravitee-management-auth", jwt.getClaim("iss").asString());
assertEquals("janedoe@example.com", jwt.getClaim("sub").asString());
assertEquals("janedoe@example.com", jwt.getClaim("email").asString());
assertEquals("Doe", jwt.getClaim("lastname").asString());
}
use of com.auth0.jwt.JWTVerifier in project nextprot-api by calipho-sib.
the class JWTCodecImpl method decodeJWT.
@Override
public Map<String, Object> decodeJWT(String token) {
try {
File publicKeyFile = new File(this.getClass().getClassLoader().getResource("keys/pubkey").toURI());
RSAPublicKey publicKey = (RSAPublicKey) PemUtils.readPublicKeyFromFile(publicKeyFile.toString(), "RSA");
Algorithm algorithm = Algorithm.RSA256(publicKey);
JWTVerifier verifier = JWT.require(algorithm).withIssuer("https://nextprot.auth0.com/").withAudience("https://nextprot.auth0.com/api/v2/").withAudience("https://nextprot.auth0.com/userinfo").build();
DecodedJWT jwt = verifier.verify(token);
Map<String, Object> map = new HashMap<>();
map.put(EMAIL, jwt.getClaim("https://www.nextprot.org/userinfo/email").asString());
return map;
} catch (IOException e) {
throw new NextprotSecurityException(e);
} catch (URISyntaxException e) {
throw new NextprotSecurityException(e);
}
}
Aggregations