Search in sources :

Example 16 with OAuth2ProtectedResourceDetails

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

the class DefaultClientAuthenticationHandler method authenticateTokenRequest.

public void authenticateTokenRequest(OAuth2ProtectedResourceDetails resource, MultiValueMap<String, String> form, HttpHeaders headers) {
    if (resource.isAuthenticationRequired()) {
        AuthenticationScheme scheme = AuthenticationScheme.header;
        if (resource.getClientAuthenticationScheme() != null) {
            scheme = resource.getClientAuthenticationScheme();
        }
        try {
            String clientSecret = resource.getClientSecret();
            clientSecret = clientSecret == null ? "" : clientSecret;
            switch(scheme) {
                case header:
                    form.remove("client_secret");
                    headers.add("Authorization", String.format("Basic %s", new String(Base64.encode(String.format("%s:%s", resource.getClientId(), clientSecret).getBytes("UTF-8")), "UTF-8")));
                    break;
                case form:
                case query:
                    form.set("client_id", resource.getClientId());
                    if (StringUtils.hasText(clientSecret)) {
                        form.set("client_secret", clientSecret);
                    }
                    break;
                default:
                    throw new IllegalStateException("Default authentication handler doesn't know how to handle scheme: " + scheme);
            }
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : AuthenticationScheme(org.springframework.security.oauth2.common.AuthenticationScheme) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)6 AuthorizationCodeResourceDetails (org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails)6 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)6 Test (org.junit.Test)5 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)5 Bean (org.springframework.context.annotation.Bean)4 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)4 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