Search in sources :

Example 6 with UserApprovalRequiredException

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

the class AbstractAuthorizationCodeProviderTests method approveAccessTokenGrant.

protected void approveAccessTokenGrant(String currentUri, boolean approved) {
    AccessTokenRequest request = context.getAccessTokenRequest();
    request.setHeaders(getAuthenticatedHeaders());
    AuthorizationCodeResourceDetails resource = (AuthorizationCodeResourceDetails) context.getResource();
    if (currentUri != null) {
        request.setCurrentUri(currentUri);
    }
    String location = null;
    try {
        // First try to obtain the access token...
        assertNotNull(context.getAccessToken());
        fail("Expected UserRedirectRequiredException");
    } catch (UserRedirectRequiredException e) {
        // Expected and necessary, so that the correct state is set up in the request...
        location = e.getRedirectUri();
    }
    assertTrue(location.startsWith(resource.getUserAuthorizationUri()));
    assertNull(request.getAuthorizationCode());
    verifyAuthorizationPage(context.getRestTemplate(), location);
    try {
        // Now try again and the token provider will redirect for user approval...
        assertNotNull(context.getAccessToken());
        fail("Expected UserRedirectRequiredException");
    } catch (UserApprovalRequiredException e) {
        // Expected and necessary, so that the user can approve the grant...
        location = e.getApprovalUri();
    }
    assertTrue(location.startsWith(resource.getUserAuthorizationUri()));
    assertNull(request.getAuthorizationCode());
    // The approval (will be processed on the next attempt to obtain an access token)...
    request.set(OAuth2Utils.USER_OAUTH_APPROVAL, "" + approved);
}
Also used : UserApprovalRequiredException(org.springframework.security.oauth2.client.resource.UserApprovalRequiredException) AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) UserRedirectRequiredException(org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)

Aggregations

UserApprovalRequiredException (org.springframework.security.oauth2.client.resource.UserApprovalRequiredException)5 UserRedirectRequiredException (org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)4 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)4 ResponseEntity (org.springframework.http.ResponseEntity)3 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)3 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)3 ResponseExtractor (org.springframework.web.client.ResponseExtractor)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 AuthorizationCodeAccessTokenProvider (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeAccessTokenProvider)2 URI (java.net.URI)1 HttpHeaders (org.springframework.http.HttpHeaders)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1