Skip to content

Commit

Permalink
Merge pull request #270 from cschwalm/feature/269_link_with
Browse files Browse the repository at this point in the history
#269 : Missing Management API Functionality for Linking User Accounts…
  • Loading branch information
lbalmaceda authored Jul 27, 2020
2 parents b80f561 + 6c9cc37 commit 984bea3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/com/auth0/client/mgmt/UsersEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,35 @@ public Request<List<Identity>> linkIdentity(String primaryUserId, String seconda
return request;
}

/**
* A token with scope update:current_user_identities is needed.
* It only works for the user the access token represents.
* See https://auth0.com/docs/api/management/v2#!/Users/post_identities
*
* @param primaryUserId the primary identity's user id associated with the access token this client was configured with.
* @param secondaryIdToken the user ID token representing the identity to link with the current user
* @return a Request to execute.
*/
public Request<List<Identity>> linkIdentity(String primaryUserId, String secondaryIdToken) {
Asserts.assertNotNull(primaryUserId, "primary user id");
Asserts.assertNotNull(secondaryIdToken, "secondary id token");

String url = baseUrl
.newBuilder()
.addPathSegments("api/v2/users")
.addPathSegment(primaryUserId)
.addPathSegment("identities")
.build()
.toString();

CustomRequest<List<Identity>> request = new CustomRequest<>(client, url, "POST", new TypeReference<List<Identity>>() {
});
request.addHeader("Authorization", "Bearer " + apiToken);
request.addParameter("link_with", secondaryIdToken);

return request;
}

/**
* Un-links two User's Identities.
* A token with scope update:users is needed.
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/auth0/client/mgmt/UsersEntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,20 @@ public void shouldThrowOnLinkUserIdentityWithNullSecondaryId() throws Exception
api.users().linkIdentity("1", null, "auth0", null);
}

@Test
public void shouldThrowOnLinkUserIdentityWithNullSecondaryToken() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'secondary id token' cannot be null!");
api.users().linkIdentity("1", null);
}

@Test
public void shouldThrowOnLinkUserIdentityWithNullUserId() throws Exception {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("'primary user id' cannot be null!");
api.users().linkIdentity(null, "2");
}

@Test
public void shouldThrowOnLinkUserIdentityWithNullProvider() throws Exception {
exception.expect(IllegalArgumentException.class);
Expand Down Expand Up @@ -602,6 +616,26 @@ public void shouldLinkUserIdentity() throws Exception {
assertThat(response, is(notNullValue()));
}

@Test
public void shouldLinkUserIdentityBySecondaryIdToken() throws Exception {
Request<List<Identity>> request = api.users().linkIdentity("1", "2");
assertThat(request, is(notNullValue()));

server.jsonResponse(MGMT_IDENTITIES_LIST, 200);
List<Identity> response = request.execute();
RecordedRequest recordedRequest = server.takeRequest();

assertThat(recordedRequest, hasMethodAndPath("POST", "/api/v2/users/1/identities"));
assertThat(recordedRequest, hasHeader("Content-Type", "application/json"));
assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));

Map<String, Object> body = bodyFromRequest(recordedRequest);
assertThat(body.size(), is(1));
assertThat(body, hasEntry("link_with", (Object) "2"));

assertThat(response, is(notNullValue()));
}

@Test
public void shouldLinkUserIdentityWithConnectionId() throws Exception {
Request<List<Identity>> request = api.users().linkIdentity("1", "2", "auth0", "123456790");
Expand Down

0 comments on commit 984bea3

Please sign in to comment.