From a5111713095e4d13a4a7dda4441ebc71120aa751 Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Thu, 8 May 2025 18:02:43 +0700 Subject: [PATCH] Add test and update javadoc for CommonOAuth2Provider Signed-off-by: Tran Ngoc Nhan --- .../oauth2/client/CommonOAuth2Provider.java | 5 +++-- .../client/CommonOAuth2ProviderTests.java | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/config/src/main/java/org/springframework/security/config/oauth2/client/CommonOAuth2Provider.java b/config/src/main/java/org/springframework/security/config/oauth2/client/CommonOAuth2Provider.java index 96d0e51eac..c2e84336f6 100644 --- a/config/src/main/java/org/springframework/security/config/oauth2/client/CommonOAuth2Provider.java +++ b/config/src/main/java/org/springframework/security/config/oauth2/client/CommonOAuth2Provider.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.security.config.oauth2.client; +import org.springframework.security.config.Customizer; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.oauth2.client.registration.ClientRegistration; import org.springframework.security.oauth2.client.registration.ClientRegistration.Builder; @@ -27,7 +28,7 @@ import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames; * Common OAuth2 Providers that can be used to create * {@link org.springframework.security.oauth2.client.registration.ClientRegistration.Builder * builders} pre-configured with sensible defaults for the - * {@link HttpSecurity#oauth2Login()} flow. + * {@link HttpSecurity#oauth2Login(Customizer)} flow. * * @author Phillip Webb * @since 5.0 diff --git a/config/src/test/java/org/springframework/security/config/oauth2/client/CommonOAuth2ProviderTests.java b/config/src/test/java/org/springframework/security/config/oauth2/client/CommonOAuth2ProviderTests.java index 81ab751a60..0a3bea1389 100644 --- a/config/src/test/java/org/springframework/security/config/oauth2/client/CommonOAuth2ProviderTests.java +++ b/config/src/test/java/org/springframework/security/config/oauth2/client/CommonOAuth2ProviderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -115,6 +115,24 @@ public class CommonOAuth2ProviderTests { assertThat(registration.getRegistrationId()).isEqualTo("123"); } + @Test + public void getBuilderWhenXShouldHaveXSettings() { + ClientRegistration registration = build(CommonOAuth2Provider.X); + ProviderDetails providerDetails = registration.getProviderDetails(); + assertThat(providerDetails.getAuthorizationUri()).isEqualTo("https://x.com/i/oauth2/authorize"); + assertThat(providerDetails.getTokenUri()).isEqualTo("https://api.x.com/2/oauth2/token"); + assertThat(providerDetails.getUserInfoEndpoint().getUri()).isEqualTo("https://api.x.com/2/users/me"); + assertThat(providerDetails.getUserInfoEndpoint().getUserNameAttributeName()).isEqualTo("username"); + assertThat(providerDetails.getJwkSetUri()).isNull(); + assertThat(registration.getClientAuthenticationMethod()) + .isEqualTo(ClientAuthenticationMethod.CLIENT_SECRET_POST); + assertThat(registration.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE); + assertThat(registration.getRedirectUri()).isEqualTo(DEFAULT_REDIRECT_URL); + assertThat(registration.getScopes()).containsOnly("users.read", "tweet.read"); + assertThat(registration.getClientName()).isEqualTo("X"); + assertThat(registration.getRegistrationId()).isEqualTo("123"); + } + private ClientRegistration build(CommonOAuth2Provider provider) { return builder(provider).build(); }