Search in sources :

Example 1 with UserRedirectRequiredException

use of org.springframework.security.oauth2.client.resource.UserRedirectRequiredException in project spring-boot by spring-projects.

the class UserInfoTokenServicesTests method badToken.

@Test
public void badToken() {
    this.services.setRestTemplate(this.template);
    given(this.template.getForEntity(any(String.class), eq(Map.class))).willThrow(new UserRedirectRequiredException("foo:bar", Collections.<String, String>emptyMap()));
    this.expected.expect(InvalidTokenException.class);
    assertThat(this.services.loadAuthentication("FOO").getName()).isEqualTo("unknown");
}
Also used : LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) Test(org.junit.Test)

Example 2 with UserRedirectRequiredException

use of org.springframework.security.oauth2.client.resource.UserRedirectRequiredException in project spring-security-oauth by spring-projects.

the class AuthorizationCodeAccessTokenProviderTests method testRedirectToAuthorizationEndpoint.

@Test
public void testRedirectToAuthorizationEndpoint() throws Exception {
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setCurrentUri("/come/back/soon");
    resource.setUserAuthorizationUri("http://localhost/oauth/authorize");
    try {
        provider.obtainAccessToken(resource, request);
        fail("Expected UserRedirectRequiredException");
    } catch (UserRedirectRequiredException e) {
        assertEquals("http://localhost/oauth/authorize", e.getRedirectUri());
        assertEquals("/come/back/soon", e.getStateToPreserve());
    }
}
Also used : DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 3 with UserRedirectRequiredException

use of org.springframework.security.oauth2.client.resource.UserRedirectRequiredException in project spring-security-oauth by spring-projects.

the class OAuth2RestTemplateTests method testTokenIsResetIfInvalid.

@Test
public void testTokenIsResetIfInvalid() throws Exception {
    DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("TEST");
    token.setExpiration(new Date(System.currentTimeMillis() - 1000));
    restTemplate.getOAuth2ClientContext().setAccessToken(token);
    restTemplate.setAccessTokenProvider(new StubAccessTokenProvider() {

        @Override
        public OAuth2AccessToken obtainAccessToken(OAuth2ProtectedResourceDetails details, AccessTokenRequest parameters) throws UserRedirectRequiredException, AccessDeniedException {
            throw new UserRedirectRequiredException("http://foo.com", Collections.<String, String>emptyMap());
        }
    });
    try {
        OAuth2AccessToken newToken = restTemplate.getAccessToken();
        assertNotNull(newToken);
        fail("Expected UserRedirectRequiredException");
    } catch (UserRedirectRequiredException e) {
    // planned
    }
    // context token should be reset as it clearly is invalid at this point
    assertNull(restTemplate.getOAuth2ClientContext().getAccessToken());
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) OAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) Date(java.util.Date) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) Test(org.junit.Test)

Example 4 with UserRedirectRequiredException

use of org.springframework.security.oauth2.client.resource.UserRedirectRequiredException in project spring-security-oauth by spring-projects.

the class AccessTokenProviderChainTests method testRequiresAuthenticationButRedirected.

@Test(expected = UserRedirectRequiredException.class)
public void testRequiresAuthenticationButRedirected() throws Exception {
    final AccessTokenRequest request = new DefaultAccessTokenRequest();
    AccessTokenProviderChain chain = new AccessTokenProviderChain(Arrays.asList(new StubAccessTokenProvider() {

        @Override
        public OAuth2AccessToken obtainAccessToken(OAuth2ProtectedResourceDetails details, AccessTokenRequest parameters) throws UserRedirectRequiredException, AccessDeniedException {
            throw new UserRedirectRequiredException("redirect test", request.toSingleValueMap());
        }
    }));
    OAuth2AccessToken token = chain.obtainAccessToken(resource, request);
    assertNotNull(token);
}
Also used : DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) BaseOAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails) OAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) Test(org.junit.Test)

Example 5 with UserRedirectRequiredException

use of org.springframework.security.oauth2.client.resource.UserRedirectRequiredException in project spring-security-oauth by spring-projects.

the class OAuth2ClientContextFilterTests method testRedirectUri.

public void testRedirectUri(String redirect, Map<String, String> params, String result) throws Exception {
    OAuth2ClientContextFilter filter = new OAuth2ClientContextFilter();
    RedirectStrategy redirectStrategy = Mockito.mock(RedirectStrategy.class);
    filter.setRedirectStrategy(redirectStrategy);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    UserRedirectRequiredException exception = new UserRedirectRequiredException(redirect, params);
    filter.redirectUser(exception, request, response);
    Mockito.verify(redirectStrategy).sendRedirect(request, response, result);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RedirectStrategy(org.springframework.security.web.RedirectStrategy) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)

Aggregations

UserRedirectRequiredException (org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)15 Test (org.junit.Test)9 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)9 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)5 UserApprovalRequiredException (org.springframework.security.oauth2.client.resource.UserApprovalRequiredException)3 OAuth2ContextConfiguration (org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration)3 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)3 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)3 HttpHeaders (org.springframework.http.HttpHeaders)2 BaseOAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails)2 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)2 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 Date (java.util.Date)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ServletException (javax.servlet.ServletException)1