Search in sources :

Example 11 with OAuth2ContextConfiguration

use of org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration in project spring-security-oauth by spring-projects.

the class AbstractAuthorizationCodeProviderTests method testSuccessfulFlowWithRegisteredRedirect.

@Test
@OAuth2ContextConfiguration(resource = MyClientWithRegisteredRedirect.class, initialize = false)
public void testSuccessfulFlowWithRegisteredRedirect() throws Exception {
    // Once the request is ready and approved, we can continue with the access token
    approveAccessTokenGrant(null, true);
    // Finally everything is in place for the grant to happen...
    assertNotNull(context.getAccessToken());
    AccessTokenRequest request = context.getAccessTokenRequest();
    assertNotNull(request.getAuthorizationCode());
    assertEquals(HttpStatus.OK, http.getStatusCode("/admin/beans"));
}
Also used : AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 12 with OAuth2ContextConfiguration

use of org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration in project spring-security-oauth by spring-projects.

the class AbstractClientCredentialsProviderTests method testPostForTokenWithNoScopes.

/**
	 * tests that the registered scopes are used as defaults
	 */
@Test
@OAuth2ContextConfiguration(NoScopeClientCredentials.class)
public void testPostForTokenWithNoScopes() throws Exception {
    OAuth2AccessToken token = context.getAccessToken();
    assertFalse("Wrong scope: " + token.getScope(), token.getScope().isEmpty());
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 13 with OAuth2ContextConfiguration

use of org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration in project spring-security-oauth by spring-projects.

the class AbstractImplicitProviderTests method testPostForNonAutomaticApprovalToken.

@Test
@OAuth2ContextConfiguration(resource = NonAutoApproveImplicit.class, initialize = false)
public void testPostForNonAutomaticApprovalToken() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Basic " + new String(Base64.encode("user:password".getBytes())));
    context.getAccessTokenRequest().setHeaders(headers);
    try {
        assertNotNull(context.getAccessToken());
        fail("Expected UserRedirectRequiredException");
    } catch (UserRedirectRequiredException e) {
    // ignore
    }
    // add user approval parameter for the second request
    context.getAccessTokenRequest().add(OAuth2Utils.USER_OAUTH_APPROVAL, "true");
    context.getAccessTokenRequest().add("scope.read", "true");
    assertNotNull(context.getAccessToken());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 14 with OAuth2ContextConfiguration

use of org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration in project spring-security-oauth by spring-projects.

the class AuthorizationCodeProviderTests method testInsufficientScopeInResourceRequest.

@Test
@OAuth2ContextConfiguration(resource = MyClientWithRegisteredRedirect.class, initialize = false)
public void testInsufficientScopeInResourceRequest() throws Exception {
    AuthorizationCodeResourceDetails resource = (AuthorizationCodeResourceDetails) context.getResource();
    resource.setScope(Arrays.asList("trust"));
    approveAccessTokenGrant("http://anywhere?key=value", true);
    assertNotNull(context.getAccessToken());
    try {
        http.getForString("/admin/beans");
        fail("Should have thrown exception");
    } catch (InsufficientScopeException ex) {
        assertTrue("Wrong summary: " + ex, ex.getSummary().contains("scope=\"read"));
    }
}
Also used : InsufficientScopeException(org.springframework.security.oauth2.common.exceptions.InsufficientScopeException) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Example 15 with OAuth2ContextConfiguration

use of org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration in project spring-security-oauth by spring-projects.

the class ResourceOwnerPasswordProviderTests method testUnsupportedMediaType.

/**
	 * tests a happy-day flow of the native application provider.
	 */
@Test
@OAuth2ContextConfiguration(ResourceOwnerWithTrustedClient.class)
public void testUnsupportedMediaType() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML));
    // Oddly enough this passes - the payload is a String so the message converter thinks it can handle it
    // the caller will get a surprise when he finds that the response is not actually XML, but that's a different
    // story.
    assertEquals(HttpStatus.OK, serverRunning.getStatusCode("/sparklr2/photos/user/message", headers));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) OAuth2ContextConfiguration(org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)28 OAuth2ContextConfiguration (org.springframework.security.oauth2.client.test.OAuth2ContextConfiguration)28 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)9 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)7 HttpHeaders (org.springframework.http.HttpHeaders)4 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)4 IOException (java.io.IOException)3 ClientCredentialsAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider)3 InsufficientScopeException (org.springframework.security.oauth2.common.exceptions.InsufficientScopeException)3 DefaultResponseErrorHandler (org.springframework.web.client.DefaultResponseErrorHandler)3 ResponseErrorHandler (org.springframework.web.client.ResponseErrorHandler)3 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)2 MediaType (org.springframework.http.MediaType)2 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)2 UserRedirectRequiredException (org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)2 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)2 RedirectMismatchException (org.springframework.security.oauth2.common.exceptions.RedirectMismatchException)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ExecutorService (java.util.concurrent.ExecutorService)1