|
17 | 17 | package org.springframework.boot.autoconfigure.security.oauth2.client;
|
18 | 18 |
|
19 | 19 | import java.util.Collections;
|
| 20 | +import java.util.HashMap; |
20 | 21 | import java.util.Map;
|
21 | 22 |
|
| 23 | +import okhttp3.mockwebserver.MockResponse; |
| 24 | +import okhttp3.mockwebserver.MockWebServer; |
| 25 | +import org.junit.After; |
22 | 26 | import org.junit.Rule;
|
23 | 27 | import org.junit.Test;
|
24 | 28 | import org.junit.rules.ExpectedException;
|
| 29 | +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; |
25 | 30 |
|
26 | 31 | import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider;
|
27 | 32 | import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Registration;
|
| 33 | +import org.springframework.http.HttpHeaders; |
| 34 | +import org.springframework.http.HttpStatus; |
| 35 | +import org.springframework.http.MediaType; |
28 | 36 | import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
29 | 37 | import org.springframework.security.oauth2.client.registration.ClientRegistration.ProviderDetails;
|
| 38 | +import org.springframework.security.oauth2.core.AuthorizationGrantType; |
| 39 | +import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
30 | 40 | import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames;
|
31 | 41 |
|
32 | 42 | import static org.assertj.core.api.Assertions.assertThat;
|
|
40 | 50 | */
|
41 | 51 | public class OAuth2ClientPropertiesRegistrationAdapterTests {
|
42 | 52 |
|
| 53 | + private MockWebServer server; |
| 54 | + |
| 55 | + @After |
| 56 | + public void cleanup() throws Exception { |
| 57 | + if (this.server != null) { |
| 58 | + this.server.shutdown(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
43 | 62 | @Rule
|
44 | 63 | public ExpectedException thrown = ExpectedException.none();
|
45 | 64 |
|
@@ -217,4 +236,92 @@ public void getClientRegistrationsWhenProviderNotSpecifiedAndUnknownProviderShou
|
217 | 236 | OAuth2ClientPropertiesRegistrationAdapter.getClientRegistrations(properties);
|
218 | 237 | }
|
219 | 238 |
|
| 239 | + @Test |
| 240 | + public void oidcProviderConfigurationWhenProviderNotSpecifiedOnRegistration() |
| 241 | + throws Exception { |
| 242 | + Registration registration = new Registration(); |
| 243 | + registration.setClientId("clientId"); |
| 244 | + registration.setClientSecret("clientSecret"); |
| 245 | + testOidcConfiguration(registration, "okta"); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + public void oidcProviderConfigurationWhenProviderSpecifiedOnRegistration() |
| 250 | + throws Exception { |
| 251 | + Registration registration = new Registration(); |
| 252 | + registration.setProvider("okta-oidc"); |
| 253 | + registration.setClientId("clientId"); |
| 254 | + registration.setClientSecret("clientSecret"); |
| 255 | + testOidcConfiguration(registration, "okta-oidc"); |
| 256 | + } |
| 257 | + |
| 258 | + private void testOidcConfiguration(Registration registration, String providerId) |
| 259 | + throws Exception { |
| 260 | + this.server = new MockWebServer(); |
| 261 | + this.server.start(); |
| 262 | + String issuer = this.server.url("").toString(); |
| 263 | + String cleanIssuerPath = cleanIssuerPath(issuer); |
| 264 | + setupMockResponse(cleanIssuerPath); |
| 265 | + OAuth2ClientProperties properties = new OAuth2ClientProperties(); |
| 266 | + Provider provider = new Provider(); |
| 267 | + provider.setIssuerUri(issuer); |
| 268 | + properties.getProvider().put(providerId, provider); |
| 269 | + properties.getRegistration().put("okta", registration); |
| 270 | + Map<String, ClientRegistration> registrations = OAuth2ClientPropertiesRegistrationAdapter |
| 271 | + .getClientRegistrations(properties); |
| 272 | + ClientRegistration adapted = registrations.get("okta"); |
| 273 | + ProviderDetails providerDetails = adapted.getProviderDetails(); |
| 274 | + assertThat(adapted.getClientAuthenticationMethod()) |
| 275 | + .isEqualTo(ClientAuthenticationMethod.BASIC); |
| 276 | + assertThat(adapted.getAuthorizationGrantType()) |
| 277 | + .isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE); |
| 278 | + assertThat(adapted.getRegistrationId()).isEqualTo("okta"); |
| 279 | + assertThat(adapted.getClientName()).isEqualTo(cleanIssuerPath); |
| 280 | + assertThat(adapted.getScopes()).containsOnly("openid"); |
| 281 | + assertThat(providerDetails.getAuthorizationUri()) |
| 282 | + .isEqualTo("https://example.com/o/oauth2/v2/auth"); |
| 283 | + assertThat(providerDetails.getTokenUri()) |
| 284 | + .isEqualTo("https://example.com/oauth2/v4/token"); |
| 285 | + assertThat(providerDetails.getJwkSetUri()) |
| 286 | + .isEqualTo("https://example.com/oauth2/v3/certs"); |
| 287 | + assertThat(providerDetails.getUserInfoEndpoint().getUri()) |
| 288 | + .isEqualTo("https://example.com/oauth2/v3/userinfo"); |
| 289 | + } |
| 290 | + |
| 291 | + private String cleanIssuerPath(String issuer) { |
| 292 | + if (issuer.endsWith("/")) { |
| 293 | + return issuer.substring(0, issuer.length() - 1); |
| 294 | + } |
| 295 | + return issuer; |
| 296 | + } |
| 297 | + |
| 298 | + private void setupMockResponse(String issuer) throws Exception { |
| 299 | + MockResponse mockResponse = new MockResponse() |
| 300 | + .setResponseCode(HttpStatus.OK.value()) |
| 301 | + .setBody(new ObjectMapper().writeValueAsString(getResponse(issuer))) |
| 302 | + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); |
| 303 | + this.server.enqueue(mockResponse); |
| 304 | + } |
| 305 | + |
| 306 | + private Map<String, Object> getResponse(String issuer) { |
| 307 | + Map<String, Object> response = new HashMap<>(); |
| 308 | + response.put("authorization_endpoint", "https://example.com/o/oauth2/v2/auth"); |
| 309 | + response.put("claims_supported", Collections.emptyList()); |
| 310 | + response.put("code_challenge_methods_supported", Collections.emptyList()); |
| 311 | + response.put("id_token_signing_alg_values_supported", Collections.emptyList()); |
| 312 | + response.put("issuer", issuer); |
| 313 | + response.put("jwks_uri", "https://example.com/oauth2/v3/certs"); |
| 314 | + response.put("response_types_supported", Collections.emptyList()); |
| 315 | + response.put("revocation_endpoint", "https://example.com/o/oauth2/revoke"); |
| 316 | + response.put("scopes_supported", Collections.singletonList("openid")); |
| 317 | + response.put("subject_types_supported", Collections.singletonList("public")); |
| 318 | + response.put("grant_types_supported", |
| 319 | + Collections.singletonList("authorization_code")); |
| 320 | + response.put("token_endpoint", "https://example.com/oauth2/v4/token"); |
| 321 | + response.put("token_endpoint_auth_methods_supported", |
| 322 | + Collections.singletonList("client_secret_basic")); |
| 323 | + response.put("userinfo_endpoint", "https://example.com/oauth2/v3/userinfo"); |
| 324 | + return response; |
| 325 | + } |
| 326 | + |
220 | 327 | }
|
0 commit comments