Skip to content

Commit

Permalink
Addressed comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Nov 21, 2024
1 parent ca7d892 commit de21353
Show file tree
Hide file tree
Showing 8 changed files with 619 additions and 250 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import org.wso2.carbon.idp.mgt.util.IdPManagementConstants;
import org.wso2.carbon.idp.mgt.util.IdPManagementUtil;
import org.wso2.carbon.idp.mgt.util.MetadataConverter;
import org.wso2.carbon.idp.mgt.util.UserDefinedAuthenticatorEndpointConfigManager;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager;
import org.wso2.carbon.user.core.UserCoreConstants;
Expand Down Expand Up @@ -100,8 +99,6 @@ public class IdentityProviderManager implements IdpManager {
private static volatile IdentityProviderManager instance = new IdentityProviderManager();
private final Pattern userDefinedAuthNameRegexPattern =
Pattern.compile(IdPManagementConstants.USER_DEFINED_AUTHENTICATOR_NAME_REGEX);
private final UserDefinedAuthenticatorEndpointConfigManager endpointConfigurationManager =
new UserDefinedAuthenticatorEndpointConfigManager();

private IdentityProviderManager() {

Expand Down Expand Up @@ -835,7 +832,6 @@ public IdentityProvider getIdPByName(String idPName, String tenantDomain,
IdentityApplicationConstants.DEFAULT_IDP_CONFIG);
}
}
populateEndpointConfig(identityProvider, tenantDomain);

return identityProvider;
}
Expand Down Expand Up @@ -869,7 +865,6 @@ public IdentityProvider getIdPById(String id, String tenantDomain,
IdentityApplicationConstants.DEFAULT_IDP_CONFIG);
}
}
populateEndpointConfig(identityProvider, tenantDomain);

