Skip to content

Commit

Permalink
Merge pull request #6363 from Thisara-Welmilla/add-new-custom-auth-mgt
Browse files Browse the repository at this point in the history
Add new method to get federated authenticator by name.
  • Loading branch information
Thisara-Welmilla authored Jan 25, 2025
2 parents a3229c5 + f9d5db7 commit 4796514
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ public LocalAuthenticatorConfig getLocalAuthenticatorByName(String name, String
return getUserDefinedLocalAuthenticator(name, tenantDomain);
}

/**
* This returns only SYSTEM defined federated authenticator by name.
*
* @param name The name of the federated application authenticator configuration.
* @return Retrieved FederatedAuthenticatorConfig.
*
* @deprecated It is recommended to use getFederatedAuthenticatorByName(String, String) in
* org.wso2.carbon.idp.mgt.IdentityProviderManager class which supports retrieving both USER and SYSTEM defined
* federated application authenticator configuration by name, or getSystemFederatedAuthenticatorByName(String)
* to retrieve only SYSTEM defined federated application authenticator configuration by name.
*/
@Deprecated
public FederatedAuthenticatorConfig getFederatedAuthenticatorByName(String name) {
for (FederatedAuthenticatorConfig federatedAuthenticator : federatedAuthenticators) {
if (federatedAuthenticator.getName().equals(name)) {
Expand All @@ -170,6 +182,16 @@ public FederatedAuthenticatorConfig getFederatedAuthenticatorByName(String name)
return null;
}

public FederatedAuthenticatorConfig getSystemDefinedFederatedAuthenticatorByName(String name) {

for (FederatedAuthenticatorConfig federatedAuthenticator : federatedAuthenticators) {
if (federatedAuthenticator.getName().equals(name)) {
return federatedAuthenticator;
}
}
return null;
}

public RequestPathAuthenticatorConfig getRequestPathAuthenticatorByName(String name) {
for (RequestPathAuthenticatorConfig reqPathAuthenticator : requestPathAuthenticators) {
if (reqPathAuthenticator.getName().equals(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ protected void unsetAuthenticator(ApplicationAuthenticator authenticator) {
appAuthenticatorService.removeLocalAuthenticator(localAuthenticatorConfig);
} else if (authenticator instanceof FederatedApplicationAuthenticator) {
FederatedAuthenticatorConfig federatedAuthenticatorConfig = appAuthenticatorService
.getFederatedAuthenticatorByName(authenticatorName);
.getSystemDefinedFederatedAuthenticatorByName(authenticatorName);
appAuthenticatorService.removeFederatedAuthenticator(federatedAuthenticatorConfig);
} else if (authenticator instanceof RequestPathApplicationAuthenticator) {
RequestPathAuthenticatorConfig reqPathAuthenticatorConfig = appAuthenticatorService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,7 @@ public IdentityProvider addIdPWithResourceId(IdentityProvider identityProvider,
}

handleMetadata(tenantId, identityProvider);
resolveAuthenticatorDefinedByProperty(identityProvider, true);
resolveAuthenticatorDefinedByProperty(identityProvider, true, tenantDomain);
String resourceId = dao.addIdP(identityProvider, tenantId, tenantDomain);
identityProvider = dao.getIdPByResourceId(resourceId, tenantId, tenantDomain);

Expand Down Expand Up @@ -1881,7 +1881,7 @@ private void updateIDP(IdentityProvider currentIdentityProvider, IdentityProvide

validateIdPIssuerName(currentIdentityProvider, newIdentityProvider, tenantId, tenantDomain);
handleMetadata(tenantId, newIdentityProvider);
resolveAuthenticatorDefinedByProperty(newIdentityProvider, false);
resolveAuthenticatorDefinedByProperty(newIdentityProvider, false, tenantDomain);
dao.updateIdP(newIdentityProvider, currentIdentityProvider, tenantId, tenantDomain);
}

Expand Down Expand Up @@ -2227,8 +2227,7 @@ private void validateFederatedAuthenticatorConfigName(FederatedAuthenticatorConf
for (FederatedAuthenticatorConfig config : federatedAuthConfigs) {
if (config.getDefinedByType() == DefinedByType.SYSTEM) {
// Check if there is a system registered authenticator given authenticator name.
if (ApplicationAuthenticatorService.getInstance()
.getFederatedAuthenticatorByName(config.getName()) == null) {
if (getFederatedAuthenticatorByName(config.getName(), tenantDomain) == null) {
throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage
.ERROR_CODE_NO_SYSTEM_AUTHENTICATOR_FOUND, new String(
Base64.getEncoder().encode(config.getName().getBytes(StandardCharsets.UTF_8))));
Expand Down Expand Up @@ -2390,6 +2389,18 @@ public FederatedAuthenticatorConfig[] getAllFederatedAuthenticators(String tenan
return allFederatedAuthenticators.toArray(new FederatedAuthenticatorConfig[0]);
}

@Override
public FederatedAuthenticatorConfig getFederatedAuthenticatorByName(
String authenticatorName, String tenantDomain) throws IdentityProviderManagementException {

for (FederatedAuthenticatorConfig fedAuth : getAllFederatedAuthenticators(tenantDomain)) {
if (fedAuth.getName().equals(authenticatorName)) {
return fedAuth;
}
}
return null;
}

private boolean isExistingAuthentication(String authenticatorName, String tenantDomain)
throws IdentityProviderManagementException {

Expand Down Expand Up @@ -2755,7 +2766,8 @@ private boolean isAnOTPLengthConfig(IdentityProviderProperty property) {
return false;
}

private void resolveAuthenticatorDefinedByProperty(IdentityProvider idp, boolean isNewFederatedAuthenticator) {
private void resolveAuthenticatorDefinedByProperty(IdentityProvider idp, boolean isNewFederatedAuthenticator,
String tenantDomain) throws IdentityProviderManagementException {

/* For new federated authenticators: If 'definedByType' is null, set it to default to SYSTEM. */
if (isNewFederatedAuthenticator) {
Expand All @@ -2772,8 +2784,8 @@ private void resolveAuthenticatorDefinedByProperty(IdentityProvider idp, boolean
if not return USER. */
for (FederatedAuthenticatorConfig federatedAuthConfig : idp.getFederatedAuthenticatorConfigs()) {
if (federatedAuthConfig.getDefinedByType() == null) {
FederatedAuthenticatorConfig authenticatorConfig = ApplicationAuthenticatorService.getInstance()
.getFederatedAuthenticatorByName(federatedAuthConfig.getName());
FederatedAuthenticatorConfig authenticatorConfig = getFederatedAuthenticatorByName
(federatedAuthConfig.getName(), tenantDomain);
federatedAuthConfig.setDefinedByType(authenticatorConfig.getDefinedByType());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -607,4 +607,19 @@ default FederatedAuthenticatorConfig[] getAllFederatedAuthenticators(String tena

return new FederatedAuthenticatorConfig[0];
}

/**
* Get the federated authenticator by authenticator name of the tenant domain
* (both system defined and user defined).
*
* @param authenticatorName Authenticator Name.
* @param tenantDomain Tenant Domain.
* @return User all federated authenticators.
* @throws IdentityProviderManagementException If an error occurred while getting the federated authenticator.
*/
default FederatedAuthenticatorConfig getFederatedAuthenticatorByName(
String authenticatorName, String tenantDomain) throws IdentityProviderManagementException {

return null;
}
}

0 comments on commit 4796514

Please sign in to comment.