Search in sources :

Example 6 with OAuth2ProtectedResourceDetails

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

the class DefaultOAuth2RequestAuthenticator method authenticate.

@Override
public void authenticate(OAuth2ProtectedResourceDetails resource, OAuth2ClientContext clientContext, ClientHttpRequest request) {
    OAuth2AccessToken accessToken = clientContext.getAccessToken();
    if (accessToken == null) {
        throw new AccessTokenRequiredException(resource);
    }
    String tokenType = accessToken.getTokenType();
    if (!StringUtils.hasText(tokenType)) {
        // we'll assume basic bearer token type if none is specified.
        tokenType = OAuth2AccessToken.BEARER_TYPE;
    }
    request.getHeaders().set("Authorization", String.format("%s %s", tokenType, accessToken.getValue()));
}
Also used : OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) AccessTokenRequiredException(org.springframework.security.oauth2.client.http.AccessTokenRequiredException)

Example 7 with OAuth2ProtectedResourceDetails

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

the class ClientApplication method resource.

@Bean
protected OAuth2ProtectedResourceDetails resource() {
    AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
    resource.setAccessTokenUri(tokenUrl);
    resource.setUserAuthorizationUri(authorizeUrl);
    resource.setClientId("my-trusted-client");
    return resource;
}
Also used : AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) Bean(org.springframework.context.annotation.Bean)

Example 8 with OAuth2ProtectedResourceDetails

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

the class ClientApplication method resource.

@Bean
protected OAuth2ProtectedResourceDetails resource() {
    AuthorizationCodeResourceDetails resource = new AuthorizationCodeResourceDetails();
    resource.setAccessTokenUri(tokenUrl);
    resource.setUserAuthorizationUri(authorizeUrl);
    resource.setClientId("my-trusted-client");
    return resource;
}
Also used : AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) Bean(org.springframework.context.annotation.Bean)

Example 9 with OAuth2ProtectedResourceDetails

use of org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails in project spring-security-oauth2-google by skate056.

the class OAuth2SecurityConfiguration method googleResource.

@Bean
@Scope("session")
public OAuth2ProtectedResourceDetails googleResource() {
    AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
    details.setId("google-oauth-client");
    details.setClientId(env.getProperty("google.client.id"));
    details.setClientSecret(env.getProperty("google.client.secret"));
    details.setAccessTokenUri(env.getProperty("google.accessTokenUri"));
    details.setUserAuthorizationUri(env.getProperty("google.userAuthorizationUri"));
    details.setTokenName(env.getProperty("google.authorization.code"));
    String commaSeparatedScopes = env.getProperty("google.auth.scope");
    details.setScope(parseScopes(commaSeparatedScopes));
    details.setPreEstablishedRedirectUri(env.getProperty("google.preestablished.redirect.url"));
    details.setUseCurrentUri(false);
    details.setAuthenticationScheme(AuthenticationScheme.query);
    details.setClientAuthenticationScheme(AuthenticationScheme.form);
    return details;
}
Also used : AuthorizationCodeResourceDetails(org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails) Scope(org.springframework.context.annotation.Scope) Bean(org.springframework.context.annotation.Bean)

Example 10 with OAuth2ProtectedResourceDetails

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

the class OAuth2ContextSetup method createRestTemplate.

private OAuth2RestTemplate createRestTemplate(OAuth2ProtectedResourceDetails resource, AccessTokenRequest request) {
    OAuth2ClientContext context = new DefaultOAuth2ClientContext(request);
    OAuth2RestTemplate client = new OAuth2RestTemplate(resource, context);
    setupConnectionFactory(client);
    client.setErrorHandler(new DefaultResponseErrorHandler() {

        // Pass errors through in response entity for status code analysis
        public boolean hasError(ClientHttpResponse response) throws IOException {
            return false;
        }
    });
    if (accessTokenProvider != null) {
        client.setAccessTokenProvider(accessTokenProvider);
    }
    return client;
}
Also used : DefaultResponseErrorHandler(org.springframework.web.client.DefaultResponseErrorHandler) DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) DefaultOAuth2ClientContext(org.springframework.security.oauth2.client.DefaultOAuth2ClientContext) OAuth2ClientContext(org.springframework.security.oauth2.client.OAuth2ClientContext) IOException(java.io.IOException) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Aggregations

OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)6 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)6 Test (org.junit.Test)5 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)5 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)5 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)4 Bean (org.springframework.context.annotation.Bean)3 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)3 DefaultOAuth2ClientContext (org.springframework.security.oauth2.client.DefaultOAuth2ClientContext)3 OAuth2ClientContext (org.springframework.security.oauth2.client.OAuth2ClientContext)3 BaseOAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails)3 UserRedirectRequiredException (org.springframework.security.oauth2.client.resource.UserRedirectRequiredException)3 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)3 Date (java.util.Date)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 ResponseExtractor (org.springframework.web.client.ResponseExtractor)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URI (java.net.URI)1 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)1