use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project cuba by cuba-platform.
the class CubaRestLastSecurityFilter method logRequest.
/**
* Method logs REST API method invocation
*/
protected void logRequest(ServletRequest request) {
if (log.isDebugEnabled()) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
String tokenValue = "";
if (authentication instanceof CubaAnonymousAuthenticationToken) {
tokenValue = "anonymous";
}
if (authentication.getDetails() instanceof OAuth2AuthenticationDetails) {
tokenValue = ((OAuth2AuthenticationDetails) authentication.getDetails()).getTokenValue();
}
log.debug("REST API request [{}] {} {} {}", tokenValue, ((HttpServletRequest) request).getMethod(), getRequestURL((HttpServletRequest) request), request.getRemoteAddr());
}
}
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-security-oauth by spring-projects.
the class OAuth2AuthenticationManager method authenticate.
/**
* Expects the incoming authentication request to have a principal value that is an access token value (e.g. from an
* authorization header). Loads an authentication from the {@link ResourceServerTokenServices} and checks that the
* resource id is contained in the {@link AuthorizationRequest} (if one is specified). Also copies authentication
* details over from the input to the output (e.g. typically so that the access token value and request details can
* be reported later).
*
* @param authentication an authentication request containing an access token value as the principal
* @return an {@link OAuth2Authentication}
*
* @see org.springframework.security.authentication.AuthenticationManager#authenticate(org.springframework.security.core.Authentication)
*/
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if (authentication == null) {
throw new InvalidTokenException("Invalid token (token not found)");
}
String token = (String) authentication.getPrincipal();
OAuth2Authentication auth = tokenServices.loadAuthentication(token);
if (auth == null) {
throw new InvalidTokenException("Invalid token: " + token);
}
Collection<String> resourceIds = auth.getOAuth2Request().getResourceIds();
if (resourceId != null && resourceIds != null && !resourceIds.isEmpty() && !resourceIds.contains(resourceId)) {
throw new OAuth2AccessDeniedException("Invalid token does not contain resource id (" + resourceId + ")");
}
checkClientDetails(auth);
if (authentication.getDetails() instanceof OAuth2AuthenticationDetails) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
// Guard against a cached copy of the same details
if (!details.equals(auth.getDetails())) {
// Preserve the authentication details from the one loaded by token services
details.setDecodedDetails(auth.getDetails());
}
}
auth.setDetails(authentication.getDetails());
auth.setAuthenticated(true);
return auth;
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-cloud-security by spring-cloud.
the class AccessTokenContextRelay method copyToken.
/**
* Attempt to copy an access token from the security context into the oauth2 context.
*
* @return true if the token was copied
*/
public boolean copyToken() {
if (context.getAccessToken() == null) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) {
Object details = authentication.getDetails();
if (details instanceof OAuth2AuthenticationDetails) {
OAuth2AuthenticationDetails holder = (OAuth2AuthenticationDetails) details;
String token = holder.getTokenValue();
DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken(token);
String tokenType = holder.getTokenType();
if (tokenType != null) {
accessToken.setTokenType(tokenType);
}
context.setAccessToken(accessToken);
return true;
}
}
}
return false;
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-cloud-security by spring-cloud.
the class OAuth2TokenRelayFilterTests method init.
@Before
public void init() {
Authentication user = new UsernamePasswordAuthenticationToken("user", "password");
AuthorizationRequest authorizationRequest = new AuthorizationRequest();
authorizationRequest.setClientId("client");
OAuth2Request request = authorizationRequest.createOAuth2Request();
auth = new OAuth2Authentication(request, user);
httpRequest.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_TYPE, "bearer");
httpRequest.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE, "FOO");
auth.setDetails(new OAuth2AuthenticationDetails(httpRequest));
}
use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project credhub by cloudfoundry-incubator.
the class UserContextFactoryTest method setupOAuthMock.
private OAuth2Authentication setupOAuthMock(String grantType) {
OAuth2Authentication authentication = mock(OAuth2Authentication.class);
OAuth2Request oauth2Request = spy(new OAuth2Request(null, "TEST_CLIENT_ID", null, false, null, null, null, null, null));
OAuth2AccessToken token = mock(OAuth2AccessToken.class);
OAuth2AuthenticationDetails authDetails = mock(OAuth2AuthenticationDetails.class);
Map<String, Object> additionalInformation = new HashMap<>();
additionalInformation.put("user_id", "TEST_USER_ID");
additionalInformation.put("user_name", "TEST_USER_NAME");
additionalInformation.put("iss", "TEST_UAA_URL");
additionalInformation.put("iat", 1413495264);
Set<String> scopes = new HashSet<>();
scopes.add("scope1");
scopes.add("scope2");
when(oauth2Request.getGrantType()).thenReturn(grantType);
when(authentication.getDetails()).thenReturn(authDetails);
when(authDetails.getTokenValue()).thenReturn("tokenValue");
when(authentication.getOAuth2Request()).thenReturn(oauth2Request);
when(token.getAdditionalInformation()).thenReturn(additionalInformation);
when(token.getExpiration()).thenReturn(Date.from(Instant.ofEpochSecond(1413538464)));
when(token.getScope()).thenReturn(scopes);
when(tokenServicesMock.readAccessToken("tokenValue")).thenReturn(token);
return authentication;
}
Aggregations