PublicClientTests.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.pkce;
  17. import java.util.Map;
  18. import org.junit.jupiter.api.Test;
  19. import org.junit.jupiter.api.extension.ExtendWith;
  20. import sample.AuthorizationCodeGrantFlow;
  21. import sample.test.SpringTestContext;
  22. import sample.test.SpringTestContextExtension;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  25. import org.springframework.context.annotation.ComponentScan;
  26. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  27. import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
  28. import org.springframework.security.oauth2.core.oidc.OidcScopes;
  29. import org.springframework.security.oauth2.core.oidc.endpoint.OidcParameterNames;
  30. import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
  31. import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
  32. import org.springframework.test.web.servlet.MockMvc;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. import static sample.AuthorizationCodeGrantFlow.withCodeChallenge;
  35. import static sample.AuthorizationCodeGrantFlow.withCodeVerifier;
  36. /**
  37. * @author Steve Riesenberg
  38. */
  39. @ExtendWith(SpringTestContextExtension.class)
  40. public class PublicClientTests {
  41. public final SpringTestContext spring = new SpringTestContext(this);
  42. @Autowired
  43. private MockMvc mockMvc;
  44. @Autowired
  45. private RegisteredClientRepository registeredClientRepository;
  46. @Test
  47. public void oidcLoginWhenPublicClientThenSuccess() throws Exception {
  48. this.spring.register(AuthorizationServerConfig.class).autowire();
  49. RegisteredClient registeredClient = this.registeredClientRepository.findByClientId("public-client");
  50. assertThat(registeredClient).isNotNull();
  51. AuthorizationCodeGrantFlow authorizationCodeGrantFlow = new AuthorizationCodeGrantFlow(this.mockMvc);
  52. authorizationCodeGrantFlow.setUsername("user");
  53. authorizationCodeGrantFlow.addScope(OidcScopes.OPENID);
  54. authorizationCodeGrantFlow.addScope(OidcScopes.PROFILE);
  55. String state = authorizationCodeGrantFlow.authorize(registeredClient, withCodeChallenge());
  56. assertThat(state).isNotNull();
  57. String authorizationCode = authorizationCodeGrantFlow.submitConsent(registeredClient, state);
  58. assertThat(authorizationCode).isNotNull();
  59. Map<String, Object> tokenResponse = authorizationCodeGrantFlow.getTokenResponse(registeredClient,
  60. authorizationCode, withCodeVerifier());
  61. assertThat(tokenResponse.get(OAuth2ParameterNames.ACCESS_TOKEN)).isNotNull();
  62. // Note: Refresh tokens are not issued to public clients
  63. assertThat(tokenResponse.get(OAuth2ParameterNames.REFRESH_TOKEN)).isNull();
  64. assertThat(tokenResponse.get(OidcParameterNames.ID_TOKEN)).isNotNull();
  65. }
  66. @EnableWebSecurity
  67. @EnableAutoConfiguration
  68. @ComponentScan
  69. static class AuthorizationServerConfig {
  70. }
  71. }