-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix token endpoint filter for authorization code flow #120 #121
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,6 +257,57 @@ public void doFilterWhenAuthorizationCodeTokenRequestValidThenAccessTokenRespons | |
assertThat(accessTokenResult.getScopes()).isEqualTo(accessToken.getScopes()); | ||
} | ||
|
||
@Test | ||
public void doFilterWhenAuthorizationCodeAndClientIdTokenRequestValidThenAccessTokenResponse() throws Exception { | ||
RegisteredClient registeredClient = TestRegisteredClients.registeredClient().build(); | ||
Authentication clientPrincipal = new OAuth2ClientAuthenticationToken(registeredClient); | ||
OAuth2AccessToken accessToken = new OAuth2AccessToken( | ||
OAuth2AccessToken.TokenType.BEARER, "token", | ||
Instant.now(), Instant.now().plus(Duration.ofHours(1)), | ||
new HashSet<>(Arrays.asList("scope1", "scope2"))); | ||
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication = | ||
new OAuth2AccessTokenAuthenticationToken( | ||
registeredClient, clientPrincipal, accessToken); | ||
|
||
when(this.authenticationManager.authenticate(any())).thenReturn(accessTokenAuthentication); | ||
|
||
SecurityContext securityContext = SecurityContextHolder.createEmptyContext(); | ||
securityContext.setAuthentication(clientPrincipal); | ||
SecurityContextHolder.setContext(securityContext); | ||
|
||
MockHttpServletRequest request = createAuthorizationCodeAndClientIdTokenRequest(registeredClient); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
FilterChain filterChain = mock(FilterChain.class); | ||
|
||
this.filter.doFilter(request, response, filterChain); | ||
|
||
verifyNoInteractions(filterChain); | ||
|
||
ArgumentCaptor<OAuth2AuthorizationCodeAuthenticationToken> authorizationCodeAuthenticationCaptor = | ||
ArgumentCaptor.forClass(OAuth2AuthorizationCodeAuthenticationToken.class); | ||
verify(this.authenticationManager).authenticate(authorizationCodeAuthenticationCaptor.capture()); | ||
|
||
OAuth2AuthorizationCodeAuthenticationToken authorizationCodeAuthentication = | ||
authorizationCodeAuthenticationCaptor.getValue(); | ||
assertThat(authorizationCodeAuthentication.getCode()).isEqualTo( | ||
request.getParameter(OAuth2ParameterNames.CODE)); | ||
assertThat(authorizationCodeAuthentication.getPrincipal()).isEqualTo(clientPrincipal); | ||
assertThat(authorizationCodeAuthentication.getRedirectUri()).isEqualTo( | ||
request.getParameter(OAuth2ParameterNames.REDIRECT_URI)); | ||
|
||
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); | ||
OAuth2AccessTokenResponse accessTokenResponse = readAccessTokenResponse(response); | ||
|
||
OAuth2AccessToken accessTokenResult = accessTokenResponse.getAccessToken(); | ||
assertThat(accessTokenResult.getTokenType()).isEqualTo(accessToken.getTokenType()); | ||
assertThat(accessTokenResult.getTokenValue()).isEqualTo(accessToken.getTokenValue()); | ||
assertThat(accessTokenResult.getIssuedAt()).isBetween( | ||
accessToken.getIssuedAt().minusSeconds(1), accessToken.getIssuedAt().plusSeconds(1)); | ||
assertThat(accessTokenResult.getExpiresAt()).isBetween( | ||
accessToken.getExpiresAt().minusSeconds(1), accessToken.getExpiresAt().plusSeconds(1)); | ||
assertThat(accessTokenResult.getScopes()).isEqualTo(accessToken.getScopes()); | ||
} | ||
|
||
@Test | ||
public void doFilterWhenTokenRequestMultipleScopeThenInvalidRequestError() throws Exception { | ||
RegisteredClient registeredClient = TestRegisteredClients.registeredClient2().build(); | ||
|
@@ -363,6 +414,21 @@ private static MockHttpServletRequest createAuthorizationCodeTokenRequest(Regist | |
return request; | ||
} | ||
|
||
private static MockHttpServletRequest createAuthorizationCodeAndClientIdTokenRequest(RegisteredClient registeredClient) { | ||
String[] redirectUris = registeredClient.getRedirectUris().toArray(new String[0]); | ||
|
||
String requestUri = OAuth2TokenEndpointFilter.DEFAULT_TOKEN_ENDPOINT_URI; | ||
MockHttpServletRequest request = new MockHttpServletRequest("POST", requestUri); | ||
request.setServletPath(requestUri); | ||
|
||
request.addParameter(OAuth2ParameterNames.GRANT_TYPE, AuthorizationGrantType.AUTHORIZATION_CODE.getValue()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vincent-hsin This request is invalid. As per spec, in Section 4.1.3. Access Token Request:
Only confidential clients are supported at the moment, so the client MUST authenticate at the token endpoint. The change in this PR would allow a malicious user to obtain a token. FYI, public client authentication support is coming in #45. I'm going to close this PR and associated issue based on the above explanation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I got it. I will contribute for #45. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
request.addParameter(OAuth2ParameterNames.CODE, "code"); | ||
request.addParameter(OAuth2ParameterNames.REDIRECT_URI, redirectUris[0]); | ||
request.addParameter(OAuth2ParameterNames.CLIENT_ID, registeredClient.getClientId()); | ||
|
||
return request; | ||
} | ||
|
||
private static MockHttpServletRequest createClientCredentialsTokenRequest(RegisteredClient registeredClient) { | ||
String requestUri = OAuth2TokenEndpointFilter.DEFAULT_TOKEN_ENDPOINT_URI; | ||
MockHttpServletRequest request = new MockHttpServletRequest("POST", requestUri); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jgrandja this unit test for #120