Skip to content

Commit 8c65dc9

Browse files
rohan-naik07jgrandja
authored andcommitted
Enable PKCE by default
Closes gh-17507 Signed-off-by: Rohan Naik <[email protected]>
1 parent 54aae36 commit 8c65dc9

File tree

8 files changed

+135
-78
lines changed

8 files changed

+135
-78
lines changed

config/src/test/java/org/springframework/security/config/annotation/web/configurers/oauth2/client/OAuth2ClientConfigurerTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public void configureWhenAuthorizationCodeRequestThenRedirectForAuthorization()
156156
.andExpect(status().is3xxRedirection()).andReturn();
157157
assertThat(mvcResult.getResponse().getRedirectedUrl())
158158
.matches("https://provider.com/oauth2/authorize\\?" + "response_type=code&client_id=client-1&"
159-
+ "scope=user&state=.{15,}&" + "redirect_uri=http://localhost/client-1");
159+
+ "scope=user&state=.{15,}&" + "redirect_uri=http://localhost/client-1&code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}&code_challenge_method=S256");
160160
// @formatter:on
161161
}
162162

@@ -166,9 +166,9 @@ public void configureWhenOauth2ClientInLambdaThenRedirectForAuthorization() thro
166166
MvcResult mvcResult = this.mockMvc.perform(get("/oauth2/authorization/registration-1"))
167167
.andExpect(status().is3xxRedirection())
168168
.andReturn();
169-
assertThat(mvcResult.getResponse().getRedirectedUrl())
170-
.matches("https://provider.com/oauth2/authorize\\?" + "response_type=code&client_id=client-1&"
171-
+ "scope=user&state=.{15,}&" + "redirect_uri=http://localhost/client-1");
169+
assertThat(mvcResult.getResponse().getRedirectedUrl()).matches("https://provider.com/oauth2/authorize\\?"
170+
+ "response_type=code&client_id=client-1&" + "scope=user&state=.{15,}&"
171+
+ "redirect_uri=http://localhost/client-1&code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}&code_challenge_method=S256");
172172
}
173173

174174
@Test
@@ -215,9 +215,9 @@ public void configureWhenRequestCacheProvidedAndClientAuthorizationRequiredExcep
215215
MvcResult mvcResult = this.mockMvc.perform(get("/resource1").with(user("user1")))
216216
.andExpect(status().is3xxRedirection())
217217
.andReturn();
218-
assertThat(mvcResult.getResponse().getRedirectedUrl())
219-
.matches("https://provider.com/oauth2/authorize\\?" + "response_type=code&client_id=client-1&"
220-
+ "scope=user&state=.{15,}&" + "redirect_uri=http://localhost/client-1");
218+
assertThat(mvcResult.getResponse().getRedirectedUrl()).matches("https://provider.com/oauth2/authorize\\?"
219+
+ "response_type=code&client_id=client-1&" + "scope=user&state=.{15,}&"
220+
+ "redirect_uri=http://localhost/client-1&code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}&code_challenge_method=S256");
221221
verify(requestCache).saveRequest(any(HttpServletRequest.class), any(HttpServletResponse.class));
222222
}
223223

config/src/test/java/org/springframework/security/config/http/OAuth2ClientBeanDefinitionParserTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ public void requestWhenAuthorizeThenRedirect() throws Exception {
112112
.andExpect(status().is3xxRedirection())
113113
.andReturn();
114114
// @formatter:on
115-
assertThat(result.getResponse().getRedirectedUrl()).matches(
116-
"https://accounts.google.com/o/oauth2/v2/auth\\?" + "response_type=code&client_id=google-client-id&"
117-
+ "scope=scope1%20scope2&state=.{15,}&redirect_uri=http://localhost/callback/google");
115+
assertThat(result.getResponse().getRedirectedUrl()).matches("https://accounts.google.com/o/oauth2/v2/auth\\?"
116+
+ "response_type=code&client_id=google-client-id&"
117+
+ "scope=scope1%20scope2&state=.{15,}&redirect_uri=http://localhost/callback/google&code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}&code_challenge_method=S256");
118118
}
119119

