Skip to content

Commit a7035d2

Browse files
committed
Update PKCE validation
1 parent e22d79c commit a7035d2

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/authentication/CodeVerifierAuthenticator.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2022 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,8 +94,10 @@ private boolean authenticate(OAuth2ClientAuthenticationToken clientAuthenticatio
9494

9595
String codeChallenge = (String) authorizationRequest.getAdditionalParameters()
9696
.get(PkceParameterNames.CODE_CHALLENGE);
97+
String codeVerifier = (String) parameters.get(PkceParameterNames.CODE_VERIFIER);
9798
if (!StringUtils.hasText(codeChallenge)) {
98-
if (registeredClient.getClientSettings().isRequireProofKey()) {
99+
if (registeredClient.getClientSettings().isRequireProofKey() ||
100+
StringUtils.hasText(codeVerifier)) {
99101
throwInvalidGrant(PkceParameterNames.CODE_CHALLENGE);
100102
} else {
101103
if (this.logger.isTraceEnabled()) {
@@ -111,7 +113,6 @@ private boolean authenticate(OAuth2ClientAuthenticationToken clientAuthenticatio
111113

112114
String codeChallengeMethod = (String) authorizationRequest.getAdditionalParameters()
113115
.get(PkceParameterNames.CODE_CHALLENGE_METHOD);
114-
String codeVerifier = (String) parameters.get(PkceParameterNames.CODE_VERIFIER);
115116
if (!codeVerifierValid(codeVerifier, codeChallenge, codeChallengeMethod)) {
116117
throwInvalidGrant(PkceParameterNames.CODE_VERIFIER);
117118
}

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/config/annotation/web/configurers/OAuth2AuthorizationCodeGrantTests.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -488,6 +488,35 @@ public void requestWhenConfidentialClientWithPkceAndMissingCodeChallengeThenErro
488488
assertThat(redirectedUrl).isEqualTo(expectedRedirectUri);
489489
}
490490

491+
@Test
492+
public void requestWhenConfidentialClientWithPkceAndMissingCodeChallengeButCodeVerifierProvidedThenBadRequest() throws Exception {
493+
this.spring.register(AuthorizationServerConfiguration.class).autowire();
494+
495+
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
496+
this.registeredClientRepository.save(registeredClient);
497+
498+
MultiValueMap<String, String> authorizationRequestParameters = getAuthorizationRequestParameters(registeredClient);
499+
MvcResult mvcResult = this.mvc.perform(get(DEFAULT_AUTHORIZATION_ENDPOINT_URI)
500+
.queryParams(authorizationRequestParameters)
501+
.with(user("user")))
502+
.andExpect(status().is3xxRedirection())
503+
.andReturn();
504+
String redirectedUrl = mvcResult.getResponse().getRedirectedUrl();
505+
String expectedRedirectUri = authorizationRequestParameters.getFirst(OAuth2ParameterNames.REDIRECT_URI);
506+
assertThat(redirectedUrl).matches(expectedRedirectUri + "\\?code=.{15,}&state=" + STATE_URL_ENCODED);
507+
508+
String authorizationCode = extractParameterFromRedirectUri(redirectedUrl, "code");
509+
OAuth2Authorization authorizationCodeAuthorization = this.authorizationService.findByToken(authorizationCode, AUTHORIZATION_CODE_TOKEN_TYPE);
510+
assertThat(authorizationCodeAuthorization).isNotNull();
511+
assertThat(authorizationCodeAuthorization.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
512+
513+
this.mvc.perform(post(DEFAULT_TOKEN_ENDPOINT_URI)
514+
.params(getTokenRequestParameters(registeredClient, authorizationCodeAuthorization))
515+
.param(PkceParameterNames.CODE_VERIFIER, S256_CODE_VERIFIER)
516+
.header(HttpHeaders.AUTHORIZATION, getAuthorizationHeader(registeredClient)))
517+
.andExpect(status().isBadRequest());
518+
}
519+
491520
@Test
492521
public void requestWhenCustomTokenGeneratorThenUsed() throws Exception {
493522
this.spring.register(AuthorizationServerConfigurationWithTokenGenerator.class).autowire();

0 commit comments

Comments
 (0)