use of com.epam.pipeline.security.UserContext 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.security.UserContext 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.security.UserContext in project cloud-pipeline by epam.
the class SAMLUserDetailsServiceImpl method loadUserBySAML.
@Override
public UserContext loadUserBySAML(SAMLCredential credential) {
String userName = credential.getNameID().getValue().toUpperCase();
List<String> groups = readAuthorities(credential);
Map<String, String> attributes = readAttributes(credential);
PipelineUser loadedUser = userManager.loadUserByName(userName);
if (loadedUser == null) {
String message = messageHelper.getMessage(MessageConstants.ERROR_USER_NAME_NOT_FOUND, userName);
if (!autoCreateUsers) {
throw new UsernameNotFoundException(message);
}
LOGGER.debug(message);
List<Long> roles = roleManager.getDefaultRolesIds();
PipelineUser createdUser = userManager.createUser(userName, roles, groups, attributes, null);
UserContext userContext = new UserContext(createdUser.getId(), userName);
userContext.setGroups(createdUser.getGroups());
LOGGER.debug("Created user {} with groups {}", userName, groups);
userContext.setRoles(createdUser.getRoles());
return userContext;
} else {
LOGGER.debug("Found user by name {}", userName);
loadedUser.setUserName(userName);
List<Long> roles = loadedUser.getRoles().stream().map(Role::getId).collect(Collectors.toList());
if (userManager.needToUpdateUser(groups, attributes, loadedUser)) {
loadedUser = userManager.updateUserSAMLInfo(loadedUser.getId(), userName, roles, groups, attributes);
LOGGER.debug("Updated user groups {} ", groups);
}
return new UserContext(loadedUser);
}
}
Aggregations