OAuth2DeviceAccessTokenResponseClient.java 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2020-2023 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.web.authentication;
  17. import java.util.Arrays;
  18. import org.springframework.http.HttpHeaders;
  19. import org.springframework.http.RequestEntity;
  20. import org.springframework.http.converter.FormHttpMessageConverter;
  21. import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient;
  22. import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler;
  23. import org.springframework.security.oauth2.client.registration.ClientRegistration;
  24. import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
  25. import org.springframework.security.oauth2.core.OAuth2Error;
  26. import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
  27. import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
  28. import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter;
  29. import org.springframework.util.LinkedMultiValueMap;
  30. import org.springframework.util.MultiValueMap;
  31. import org.springframework.web.client.RestClientException;
  32. import org.springframework.web.client.RestOperations;
  33. import org.springframework.web.client.RestTemplate;
  34. /**
  35. * @author Steve Riesenberg
  36. * @since 1.1
  37. */
  38. public final class OAuth2DeviceAccessTokenResponseClient implements OAuth2AccessTokenResponseClient<OAuth2DeviceGrantRequest> {
  39. private RestOperations restOperations;
  40. public OAuth2DeviceAccessTokenResponseClient() {
  41. RestTemplate restTemplate = new RestTemplate(Arrays.asList(new FormHttpMessageConverter(),
  42. new OAuth2AccessTokenResponseHttpMessageConverter()));
  43. restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler());
  44. this.restOperations = restTemplate;
  45. }
  46. public void setRestOperations(RestOperations restOperations) {
  47. this.restOperations = restOperations;
  48. }
  49. @Override
  50. public OAuth2AccessTokenResponse getTokenResponse(OAuth2DeviceGrantRequest deviceGrantRequest) {
  51. ClientRegistration clientRegistration = deviceGrantRequest.getClientRegistration();
  52. HttpHeaders headers = new HttpHeaders();
  53. headers.setBasicAuth(clientRegistration.getClientId(), clientRegistration.getClientSecret());
  54. MultiValueMap<String, Object> requestParameters = new LinkedMultiValueMap<>();
  55. requestParameters.add(OAuth2ParameterNames.GRANT_TYPE, deviceGrantRequest.getGrantType().getValue());
  56. requestParameters.add(OAuth2ParameterNames.CLIENT_ID, clientRegistration.getClientId());
  57. requestParameters.add(OAuth2ParameterNames.DEVICE_CODE, deviceGrantRequest.getDeviceCode());
  58. // @formatter:off
  59. RequestEntity<MultiValueMap<String, Object>> requestEntity =
  60. RequestEntity.post(deviceGrantRequest.getClientRegistration().getProviderDetails().getTokenUri())
  61. .headers(headers)
  62. .body(requestParameters);
  63. // @formatter:on
  64. try {
  65. return this.restOperations.exchange(requestEntity, OAuth2AccessTokenResponse.class).getBody();
  66. } catch (RestClientException ex) {
  67. OAuth2Error oauth2Error = new OAuth2Error("invalid_token_response",
  68. "An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: "
  69. + ex.getMessage(), null);
  70. throw new OAuth2AuthorizationException(oauth2Error, ex);
  71. }
  72. }
  73. }