Search in sources :

Example 16 with ClientDetailsService

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

the class DefaultTokenServices method loadAuthentication.

public OAuth2Authentication loadAuthentication(String accessTokenValue) throws AuthenticationException, InvalidTokenException {
    OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue);
    if (accessToken == null) {
        throw new InvalidTokenException("Invalid access token: " + accessTokenValue);
    } else if (accessToken.isExpired()) {
        tokenStore.removeAccessToken(accessToken);
        throw new InvalidTokenException("Access token expired: " + accessTokenValue);
    }
    OAuth2Authentication result = tokenStore.readAuthentication(accessToken);
    if (result == null) {
        // in case of race condition
        throw new InvalidTokenException("Invalid access token: " + accessTokenValue);
    }
    if (clientDetailsService != null) {
        String clientId = result.getOAuth2Request().getClientId();
        try {
            clientDetailsService.loadClientByClientId(clientId);
        } catch (ClientRegistrationException e) {
            throw new InvalidTokenException("Client not valid: " + clientId, e);
        }
    }
    return result;
}
Also used : InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) DefaultOAuth2AccessToken(org.springframework.security.oauth2.common.DefaultOAuth2AccessToken) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) ClientRegistrationException(org.springframework.security.oauth2.provider.ClientRegistrationException)

Example 17 with ClientDetailsService

use of org.springframework.security.oauth2.provider.ClientDetailsService in project ORCID-Source by ORCID.

the class OrcidClientCredentialsCheckerTest method setup.

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    oAuth2RequestFactory = new DefaultOAuth2RequestFactory(clientDetailsService);
    checker = new OrcidClientCredentialsChecker(oAuth2RequestFactory);
    checker.setClientDetailsEntityCacheManager(clientDetailsEntityCacheManager);
    checker.setOrcidOAuth2RequestValidator(orcidOAuth2RequestValidator);
}
Also used : DefaultOAuth2RequestFactory(org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory) Before(org.junit.Before)

Example 18 with ClientDetailsService

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

the class SparklrUserApprovalHandler method checkForPreApproval.

/**
	 * Allows automatic approval for a white list of clients in the implicit grant case.
	 * 
	 * @param authorizationRequest The authorization request.
	 * @param userAuthentication the current user authentication
	 * 
	 * @return An updated request if it has already been approved by the current user.
	 */
@Override
public AuthorizationRequest checkForPreApproval(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
    boolean approved = false;
    // If we are allowed to check existing approvals this will short circuit the decision
    if (useApprovalStore) {
        authorizationRequest = super.checkForPreApproval(authorizationRequest, userAuthentication);
        approved = authorizationRequest.isApproved();
    } else {
        if (clientDetailsService != null) {
            Collection<String> requestedScopes = authorizationRequest.getScope();
            try {
                ClientDetails client = clientDetailsService.loadClientByClientId(authorizationRequest.getClientId());
                for (String scope : requestedScopes) {
                    if (client.isAutoApprove(scope)) {
                        approved = true;
                        break;
                    }
                }
            } catch (ClientRegistrationException e) {
            }
        }
    }
    authorizationRequest.setApproved(approved);
    return authorizationRequest;
}
Also used : ClientDetails(org.springframework.security.oauth2.provider.ClientDetails) ClientRegistrationException(org.springframework.security.oauth2.provider.ClientRegistrationException)

Example 19 with ClientDetailsService

use of org.springframework.security.oauth2.provider.ClientDetailsService 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 20 with ClientDetailsService

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

the class AuthorizationServerEndpointsConfigurer method createDefaultTokenServices.

private DefaultTokenServices createDefaultTokenServices() {
    DefaultTokenServices tokenServices = new DefaultTokenServices();
    tokenServices.setTokenStore(tokenStore());
    tokenServices.setSupportRefreshToken(true);
    tokenServices.setReuseRefreshToken(reuseRefreshToken);
    tokenServices.setClientDetailsService(clientDetailsService());
    tokenServices.setTokenEnhancer(tokenEnhancer());
    addUserDetailsService(tokenServices, this.userDetailsService);
    return tokenServices;
}
Also used : DefaultTokenServices(org.springframework.security.oauth2.provider.token.DefaultTokenServices)

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