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

be.looorent.micronaut.security.TokenParserImpl Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package be.looorent.micronaut.security;



/**
 * Wraps the logic to verify & parse a JWT and returns its content.
 */
import io.jsonwebtoken.*;
import io.micronaut.context.annotation.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Singleton;

import static be.looorent.micronaut.security.SecurityErrorType.*;

/**
 * Default implementation that uses jjwt to read and validate a JWT.
 * @author Lorent Lempereur - [email protected]
 */
@Singleton
class TokenParserImpl implements TokenParser {

    private static Logger LOG = LoggerFactory.getLogger(TokenParser.class);

    private final TokenValidator tokenValidator;
    private final SecurityContextFactory securityContextFactory;
    private final JwtParser parser;

    TokenParserImpl(@Value("${security.token.issuer}") String tokenIssuer,
                    PublicKeyResolver publicKeyResolver,
                    SecurityContextFactory securityContextFactory,
                    TokenValidator tokenValidator) {
        if (tokenIssuer == null || tokenIssuer.isEmpty()) {
            throw new IllegalArgumentException("Property 'security.token.issuer' (string) must be set");
        }
        if (publicKeyResolver == null) {
            throw new IllegalArgumentException("No implementation of PublicKeyResolver has been found");
        }
        if (securityContextFactory == null) {
            throw new IllegalArgumentException("No implementation of SecurityContextFactory has been found");
        }
        if (tokenValidator == null) {
            throw new IllegalArgumentException("No implementation of TokenValidator has been found");
        }

        this.tokenValidator = tokenValidator;
        this.securityContextFactory = securityContextFactory;
        this.parser = Jwts.parser()
                .setSigningKeyResolver(publicKeyResolver)
                .requireIssuer(tokenIssuer);
    }

    @Override
    public SecurityContext parse(String token) throws SecurityException {
        try {
            Claims claims = parser.parseClaimsJws(token).getBody();
            tokenValidator.validate(claims);
            return securityContextFactory.createSecurityContext(claims);
        } catch (UnsupportedJwtException e) {
            throw JWT_UNSUPPORTED.toException();
        } catch (MalformedJwtException e) {
            throw JWT_MALFORMED.toException();
        } catch (SignatureException e) {
            throw JWT_WRONG_SIGNATURE.toException();
        } catch (ExpiredJwtException e) {
            throw JWT_EXPIRED.toException();
        } catch (SecurityException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("An error occurred when parsing a JWT", e);
            throw new SecurityException(UNKWOWN, e.getMessage());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy