Search in sources :

Example 1 with JwtTokenClaims

use of com.epam.pipeline.entity.security.JwtTokenClaims in project cloud-pipeline by epam.

the class JwtAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) {
    JwtRawToken jwtRawToken = (JwtRawToken) authentication.getCredentials();
    if (jwtRawToken == null) {
        throw new AuthenticationServiceException("Authentication error: missing token");
    }
    JwtTokenClaims claims;
    try {
        claims = tokenVerifier.readClaims(jwtRawToken.getToken());
    } catch (TokenVerificationException e) {
        throw new AuthenticationServiceException("Authentication error", e);
    }
    UserContext context = new UserContext(jwtRawToken, claims);
    return new JwtAuthenticationToken(context, context.getAuthorities());
}
Also used : JwtTokenClaims(com.epam.pipeline.entity.security.JwtTokenClaims) UserContext(com.epam.pipeline.security.UserContext) JwtRawToken(com.epam.pipeline.entity.security.JwtRawToken) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException)

Example 2 with JwtTokenClaims

use of com.epam.pipeline.entity.security.JwtTokenClaims in project cloud-pipeline by epam.

the class JwtFilterAuthenticationFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    JwtRawToken rawToken;
    String authorizationHeader = extractAuthHeader(request);
    try {
        if (!StringUtils.isEmpty(authorizationHeader)) {
            // attempt obtain JWT token from HTTP header
            rawToken = JwtRawToken.fromHeader(authorizationHeader);
            LOGGER.trace("Extracted JWT token from authorization HTTP header");
        } else {
            // else try to get token from cookies
            Cookie authCookie = extractAuthCookie(request);
            rawToken = JwtRawToken.fromCookie(authCookie);
            LOGGER.trace("Extracted JWT token from authorization cookie");
        }
        JwtTokenClaims claims = tokenVerifier.readClaims(rawToken.getToken());
        UserContext context = new UserContext(rawToken, claims);
        JwtAuthenticationToken token = new JwtAuthenticationToken(context, context.getAuthorities());
        token.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
        SecurityContextHolder.getContext().setAuthentication(token);
    } catch (AuthenticationServiceException | TokenVerificationException e) {
        LOGGER.trace(e.getMessage(), e);
    }
    filterChain.doFilter(request, response);
}
Also used : Cookie(javax.servlet.http.Cookie) JwtTokenClaims(com.epam.pipeline.entity.security.JwtTokenClaims) UserContext(com.epam.pipeline.security.UserContext) JwtRawToken(com.epam.pipeline.entity.security.JwtRawToken) WebAuthenticationDetailsSource(org.springframework.security.web.authentication.WebAuthenticationDetailsSource) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException)

Example 3 with JwtTokenClaims

use of com.epam.pipeline.entity.security.JwtTokenClaims in project cloud-pipeline by epam.

the class JwtTokenVerifier method readClaims.

public JwtTokenClaims readClaims(String jwtToken) {
    DecodedJWT decodedToken;
    try {
        decodedToken = JWT.require(Algorithm.RSA512(publicKey)).build().verify(jwtToken);
    } catch (JWTVerificationException jve) {
        throw new TokenVerificationException(jve);
    }
    JwtTokenClaims tokenClaims = JwtTokenClaims.builder().jwtTokenId(decodedToken.getId()).userName(decodedToken.getSubject()).userId(decodedToken.getClaim(CLAIM_USER_ID).asString()).orgUnitId(decodedToken.getClaim(CLAIM_ORG_UNIT_ID).asString()).roles(Arrays.asList(decodedToken.getClaim(CLAIM_ROLES).asArray(String.class))).groups(Arrays.asList(decodedToken.getClaim(CLAIM_GROUPS).asArray(String.class))).issuedAt(decodedToken.getIssuedAt().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()).expiresAt(decodedToken.getExpiresAt().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime()).external(!decodedToken.getClaim(CLAIM_EXTERNAL).isNull() && decodedToken.getClaim(CLAIM_EXTERNAL).asBoolean()).build();
    return validateClaims(tokenClaims);
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) JwtTokenClaims(com.epam.pipeline.entity.security.JwtTokenClaims) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Aggregations

JwtTokenClaims (com.epam.pipeline.entity.security.JwtTokenClaims)3 JwtRawToken (com.epam.pipeline.entity.security.JwtRawToken)2 UserContext (com.epam.pipeline.security.UserContext)2 AuthenticationServiceException (org.springframework.security.authentication.AuthenticationServiceException)2 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)1 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 Cookie (javax.servlet.http.Cookie)1 WebAuthenticationDetailsSource (org.springframework.security.web.authentication.WebAuthenticationDetailsSource)1