120120
@Test
@@ -134,9 +134,9 @@ public void requestWhenCustomClientRegistrationRepositoryThenCalled() throws Exc
134134
.andExpect(status().is3xxRedirection())
135135
.andReturn();
136136
// @formatter:on
137-
assertThat(result.getResponse().getRedirectedUrl()).matches(
138-
"https://accounts.google.com/o/oauth2/v2/auth\\?" + "response_type=code&client_id=google-client-id&"
139-
+ "scope=scope1%20scope2&state=.{15,}&redirect_uri=http://localhost/callback/google");
137+
assertThat(result.getResponse().getRedirectedUrl()).matches("https://accounts.google.com/o/oauth2/v2/auth\\?"
138+
+ "response_type=code&client_id=google-client-id&"
139+
+ "scope=scope1%20scope2&state=.{15,}&redirect_uri=http://localhost/callback/google&code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}&code_challenge_method=S256");
140140
verify(this.clientRegistrationRepository).findByRegistrationId(any());
141141
}
142142

docs/modules/ROOT/pages/servlet/oauth2/client/core.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ This information is available only if the Spring Boot property `spring.security.
6969
<15> `(userInfoEndpoint)authenticationMethod`: The authentication method used when sending the access token to the UserInfo Endpoint.
7070
The supported values are *header*, *form*, and *query*.
7171
<16> `userNameAttributeName`: The name of the attribute returned in the UserInfo Response that references the Name or Identifier of the end-user.
72-
<17> [[oauth2Client-client-registration-requireProofKey]]`requireProofKey`: If `true` or if `authorizationGrantType` is `none`, then PKCE will be enabled by default.
72+
<17> [[oauth2Client-client-registration-requireProofKey]]`requireProofKey`: If `true` or if `clientAuthenticationMethod` is `none`, then PKCE will be enabled.
7373

7474
You can initially configure a `ClientRegistration` by using discovery of an OpenID Connect Provider's https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig[Configuration endpoint] or an Authorization Server's https://tools.ietf.org/html/rfc8414#section-3[Metadata endpoint].
7575

oauth2/oauth2-client/src/main/java/org/springframework/security/oauth2/client/registration/ClientRegistration.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,10 @@ private ClientRegistration create() {
651651
clientRegistration.clientName = StringUtils.hasText(this.clientName) ? this.clientName
652652
: this.registrationId;
653653
clientRegistration.clientSettings = this.clientSettings;
654+
if (clientRegistration.clientSettings.requireProofKey) {
655+
clientRegistration.clientSettings.requireProofKey = AuthorizationGrantType.AUTHORIZATION_CODE
656+
.equals(this.authorizationGrantType);
657+
}
654658
return clientRegistration;
655659
}
656660

@@ -701,12 +705,6 @@ private void validateAuthorizationGrantTypes() {
701705
"AuthorizationGrantType: %s does not match the pre-defined constant %s and won't match a valid OAuth2AuthorizedClientProvider",
702706
this.authorizationGrantType, authorizationGrantType));
703707
}
704-
if (!AuthorizationGrantType.AUTHORIZATION_CODE.equals(this.authorizationGrantType)
705-
&& this.clientSettings.isRequireProofKey()) {
706-
throw new IllegalStateException(
707-
"clientSettings.isRequireProofKey=true is only valid with authorizationGrantType=AUTHORIZATION_CODE. Got authorizationGrantType="
708-
+ this.authorizationGrantType);
709-
}
710708
}
711709
}
712710

@@ -779,7 +777,7 @@ public static Builder builder() {
779777

780778
public static final class Builder {
781779

782-
private boolean requireProofKey;
780+
private boolean requireProofKey = true;
783781

784782
private Builder() {
785783
}

oauth2/oauth2-client/src/test/java/org/springframework/security/oauth2/client/registration/ClientRegistrationTests.java

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
import org.springframework.security.oauth2.core.AuthorizationGrantType;
3636
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
3737

38-
import static org.assertj.core.api.Assertions.assertThat;
39-
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
40-
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
38+
import static org.assertj.core.api.Assertions.*;
4139

4240
/**
4341
* Tests for {@link ClientRegistration}.
@@ -683,7 +681,7 @@ void buildWhenDefaultClientSettingsThenDefaulted() {
683681
// should not be null
684682
assertThat(clientRegistration.getClientSettings()).isNotNull();
685683
// proof key should be false for passivity
686-
assertThat(clientRegistration.getClientSettings().isRequireProofKey()).isFalse();
684+
assertThat(clientRegistration.getClientSettings().isRequireProofKey()).isTrue();
687685
}
688686

689687
// gh-16382
@@ -701,28 +699,70 @@ void buildWhenNewAuthorizationCodeAndPkceThenBuilds() {
701699
.tokenUri(TOKEN_URI)
702700
.build();
703701

704-
// proof key should be false for passivity
705702
assertThat(clientRegistration.getClientSettings().isRequireProofKey()).isTrue();
706703
}
707704

705+
@Test
706+
void buildWhenNewAuthorizationCodeAndPkceDisabledThenBuilds() {
707+
ClientRegistration.ClientSettings pkceDisabled = ClientRegistration.ClientSettings.builder()
708+
.requireProofKey(false)
709+
.build();
710+
ClientRegistration clientRegistration = ClientRegistration.withRegistrationId(REGISTRATION_ID)
711+
.clientId(CLIENT_ID)
712+
.clientSettings(pkceDisabled)
713+
.authorizationGrantType(new AuthorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue()))
714+
.redirectUri(REDIRECT_URI)
715+
.authorizationUri(AUTHORIZATION_URI)
716+
.tokenUri(TOKEN_URI)
717+
.build();
718+
719+
assertThat(clientRegistration.getClientSettings().isRequireProofKey()).isFalse();
720+
}
721+
722+
@Test
723+
void buildWhenNewAuthorizationCodeAndPrivateClientThenPkceEnabledAndExceptionThrown() {
724+
List<ClientAuthenticationMethod> clientAuthenticationMethods = Arrays
725+
.stream(ClientAuthenticationMethod.class.getFields())
726+
.filter((field) -> Modifier.isFinal(field.getModifiers())
727+
&& field.getType() == ClientAuthenticationMethod.class)
728+
.map((field) -> getStaticValue(field, ClientAuthenticationMethod.class))
729+
.filter((authenticationMethod) -> authenticationMethod != ClientAuthenticationMethod.NONE)
730+
.map((authenticationMethod) -> new ClientAuthenticationMethod(authenticationMethod.getValue()))
731+
.toList();
732+
for (ClientAuthenticationMethod clientAuthenticationMethod : clientAuthenticationMethods) {
733+
ClientRegistration.ClientSettings pkceEnabled = ClientRegistration.ClientSettings.builder()
734+
.requireProofKey(true)
735+
.build();
736+
ClientRegistration clientRegistration = ClientRegistration.withRegistrationId(REGISTRATION_ID)
737+
.clientId(CLIENT_ID)
738+
.clientSettings(pkceEnabled)
739+
.authorizationGrantType(
740+
new AuthorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE.getValue()))
741+
.clientAuthenticationMethod(clientAuthenticationMethod)
742+
.redirectUri(REDIRECT_URI)
743+
.authorizationUri(AUTHORIZATION_URI)
744+
.tokenUri(TOKEN_URI)
745+
.build();
746+
assertThat(clientRegistration.getClientSettings().isRequireProofKey()).isTrue();
747+
}
748+
}
749+
708750
@ParameterizedTest
709751
@MethodSource("invalidPkceGrantTypes")
710752
void buildWhenInvalidGrantTypeForPkceThenException(AuthorizationGrantType invalidGrantType) {
711753
ClientRegistration.ClientSettings pkceEnabled = ClientRegistration.ClientSettings.builder()
712754
.requireProofKey(true)
713755
.build();
714-
ClientRegistration.Builder builder = ClientRegistration.withRegistrationId(REGISTRATION_ID)
756+
ClientRegistration clientRegistration = ClientRegistration.withRegistrationId(REGISTRATION_ID)
715757
.clientId(CLIENT_ID)
716758
.clientSettings(pkceEnabled)
717759
.authorizationGrantType(invalidGrantType)
718760
.redirectUri(REDIRECT_URI)
719761
.authorizationUri(AUTHORIZATION_URI)
720-
.tokenUri(TOKEN_URI);
762+
.tokenUri(TOKEN_URI)
763+
.build();
721764

722-
assertThatIllegalStateException().describedAs(
723-
"clientSettings.isRequireProofKey=true is only valid with authorizationGrantType=AUTHORIZATION_CODE. Got authorizationGrantType={}",
724-
invalidGrantType)
725-
.isThrownBy(builder::build);
765+
assertThat(clientRegistration.getClientSettings().isRequireProofKey()).isFalse();
726766
}
727767

728768
static List<AuthorizationGrantType> invalidPkceGrantTypes() {

0 commit comments

Comments
 (0)