Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhoelzl-sap committed Feb 23, 2024
1 parent 37c160a commit 0fb5e3a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.cloudfoundry.identity.uaa.constants.OriginKeys.UAA;
import static org.springframework.util.StringUtils.hasText;

import java.util.Objects;
import java.util.Optional;

import org.cloudfoundry.identity.uaa.EntityWithAlias;
Expand Down Expand Up @@ -236,7 +237,7 @@ public final Optional<T> retrieveAliasEntity(final T originalEntity) {

protected abstract T createEntity(final T entity, final String zoneId) throws EntityAliasFailedException;

protected static <T extends EntityWithAlias> boolean isCorrectAliasPair(final T entity1, final T entity2) {
protected static <T extends EntityWithAlias> boolean isValidAliasPair(final T entity1, final T entity2) {
// check if both entities have an alias
final boolean entity1HasAlias = hasText(entity1.getAliasId()) && hasText(entity1.getAliasZid());
final boolean entity2HasAlias = hasText(entity2.getAliasId()) && hasText(entity2.getAliasZid());
Expand All @@ -245,10 +246,10 @@ protected static <T extends EntityWithAlias> boolean isCorrectAliasPair(final T
}

// check if they reference each other
final boolean entity1ReferencesEntity2 = entity1.getAliasId().equals(entity2.getId())
&& entity1.getAliasZid().equals(entity2.getZoneId());
final boolean entity2ReferencesEntity1 = entity2.getAliasId().equals(entity1.getId())
&& entity2.getAliasZid().equals(entity1.getZoneId());
final boolean entity1ReferencesEntity2 = Objects.equals(entity1.getAliasId(), entity2.getId()) &&
Objects.equals(entity1.getAliasZid(), entity2.getZoneId());
final boolean entity2ReferencesEntity1 = Objects.equals(entity2.getAliasId(), entity1.getId()) &&
Objects.equals(entity2.getAliasZid(), entity1.getZoneId());
return entity1ReferencesEntity2 && entity2ReferencesEntity1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.cloudfoundry.identity.uaa.zone.beans.IdentityZoneManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

Expand All @@ -36,25 +36,33 @@ protected ScimUserAliasHandler(

@Override
protected boolean additionalValidationChecksForNewAlias(final ScimUser requestBody) {
// check if the IdP also exists as an alias IdP in the alias zone
/* check if an IdP with the user's origin exists in both the current and the alias zone and that they are
* aliases of each other */
final IdentityProvider<?> idpInAliasZone = retrieveIdpByOrigin(
requestBody.getOrigin(),
requestBody.getAliasZid()
);
final IdentityProvider<?> idpInCurrentZone = retrieveIdpByOrigin(
requestBody.getOrigin(),
identityZoneManager.getCurrentIdentityZoneId()
);
return EntityAliasHandler.isValidAliasPair(idpInCurrentZone, idpInAliasZone);
}

private IdentityProvider<?> retrieveIdpByOrigin(final String originKey, final String zoneId) {
final IdentityProvider<?> idpInAliasZone;
try {
idpInAliasZone = identityProviderProvisioning.retrieveByOrigin(
requestBody.getOrigin(),
requestBody.getAliasZid()
originKey,
zoneId
);
} catch (final DataAccessException e) {
} catch (final EmptyResultDataAccessException e) {
throw new ScimException(
String.format("No IdP with the origin '%s' exists in the alias zone.", requestBody.getOrigin()),
String.format("No IdP with the origin '%s' exists in the zone '%s'.", originKey, zoneId),
HttpStatus.BAD_REQUEST
);
}

final IdentityProvider<?> idpInCurrentZone = identityProviderProvisioning.retrieveByOrigin(
requestBody.getOrigin(),
identityZoneManager.getCurrentIdentityZoneId()
);
return EntityAliasHandler.isCorrectAliasPair(idpInCurrentZone, idpInAliasZone);
return idpInAliasZone;
}

@Override
Expand Down

0 comments on commit 0fb5e3a

Please sign in to comment.