Skip to content

Always attempt to use PCKE for OAuth 2 auth code flows #7804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ public void configureWhenAuthorizationCodeRequestThenRedirectForAuthorization()
assertThat(mvcResult.getResponse().getRedirectedUrl()).matches("https://provider.com/oauth2/authorize\\?" +
"response_type=code&client_id=client-1&" +
"scope=user&state=.{15,}&" +
"redirect_uri=http://localhost/client-1");
"redirect_uri=http://localhost/client-1&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand All @@ -151,7 +153,9 @@ public void configureWhenOauth2ClientInLambdaThenRedirectForAuthorization() thro
assertThat(mvcResult.getResponse().getRedirectedUrl()).matches("https://provider.com/oauth2/authorize\\?" +
"response_type=code&client_id=client-1&" +
"scope=user&state=.{15,}&" +
"redirect_uri=http://localhost/client-1");
"redirect_uri=http://localhost/client-1&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand Down Expand Up @@ -203,7 +207,9 @@ public void configureWhenRequestCacheProvidedAndClientAuthorizationRequiredExcep
assertThat(mvcResult.getResponse().getRedirectedUrl()).matches("https://provider.com/oauth2/authorize\\?" +
"response_type=code&client_id=client-1&" +
"scope=user&state=.{15,}&" +
"redirect_uri=http://localhost/client-1");
"redirect_uri=http://localhost/client-1&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");

verify(requestCache).saveRequest(any(HttpServletRequest.class), any(HttpServletResponse.class));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.security.oauth2.core.endpoint.PkceParameterNames;
Expand Down Expand Up @@ -130,9 +129,7 @@ private OAuth2AuthorizationRequest resolve(HttpServletRequest request, String re
// REQUIRED. OpenID Connect requests MUST contain the "openid" scope value.
addNonceParameters(attributes, additionalParameters);
}
if (ClientAuthenticationMethod.NONE.equals(clientRegistration.getClientAuthenticationMethod())) {
addPkceParameters(attributes, additionalParameters);
}
addPkceParameters(attributes, additionalParameters);
builder.additionalParameters(additionalParameters);
} else if (AuthorizationGrantType.IMPLICIT.equals(clientRegistration.getAuthorizationGrantType())) {
builder = OAuth2AuthorizationRequest.implicit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
import org.springframework.security.oauth2.core.endpoint.PkceParameterNames;
Expand Down Expand Up @@ -144,9 +143,7 @@ private OAuth2AuthorizationRequest authorizationRequest(ServerWebExchange exchan
// REQUIRED. OpenID Connect requests MUST contain the "openid" scope value.
addNonceParameters(attributes, additionalParameters);
}
if (ClientAuthenticationMethod.NONE.equals(clientRegistration.getClientAuthenticationMethod())) {
addPkceParameters(attributes, additionalParameters);
}
addPkceParameters(attributes, additionalParameters);
builder.additionalParameters(additionalParameters);
} else if (AuthorizationGrantType.IMPLICIT.equals(clientRegistration.getAuthorizationGrantType())) {
builder = OAuth2AuthorizationRequest.implicit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,15 @@ public void resolveWhenAuthorizationRequestWithValidClientThenResolves() {
assertThat(authorizationRequest.getState()).isNotNull();
assertThat(authorizationRequest.getAdditionalParameters()).doesNotContainKey(OAuth2ParameterNames.REGISTRATION_ID);
assertThat(authorizationRequest.getAttributes())
.containsExactly(entry(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId()));
.containsOnlyKeys(OAuth2ParameterNames.REGISTRATION_ID, PkceParameterNames.CODE_VERIFIER)
.containsEntry(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId());
assertThat(authorizationRequest.getAuthorizationRequestUri())
.matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=http://localhost/login/oauth2/code/registration-id");
"redirect_uri=http://localhost/login/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand All @@ -141,7 +144,8 @@ public void resolveWhenClientAuthorizationRequiredExceptionAvailableThenResolves
OAuth2AuthorizationRequest authorizationRequest = this.resolver.resolve(request, clientRegistration.getRegistrationId());
assertThat(authorizationRequest).isNotNull();
assertThat(authorizationRequest.getAttributes())
.containsExactly(entry(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId()));
.containsOnlyKeys(OAuth2ParameterNames.REGISTRATION_ID, PkceParameterNames.CODE_VERIFIER)
.containsEntry(OAuth2ParameterNames.REGISTRATION_ID, clientRegistration.getRegistrationId());
}

@Test
Expand Down Expand Up @@ -263,7 +267,9 @@ public void resolveWhenAuthorizationRequestIncludesPort80ThenExpandedRedirectUri
.matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=http://localhost/login/oauth2/code/registration-id");
"redirect_uri=http://localhost/login/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand All @@ -281,7 +287,9 @@ public void resolveWhenAuthorizationRequestIncludesPort443ThenExpandedRedirectUr
.matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=https://example.com/login/oauth2/code/registration-id");
"redirect_uri=https://example.com/login/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand All @@ -296,7 +304,9 @@ public void resolveWhenClientAuthorizationRequiredExceptionAvailableThenRedirect
.matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=http://localhost/authorize/oauth2/code/registration-id");
"redirect_uri=http://localhost/authorize/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand All @@ -311,7 +321,9 @@ public void resolveWhenAuthorizationRequestOAuth2LoginThenRedirectUriIsLogin() {
.matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id-2&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=http://localhost/login/oauth2/code/registration-id-2");
"redirect_uri=http://localhost/login/oauth2/code/registration-id-2&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand All @@ -327,7 +339,9 @@ public void resolveWhenAuthorizationRequestHasActionParameterAuthorizeThenRedire
.matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=http://localhost/authorize/oauth2/code/registration-id");
"redirect_uri=http://localhost/authorize/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand All @@ -343,7 +357,9 @@ public void resolveWhenAuthorizationRequestHasActionParameterLoginThenRedirectUr
.matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id-2&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=http://localhost/login/oauth2/code/registration-id-2");
"redirect_uri=http://localhost/login/oauth2/code/registration-id-2&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand Down Expand Up @@ -411,7 +427,9 @@ public void resolveWhenAuthenticationRequestWithValidOidcClientThenResolves() {
"response_type=code&client_id=client-id&" +
"scope=openid&state=.{15,}&" +
"redirect_uri=http://localhost/login/oauth2/code/oidc-registration-id&" +
"nonce=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
"code_challenge_method=S256&" +
"nonce=([a-zA-Z0-9\\-\\.\\_\\~]){43}&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

private static ClientRegistration.Builder fineRedirectUriTemplateClientRegistration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ public void doFilterWhenAuthorizationRequestOAuth2LoginThenRedirectForAuthorizat
assertThat(response.getRedirectedUrl()).matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=http://localhost/login/oauth2/code/registration-id");
"redirect_uri=http://localhost/login/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand Down Expand Up @@ -234,7 +236,9 @@ public void doFilterWhenCustomAuthorizationRequestBaseUriThenRedirectForAuthoriz
assertThat(response.getRedirectedUrl()).matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=http://localhost/login/oauth2/code/registration-id");
"redirect_uri=http://localhost/login/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand All @@ -255,7 +259,9 @@ public void doFilterWhenNotAuthorizationRequestAndClientAuthorizationRequiredExc
assertThat(response.getRedirectedUrl()).matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=http://localhost/authorize/oauth2/code/registration-id");
"redirect_uri=http://localhost/authorize/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
verify(this.requestCache).saveRequest(any(HttpServletRequest.class), any(HttpServletResponse.class));
}

