|
| 1 | +/* |
| 2 | + * Copyright 2020-2024 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 | + * https://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 sample.authorization; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | + |
| 20 | +import org.springframework.core.convert.converter.Converter; |
| 21 | +import org.springframework.http.RequestEntity; |
| 22 | +import org.springframework.http.ResponseEntity; |
| 23 | +import org.springframework.http.converter.FormHttpMessageConverter; |
| 24 | +import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; |
| 25 | +import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler; |
| 26 | +import org.springframework.security.oauth2.core.OAuth2AuthorizationException; |
| 27 | +import org.springframework.security.oauth2.core.OAuth2Error; |
| 28 | +import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse; |
| 29 | +import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter; |
| 30 | +import org.springframework.util.Assert; |
| 31 | +import org.springframework.web.client.RestClientException; |
| 32 | +import org.springframework.web.client.RestOperations; |
| 33 | +import org.springframework.web.client.RestTemplate; |
| 34 | + |
| 35 | +/** |
| 36 | + * @author Steve Riesenberg |
| 37 | + * @since 1.3 |
| 38 | + */ |
| 39 | +public final class DefaultTokenExchangeTokenResponseClient |
| 40 | + implements OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> { |
| 41 | + |
| 42 | + private static final String INVALID_TOKEN_RESPONSE_ERROR_CODE = "invalid_token_response"; |
| 43 | + |
| 44 | + private Converter<TokenExchangeGrantRequest, RequestEntity<?>> requestEntityConverter = new TokenExchangeGrantRequestEntityConverter(); |
| 45 | + |
| 46 | + private RestOperations restOperations; |
| 47 | + |
| 48 | + public DefaultTokenExchangeTokenResponseClient() { |
| 49 | + RestTemplate restTemplate = new RestTemplate(Arrays.asList(new FormHttpMessageConverter(), |
| 50 | + new OAuth2AccessTokenResponseHttpMessageConverter())); |
| 51 | + restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler()); |
| 52 | + this.restOperations = restTemplate; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public OAuth2AccessTokenResponse getTokenResponse(TokenExchangeGrantRequest tokenExchangeGrantRequest) { |
| 57 | + Assert.notNull(tokenExchangeGrantRequest, "tokenExchangeGrantRequest cannot be null"); |
| 58 | + RequestEntity<?> requestEntity = this.requestEntityConverter.convert(tokenExchangeGrantRequest); |
| 59 | + ResponseEntity<OAuth2AccessTokenResponse> responseEntity = getResponse(requestEntity); |
| 60 | + |
| 61 | + return responseEntity.getBody(); |
| 62 | + } |
| 63 | + |
| 64 | + private ResponseEntity<OAuth2AccessTokenResponse> getResponse(RequestEntity<?> requestEntity) { |
| 65 | + try { |
| 66 | + return this.restOperations.exchange(requestEntity, OAuth2AccessTokenResponse.class); |
| 67 | + } catch (RestClientException ex) { |
| 68 | + OAuth2Error oauth2Error = new OAuth2Error(INVALID_TOKEN_RESPONSE_ERROR_CODE, |
| 69 | + "An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: " |
| 70 | + + ex.getMessage(), null); |
| 71 | + throw new OAuth2AuthorizationException(oauth2Error, ex); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public void setRequestEntityConverter(Converter<TokenExchangeGrantRequest, RequestEntity<?>> requestEntityConverter) { |
| 76 | + Assert.notNull(requestEntityConverter, "requestEntityConverter cannot be null"); |
| 77 | + this.requestEntityConverter = requestEntityConverter; |
| 78 | + } |
| 79 | + |
| 80 | + public void setRestOperations(RestOperations restOperations) { |
| 81 | + Assert.notNull(restOperations, "restOperations cannot be null"); |
| 82 | + this.restOperations = restOperations; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments