Search in sources :

Example 41 with ClientDetailsService

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

the class ResourceOwnerPasswordTokenGranterTests method testAccountLocked.

@Test(expected = InvalidGrantException.class)
public void testAccountLocked() {
    ResourceOwnerPasswordTokenGranter granter = new ResourceOwnerPasswordTokenGranter(new AuthenticationManager() {

        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            throw new LockedException("test");
        }
    }, providerTokenServices, clientDetailsService, requestFactory);
    granter.grant("password", tokenRequest);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) LockedException(org.springframework.security.authentication.LockedException) AuthenticationException(org.springframework.security.core.AuthenticationException) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) Test(org.junit.Test)

Example 42 with ClientDetailsService

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

the class ResourceOwnerPasswordTokenGranterTests method testExtraParameters.

@Test
public void testExtraParameters() {
    authenticationManager = new AuthenticationManager() {

        @Override
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            if (authentication instanceof UsernamePasswordAuthenticationToken) {
                UsernamePasswordAuthenticationToken user = (UsernamePasswordAuthenticationToken) authentication;
                user = new UsernamePasswordAuthenticationToken(user.getPrincipal(), "N/A", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
                @SuppressWarnings("unchecked") Map<String, String> details = (Map<String, String>) authentication.getDetails();
                assertNull(details.get("password"));
                return user;
            }
            return authentication;
        }
    };
    ResourceOwnerPasswordTokenGranter granter = new ResourceOwnerPasswordTokenGranter(authenticationManager, providerTokenServices, clientDetailsService, requestFactory);
    OAuth2AccessToken token = granter.grant("password", tokenRequest);
    OAuth2Authentication authentication = providerTokenServices.loadAuthentication(token.getValue());
    assertTrue(authentication.isAuthenticated());
    assertNull(authentication.getUserAuthentication().getDetails());
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AuthenticationException(org.springframework.security.core.AuthenticationException) 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) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 43 with ClientDetailsService

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

the class AuthorizationEndpointTests method init.

@Before
public void init() throws Exception {
    client = new BaseClientDetails();
    client.setRegisteredRedirectUri(Collections.singleton("http://anywhere.com"));
    client.setAuthorizedGrantTypes(Arrays.asList("authorization_code", "implicit"));
    endpoint.setClientDetailsService(new ClientDetailsService() {

        public ClientDetails loadClientByClientId(String clientId) throws OAuth2Exception {
            return client;
        }
    });
    endpoint.setTokenGranter(new TokenGranter() {

        public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
            return null;
        }
    });
    endpoint.setRedirectResolver(new DefaultRedirectResolver());
    endpoint.afterPropertiesSet();
}
Also used : BaseClientDetails(org.springframework.security.oauth2.provider.client.BaseClientDetails) BaseClientDetails(org.springframework.security.oauth2.provider.client.BaseClientDetails) ClientDetails(org.springframework.security.oauth2.provider.ClientDetails) TokenGranter(org.springframework.security.oauth2.provider.TokenGranter) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) TokenRequest(org.springframework.security.oauth2.provider.TokenRequest) ClientDetailsService(org.springframework.security.oauth2.provider.ClientDetailsService) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception) Before(org.junit.Before)

Example 44 with ClientDetailsService

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

the class ResourceOwnerPasswordTokenGranterTests method testSunnyDay.

@Test
public void testSunnyDay() {
    ResourceOwnerPasswordTokenGranter granter = new ResourceOwnerPasswordTokenGranter(authenticationManager, providerTokenServices, clientDetailsService, requestFactory);
    OAuth2AccessToken token = granter.grant("password", tokenRequest);
    OAuth2Authentication authentication = providerTokenServices.loadAuthentication(token.getValue());
    assertTrue(authentication.isAuthenticated());
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Test(org.junit.Test)

Example 45 with ClientDetailsService

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

the class ResourceOwnerPasswordTokenGranterTests method testPasswordRemoved.

@Test
public void testPasswordRemoved() {
    ResourceOwnerPasswordTokenGranter granter = new ResourceOwnerPasswordTokenGranter(authenticationManager, providerTokenServices, clientDetailsService, requestFactory);
    OAuth2AccessToken token = granter.grant("password", tokenRequest);
    OAuth2Authentication authentication = providerTokenServices.loadAuthentication(token.getValue());
    assertNotNull(authentication.getOAuth2Request().getRequestParameters().get("username"));
    assertNull(authentication.getOAuth2Request().getRequestParameters().get("password"));
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)27 ClientDetails (org.springframework.security.oauth2.provider.ClientDetails)18 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)16 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)14 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)13 ClientDetailsService (org.springframework.security.oauth2.provider.ClientDetailsService)11 Authentication (org.springframework.security.core.Authentication)8 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)7 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)7 OAuth2Exception (org.springframework.security.oauth2.common.exceptions.OAuth2Exception)6 ClientRegistrationException (org.springframework.security.oauth2.provider.ClientRegistrationException)6 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)6 HashMap (java.util.HashMap)5 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)5 InMemoryClientDetailsService (org.springframework.security.oauth2.provider.client.InMemoryClientDetailsService)5 Before (org.junit.Before)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 DefaultOAuth2RequestFactory (org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory)4 Date (java.util.Date)3 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)3