Search in sources :

Example 6 with AuthorizationCodeTokenGranter

use of org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter in project spring-security-oauth by spring-projects.

the class AuthorizationCodeTokenGranterTests method testAuthorizationRedirectMismatch.

@Test
public void testAuthorizationRedirectMismatch() {
    Map<String, String> initialParameters = new HashMap<String, String>();
    initialParameters.put(OAuth2Utils.REDIRECT_URI, "https://redirectMe");
    //AuthorizationRequest initialRequest = createFromParameters(initialParameters);
    // we fake a valid resolvedRedirectUri because without the client would never come this far
    //initialRequest.setRedirectUri(initialParameters.get(REDIRECT_URI));
    parameters.clear();
    parameters.put(OAuth2Utils.REDIRECT_URI, "https://redirectMe");
    parameters.put(OAuth2Utils.CLIENT_ID, "foo");
    OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request(parameters, "foo", null, true, null, null, "https://redirectMe", null, null);
    Authentication userAuthentication = new UsernamePasswordAuthenticationToken("marissa", "koala", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    String code = authorizationCodeServices.createAuthorizationCode(new OAuth2Authentication(storedOAuth2Request, userAuthentication));
    Map<String, String> authorizationParameters = new HashMap<String, String>();
    authorizationParameters.put("code", code);
    //AuthorizationRequest oAuth2Request = createFromParameters(initialParameters);
    //oAuth2Request.setRequestParameters(authorizationParameters);
    TokenRequest tokenRequest = requestFactory.createTokenRequest(parameters, client);
    tokenRequest.setRequestParameters(authorizationParameters);
    AuthorizationCodeTokenGranter granter = new AuthorizationCodeTokenGranter(providerTokenServices, authorizationCodeServices, clientDetailsService, requestFactory);
    try {
        granter.getOAuth2Authentication(client, tokenRequest);
        fail("RedirectMismatchException because of null redirect_uri in authorizationRequest");
    } catch (RedirectMismatchException e) {
    }
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) HashMap(java.util.HashMap) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest) RedirectMismatchException(org.springframework.security.oauth2.common.exceptions.RedirectMismatchException) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)5 Authentication (org.springframework.security.core.Authentication)5 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)5 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)5 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)5 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)4 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 RedirectMismatchException (org.springframework.security.oauth2.common.exceptions.RedirectMismatchException)1 ClientDetailsService (org.springframework.security.oauth2.provider.ClientDetailsService)1 CompositeTokenGranter (org.springframework.security.oauth2.provider.CompositeTokenGranter)1 OAuth2RequestFactory (org.springframework.security.oauth2.provider.OAuth2RequestFactory)1 TokenGranter (org.springframework.security.oauth2.provider.TokenGranter)1 ClientCredentialsTokenGranter (org.springframework.security.oauth2.provider.client.ClientCredentialsTokenGranter)1 InMemoryClientDetailsService (org.springframework.security.oauth2.provider.client.InMemoryClientDetailsService)1 AuthorizationCodeServices (org.springframework.security.oauth2.provider.code.AuthorizationCodeServices)1 AuthorizationCodeTokenGranter (org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter)1 InMemoryAuthorizationCodeServices (org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices)1 ImplicitTokenGranter (org.springframework.security.oauth2.provider.implicit.ImplicitTokenGranter)1