Search in sources :

Example 16 with DefaultAccessTokenRequest

use of org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest in project spring-security-oauth by spring-projects.

the class AuthorizationCodeResourceDetailsTests method testGetOverrideRedirectUri.

@Test
public void testGetOverrideRedirectUri() {
    details.setPreEstablishedRedirectUri("http://anywhere.com");
    details.setUseCurrentUri(false);
    DefaultAccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setCurrentUri("http://nowhere.com");
    assertEquals("http://anywhere.com", details.getRedirectUri(request));
}
Also used : DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 17 with DefaultAccessTokenRequest

use of org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest in project spring-security-oauth by spring-projects.

the class AuthorizationCodeResourceDetailsTests method testGetDefaultRedirectUri.

@Test
public void testGetDefaultRedirectUri() {
    details.setPreEstablishedRedirectUri("http://anywhere.com");
    DefaultAccessTokenRequest request = new DefaultAccessTokenRequest();
    request.setCurrentUri("http://nowhere.com");
    assertEquals("http://nowhere.com", details.getRedirectUri(request));
}
Also used : DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 18 with DefaultAccessTokenRequest

use of org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest in project spring-security-oauth by spring-projects.

the class ImplicitAccessTokenProviderTests method testRedirectNotSpecified.

@Test(expected = IllegalStateException.class)
public void testRedirectNotSpecified() throws Exception {
    AccessTokenRequest request = new DefaultAccessTokenRequest();
    provider.obtainAccessToken(resource, request);
}
Also used : DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Example 19 with DefaultAccessTokenRequest

use of org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest in project spring-security-oauth by spring-projects.

the class OAuth2ContextSetup method initializeIfNecessary.

private void initializeIfNecessary(FrameworkMethod method, final Object target) {
    final TestClass testClass = new TestClass(target.getClass());
    OAuth2ContextConfiguration contextConfiguration = findOAuthContextConfiguration(method, testClass);
    if (contextConfiguration == null) {
        // Nothing to do
        return;
    }
    this.initializeAccessToken = contextConfiguration.initialize();
    this.resource = creatResource(target, contextConfiguration);
    final List<FrameworkMethod> befores = testClass.getAnnotatedMethods(BeforeOAuth2Context.class);
    if (!befores.isEmpty()) {
        logger.debug("Running @BeforeOAuth2Context methods");
        for (FrameworkMethod before : befores) {
            RestOperations savedServerClient = clientHolder.getRestTemplate();
            OAuth2ContextConfiguration beforeConfiguration = findOAuthContextConfiguration(before, testClass);
            if (beforeConfiguration != null) {
                OAuth2ProtectedResourceDetails resource = creatResource(target, beforeConfiguration);
                AccessTokenRequest beforeRequest = new DefaultAccessTokenRequest();
                beforeRequest.setAll(parameters);
                OAuth2RestTemplate client = createRestTemplate(resource, beforeRequest);
                clientHolder.setRestTemplate(client);
            }
            AccessTokenRequest request = new DefaultAccessTokenRequest();
            request.setAll(parameters);
            this.client = createRestTemplate(this.resource, request);
            List<FrameworkMethod> list = Arrays.asList(before);
            try {
                new RunBefores(new Statement() {

                    public void evaluate() {
                    }
                }, list, target).evaluate();
            } catch (AssumptionViolatedException e) {
                throw e;
            } catch (RuntimeException e) {
                throw e;
            } catch (AssertionError e) {
                throw e;
            } catch (Throwable e) {
                logger.debug("Exception in befores", e);
                Assert.assertThat(e, CoreMatchers.not(CoreMatchers.anything()));
            } finally {
                clientHolder.setRestTemplate(savedServerClient);
            }
        }
    }
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) OAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails) TestClass(org.junit.runners.model.TestClass) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) RestOperations(org.springframework.web.client.RestOperations) RunBefores(org.junit.internal.runners.statements.RunBefores) FrameworkMethod(org.junit.runners.model.FrameworkMethod) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)

Example 20 with DefaultAccessTokenRequest

use of org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest in project spring-security-oauth by spring-projects.

the class AuthorizationCodeGrantTests method testAttemptedTokenAcquisitionWithNoRedirect.

@Test
public void testAttemptedTokenAcquisitionWithNoRedirect() throws Exception {
    AuthorizationCodeAccessTokenProvider provider = new AuthorizationCodeAccessTokenProvider();
    try {
        OAuth2AccessToken token = provider.obtainAccessToken(resource, new DefaultAccessTokenRequest());
        fail("Expected UserRedirectRequiredException");
        assertNotNull(token);
    } catch (UserRedirectRequiredException e) {
        String message = e.getMessage();
        assertTrue("Wrong message: " + message, message.contains("A redirect is required"));
    }
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) AuthorizationCodeAccessTokenProvider(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)33 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)22 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)18 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)17 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)16 IOException (java.io.IOException)4 URI (java.net.URI)4 Date (java.util.Date)4 HttpMethod (org.springframework.http.HttpMethod)4 ClientHttpRequest (org.springframework.http.client.ClientHttpRequest)4 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)4 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)3 UserRedirectRequiredException (org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)3 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)3 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 HttpHeaders (org.springframework.http.HttpHeaders)2 DefaultOAuth2ClientContext (org.springframework.security.oauth2.client.DefaultOAuth2ClientContext)2 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)2 ClientCredentialsResourceDetails (org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails)2