Skip to content

Commit 4ab33f7

Browse files
committed
add github example for Oauth2.0 identity provider test
1 parent f7dd6eb commit 4ab33f7

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

server/src/test/java/org/cloudfoundry/identity/uaa/provider/oauth/ExternalOAuthAuthenticationManagerIT.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.apache.commons.lang3.RandomStringUtils;
88
import org.cloudfoundry.identity.uaa.authentication.AccountNotPreCreatedException;
99
import org.cloudfoundry.identity.uaa.authentication.UaaAuthentication;
10+
import org.cloudfoundry.identity.uaa.authentication.UaaPrincipal;
1011
import org.cloudfoundry.identity.uaa.authentication.event.IdentityProviderAuthenticationSuccessEvent;
1112
import org.cloudfoundry.identity.uaa.authentication.manager.ExternalGroupAuthorizationEvent;
1213
import org.cloudfoundry.identity.uaa.authentication.manager.InvitedUserAuthenticatedEvent;
@@ -95,6 +96,7 @@
9596
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
9697
import static org.assertj.core.api.Assertions.assertThatThrownBy;
9798
import static org.assertj.core.api.Assertions.fail;
99+
import static org.cloudfoundry.identity.uaa.constants.OriginKeys.OAUTH20;
98100
import static org.cloudfoundry.identity.uaa.oauth.token.ClaimConstants.ISS;
99101
import static org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition.GROUP_ATTRIBUTE_NAME;
100102
import static org.cloudfoundry.identity.uaa.provider.ExternalIdentityProviderDefinition.USER_NAME_ATTRIBUTE_NAME;
@@ -109,6 +111,7 @@
109111
import static org.mockito.Mockito.eq;
110112
import static org.mockito.Mockito.mock;
111113
import static org.mockito.Mockito.never;
114+
import static org.mockito.Mockito.reset;
112115
import static org.mockito.Mockito.same;
113116
import static org.mockito.Mockito.spy;
114117
import static org.mockito.Mockito.times;
@@ -515,6 +518,55 @@ void discoveryURL_is_used() throws MalformedURLException {
515518

516519
}
517520

521+
@Test
522+
void oauth20_flow_works_with_non_jwt_token() throws Exception {
523+
String userInfoResponse = """
524+
{
525+
"login": "octocat",
526+
"id": 1,
527+
"type": "User",
528+
"site_admin": false,
529+
"name": "monalisa octocat",
530+
"company": "GitHub",
531+
"email": "[email protected]"
532+
}""";
533+
534+
CompositeToken accessToken = getCompositeAccessToken();
535+
accessToken.setIdTokenValue(null); //DOES NOT EXIST FOR OAUTH2.0
536+
String oauth2TokenResponse = JsonUtils.writeValueAsString(accessToken);
537+
538+
//UAA exchanges the code for a token
539+
mockUaaServer.expect(requestTo("http://localhost/oauth/token"))
540+
.andExpect(header("Authorization", "Basic " + new String(Base64.encodeBase64("identity:identitysecret".getBytes()))))
541+
.andExpect(header("Accept", "application/json"))
542+
.andExpect(content().string(containsString("grant_type=authorization_code")))
543+
.andExpect(content().string(containsString("code=the_code")))
544+
.andExpect(content().string(containsString("redirect_uri=http%3A%2F%2Flocalhost%2Fcallback%2Fthe_origin")))
545+
.andExpect(content().string(containsString("response_type=code")))
546+
.andRespond(withStatus(OK).contentType(APPLICATION_JSON).body(oauth2TokenResponse));
547+
548+
//UAA retrieves user info using an access token
549+
mockUaaServer.expect(requestTo(config.getUserInfoUrl().toString()))
550+
.andRespond(withStatus(OK).contentType(APPLICATION_JSON).body(userInfoResponse));
551+
552+
IdentityProvider<RawExternalOAuthIdentityProviderDefinition> identityProvider = getOauth20Provider();
553+
identityProvider.getConfig().setResponseType("code");
554+
reset(provisioning);
555+
when(provisioning.retrieveByOrigin(eq(ORIGIN), anyString())).thenReturn(identityProvider);
556+
557+
addTheUserOnAuth();
558+
559+
Authentication authentication = externalOAuthAuthenticationManager.authenticate(xCodeToken);
560+
assertThat(authentication).isNotNull();
561+
assertThat(authentication.getPrincipal()).isInstanceOf(UaaPrincipal.class);
562+
UaaPrincipal principal = (UaaPrincipal) authentication.getPrincipal();
563+
assertThat(principal).isNotNull();
564+
assertThat(principal.getName()).isEqualTo("octocat");
565+
assertThat(principal.getEmail()).isEqualTo("[email protected]");
566+
mockUaaServer.verify();
567+
568+
}
569+
518570
@Test
519571
void clientAuthInBody_is_used() {
520572
config.setClientAuthInBody(true);
@@ -1291,6 +1343,31 @@ private IdentityProvider<OIDCIdentityProviderDefinition> getProvider() {
12911343
return identityProvider;
12921344
}
12931345

1346+
private IdentityProvider<RawExternalOAuthIdentityProviderDefinition> getOauth20Provider() throws Exception {
1347+
RawExternalOAuthIdentityProviderDefinition config = new RawExternalOAuthIdentityProviderDefinition()
1348+
.setAuthUrl(URI.create("http://localhost/oauth/authorize").toURL())
1349+
.setTokenUrl(URI.create("http://localhost/oauth/token").toURL())
1350+
.setIssuer("http://localhost/oauth/token")
1351+
.setShowLinkText(true)
1352+
.setLinkText("My oauth20 Provider")
1353+
.setRelyingPartyId("identity")
1354+
.setRelyingPartySecret("identitysecret")
1355+
.setUserInfoUrl(URI.create("http://localhost/userinfo").toURL())
1356+
.setTokenKey(PUBLIC_KEY);
1357+
config.setExternalGroupsWhitelist(Collections.singletonList("*"));
1358+
attributeMappings.put(USER_NAME_ATTRIBUTE_NAME, "login");
1359+
config.setAttributeMappings(attributeMappings);
1360+
config.setResponseType("code");
1361+
1362+
IdentityProvider<RawExternalOAuthIdentityProviderDefinition> identityProvider = new IdentityProvider<>();
1363+
identityProvider.setName("my oauth20 provider");
1364+
identityProvider.setIdentityZoneId(OriginKeys.UAA);
1365+
identityProvider.setType(OAUTH20);
1366+
identityProvider.setConfig(config);
1367+
identityProvider.setOriginKey(ORIGIN);
1368+
return identityProvider;
1369+
}
1370+
12941371
private void testTokenHasAuthoritiesFromIdTokenRoles() {
12951372
attributeMappings.put(GROUP_ATTRIBUTE_NAME, "scope");
12961373
mockToken();

0 commit comments

Comments
 (0)