List of usage examples for org.springframework.security.authentication UsernamePasswordAuthenticationToken setDetails
public void setDetails(Object details)
From source file:org.cloudfoundry.identity.uaa.authentication.AbstractClientParametersAuthenticationFilter.java
private Authentication performClientAuthentication(HttpServletRequest req, Map<String, String> loginInfo, String clientId) {/*from w w w.j a v a 2s.co m*/ if (clientId != null) { Result policyResult = loginPolicy.isAllowed(clientId); if (!policyResult.isAllowed()) { throw new ClientLockoutException("Client " + clientId + " has " + policyResult.getFailureCount() + " failed authentications within the last checking period."); } } String clientSecret = loginInfo.get(CLIENT_SECRET); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(clientId, clientSecret); authentication.setDetails(new UaaAuthenticationDetails(req, clientId)); try { Authentication auth = clientAuthenticationManager.authenticate(authentication); if (auth == null || !auth.isAuthenticated()) { throw new BadCredentialsException("Client Authentication failed."); } loginInfo.remove(CLIENT_SECRET); AuthorizationRequest authorizationRequest = new AuthorizationRequest(clientId, getScope(req)); authorizationRequest.setRequestParameters(getSingleValueMap(req)); authorizationRequest.setApproved(true); //must set this to true in order for //Authentication.isAuthenticated to return true OAuth2Authentication result = new OAuth2Authentication(authorizationRequest.createOAuth2Request(), null); result.setAuthenticated(true); return result; } catch (AuthenticationException e) { throw new BadCredentialsException(e.getMessage(), e); } catch (Exception e) { logger.debug("Unable to authenticate client: " + clientId, e); throw new BadCredentialsException(e.getMessage(), e); } }
From source file:org.cloudfoundry.identity.uaa.authentication.BackwardsCompatibleTokenEndpointAuthenticationFilter.java
/** * If the incoming request contains user credentials in headers or parameters then extract them here into an * Authentication token that can be validated later. This implementation only recognises password grant requests and * extracts the username and password.// w w w. j a va 2 s .c om * * @param request the incoming request, possibly with user credentials * @return an authentication for validation (or null if there is no further authentication) */ protected Authentication extractCredentials(HttpServletRequest request) { String username = request.getParameter("username"); String password = request.getParameter("password"); UsernamePasswordAuthenticationToken credentials = new UsernamePasswordAuthenticationToken(username, password); credentials.setDetails(authenticationDetailsSource.buildDetails(request)); return credentials; }
From source file:org.cloudfoundry.identity.uaa.authentication.ClientParametersAuthenticationFilter.java
private Authentication performClientAuthentication(HttpServletRequest req, Map<String, String> loginInfo, String clientId) {// www . j a va2 s .c om String clientSecret = loginInfo.get(CLIENT_SECRET); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(clientId, clientSecret); authentication.setDetails(new UaaAuthenticationDetails(req, clientId)); try { Authentication auth = clientAuthenticationManager.authenticate(authentication); if (auth == null || !auth.isAuthenticated()) { throw new BadCredentialsException("Client Authentication failed."); } loginInfo.remove(CLIENT_SECRET); AuthorizationRequest authorizationRequest = new AuthorizationRequest(clientId, getScope(req)); authorizationRequest.setRequestParameters(getSingleValueMap(req)); authorizationRequest.setApproved(true); //must set this to true in order for //Authentication.isAuthenticated to return true OAuth2Authentication result = new OAuth2Authentication(authorizationRequest.createOAuth2Request(), null); result.setAuthenticated(true); return result; } catch (AuthenticationException e) { throw new BadCredentialsException(e.getMessage(), e); } catch (Exception e) { logger.debug("Unable to authenticate client: " + clientId, e); throw new BadCredentialsException(e.getMessage(), e); } }
From source file:org.cloudfoundry.identity.uaa.authentication.login.RemoteAuthenticationEndpoint.java
@RequestMapping(value = { "/authenticate" }, method = RequestMethod.POST) @ResponseBody//from www . jav a 2s . c o m public HttpEntity<Map<String, String>> authenticate(HttpServletRequest request, @RequestParam(value = "username", required = true) String username, @RequestParam(value = "password", required = true) String password) { Map<String, String> responseBody = new HashMap<>(); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); token.setDetails(new UaaAuthenticationDetails(request)); HttpStatus status = HttpStatus.UNAUTHORIZED; try { Authentication a = authenticationManager.authenticate(token); responseBody.put("username", a.getName()); if (a.getPrincipal() != null && a.getPrincipal() instanceof UaaPrincipal) { responseBody.put("email", ((UaaPrincipal) a.getPrincipal()).getEmail()); } processAdditionalInformation(responseBody, a); status = HttpStatus.OK; } catch (AccountNotVerifiedException e) { responseBody.put("error", "account not verified"); status = HttpStatus.FORBIDDEN; } catch (AuthenticationException e) { responseBody.put("error", "authentication failed"); } catch (Exception e) { logger.debug("Failed to authenticate user ", e); responseBody.put("error", "error"); status = HttpStatus.INTERNAL_SERVER_ERROR; } return new ResponseEntity<>(responseBody, status); }
From source file:org.cloudfoundry.identity.uaa.authentication.manager.ChainedAuthenticationManager.java
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (authentication == null) { return authentication; }/*from w w w. j a v a2s .c o m*/ UsernamePasswordAuthenticationToken output = null; if (authentication instanceof UsernamePasswordAuthenticationToken) { output = (UsernamePasswordAuthenticationToken) authentication; } else { output = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), authentication.getCredentials(), authentication.getAuthorities()); output.setDetails(authentication.getDetails()); } boolean authenticated = false; Authentication auth = null; AuthenticationException lastException = null; boolean lastResult = false; boolean shallContinue = true; if (delegates == null || delegates.length == 0) { throw new ProviderNotFoundException("No available authentication providers."); } for (int i = 0; shallContinue && i < delegates.length; i++) { boolean shallAuthenticate = (i == 0) || (lastResult && IF_PREVIOUS_TRUE.equals(delegates[i].getRequired())) || ((!lastResult) && IF_PREVIOUS_FALSE.equals(delegates[i].getRequired())); if (shallAuthenticate) { if (logger.isDebugEnabled()) { logger.debug("Attempting chained authentication of " + output + " with manager:" + delegates[i].getAuthenticationManager() + " required:" + delegates[i].getRequired()); } Authentication thisAuth = null; try { thisAuth = delegates[i].getAuthenticationManager().authenticate(auth != null ? auth : output); } catch (AuthenticationException x) { if (logger.isDebugEnabled()) { logger.debug("Chained authentication exception:" + x.getMessage() + " at:" + (x.getStackTrace().length > 0 ? x.getStackTrace()[0] : "(no stack trace)")); } lastException = x; if (delegates[i].getStopIf() != null) { for (Class<? extends AuthenticationException> exceptionClass : delegates[i].getStopIf()) { if (exceptionClass.isAssignableFrom(x.getClass())) { shallContinue = false; break; } } } } lastResult = thisAuth != null && thisAuth.isAuthenticated(); if (lastResult) { authenticated = true; auth = thisAuth; } else { authenticated = false; auth = null; } } else { shallContinue = false; } if (logger.isDebugEnabled()) { logger.debug("Chained Authentication status of " + output + " with manager:" + delegates[i] + "; Authenticated:" + authenticated); } } if (authenticated) { return auth; } else if (lastException != null) { //we had at least one authentication exception, throw it throw lastException; } else { //not authenticated, but return the last of the result return auth; } }
From source file:org.cloudfoundry.identity.uaa.authentication.manager.UsernamePasswordExtractingAuthenticationManager.java
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (authentication == null) { return authentication; }/*w w w . j a v a2 s .c om*/ UsernamePasswordAuthenticationToken output = null; if (authentication instanceof UsernamePasswordAuthenticationToken) { output = (UsernamePasswordAuthenticationToken) authentication; } else { output = new UsernamePasswordAuthenticationToken(authentication, authentication.getCredentials(), authentication.getAuthorities()); output.setAuthenticated(authentication.isAuthenticated()); output.setDetails(authentication.getDetails()); } return delegate.authenticate(output); }
From source file:org.cloudfoundry.identity.uaa.authentication.RemoteAuthenticationEndpoint.java
@RequestMapping(value = { "/authenticate" }, method = RequestMethod.POST) @ResponseBody//from w w w.j av a 2s. c o m public HttpEntity<AuthenticationResponse> authenticate(HttpServletRequest request, @RequestParam(value = "username", required = true) String username, @RequestParam(value = "password", required = true) String password) { AuthenticationResponse response = new AuthenticationResponse(); UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); token.setDetails(new UaaAuthenticationDetails(request)); HttpStatus status = HttpStatus.UNAUTHORIZED; try { Authentication a = authenticationManager.authenticate(token); response.setUsername(a.getName()); if (a.getPrincipal() != null && a.getPrincipal() instanceof UaaPrincipal) { response.setEmail(((UaaPrincipal) a.getPrincipal()).getEmail()); } processAdditionalInformation(response, a); status = HttpStatus.OK; } catch (AccountNotVerifiedException e) { response.setError("account not verified"); status = HttpStatus.FORBIDDEN; } catch (AuthenticationException e) { response.setError("authentication failed"); } catch (Exception e) { logger.debug("Failed to authenticate user ", e); response.setError("error"); status = HttpStatus.INTERNAL_SERVER_ERROR; } return new ResponseEntity<>(response, status); }
From source file:org.cloudfoundry.identity.uaa.client.ClientAdminEndpoints.java
private boolean authenticateClient(String clientId, String clientSecret) { UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(clientId, clientSecret);/* w ww.j a v a 2s. c o m*/ try { HttpServletRequest curRequest = ((ServletRequestAttributes) RequestContextHolder .currentRequestAttributes()).getRequest(); if (curRequest != null) { authentication.setDetails(new UaaAuthenticationDetails(curRequest, clientId)); } } catch (IllegalStateException x) { //ignore - means no thread bound request found } try { Authentication auth = authenticationManager.authenticate(authentication); return auth.isAuthenticated(); } catch (AuthenticationException e) { return false; } catch (Exception e) { logger.debug("Unable to authenticate/validate " + clientId, e); return false; } }
From source file:org.cloudfoundry.identity.uaa.login.AutologinAuthenticationManager.java
@Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { if (!(authentication instanceof AuthzAuthenticationRequest)) { return null; }//www . j a v a2 s . c o m AuthzAuthenticationRequest request = (AuthzAuthenticationRequest) authentication; Map<String, String> info = request.getInfo(); String code = info.get("code"); ExpiringCode ec = doRetrieveCode(code); SocialClientUserDetails user = null; try { if (ec != null) { user = new ObjectMapper().readValue(ec.getData(), SocialClientUserDetails.class); } } catch (IOException x) { throw new BadCredentialsException("JsonConversion error", x); } if (user == null) { throw new BadCredentialsException("Cannot redeem provided code for user"); } // ensure that we stored clientId String clientId = null; String origin = null; String userId = null; Object principal = user.getUsername(); if (user.getDetails() instanceof String) { clientId = (String) user.getDetails(); } else if (user.getDetails() instanceof Map) { Map<String, String> map = (Map<String, String>) user.getDetails(); clientId = map.get("client_id"); origin = map.get(Origin.ORIGIN); userId = map.get("user_id"); principal = new UaaPrincipal(userId, user.getUsername(), null, origin, null); } if (clientId == null) { throw new BadCredentialsException("Cannot redeem provided code for user, client id missing"); } // validate the client Id if (!(authentication.getDetails() instanceof UaaAuthenticationDetails)) { throw new BadCredentialsException("Cannot redeem provided code for user, auth details missing"); } UaaAuthenticationDetails details = (UaaAuthenticationDetails) authentication.getDetails(); if (!clientId.equals(details.getClientId())) { throw new BadCredentialsException("Cannot redeem provided code for user, client mismatch"); } UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(principal, null, user.getAuthorities()); result.setDetails(authentication.getDetails()); return result; }
From source file:org.egov.infra.config.security.authentication.filter.ApplicationAuthenticationFilter.java
@Override protected void setDetails(HttpServletRequest request, UsernamePasswordAuthenticationToken authToken) { authToken.setDetails(authenticationDetailsSource.buildDetails(request)); }