return identityProvider;
}
Expand All @@ -881,7 +876,6 @@ public IdentityProvider getIdPByResourceId(String resourceId, String tenantDomai
validateGetIdPInputValues(resourceId);
int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
IdentityProvider identityProvider = dao.getIdPByResourceId(resourceId, tenantId, tenantDomain);
populateEndpointConfig(identityProvider, tenantDomain);
if (identityProvider == null) {
identityProvider = new FileBasedIdPMgtDAO().getIdPByResourceId(resourceId, tenantDomain);
if (identityProvider == null) {
Expand Down Expand Up @@ -926,7 +920,6 @@ public IdentityProvider getEnabledIdPByName(String idPName, String tenantDomain,
throws IdentityProviderManagementException {

IdentityProvider idp = getIdPByName(idPName, tenantDomain, ignoreFileBasedIdps);
populateEndpointConfig(idp, tenantDomain);
if (idp != null && idp.isEnable()) {
return idp;
}
Expand Down Expand Up @@ -977,7 +970,6 @@ public IdentityProvider getIdPByAuthenticatorPropertyValue(String property, Stri

IdentityProvider identityProvider = dao.getIdPByAuthenticatorPropertyValue(
null, property, value, tenantId, tenantDomain);
populateEndpointConfig(identityProvider, tenantDomain);

if (identityProvider == null && !ignoreFileBasedIdps) {
identityProvider = new FileBasedIdPMgtDAO()
Expand Down Expand Up @@ -1009,7 +1001,6 @@ public IdentityProvider getIdPByAuthenticatorPropertyValue(String property, Stri

IdentityProvider identityProvider = dao.getIdPByAuthenticatorPropertyValue(
null, property, value, authenticator, tenantId, tenantDomain);
populateEndpointConfig(identityProvider, tenantDomain);

if (identityProvider == null && !ignoreFileBasedIdps) {
identityProvider = new FileBasedIdPMgtDAO()
Expand Down Expand Up @@ -1540,18 +1531,8 @@ public IdentityProvider addIdPWithResourceId(IdentityProvider identityProvider,

handleMetadata(tenantId, identityProvider);
resolveAuthenticatorDefinedByProperty(identityProvider, true);

String resourceId;
addEndpointConfig(identityProvider, tenantDomain);
try {
resourceId = dao.addIdP(identityProvider, tenantId, tenantDomain);
} catch (IdentityProviderManagementException e) {
deleteEndpointConfig(identityProvider, tenantDomain);
throw e;
}

String resourceId = dao.addIdP(identityProvider, tenantId, tenantDomain);
identityProvider = dao.getIdPByResourceId(resourceId, tenantId, tenantDomain);
populateEndpointConfig(identityProvider, tenantDomain);

// invoking the post listeners
for (IdentityProviderMgtListener listener : listeners) {
Expand Down Expand Up @@ -1612,7 +1593,7 @@ public void deleteIdP(String idPName, String tenantDomain) throws IdentityProvid
if (identityProvider == null) {
return;
}
deleteIDP(identityProvider, tenantDomain);
deleteIDP(identityProvider.getResourceId(), idPName, tenantDomain);

// Invoking the post listeners.
for (IdentityProviderMgtListener listener : listeners) {
Expand Down Expand Up @@ -1678,7 +1659,7 @@ public void deleteIdPByResourceId(String resourceId, String tenantDomain) throws
if (identityProvider == null) {
return;
}
deleteIDP(identityProvider, tenantDomain);
deleteIDP(resourceId, identityProvider.getIdentityProviderName(), tenantDomain);

// Invoking the post listeners.
for (IdentityProviderMgtListener listener : listeners) {
Expand Down Expand Up @@ -1708,27 +1689,20 @@ private void deleteMetadataStrings(String idpName, int tenantId) throws Identity
/**
* Delete an IDP.
*
* @param identityProvider Identity Provider
* @param resourceId Resource Id
* @param idpName Name of the IDP
* @param tenantDomain Tenant Domain
* @throws IdentityProviderManagementException
*/
private void deleteIDP(IdentityProvider identityProvider, String tenantDomain) throws
private void deleteIDP(String resourceId, String idpName, String tenantDomain) throws
IdentityProviderManagementException {

int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);

// Delete metadata strings of the IDP
deleteMetadataStrings(identityProvider.getIdentityProviderName(), tenantId);

deleteEndpointConfig(identityProvider, tenantDomain);

try {
dao.deleteIdPByResourceId(identityProvider.getResourceId(), tenantId, tenantDomain);
} catch (IdentityProviderManagementException e) {
addEndpointConfig(identityProvider, tenantDomain);
throw e;
}
deleteMetadataStrings(idpName, tenantId);

dao.deleteIdPByResourceId(resourceId, tenantId, tenantDomain);
}

/**
Expand Down Expand Up @@ -1756,7 +1730,7 @@ public void forceDeleteIdp(String idpName, String tenantDomain) throws IdentityP
throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage
.ERROR_CODE_IDP_NAME_DOES_NOT_EXIST, idpName);
}
forceDeleteIDP(identityProvider, tenantDomain);
forceDeleteIDP(identityProvider.getResourceId(), idpName, tenantDomain);

// Invoking the post listeners.
for (IdentityProviderMgtListener listener : listeners) {
Expand Down Expand Up @@ -1789,7 +1763,7 @@ public void forceDeleteIdpByResourceId(String resourceId, String tenantDomain) t
throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage
.ERROR_CODE_IDP_DOES_NOT_EXIST, resourceId);
}
forceDeleteIDP(identityProvider, tenantDomain);
forceDeleteIDP(resourceId, identityProvider.getIdentityProviderName(), tenantDomain);

// Invoking the post listeners
for (IdentityProviderMgtListener listener : listeners) {
Expand All @@ -1800,23 +1774,17 @@ public void forceDeleteIdpByResourceId(String resourceId, String tenantDomain) t
}
}

private void forceDeleteIDP(IdentityProvider identityProvider, String tenantDomain) throws
private void forceDeleteIDP(String resourceId, String idpName, String tenantDomain) throws
IdentityProviderManagementException {

int tenantId = IdentityTenantUtil.getTenantId(tenantDomain);
for (MetadataConverter metadataConverter : IdpMgtServiceComponentHolder.getInstance().getMetadataConverters()) {
if (metadataConverter.canDelete(tenantId, identityProvider.getIdentityProviderName())) {
metadataConverter.deleteMetadataString(tenantId, identityProvider.getIdentityProviderName());
if (metadataConverter.canDelete(tenantId, idpName)) {
metadataConverter.deleteMetadataString(tenantId, idpName);
}
}

deleteEndpointConfig(identityProvider, tenantDomain);
try {
dao.forceDeleteIdPByResourceId(identityProvider.getResourceId(), tenantId, tenantDomain);
} catch (IdentityProviderManagementException e) {
addEndpointConfig(identityProvider, tenantDomain);
throw e;
}
dao.forceDeleteIdPByResourceId(resourceId, tenantId, tenantDomain);
}

/**
Expand Down Expand Up @@ -1896,9 +1864,7 @@ public IdentityProvider updateIdPByResourceId(String resourceId, IdentityProvide
return null;
}
}
IdentityProvider identityProvider = dao.getUpdatedIdPByResourceId(resourceId, tenantId, tenantDomain);
populateEndpointConfig(identityProvider, tenantDomain);
return identityProvider;
return dao.getUpdatedIdPByResourceId(resourceId, tenantId, tenantDomain);
}

private void updateIDP(IdentityProvider currentIdentityProvider, IdentityProvider newIdentityProvider, int tenantId,
Expand All @@ -1915,14 +1881,7 @@ private void updateIDP(IdentityProvider currentIdentityProvider, IdentityProvide
validateIdPIssuerName(currentIdentityProvider, newIdentityProvider, tenantId, tenantDomain);
handleMetadata(tenantId, newIdentityProvider);
resolveAuthenticatorDefinedByProperty(newIdentityProvider, false);
updateEndpointConfig(newIdentityProvider, currentIdentityProvider, tenantDomain);
try {
dao.updateIdP(newIdentityProvider, currentIdentityProvider, tenantId, tenantDomain);
} catch (IdentityProviderManagementException e) {
updateEndpointConfig(currentIdentityProvider, newIdentityProvider, tenantDomain);
throw e;
}

dao.updateIdP(newIdentityProvider, currentIdentityProvider, tenantId, tenantDomain);
}

/**
Expand Down Expand Up @@ -2703,6 +2662,7 @@ private Map<String, List<String>> createFedAuthConfidentialPropsMap() throws Ide
Map<String, List<String>> metaFedAuthConfigMap = new HashMap<>();
FederatedAuthenticatorConfig[] metaFedAuthConfigs = getAllFederatedAuthenticators();
for (FederatedAuthenticatorConfig metaFedAuthConfig : metaFedAuthConfigs) {
// Continuing as user defined authenticators does not have any authenticator properties having secrets.
if (metaFedAuthConfig.getDefinedByType() == DefinedByType.USER) {
continue;
}
Expand Down Expand Up @@ -2788,56 +2748,4 @@ private void resolveAuthenticatorDefinedByProperty(IdentityProvider idp, boolean
}
}
}

private void populateEndpointConfig(IdentityProvider identityProvider, String tenantDomain)
throws AuthenticatorEndpointConfigServerException {

if (identityProvider == null || identityProvider.getFederatedAuthenticatorConfigs().length != 1) {
return;
}
endpointConfigurationManager.resolveEndpointConfig(identityProvider.getFederatedAuthenticatorConfigs()[0],
tenantDomain);
}

private void addEndpointConfig(IdentityProvider identityProvider, String tenantDomain)
throws AuthenticatorEndpointConfigServerException {

if (identityProvider == null || identityProvider.getFederatedAuthenticatorConfigs().length != 1) {
return;
}
endpointConfigurationManager.addEndpointConfig(identityProvider.getFederatedAuthenticatorConfigs()[0],
tenantDomain);
}

private void updateEndpointConfig(IdentityProvider newIdentityProvider, IdentityProvider oldIdentityProvider,
String tenantDomain)
throws AuthenticatorEndpointConfigServerException {

if (newIdentityProvider == null || newIdentityProvider.getFederatedAuthenticatorConfigs().length != 1) {
return;
}
FederatedAuthenticatorConfig newFederatedAuth = newIdentityProvider.getFederatedAuthenticatorConfigs()[0];
FederatedAuthenticatorConfig oldFederatedAuth = oldIdentityProvider.getFederatedAuthenticatorConfigs()[0];
if (newFederatedAuth.getDefinedByType() == DefinedByType.SYSTEM) {
return;
}

if (StringUtils.equals(newFederatedAuth.getName(), oldFederatedAuth.getName())) {
endpointConfigurationManager.updateEndpointConfig(newIdentityProvider.getFederatedAuthenticatorConfigs()[0],
oldIdentityProvider.getFederatedAuthenticatorConfigs()[0],
tenantDomain);
}
endpointConfigurationManager.deleteEndpointConfig(oldFederatedAuth, tenantDomain);
endpointConfigurationManager.addEndpointConfig(newFederatedAuth, tenantDomain);
}

private void deleteEndpointConfig(IdentityProvider identityProvider, String tenantDomain)
throws AuthenticatorEndpointConfigServerException {

if (identityProvider == null || identityProvider.getFederatedAuthenticatorConfigs().length != 1) {
return;
}
endpointConfigurationManager.deleteEndpointConfig(identityProvider.getFederatedAuthenticatorConfigs()[0],
tenantDomain);
}
}
Loading

0 comments on commit de21353

Please sign in to comment.