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

de.cidaas.interceptor.authentication.PreAuthenticatedAuthenticationJsonWebToken Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package de.cidaas.interceptor.authentication;

import de.cidaas.jwt.JWT;
import de.cidaas.jwt.JWTVerifier;
import de.cidaas.jwt.exceptions.JWTDecodeException;
import de.cidaas.jwt.exceptions.JWTVerificationException;
import de.cidaas.jwt.interfaces.DecodedJWT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;
import java.util.Collections;

public class PreAuthenticatedAuthenticationJsonWebToken implements Authentication, JwtAuthentication {

    private static Logger logger = LoggerFactory.getLogger(PreAuthenticatedAuthenticationJsonWebToken.class);

    private final DecodedJWT token;

    PreAuthenticatedAuthenticationJsonWebToken(DecodedJWT token) {
        this.token = token;
    }

    @Override
    public Collection getAuthorities() {
        return Collections.emptyList();
    }

    @Override
    public Object getCredentials() {
        return token.getToken();
    }

    @Override
    public Object getDetails() {
        return token;
    }

    @Override
    public Object getPrincipal() {
        return token.getSubject();
    }

    @Override
    public boolean isAuthenticated() {
        return false;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {

    }

    @Override
    public String getName() {
        return token.getSubject();
    }

    public static PreAuthenticatedAuthenticationJsonWebToken usingToken(String token) {
        if (token == null) {
            logger.debug("No token was provided to build {}", PreAuthenticatedAuthenticationJsonWebToken.class.getName());
            return null;
        }
        try {
            DecodedJWT jwt = JWT.decode(token);
            return new PreAuthenticatedAuthenticationJsonWebToken(jwt);
        } catch (JWTDecodeException e) {
            logger.debug("Failed to decode token as jwt", e);
            return null;
        }
    }

    @Override
    public String getToken() {
        return token.getToken();
    }

    @Override
    public String getKeyId() {
        return token.getKeyId();
    }

    @Override
    public Authentication verify(JWTVerifier verifier) throws JWTVerificationException {
        return new AuthenticationJsonWebToken(token.getToken(), verifier);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy