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

it.cosenonjaviste.security.jwt.model.JwtAdapter Maven / Gradle / Ivy

The newest version!
package it.cosenonjaviste.security.jwt.model;

import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import it.cosenonjaviste.security.jwt.utils.JwtConstants;

import java.util.List;

public class JwtAdapter {

    private Algorithm algorithm;

    private DecodedJWT jwt;

    private String userIdClaim;

    private String rolesClaim;

    public JwtAdapter(Algorithm algorithm, DecodedJWT jwt) {
        this(algorithm, jwt, JwtConstants.USER_ID, JwtConstants.ROLES);
    }

    public JwtAdapter(Algorithm algorithm, DecodedJWT jwt, String userIdClaim, String rolesClaim) {
        this.algorithm = algorithm;
        this.jwt = jwt;
        this.userIdClaim = userIdClaim != null ? userIdClaim : JwtConstants.USER_ID;
        this.rolesClaim = rolesClaim != null ? rolesClaim: JwtConstants.ROLES;
    }

    /**
     * Convenience method to retrieve userId value from token claim
     *
     * @return userId value
     */
    public String getUserId() {
        return this.jwt.getClaim(userIdClaim).asString();
    }

    /**
     * Convenience method to retrieve roles value from token claim
     *
     * @return roles value collection
     */
    public List getRoles() {
        return this.jwt.getClaim(rolesClaim).asList(String.class);
    }

    /**
     * Return validated decodedJWT
     *
     * @return DecodedJWT instance
     */
    public DecodedJWT getDecodedJWT() {
        return this.jwt;
    }

    /**
     * @return algorithm used for encoding the token
     */
    public Algorithm getAlgorithm() {
        return algorithm;
    }

    /**
     * @return custom user id claim
     */
    public String getUserIdClaim() {
        return userIdClaim;
    }

    /**
     * @return custom roles claim
     */
    public String getRolesClaim() {
        return rolesClaim;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy