Search in sources :

Example 1 with AuthorizationCodeServices

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

the class AuthorizationCodeTokenGranterTests method testAuthorizationCodeGrant.

@Test
public void testAuthorizationCodeGrant() {
    Authentication userAuthentication = new UsernamePasswordAuthenticationToken("marissa", "koala", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    parameters.clear();
    parameters.put(OAuth2Utils.CLIENT_ID, "foo");
    parameters.put(OAuth2Utils.SCOPE, "scope");
    OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request(parameters, "foo", true, Collections.singleton("scope"));
    String code = authorizationCodeServices.createAuthorizationCode(new OAuth2Authentication(storedOAuth2Request, userAuthentication));
    parameters.putAll(storedOAuth2Request.getRequestParameters());
    parameters.put("code", code);
    TokenRequest tokenRequest = requestFactory.createTokenRequest(parameters, client);
    AuthorizationCodeTokenGranter granter = new AuthorizationCodeTokenGranter(providerTokenServices, authorizationCodeServices, clientDetailsService, requestFactory);
    OAuth2AccessToken token = granter.grant("authorization_code", tokenRequest);
    assertTrue(providerTokenServices.loadAuthentication(token.getValue()).isAuthenticated());
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 2 with AuthorizationCodeServices

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

the class AuthorizationCodeTokenGranterTests method testAuthorizationRequestPreserved.

@Test
public void testAuthorizationRequestPreserved() {
    parameters.clear();
    parameters.put(OAuth2Utils.CLIENT_ID, "foo");
    parameters.put(OAuth2Utils.SCOPE, "read");
    OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request(parameters, "foo", null, true, Collections.singleton("read"), Collections.singleton("resource"), null, null, null);
    Authentication userAuthentication = new UsernamePasswordAuthenticationToken("marissa", "koala", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    String code = authorizationCodeServices.createAuthorizationCode(new OAuth2Authentication(storedOAuth2Request, userAuthentication));
    parameters.put("code", code);
    // Ensure even if token request asks for more scope they are not granted
    parameters.put(OAuth2Utils.SCOPE, "read write");
    TokenRequest tokenRequest = requestFactory.createTokenRequest(parameters, client);
    AuthorizationCodeTokenGranter granter = new AuthorizationCodeTokenGranter(providerTokenServices, authorizationCodeServices, clientDetailsService, requestFactory);
    OAuth2AccessToken token = granter.grant("authorization_code", tokenRequest);
    OAuth2Request finalRequest = providerTokenServices.loadAuthentication(token.getValue()).getOAuth2Request();
    assertEquals("[read]", finalRequest.getScope().toString());
    assertEquals("[resource]", finalRequest.getResourceIds().toString());
    assertTrue(finalRequest.isApproved());
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Test(org.junit.Test)

Example 3 with AuthorizationCodeServices

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

the class AuthorizationServerEndpointsConfiguration method authorizationEndpoint.

@Bean
public AuthorizationEndpoint authorizationEndpoint() throws Exception {
    AuthorizationEndpoint authorizationEndpoint = new AuthorizationEndpoint();
    FrameworkEndpointHandlerMapping mapping = getEndpointsConfigurer().getFrameworkEndpointHandlerMapping();
    authorizationEndpoint.setUserApprovalPage(extractPath(mapping, "/oauth/confirm_access"));
    authorizationEndpoint.setProviderExceptionHandler(exceptionTranslator());
    authorizationEndpoint.setErrorPage(extractPath(mapping, "/oauth/error"));
    authorizationEndpoint.setTokenGranter(tokenGranter());
    authorizationEndpoint.setClientDetailsService(clientDetailsService);
    authorizationEndpoint.setAuthorizationCodeServices(authorizationCodeServices());
    authorizationEndpoint.setOAuth2RequestFactory(oauth2RequestFactory());
    authorizationEndpoint.setOAuth2RequestValidator(oauth2RequestValidator());
    authorizationEndpoint.setUserApprovalHandler(userApprovalHandler());
    return authorizationEndpoint;
}
Also used : FrameworkEndpointHandlerMapping(org.springframework.security.oauth2.provider.endpoint.FrameworkEndpointHandlerMapping) AuthorizationEndpoint(org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint) AbstractFactoryBean(org.springframework.beans.factory.config.AbstractFactoryBean) FactoryBean(org.springframework.beans.factory.FactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with AuthorizationCodeServices

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

the class AuthorizationServerEndpointsConfigurer method getDefaultTokenGranters.

private List<TokenGranter> getDefaultTokenGranters() {
    ClientDetailsService clientDetails = clientDetailsService();
    AuthorizationServerTokenServices tokenServices = tokenServices();
    AuthorizationCodeServices authorizationCodeServices = authorizationCodeServices();
    OAuth2RequestFactory requestFactory = requestFactory();
    List<TokenGranter> tokenGranters = new ArrayList<TokenGranter>();
    tokenGranters.add(new AuthorizationCodeTokenGranter(tokenServices, authorizationCodeServices, clientDetails, requestFactory));
    tokenGranters.add(new RefreshTokenGranter(tokenServices, clientDetails, requestFactory));
    ImplicitTokenGranter implicit = new ImplicitTokenGranter(tokenServices, clientDetails, requestFactory);
    tokenGranters.add(implicit);
    tokenGranters.add(new ClientCredentialsTokenGranter(tokenServices, clientDetails, requestFactory));
    if (authenticationManager != null) {
        tokenGranters.add(new ResourceOwnerPasswordTokenGranter(authenticationManager, tokenServices, clientDetails, requestFactory));
    }
    return tokenGranters;
}
Also used : AuthorizationCodeServices(org.springframework.security.oauth2.provider.code.AuthorizationCodeServices) InMemoryAuthorizationCodeServices(org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices) ImplicitTokenGranter(org.springframework.security.oauth2.provider.implicit.ImplicitTokenGranter) DefaultOAuth2RequestFactory(org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory) OAuth2RequestFactory(org.springframework.security.oauth2.provider.OAuth2RequestFactory) CompositeTokenGranter(org.springframework.security.oauth2.provider.CompositeTokenGranter) ImplicitTokenGranter(org.springframework.security.oauth2.provider.implicit.ImplicitTokenGranter) RefreshTokenGranter(org.springframework.security.oauth2.provider.refresh.RefreshTokenGranter) AuthorizationCodeTokenGranter(org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter) ClientCredentialsTokenGranter(org.springframework.security.oauth2.provider.client.ClientCredentialsTokenGranter) TokenGranter(org.springframework.security.oauth2.provider.TokenGranter) ResourceOwnerPasswordTokenGranter(org.springframework.security.oauth2.provider.password.ResourceOwnerPasswordTokenGranter) AuthorizationServerTokenServices(org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices) AuthorizationCodeTokenGranter(org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter) ResourceOwnerPasswordTokenGranter(org.springframework.security.oauth2.provider.password.ResourceOwnerPasswordTokenGranter) ArrayList(java.util.ArrayList) ClientCredentialsTokenGranter(org.springframework.security.oauth2.provider.client.ClientCredentialsTokenGranter) ClientDetailsService(org.springframework.security.oauth2.provider.ClientDetailsService) InMemoryClientDetailsService(org.springframework.security.oauth2.provider.client.InMemoryClientDetailsService) RefreshTokenGranter(org.springframework.security.oauth2.provider.refresh.RefreshTokenGranter)

Example 5 with AuthorizationCodeServices

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

the class AuthorizationCodeTokenGranterTests method testAuthorizationCodeGrantWithNoClientAuthorities.

@Test
public void testAuthorizationCodeGrantWithNoClientAuthorities() {
    parameters.clear();
    parameters.put(OAuth2Utils.CLIENT_ID, "foo");
    parameters.put(OAuth2Utils.SCOPE, "scope");
    OAuth2Request storedOAuth2Request = RequestTokenFactory.createOAuth2Request(parameters, "foo", Collections.<GrantedAuthority>emptySet(), true, Collections.singleton("scope"), null, null, null, null);
    Authentication userAuthentication = new UsernamePasswordAuthenticationToken("marissa", "koala", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
    String code = authorizationCodeServices.createAuthorizationCode(new OAuth2Authentication(storedOAuth2Request, userAuthentication));
    parameters.put("code", code);
    TokenRequest tokenRequest = requestFactory.createTokenRequest(parameters, client);
    AuthorizationCodeTokenGranter granter = new AuthorizationCodeTokenGranter(providerTokenServices, authorizationCodeServices, clientDetailsService, requestFactory);
    OAuth2AccessToken token = granter.grant("authorization_code", tokenRequest);
    assertTrue(providerTokenServices.loadAuthentication(token.getValue()).isAuthenticated());
}
Also used : OAuth2Request(org.springframework.security.oauth2.provider.OAuth2Request) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest) 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 FactoryBean (org.springframework.beans.factory.FactoryBean)1 AbstractFactoryBean (org.springframework.beans.factory.config.AbstractFactoryBean)1 Bean (org.springframework.context.annotation.Bean)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