Skip to content

Commit b0fca27

Browse files
sylvain-costanzojgrandja
authored andcommitted
Support POST for authorization code request flow
Closes gh-1811 Signed-off-by: sylvain-costanzo <sylvain.costanzo1@decathlon.com>
1 parent 1e8c463 commit b0fca27

File tree

3 files changed

+8
-21
lines changed

3 files changed

+8
-21
lines changed

oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/web/OAuth2AuthorizationEndpointFilter.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import org.springframework.security.oauth2.core.OAuth2Error;
4040
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse;
4141
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
42-
import org.springframework.security.oauth2.core.oidc.OidcScopes;
4342
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationException;
4443
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationProvider;
4544
import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationToken;
@@ -151,16 +150,12 @@ private static RequestMatcher createDefaultRequestMatcher(String authorizationEn
151150
HttpMethod.GET.name());
152151
RequestMatcher authorizationRequestPostMatcher = new AntPathRequestMatcher(authorizationEndpointUri,
153152
HttpMethod.POST.name());
154-
RequestMatcher openidScopeMatcher = (request) -> {
155-
String scope = request.getParameter(OAuth2ParameterNames.SCOPE);
156-
return StringUtils.hasText(scope) && scope.contains(OidcScopes.OPENID);
157-
};
153+
158154
RequestMatcher responseTypeParameterMatcher = (
159155
request) -> request.getParameter(OAuth2ParameterNames.RESPONSE_TYPE) != null;
160156

161157
RequestMatcher authorizationRequestMatcher = new OrRequestMatcher(authorizationRequestGetMatcher,
162-
new AndRequestMatcher(authorizationRequestPostMatcher, responseTypeParameterMatcher,
163-
openidScopeMatcher));
158+
new AndRequestMatcher(authorizationRequestPostMatcher, responseTypeParameterMatcher));
164159
RequestMatcher authorizationConsentMatcher = new AndRequestMatcher(authorizationRequestPostMatcher,
165160
new NegatedRequestMatcher(responseTypeParameterMatcher));
166161

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public final class OAuth2AuthorizationCodeRequestAuthenticationConverter impleme
6464
private static final Authentication ANONYMOUS_AUTHENTICATION = new AnonymousAuthenticationToken("anonymous",
6565
"anonymousUser", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
6666

67-
private static final RequestMatcher OIDC_REQUEST_MATCHER = createOidcRequestMatcher();
67+
private static final RequestMatcher POST_WITH_RESPONSE_TYPE_REQUEST_MATCHER = createPostWithResponseTypeRequestMatcher();
6868

6969
@Override
7070
public Authentication convert(HttpServletRequest request) {
71-
if (!"GET".equals(request.getMethod()) && !OIDC_REQUEST_MATCHER.matches(request)) {
71+
if (!"GET".equals(request.getMethod()) && !POST_WITH_RESPONSE_TYPE_REQUEST_MATCHER.matches(request)) {
7272
return null;
7373
}
7474

@@ -153,15 +153,11 @@ else if (!responseType.equals(OAuth2AuthorizationResponseType.CODE.getValue()))
153153
state, scopes, additionalParameters);
154154
}
155155

156-
private static RequestMatcher createOidcRequestMatcher() {
156+
private static RequestMatcher createPostWithResponseTypeRequestMatcher() {
157157
RequestMatcher postMethodMatcher = (request) -> "POST".equals(request.getMethod());
158158
RequestMatcher responseTypeParameterMatcher = (
159159
request) -> request.getParameter(OAuth2ParameterNames.RESPONSE_TYPE) != null;
160-
RequestMatcher openidScopeMatcher = (request) -> {
161-
String scope = request.getParameter(OAuth2ParameterNames.SCOPE);
162-
return StringUtils.hasText(scope) && scope.contains(OidcScopes.OPENID);
163-
};
164-
return new AndRequestMatcher(postMethodMatcher, responseTypeParameterMatcher, openidScopeMatcher);
160+
return new AndRequestMatcher(postMethodMatcher, responseTypeParameterMatcher);
165161
}
166162

167163
private static void throwError(String errorCode, String parameterName) {

oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/server/authorization/web/OAuth2AuthorizationEndpointFilterTests.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -611,19 +611,15 @@ public void doFilterWhenAuthorizationRequestAuthenticatedThenAuthorizationRespon
611611

612612
@Test
613613
public void doFilterWhenAuthenticationRequestAuthenticatedThenAuthorizationResponse() throws Exception {
614-
// Setup OpenID Connect request
615-
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().scopes((scopes) -> {
616-
scopes.clear();
617-
scopes.add(OidcScopes.OPENID);
618-
}).build();
614+
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().scopes(Set::clear).build();
619615
OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthenticationResult = new OAuth2AuthorizationCodeRequestAuthenticationToken(
620616
AUTHORIZATION_URI, registeredClient.getClientId(), this.principal, this.authorizationCode,
621617
registeredClient.getRedirectUris().iterator().next(), STATE, registeredClient.getScopes());
622618
authorizationCodeRequestAuthenticationResult.setAuthenticated(true);
623619
given(this.authenticationManager.authenticate(any())).willReturn(authorizationCodeRequestAuthenticationResult);
624620

625621
MockHttpServletRequest request = createAuthorizationRequest(registeredClient);
626-
request.setMethod("POST"); // OpenID Connect supports POST method
622+
request.setMethod("POST");
627623
request.setQueryString(null);
628624
MockHttpServletResponse response = new MockHttpServletResponse();
629625
FilterChain filterChain = mock(FilterChain.class);

0 commit comments

Comments
 (0)