use of org.springframework.security.authentication.AbstractAuthenticationToken in project spring-security by spring-projects.
the class OpenSaml4AuthenticationProvider method authenticate.
/**
* @param authentication the authentication request object, must be of type
* {@link Saml2AuthenticationToken}
* @return {@link Saml2Authentication} if the assertion is valid
* @throws AuthenticationException if a validation exception occurs
*/
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
try {
Saml2AuthenticationToken token = (Saml2AuthenticationToken) authentication;
String serializedResponse = token.getSaml2Response();
Response response = parse(serializedResponse);
process(token, response);
AbstractAuthenticationToken authenticationResponse = this.responseAuthenticationConverter.convert(new ResponseToken(response, token));
if (authenticationResponse != null) {
authenticationResponse.setDetails(authentication.getDetails());
}
return authenticationResponse;
} catch (Saml2AuthenticationException ex) {
throw ex;
} catch (Exception ex) {
throw createAuthenticationException(Saml2ErrorCodes.INTERNAL_VALIDATION_ERROR, ex.getMessage(), ex);
}
}
use of org.springframework.security.authentication.AbstractAuthenticationToken in project ranger by apache.
the class RangerKRBAuthenticationFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
String authtype = PropertiesUtil.getProperty(RANGER_AUTH_TYPE);
HttpServletRequest httpRequest = (HttpServletRequest) request;
Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication();
if (isSpnegoEnable(authtype) && (existingAuth == null || !existingAuth.isAuthenticated())) {
KerberosName.setRules(PropertiesUtil.getProperty(NAME_RULES, "DEFAULT"));
String userName = null;
Cookie[] cookie = httpRequest.getCookies();
if (cookie != null) {
for (Cookie c : cookie) {
String cname = c.getName();
if (cname != null && "u".equalsIgnoreCase(cname)) {
int ustr = cname.indexOf("u=");
if (ustr != -1) {
int andStr = cname.indexOf("&", ustr);
if (andStr != -1) {
userName = cname.substring(ustr + 2, andStr);
}
}
} else if (cname != null && AUTH_COOKIE_NAME.equalsIgnoreCase(cname)) {
int ustr = cname.indexOf("u=");
if (ustr != -1) {
int andStr = cname.indexOf("&", ustr);
if (andStr != -1) {
userName = cname.substring(ustr + 2, andStr);
}
}
}
}
}
if ((existingAuth == null || !existingAuth.isAuthenticated()) && (!StringUtils.isEmpty(userName))) {
// --------------------------- To Create Ranger Session --------------------------------------
String rangerLdapDefaultRole = PropertiesUtil.getProperty("ranger.ldap.default.role", "ROLE_USER");
// if we get the userName from the token then log into ranger using the same user
final List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority(rangerLdapDefaultRole));
final UserDetails principal = new User(userName, "", grantedAuths);
final Authentication finalAuthentication = new UsernamePasswordAuthenticationToken(principal, "", grantedAuths);
WebAuthenticationDetails webDetails = new WebAuthenticationDetails(httpRequest);
((AbstractAuthenticationToken) finalAuthentication).setDetails(webDetails);
RangerAuthenticationProvider authenticationProvider = new RangerAuthenticationProvider();
Authentication authentication = authenticationProvider.authenticate(finalAuthentication);
authentication = getGrantedAuthority(authentication);
SecurityContextHolder.getContext().setAuthentication(authentication);
request.setAttribute("spnegoEnabled", true);
if (LOG.isDebugEnabled()) {
LOG.debug("Logged into Ranger as = " + userName);
}
} else {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("isSpnegoEnable = " + isSpnegoEnable(authtype) + " userName = " + userName + " request URL = " + getRequestURL(httpRequest));
if (existingAuth != null) {
LOG.debug("isAuthenticated: " + existingAuth.isAuthenticated());
}
}
if (StringUtils.equals(httpRequest.getParameter("action"), RestUtil.TIMEOUT_ACTION)) {
handleTimeoutRequest(httpRequest, (HttpServletResponse) response);
} else {
super.doFilter(request, response, filterChain);
}
} catch (Exception e) {
throw restErrorUtil.createRESTException("RangerKRBAuthenticationFilter Failed : " + e.getMessage());
}
}
} else {
String action = httpRequest.getParameter("action");
String doAsUser = request.getParameter("doAs");
if (LOG.isDebugEnabled()) {
LOG.debug("RangerKRBAuthenticationFilter: request URL = " + httpRequest.getRequestURI());
}
boolean allowTrustedProxy = PropertiesUtil.getBooleanProperty(ALLOW_TRUSTED_PROXY, false);
if (allowTrustedProxy && StringUtils.isNotEmpty(doAsUser) && existingAuth.isAuthenticated() && StringUtils.equals(action, RestUtil.TIMEOUT_ACTION)) {
HttpServletResponse httpResponse = (HttpServletResponse) response;
handleTimeoutRequest(httpRequest, httpResponse);
} else {
filterChain.doFilter(request, response);
}
}
}
Aggregations