-
Notifications
You must be signed in to change notification settings - Fork 841
Fix broken OAUTH2.0 authorization_code flow. #3643
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
Merged
fhanik
merged 2 commits into
cloudfoundry:develop
from
fhanik:pr/fix-oauth2.0-authorization-flow
Oct 16, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| import org.apache.commons.lang3.RandomStringUtils; | ||
| import org.cloudfoundry.identity.uaa.authentication.AccountNotPreCreatedException; | ||
| import org.cloudfoundry.identity.uaa.authentication.UaaAuthentication; | ||
| import org.cloudfoundry.identity.uaa.authentication.UaaPrincipal; | ||
| import org.cloudfoundry.identity.uaa.authentication.event.IdentityProviderAuthenticationSuccessEvent; | ||
| import org.cloudfoundry.identity.uaa.authentication.manager.ExternalGroupAuthorizationEvent; | ||
| import org.cloudfoundry.identity.uaa.authentication.manager.InvitedUserAuthenticatedEvent; | ||
|
|
@@ -95,6 +96,7 @@ | |
| import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.assertj.core.api.Assertions.fail; | ||
| import static org.cloudfoundry.identity.uaa.constants.OriginKeys.OAUTH20; | ||
| import static org.cloudfoundry.identity.uaa.oauth.token.ClaimConstants.ISS; | ||
| import static org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition.GROUP_ATTRIBUTE_NAME; | ||
| import static org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition.USER_NAME_ATTRIBUTE_NAME; | ||
|
|
@@ -109,6 +111,7 @@ | |
| import static org.mockito.Mockito.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.reset; | ||
| import static org.mockito.Mockito.same; | ||
| import static org.mockito.Mockito.spy; | ||
| import static org.mockito.Mockito.times; | ||
|
|
@@ -515,6 +518,55 @@ void discoveryURL_is_used() throws MalformedURLException { | |
|
|
||
| } | ||
|
|
||
| @Test | ||
| void oauth20_flow_works_with_non_jwt_token() throws Exception { | ||
| String userInfoResponse = """ | ||
| { | ||
| "login": "octocat", | ||
| "id": 1, | ||
| "type": "User", | ||
| "site_admin": false, | ||
| "name": "monalisa octocat", | ||
| "company": "GitHub", | ||
| "email": "[email protected]" | ||
| }"""; | ||
|
|
||
| CompositeToken accessToken = getCompositeAccessToken(); | ||
| accessToken.setIdTokenValue(null); //DOES NOT EXIST FOR OAUTH2.0 | ||
| String oauth2TokenResponse = JsonUtils.writeValueAsString(accessToken); | ||
|
|
||
| //UAA exchanges the code for a token | ||
| mockUaaServer.expect(requestTo("http://localhost/oauth/token")) | ||
| .andExpect(header("Authorization", "Basic " + new String(Base64.encodeBase64("identity:identitysecret".getBytes())))) | ||
| .andExpect(header("Accept", "application/json")) | ||
| .andExpect(content().string(containsString("grant_type=authorization_code"))) | ||
| .andExpect(content().string(containsString("code=the_code"))) | ||
| .andExpect(content().string(containsString("redirect_uri=http%3A%2F%2Flocalhost%2Fcallback%2Fthe_origin"))) | ||
| .andExpect(content().string(containsString("response_type=code"))) | ||
| .andRespond(withStatus(OK).contentType(APPLICATION_JSON).body(oauth2TokenResponse)); | ||
|
|
||
| //UAA retrieves user info using an access token | ||
| mockUaaServer.expect(requestTo(config.getUserInfoUrl().toString())) | ||
| .andRespond(withStatus(OK).contentType(APPLICATION_JSON).body(userInfoResponse)); | ||
|
|
||
| IdentityProvider<RawExternalOAuthIdentityProviderDefinition> identityProvider = getOauth20Provider(); | ||
| identityProvider.getConfig().setResponseType("code"); | ||
| reset(provisioning); | ||
| when(provisioning.retrieveByOrigin(eq(ORIGIN), anyString())).thenReturn(identityProvider); | ||
|
|
||
| addTheUserOnAuth(); | ||
|
|
||
| Authentication authentication = externalOAuthAuthenticationManager.authenticate(xCodeToken); | ||
| assertThat(authentication).isNotNull(); | ||
| assertThat(authentication.getPrincipal()).isInstanceOf(UaaPrincipal.class); | ||
| UaaPrincipal principal = (UaaPrincipal) authentication.getPrincipal(); | ||
| assertThat(principal).isNotNull(); | ||
| assertThat(principal.getName()).isEqualTo("octocat"); | ||
| assertThat(principal.getEmail()).isEqualTo("[email protected]"); | ||
| mockUaaServer.verify(); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| void clientAuthInBody_is_used() { | ||
| config.setClientAuthInBody(true); | ||
|
|
@@ -1291,6 +1343,31 @@ private IdentityProvider<OIDCIdentityProviderDefinition> getProvider() { | |
| return identityProvider; | ||
| } | ||
|
|
||
| private IdentityProvider<RawExternalOAuthIdentityProviderDefinition> getOauth20Provider() throws Exception { | ||
| RawExternalOAuthIdentityProviderDefinition config = new RawExternalOAuthIdentityProviderDefinition() | ||
| .setAuthUrl(URI.create("http://localhost/oauth/authorize").toURL()) | ||
| .setTokenUrl(URI.create("http://localhost/oauth/token").toURL()) | ||
| .setIssuer("http://localhost/oauth/token") | ||
| .setShowLinkText(true) | ||
| .setLinkText("My oauth20 Provider") | ||
| .setRelyingPartyId("identity") | ||
| .setRelyingPartySecret("identitysecret") | ||
| .setUserInfoUrl(URI.create("http://localhost/userinfo").toURL()) | ||
| .setTokenKey(PUBLIC_KEY); | ||
| config.setExternalGroupsWhitelist(Collections.singletonList("*")); | ||
| attributeMappings.put(USER_NAME_ATTRIBUTE_NAME, "login"); | ||
| config.setAttributeMappings(attributeMappings); | ||
| config.setResponseType("code"); | ||
|
|
||
| IdentityProvider<RawExternalOAuthIdentityProviderDefinition> identityProvider = new IdentityProvider<>(); | ||
| identityProvider.setName("my oauth20 provider"); | ||
| identityProvider.setIdentityZoneId(OriginKeys.UAA); | ||
| identityProvider.setType(OAUTH20); | ||
| identityProvider.setConfig(config); | ||
| identityProvider.setOriginKey(ORIGIN); | ||
| return identityProvider; | ||
| } | ||
|
|
||
| private void testTokenHasAuthoritiesFromIdTokenRoles() { | ||
| attributeMappings.put(GROUP_ATTRIBUTE_NAME, "scope"); | ||
| mockToken(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The hardcoded string 'access_token' should be extracted to a constant to improve maintainability and reduce the risk of typos.