All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.hcp.cf.logging.servlet.dynlog.TokenDecoder Maven / Gradle / Ivy

There is a newer version: 3.8.4
Show newest version
package com.sap.hcp.cf.logging.servlet.dynlog;

import java.security.interfaces.RSAPublicKey;
import java.util.Date;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;

public class TokenDecoder {

    private JWTVerifier verifier;

    public TokenDecoder(RSAPublicKey key) {
        verifier = JWT.require(Algorithm.RSA256((RSAPublicKey) key, null)).build();
    }

    /**
     * This method validates if a token has a valid signature as well as a valid
     * timestamp and returns the decoded token
     *
     * @throws DynamicLogLevelException
     */
    public DecodedJWT validateAndDecodeToken(String token) throws DynamicLogLevelException {
        try {
            DecodedJWT jwt = verifier.verify(token);
            Date exp = jwt.getExpiresAt();
            Date iat = jwt.getIssuedAt();
            Date now = new Date();

            if (exp != null && iat != null && now.after(iat) && now.before(exp)) {
                return jwt;
            } else {
                throw new DynamicLogLevelException("Token provided to dynamically change the log-level on thread-level is outdated");
            }
        } catch (JWTVerificationException e) {
            // Exception is not attached to avoid logging of JWT token
            throw new DynamicLogLevelException("Token could not be verified");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy