AuthorizationController.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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;
  17. import jakarta.servlet.http.HttpServletRequest;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
  20. import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
  21. import org.springframework.security.oauth2.core.OAuth2Error;
  22. import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
  23. import org.springframework.stereotype.Controller;
  24. import org.springframework.ui.Model;
  25. import org.springframework.util.StringUtils;
  26. import org.springframework.web.bind.annotation.GetMapping;
  27. import org.springframework.web.reactive.function.client.WebClient;
  28. import static org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.clientRegistrationId;
  29. import static org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;
  30. /**
  31. * @author Joe Grandja
  32. * @since 0.0.1
  33. */
  34. @Controller
  35. public class AuthorizationController {
  36. private final WebClient webClient;
  37. private final String messagesBaseUri;
  38. public AuthorizationController(WebClient webClient,
  39. @Value("${messages.base-uri}") String messagesBaseUri) {
  40. this.webClient = webClient;
  41. this.messagesBaseUri = messagesBaseUri;
  42. }
  43. @GetMapping(value = "/authorize", params = "grant_type=authorization_code")
  44. public String authorizationCodeGrant(Model model,
  45. @RegisteredOAuth2AuthorizedClient("messaging-client-authorization-code")
  46. OAuth2AuthorizedClient authorizedClient) {
  47. String[] messages = this.webClient
  48. .get()
  49. .uri(this.messagesBaseUri)
  50. .attributes(oauth2AuthorizedClient(authorizedClient))
  51. .retrieve()
  52. .bodyToMono(String[].class)
  53. .block();
  54. model.addAttribute("messages", messages);
  55. return "index";
  56. }
  57. // '/authorized' is the registered 'redirect_uri' for authorization_code
  58. @GetMapping(value = "/authorized", params = OAuth2ParameterNames.ERROR)
  59. public String authorizationFailed(Model model, HttpServletRequest request) {
  60. String errorCode = request.getParameter(OAuth2ParameterNames.ERROR);
  61. if (StringUtils.hasText(errorCode)) {
  62. model.addAttribute("error",
  63. new OAuth2Error(
  64. errorCode,
  65. request.getParameter(OAuth2ParameterNames.ERROR_DESCRIPTION),
  66. request.getParameter(OAuth2ParameterNames.ERROR_URI))
  67. );
  68. }
  69. return "index";
  70. }
  71. @GetMapping(value = "/authorize", params = "grant_type=client_credentials")
  72. public String clientCredentialsGrant(Model model) {
  73. String[] messages = this.webClient
  74. .get()
  75. .uri(this.messagesBaseUri)
  76. .attributes(clientRegistrationId("messaging-client-client-credentials"))
  77. .retrieve()
  78. .bodyToMono(String[].class)
  79. .block();
  80. model.addAttribute("messages", messages);
  81. return "index";
  82. }
  83. @GetMapping(value = "/authorize", params = "grant_type=device_code")
  84. public String deviceCodeGrant() {
  85. return "device-activate";
  86. }
  87. }