Skip to content

Commit

Permalink
Add Auth0 migration service
Browse files Browse the repository at this point in the history
  • Loading branch information
vpaturet committed Dec 12, 2023
1 parent 8b700da commit d714585
Showing 1 changed file with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ public void createUser(User user) {
@Override
public void updateUser(User user) {
logger.info("Updating user {} in Auth0", user.getUsername());
com.auth0.json.mgmt.users.User existingAuth0User = getAuth0UserByUsername(user.getUsername());

com.auth0.json.mgmt.users.User existingAuth0User = getAuth0UserByEmail(user.getContactDetails().getEmail());


com.auth0.json.mgmt.users.User updatedAuth0User = toAuth0User(user);
// The Auth0 API refuses to update both the username and the email at the same time
updatedAuth0User.setUsername(null);
Expand All @@ -102,7 +105,7 @@ public void updateUser(User user) {
public void resetPassword(User user) {
logger.info("Resetting password in Auth0 for user {}", user.getUsername());
String password = generatePassword();
com.auth0.json.mgmt.users.User existingAuth0User = getAuth0UserByUsername(user.getUsername());
com.auth0.json.mgmt.users.User existingAuth0User = getAuth0UserByEmail(user.getContactDetails().getEmail());
com.auth0.json.mgmt.users.User updatedAuth0User = new com.auth0.json.mgmt.users.User();
updatedAuth0User.setPassword(password.toCharArray());
try {
Expand All @@ -119,7 +122,7 @@ public void removeUser(User user) {
logger.info("Removing user {} from Auth0", user.getUsername());
com.auth0.json.mgmt.users.User existingAuth0User;
try {
existingAuth0User = getAuth0UserByUsername(user.getUsername());
existingAuth0User = getAuth0UserByEmail(user.getContactDetails().getEmail());
} catch (OAuth2UserNotFoundException nfe) {
logger.warn("Ignoring user removal for user {} that does not exist in the Auth0 tenant", user.getUsername());
return;
Expand Down Expand Up @@ -165,7 +168,7 @@ public void updateResponsibilitySet(ResponsibilitySet responsibilitySet) {
List<Role> systemRoles = roleRepository.findAll();
try {
userRepository.findUsersWithResponsibilitySet(responsibilitySet).forEach(u -> {
com.auth0.json.mgmt.users.User auth0User = getAuth0UserByUsername(u.getUsername());
com.auth0.json.mgmt.users.User auth0User = getAuth0UserByEmail(u.getContactDetails().getEmail());
updateRoles(u, auth0User, systemRoles);
});
} catch (Exception e) {
Expand Down Expand Up @@ -289,18 +292,18 @@ private com.auth0.json.mgmt.roles.Role toAuth0Role(Role role) {
return auth0Role;
}

private com.auth0.json.mgmt.users.User getAuth0UserByUsername(String username) {
private com.auth0.json.mgmt.users.User getAuth0UserByEmail(String email) {
try {
List<com.auth0.json.mgmt.users.User> matchingUsers = getManagementAPI().users().list(new UserFilter().withQuery("username:\"" + username + "\"")).execute().getBody().getItems();
List<com.auth0.json.mgmt.users.User> matchingUsers = getManagementAPI().users().list(new UserFilter().withQuery("email:\"" + email + "\"")).execute().getBody().getItems();
if (matchingUsers.isEmpty()) {
throw new OAuth2UserNotFoundException("User not found: " + username);
throw new OAuth2UserNotFoundException("User not found: " + email);
} else if (matchingUsers.size() > 1) {
logger.error("More than one user found in Auth0 tenant with username: {}", username);
throw new OrganisationException("More than one user found with username: " + username);
logger.error("More than one user found in Auth0 tenant with email: {}", email);
throw new OrganisationException("More than one user found with email: " + email);
}
return matchingUsers.get(0);
} catch (Auth0Exception e) {
logger.error("Failed to retrieve the user {} in Auth0", username, e);
logger.error("Failed to retrieve the user {} in Auth0", email, e);
throw new OrganisationException("Failed to retrieve the user");
}
}
Expand Down

0 comments on commit d714585

Please sign in to comment.