Search in sources :

Example 11 with OAuth2AuthenticationDetails

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());
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Authentication(org.springframework.security.core.Authentication) OAuth2AuthenticationDetails(org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails)

Example 12 with OAuth2AuthenticationDetails

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;
}
Also used : InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) OAuth2AccessDeniedException(org.springframework.security.oauth2.client.resource.OAuth2AccessDeniedException) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication)

Example 13 with OAuth2AuthenticationDetails

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;
}
Also used : Authentication(org.springframework.security.core.Authentication) OAuth2AuthenticationDetails(org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)

Example 14 with OAuth2AuthenticationDetails

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));
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) AuthorizationRequest(org.springframework.security.oauth2.provider.AuthorizationRequest) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) OAuth2AuthenticationDetails(org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails) Before(org.junit.Before)

Example 15 with OAuth2AuthenticationDetails

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;
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) HashMap(java.util.HashMap) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OAuth2AuthenticationDetails(org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails) StringContains.containsString(org.hamcrest.core.StringContains.containsString) HashSet(java.util.HashSet)

Aggregations

OAuth2AuthenticationDetails (org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails)11 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)9 Authentication (org.springframework.security.core.Authentication)8 Test (org.junit.Test)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)4 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)3 HashSet (java.util.HashSet)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)2 PreAuthenticatedAuthenticationToken (org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken)2 Gson (com.google.gson.Gson)1 RequestContext (com.netflix.zuul.context.RequestContext)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 StringContains.containsString (org.hamcrest.core.StringContains.containsString)1 Before (org.junit.Before)1 EventListener (org.springframework.context.event.EventListener)1