Search in sources :

Example 1 with TokenExpiredException

use of com.auth0.jwt.exceptions.TokenExpiredException in project java-docs-samples by GoogleCloudPlatform.

the class VerifyingInstance method verifyToken.

void verifyToken(String token) {
    TokenVerifier gtv = new TokenVerifier();
    // Following are examples how to handle verification failure.
    try {
        DecodedGoogleJWTWrapper decodedJwt = gtv.verifyWithAudience(audience, token);
        System.out.println("Project id : " + decodedJwt.getProjectId());
        System.out.println("Project number : " + decodedJwt.getProjectNumber());
    // This are examples how to handle exceptions, which indicate verification failure.
    } catch (AlgorithmMismatchException e) {
        // We assume that downloaded certs are RSA256, this exception will happen if this changes.
        throw e;
    } catch (SignatureVerificationException e) {
        // Could not verify signature of a token, possibly someone provided forged token.
        throw e;
    } catch (TokenExpiredException e) {
        // We encountered old token, possibly replay attack.
        throw e;
    } catch (InvalidClaimException e) {
        // Different Audience for token and for verification, possibly token for other verifier.
        throw e;
    } catch (JWTVerificationException e) {
        // - InvalidClaimException
        throw e;
    }
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) TokenExpiredException(com.auth0.jwt.exceptions.TokenExpiredException) TokenVerifier(com.example.compute.signedmetadata.token.TokenVerifier) DecodedGoogleJWTWrapper(com.example.compute.signedmetadata.token.DecodedGoogleJWTWrapper) SignatureVerificationException(com.auth0.jwt.exceptions.SignatureVerificationException) InvalidClaimException(com.auth0.jwt.exceptions.InvalidClaimException) AlgorithmMismatchException(com.auth0.jwt.exceptions.AlgorithmMismatchException)

Example 2 with TokenExpiredException

use of com.auth0.jwt.exceptions.TokenExpiredException 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;
}
Also used : UtilClassException(org.neusoft.neubbs.exception.UtilClassException) TokenExpiredException(com.auth0.jwt.exceptions.TokenExpiredException) UserDO(org.neusoft.neubbs.entity.UserDO) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) JWTVerifier(com.auth0.jwt.JWTVerifier)

Aggregations

TokenExpiredException (com.auth0.jwt.exceptions.TokenExpiredException)2 JWTVerifier (com.auth0.jwt.JWTVerifier)1 AlgorithmMismatchException (com.auth0.jwt.exceptions.AlgorithmMismatchException)1 InvalidClaimException (com.auth0.jwt.exceptions.InvalidClaimException)1 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)1 SignatureVerificationException (com.auth0.jwt.exceptions.SignatureVerificationException)1 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 DecodedGoogleJWTWrapper (com.example.compute.signedmetadata.token.DecodedGoogleJWTWrapper)1 TokenVerifier (com.example.compute.signedmetadata.token.TokenVerifier)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 UserDO (org.neusoft.neubbs.entity.UserDO)1 UtilClassException (org.neusoft.neubbs.exception.UtilClassException)1