Skip to content

Commit 06e9763

Browse files
committed
Merge branch '1.1.x' into 1.2.x
2 parents d299926 + a7035d2 commit 06e9763

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-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.
@@ -95,8 +95,10 @@ private boolean authenticate(OAuth2ClientAuthenticationToken clientAuthenticatio
9595

9696
String codeChallenge = (String) authorizationRequest.getAdditionalParameters()
9797
.get(PkceParameterNames.CODE_CHALLENGE);
98+
String codeVerifier = (String) parameters.get(PkceParameterNames.CODE_VERIFIER);
9899
if (!StringUtils.hasText(codeChallenge)) {
99-
if (registeredClient.getClientSettings().isRequireProofKey()) {
100+
if (registeredClient.getClientSettings().isRequireProofKey() ||
101+
StringUtils.hasText(codeVerifier)) {
100102
if (this.logger.isDebugEnabled()) {
101103
this.logger.debug(LogMessage.format("Invalid request: code_challenge is required" +
102104
" for registered client '%s'", registeredClient.getId()));
@@ -116,7 +118,6 @@ private boolean authenticate(OAuth2ClientAuthenticationToken clientAuthenticatio
116118

117119
String codeChallengeMethod = (String) authorizationRequest.getAdditionalParameters()
118120
.get(PkceParameterNames.CODE_CHALLENGE_METHOD);
119-
String codeVerifier = (String) parameters.get(PkceParameterNames.CODE_VERIFIER);
120121
if (!codeVerifierValid(codeVerifier, codeChallenge, codeChallengeMethod)) {
121122
if (this.logger.isDebugEnabled()) {
122123
this.logger.debug(LogMessage.format("Invalid request: code_verifier is missing or invalid" +

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.
@@ -540,6 +540,35 @@ public void requestWhenConfidentialClientWithPkceAndMissingCodeChallengeThenErro
540540
assertThat(redirectedUrl).isEqualTo(expectedRedirectUri);
541541
}
542542

543+
@Test
544+
public void requestWhenConfidentialClientWithPkceAndMissingCodeChallengeButCodeVerifierProvidedThenBadRequest() throws Exception {
545+
this.spring.register(AuthorizationServerConfiguration.class).autowire();
546+
547+
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build();
548+
this.registeredClientRepository.save(registeredClient);
549+
550+
MultiValueMap<String, String> authorizationRequestParameters = getAuthorizationRequestParameters(registeredClient);
551+
MvcResult mvcResult = this.mvc.perform(get(DEFAULT_AUTHORIZATION_ENDPOINT_URI)
552+
.queryParams(authorizationRequestParameters)
553+
.with(user("user")))
554+
.andExpect(status().is3xxRedirection())
555+
.andReturn();
556+
String redirectedUrl = mvcResult.getResponse().getRedirectedUrl();
557+
String expectedRedirectUri = authorizationRequestParameters.getFirst(OAuth2ParameterNames.REDIRECT_URI);
558+
assertThat(redirectedUrl).matches(expectedRedirectUri + "\\?code=.{15,}&state=" + STATE_URL_ENCODED);
559+
560+
String authorizationCode = extractParameterFromRedirectUri(redirectedUrl, "code");
561+
OAuth2Authorization authorizationCodeAuthorization = this.authorizationService.findByToken(authorizationCode, AUTHORIZATION_CODE_TOKEN_TYPE);
562+
assertThat(authorizationCodeAuthorization).isNotNull();
563+
assertThat(authorizationCodeAuthorization.getAuthorizationGrantType()).isEqualTo(AuthorizationGrantType.AUTHORIZATION_CODE);
564+
565+
this.mvc.perform(post(DEFAULT_TOKEN_ENDPOINT_URI)
566+
.params(getTokenRequestParameters(registeredClient, authorizationCodeAuthorization))
567+
.param(PkceParameterNames.CODE_VERIFIER, S256_CODE_VERIFIER)
568+
.header(HttpHeaders.AUTHORIZATION, getAuthorizationHeader(registeredClient)))
569+
.andExpect(status().isBadRequest());
570+
}
571+
543572
@Test
544573
public void requestWhenCustomTokenGeneratorThenUsed() throws Exception {
545574
this.spring.register(AuthorizationServerConfigurationWithTokenGenerator.class).autowire();

0 commit comments

Comments
 (0)