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());
}
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);
}
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);
}
Aggregations