|
31 | 31 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
32 | 32 | import org.springframework.security.web.util.matcher.RequestMatcher;
|
33 | 33 | import org.springframework.util.Assert;
|
34 |
| -import org.springframework.util.CollectionUtils; |
35 | 34 |
|
36 | 35 | import javax.servlet.ServletException;
|
37 | 36 | import javax.servlet.http.HttpServletRequest;
|
38 | 37 | import javax.servlet.http.HttpServletResponse;
|
39 | 38 | import java.io.IOException;
|
40 | 39 |
|
41 |
| -import static org.springframework.security.oauth2.client.authentication.AuthorizationCodeRequestRedirectFilter.isDefaultRedirectUri; |
42 |
| - |
43 | 40 | /**
|
44 | 41 | * An implementation of an {@link AbstractAuthenticationProcessingFilter} that handles
|
45 | 42 | * the processing of an <i>OAuth 2.0 Authorization Response</i> for the authorization code grant flow.
|
@@ -136,12 +133,16 @@ public Authentication attemptAuthentication(HttpServletRequest request, HttpServ
|
136 | 133 | ClientRegistration clientRegistration = this.getClientRegistrationRepository().getRegistrationByClientId(
|
137 | 134 | matchingAuthorizationRequest.getClientId());
|
138 | 135 |
|
139 |
| - // If clientRegistration.redirectUri is the default one (with Uri template variables) |
140 |
| - // then use matchingAuthorizationRequest.redirectUri instead |
141 |
| - if (isDefaultRedirectUri(clientRegistration)) { |
142 |
| - clientRegistration = new ClientRegistrationBuilderWithUriOverrides( |
143 |
| - clientRegistration, matchingAuthorizationRequest.getRedirectUri()).build(); |
144 |
| - } |
| 136 | + // The clientRegistration.redirectUri may contain Uri template variables, whether it's configured by |
| 137 | + // the user or configured by default. In these cases, the redirectUri will be expanded and ultimately changed |
| 138 | + // (by AuthorizationCodeRequestRedirectFilter) before setting it in the authorization request. |
| 139 | + // The resulting redirectUri used for the authorization request and saved within the AuthorizationRequestRepository |
| 140 | + // MUST BE the same one used to complete the authorization code flow. |
| 141 | + // Therefore, we'll create a copy of the clientRegistration and override the redirectUri |
| 142 | + // with the one contained in matchingAuthorizationRequest. |
| 143 | + clientRegistration = new ClientRegistration.Builder(clientRegistration) |
| 144 | + .redirectUri(matchingAuthorizationRequest.getRedirectUri()) |
| 145 | + .build(); |
145 | 146 |
|
146 | 147 | AuthorizationCodeAuthorizationResponseAttributes authorizationCodeResponseAttributes =
|
147 | 148 | this.authorizationCodeResponseConverter.apply(request);
|
@@ -203,24 +204,4 @@ private void assertMatchingAuthorizationRequest(HttpServletRequest request, Auth
|
203 | 204 | throw new OAuth2AuthenticationException(oauth2Error, oauth2Error.toString());
|
204 | 205 | }
|
205 | 206 | }
|
206 |
| - |
207 |
| - private static class ClientRegistrationBuilderWithUriOverrides extends ClientRegistration.Builder { |
208 |
| - |
209 |
| - private ClientRegistrationBuilderWithUriOverrides(ClientRegistration clientRegistration, String redirectUri) { |
210 |
| - super(clientRegistration.getClientId()); |
211 |
| - this.clientSecret(clientRegistration.getClientSecret()); |
212 |
| - this.clientAuthenticationMethod(clientRegistration.getClientAuthenticationMethod()); |
213 |
| - this.authorizedGrantType(clientRegistration.getAuthorizedGrantType()); |
214 |
| - this.redirectUri(redirectUri); |
215 |
| - if (!CollectionUtils.isEmpty(clientRegistration.getScopes())) { |
216 |
| - this.scopes(clientRegistration.getScopes().stream().toArray(String[]::new)); |
217 |
| - } |
218 |
| - this.authorizationUri(clientRegistration.getProviderDetails().getAuthorizationUri()); |
219 |
| - this.tokenUri(clientRegistration.getProviderDetails().getTokenUri()); |
220 |
| - this.userInfoUri(clientRegistration.getProviderDetails().getUserInfoUri()); |
221 |
| - this.jwkSetUri(clientRegistration.getProviderDetails().getJwkSetUri()); |
222 |
| - this.clientName(clientRegistration.getClientName()); |
223 |
| - this.clientAlias(clientRegistration.getClientAlias()); |
224 |
| - } |
225 |
| - } |
226 | 207 | }
|
0 commit comments