Expand Down Expand Up @@ -359,6 +365,8 @@ public void doFilterWhenAuthorizationRequestAndCustomAuthorizationRequestUriSetT
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.{15,}&" +
"redirect_uri=http://localhost/login/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}&" +
"login_hint=user@provider\\.com");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ public void resolveWhenClientRegistrationFoundThenWorks() {
assertThat(request.getAuthorizationRequestUri()).matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.*?&" +
"redirect_uri=/login/oauth2/code/registration-id");
"redirect_uri=/login/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand All @@ -98,7 +100,9 @@ public void resolveWhenForwardedHeadersClientRegistrationFoundThenWorks() {
assertThat(request.getAuthorizationRequestUri()).matches("https://example.com/login/oauth/authorize\\?" +
"response_type=code&client_id=client-id&" +
"scope=read:user&state=.*?&" +
"redirect_uri=/login/oauth2/code/registration-id");
"redirect_uri=/login/oauth2/code/registration-id&" +
"code_challenge_method=S256&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

@Test
Expand Down Expand Up @@ -136,7 +140,9 @@ public void resolveWhenAuthenticationRequestWithValidOidcClientThenResolves() {
"response_type=code&client_id=client-id&" +
"scope=openid&state=.*?&" +
"redirect_uri=/login/oauth2/code/registration-id&" +
"nonce=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
"code_challenge_method=S256&" +
"nonce=([a-zA-Z0-9\\-\\.\\_\\~]){43}&" +
"code_challenge=([a-zA-Z0-9\\-\\.\\_\\~]){43}");
}

private OAuth2AuthorizationRequest resolve(String path) {
Expand Down