DynamicClientRegistrationTests.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.registration;
  17. import com.jayway.jsonpath.JsonPath;
  18. import org.junit.jupiter.api.Test;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  21. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  22. import org.springframework.boot.test.context.SpringBootTest;
  23. import org.springframework.boot.test.web.server.LocalServerPort;
  24. import org.springframework.context.annotation.ComponentScan;
  25. import org.springframework.http.MediaType;
  26. import org.springframework.mock.web.MockHttpServletResponse;
  27. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  28. import org.springframework.security.oauth2.core.AuthorizationGrantType;
  29. import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
  30. import org.springframework.test.web.servlet.MockMvc;
  31. import org.springframework.web.reactive.function.client.WebClient;
  32. import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic;
  33. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  34. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  35. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  36. /**
  37. * Tests for Dynamic Client Registration how-to guide.
  38. *
  39. * @author Dmitriy Dubson
  40. */
  41. @SpringBootTest(
  42. webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
  43. classes = {DynamicClientRegistrationTests.AuthorizationServerConfig.class}
  44. )
  45. @AutoConfigureMockMvc
  46. public class DynamicClientRegistrationTests {
  47. @Autowired
  48. private MockMvc mvc;
  49. @LocalServerPort
  50. private String port;
  51. @Test
  52. public void dynamicallyRegisterClientWithCustomClientMetadata() throws Exception {
  53. MockHttpServletResponse tokenResponse = this.mvc.perform(post("/oauth2/token")
  54. .with(httpBasic("registrar-client", "secret"))
  55. .param(OAuth2ParameterNames.GRANT_TYPE, AuthorizationGrantType.CLIENT_CREDENTIALS.getValue())
  56. .param(OAuth2ParameterNames.SCOPE, "client.create")
  57. .contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE))
  58. .andExpect(status().isOk())
  59. .andExpect(jsonPath("$.access_token").isNotEmpty())
  60. .andReturn()
  61. .getResponse();
  62. String initialAccessToken = JsonPath.parse(tokenResponse.getContentAsString()).read("$.access_token");
  63. WebClient webClient = WebClient.builder().baseUrl("http://127.0.0.1:%s".formatted(this.port)).build();
  64. ClientRegistrar clientRegistrar = new ClientRegistrar(webClient);
  65. clientRegistrar.exampleRegistration(initialAccessToken);
  66. }
  67. @EnableAutoConfiguration
  68. @EnableWebSecurity
  69. @ComponentScan
  70. static class AuthorizationServerConfig {
  71. }
  72. }