Search in sources :

Example 1 with OAuth2AuthenticationDetails

use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-security-oauth by spring-projects.

the class OAuth2ClientAuthenticationProcessingFilterTests method testAuthenticationWithTokenType.

@Test
public void testAuthenticationWithTokenType() throws Exception {
    filter.setRestTemplate(restTemplate);
    filter.setTokenServices(tokenServices);
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
    token.setTokenType("foo");
    Mockito.when(restTemplate.getAccessToken()).thenReturn(token);
    Set<String> scopes = new HashSet<String>();
    scopes.addAll(Arrays.asList("read", "write"));
    OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request("client", false, scopes);
    this.authentication = new OAuth2Authentication(storedOAuth2Request, null);
    Mockito.when(tokenServices.loadAuthentication("FOO")).thenReturn(authentication);
    Authentication authentication = filter.attemptAuthentication(new MockHttpServletRequest(), null);
    assertEquals("foo", ((OAuth2AuthenticationDetails) authentication.getDetails()).getTokenType());
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with OAuth2AuthenticationDetails

use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-security-oauth by spring-projects.

the class OAuth2AuthenticationManagerTests method testDetailsEnhancedOnce.

@Test
public void testDetailsEnhancedOnce() throws Exception {
    authentication.setDetails("DETAILS");
    Mockito.when(tokenServices.loadAuthentication("FOO")).thenReturn(authentication);
    PreAuthenticatedAuthenticationToken request = new PreAuthenticatedAuthenticationToken("FOO", "");
    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE, "BAR");
    OAuth2AuthenticationDetails details = new OAuth2AuthenticationDetails(servletRequest);
    request.setDetails(details);
    Authentication result = manager.authenticate(request);
    // Authenticate the same request again to simulate what happens if the app is caching the result from
    // tokenServices.loadAuthentication():
    result = manager.authenticate(request);
    assertEquals(authentication, result);
    assertEquals("BAR", ((OAuth2AuthenticationDetails) result.getDetails()).getTokenValue());
    assertEquals("DETAILS", ((OAuth2AuthenticationDetails) result.getDetails()).getDecodedDetails());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) PreAuthenticatedAuthenticationToken(org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken) Test(org.junit.Test)

Example 3 with OAuth2AuthenticationDetails

use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-security-oauth by spring-projects.

the class ResourceServerConfigurationTests method testCustomAuthenticationDetailsSource.

@Test
public void testCustomAuthenticationDetailsSource() throws Exception {
    tokenStore.storeAccessToken(token, authentication);
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(AuthenticationDetailsSourceContext.class);
    context.refresh();
    MockMvc mvc = buildMockMvc(context);
    mvc.perform(MockMvcRequestBuilders.get("/").header("Authorization", "Bearer FOO")).andExpect(MockMvcResultMatchers.status().isNotFound());
    context.close();
    OAuth2AuthenticationDetails authenticationDetails = (OAuth2AuthenticationDetails) authentication.getDetails();
    assertEquals("Basic", authenticationDetails.getTokenType());
    assertEquals("BAR", authenticationDetails.getTokenValue());
}
Also used : OAuth2AuthenticationDetails(org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 4 with OAuth2AuthenticationDetails

use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-cloud-framework by zhuwj921.

the class WebContextUtil method getDetails.

/**
 * 获取当前上下文token的信息
 * @return
 */
public static OAuth2AuthenticationDetails getDetails() {
    Authentication authentication = getAuthentication();
    if (authentication == null) {
        return null;
    }
    OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
    return details;
}
Also used : Authentication(org.springframework.security.core.Authentication) OAuth2AuthenticationDetails(org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails)

Example 5 with OAuth2AuthenticationDetails

use of org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails in project spring-cloud-security by spring-cloud.

the class OAuth2TokenRelayFilter method shouldFilter.

@Override
public boolean shouldFilter() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth instanceof OAuth2Authentication) {
        Object details = auth.getDetails();
        if (details instanceof OAuth2AuthenticationDetails) {
            OAuth2AuthenticationDetails oauth = (OAuth2AuthenticationDetails) details;
            RequestContext ctx = RequestContext.getCurrentContext();
            if (ctx.containsKey("proxy")) {
                String id = (String) ctx.get("proxy");
                if (routes.containsKey(id)) {
                    if (!Route.Scheme.OAUTH2.matches(routes.get(id).getScheme())) {
                        return false;
                    }
                }
            }
            ctx.set(ACCESS_TOKEN, oauth.getTokenValue());
            ctx.set(TOKEN_TYPE, oauth.getTokenType() == null ? "Bearer" : oauth.getTokenType());
            return true;
        }
    }
    return false;
}
Also used : OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) OAuth2AuthenticationDetails(org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails) RequestContext(com.netflix.zuul.context.RequestContext)

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