|
| 1 | +/* |
| 2 | + * Copyright 2002-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.security.oauth2.client.endpoint; |
| 17 | + |
| 18 | +import org.springframework.core.convert.converter.Converter; |
| 19 | +import org.springframework.http.RequestEntity; |
| 20 | +import org.springframework.http.ResponseEntity; |
| 21 | +import org.springframework.http.converter.FormHttpMessageConverter; |
| 22 | +import org.springframework.http.converter.HttpMessageConverter; |
| 23 | +import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler; |
| 24 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 25 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; |
| 26 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 27 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; |
| 28 | +import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter; |
| 29 | +import org.springframework.util.Assert; |
| 30 | +import org.springframework.util.CollectionUtils; |
| 31 | +import org.springframework.web.client.ResponseErrorHandler; |
| 32 | +import org.springframework.web.client.RestClientException; |
| 33 | +import org.springframework.web.client.RestOperations; |
| 34 | +import org.springframework.web.client.RestTemplate; |
| 35 | + |
| 36 | +import java.util.Arrays; |
| 37 | + |
| 38 | +/** |
| 39 | + * The default implementation of an {@link OAuth2AccessTokenResponseClient} |
| 40 | + * for the {@link AuthorizationGrantType#AUTHORIZATION_CODE authorization_code} grant. |
| 41 | + * This implementation uses a {@link RestOperations} when requesting |
| 42 | + * an access token credential at the Authorization Server's Token Endpoint. |
| 43 | + * |
| 44 | + * @author Joe Grandja |
| 45 | + * @since 5.1 |
| 46 | + * @see OAuth2AccessTokenResponseClient |
| 47 | + * @see OAuth2AuthorizationCodeGrantRequest |
| 48 | + * @see OAuth2AccessTokenResponse |
| 49 | + * @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-4.1.3">Section 4.1.3 Access Token Request (Authorization Code Grant)</a> |
| 50 | + * @see <a target="_blank" href="https://tools.ietf.org/html/rfc6749#section-4.1.4">Section 4.1.4 Access Token Response (Authorization Code Grant)</a> |
| 51 | + */ |
| 52 | +public final class DefaultAuthorizationCodeTokenResponseClient implements OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> { |
| 53 | + private static final String INVALID_TOKEN_RESPONSE_ERROR_CODE = "invalid_token_response"; |
| 54 | + |
| 55 | + private Converter<OAuth2AuthorizationCodeGrantRequest, RequestEntity<?>> requestEntityConverter = |
| 56 | + new OAuth2AuthorizationCodeGrantRequestEntityConverter(); |
| 57 | + |
| 58 | + private RestOperations restOperations; |
| 59 | + |
| 60 | + public DefaultAuthorizationCodeTokenResponseClient() { |
| 61 | + RestTemplate restTemplate = new RestTemplate(Arrays.asList( |
| 62 | + new FormHttpMessageConverter(), new OAuth2AccessTokenResponseHttpMessageConverter())); |
| 63 | + restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler()); |
| 64 | + this.restOperations = restTemplate; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public OAuth2AccessTokenResponse getTokenResponse(OAuth2AuthorizationCodeGrantRequest authorizationCodeGrantRequest) throws OAuth2AuthenticationException { |
| 69 | + Assert.notNull(authorizationCodeGrantRequest, "authorizationCodeGrantRequest cannot be null"); |
| 70 | + |
| 71 | + RequestEntity<?> request = this.requestEntityConverter.convert(authorizationCodeGrantRequest); |
| 72 | + |
| 73 | + ResponseEntity<OAuth2AccessTokenResponse> response; |
| 74 | + try { |
| 75 | + response = this.restOperations.exchange(request, OAuth2AccessTokenResponse.class); |
| 76 | + } catch (RestClientException ex) { |
| 77 | + OAuth2Error oauth2Error = new OAuth2Error(INVALID_TOKEN_RESPONSE_ERROR_CODE, |
| 78 | + "An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: " + ex.getMessage(), null); |
| 79 | + throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString(), ex); |
| 80 | + } |
| 81 | + |
| 82 | + OAuth2AccessTokenResponse tokenResponse = response.getBody(); |
| 83 | + |
| 84 | + if (CollectionUtils.isEmpty(tokenResponse.getAccessToken().getScopes())) { |
| 85 | + // As per spec, in Section 5.1 Successful Access Token Response |
| 86 | + // https://tools.ietf.org/html/rfc6749#section-5.1 |
| 87 | + // If AccessTokenResponse.scope is empty, then default to the scope |
| 88 | + // originally requested by the client in the Token Request |
| 89 | + tokenResponse = OAuth2AccessTokenResponse.withResponse(tokenResponse) |
| 90 | + .scopes(authorizationCodeGrantRequest.getClientRegistration().getScopes()) |
| 91 | + .build(); |
| 92 | + } |
| 93 | + |
| 94 | + return tokenResponse; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Sets the {@link Converter} used for converting the {@link OAuth2AuthorizationCodeGrantRequest} |
| 99 | + * to a {@link RequestEntity} representation of the OAuth 2.0 Access Token Request. |
| 100 | + * |
| 101 | + * @param requestEntityConverter the {@link Converter} used for converting to a {@link RequestEntity} representation of the Access Token Request |
| 102 | + */ |
| 103 | + public void setRequestEntityConverter(Converter<OAuth2AuthorizationCodeGrantRequest, RequestEntity<?>> requestEntityConverter) { |
| 104 | + Assert.notNull(requestEntityConverter, "requestEntityConverter cannot be null"); |
| 105 | + this.requestEntityConverter = requestEntityConverter; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Sets the {@link RestOperations} used when requesting the OAuth 2.0 Access Token Response. |
| 110 | + * |
| 111 | + * <p> |
| 112 | + * <b>NOTE:</b> At a minimum, the supplied {@code restOperations} must be configured with the following: |
| 113 | + * <ol> |
| 114 | + * <li>{@link HttpMessageConverter}'s - {@link FormHttpMessageConverter} and {@link OAuth2AccessTokenResponseHttpMessageConverter}</li> |
| 115 | + * <li>{@link ResponseErrorHandler} - {@link OAuth2ErrorResponseErrorHandler}</li> |
| 116 | + * </ol> |
| 117 | + * |
| 118 | + * @param restOperations the {@link RestOperations} used when requesting the Access Token Response |
| 119 | + */ |
| 120 | + public void setRestOperations(RestOperations restOperations) { |
| 121 | + Assert.notNull(restOperations, "restOperations cannot be null"); |
| 122 | + this.restOperations = restOperations; |
| 123 | + } |
| 124 | +} |
0 commit comments