Skip to content

Commit

Permalink
Add new method to get federated authenticator by name.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Jan 25, 2025
1 parent 9b739c1 commit 3935c78
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.authentication.framework.exception.ApplicationAuthenticationException;
import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceComponent;
import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager;

import java.util.ArrayList;
import java.util.List;

/**
* Application authentication service.
* Application authentication service. This server only return the system defined authenticators.
* This server is exposed to external and currently only being used for API based authenticator which is only support
* for system defined authenticators.
*/
@Deprecated
public class ApplicationAuthenticationService {

private static final Log log = LogFactory.getLog(ApplicationAuthenticationService.class);
Expand All @@ -43,7 +46,8 @@ public ApplicationAuthenticator getAuthenticator(String name) throws Application

ApplicationAuthenticator appAuthenticator = null;

for (ApplicationAuthenticator authenticator : FrameworkServiceComponent.getAuthenticators()) {
for (ApplicationAuthenticator authenticator :
ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()) {

if (authenticator.getName().equals(name)) {
appAuthenticator = authenticator;
Expand All @@ -54,14 +58,15 @@ public ApplicationAuthenticator getAuthenticator(String name) throws Application
}

public List<ApplicationAuthenticator> getAllAuthenticators() throws ApplicationAuthenticationException {
return FrameworkServiceComponent.getAuthenticators();
return ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators();
}

public List<ApplicationAuthenticator> getLocalAuthenticators() throws ApplicationAuthenticationException {

List<ApplicationAuthenticator> localAuthenticators = new ArrayList<ApplicationAuthenticator>();

for (ApplicationAuthenticator authenticator : FrameworkServiceComponent.getAuthenticators()) {
for (ApplicationAuthenticator authenticator :
ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()) {

if (authenticator instanceof LocalApplicationAuthenticator) {
localAuthenticators.add(authenticator);
Expand All @@ -75,7 +80,8 @@ public List<ApplicationAuthenticator> getFederatedAuthenticators() throws Applic

List<ApplicationAuthenticator> federatedAuthenticators = new ArrayList<ApplicationAuthenticator>();

for (ApplicationAuthenticator authenticator : FrameworkServiceComponent.getAuthenticators()) {
for (ApplicationAuthenticator authenticator :
ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()) {

if (authenticator instanceof FederatedApplicationAuthenticator) {
federatedAuthenticators.add(authenticator);
Expand All @@ -89,7 +95,8 @@ public List<ApplicationAuthenticator> getRequestPathAuthenticators() throws Appl

List<ApplicationAuthenticator> reqPathAuthenticators = new ArrayList<ApplicationAuthenticator>();

for (ApplicationAuthenticator authenticator : FrameworkServiceComponent.getAuthenticators()) {
for (ApplicationAuthenticator authenticator :
ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators()) {

if (authenticator instanceof RequestPathApplicationAuthenticator) {
reqPathAuthenticators.add(authenticator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.wso2.carbon.identity.application.authentication.framework.cache.AuthenticationResultCacheEntry;
import org.wso2.carbon.identity.application.authentication.framework.exception.auth.service.AuthServiceClientException;
import org.wso2.carbon.identity.application.authentication.framework.exception.auth.service.AuthServiceException;
import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticationResult;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatorData;
import org.wso2.carbon.identity.application.authentication.framework.model.auth.service.AuthServiceErrorInfo;
Expand Down Expand Up @@ -140,7 +141,7 @@ private void handleIntermediateAuthResponse(AuthServiceRequestWrapper request, A
List<AuthenticatorData> authenticatorDataList;
if (isMultiOptionsResponse) {
responseData.setAuthenticatorSelectionRequired(true);
authenticatorDataList = getAuthenticatorBasicData(response.getAuthenticators(),
authenticatorDataList = getAuthenticatorBasicData(request, response.getAuthenticators(),
request.getAuthInitiationData());
} else {
authenticatorDataList = request.getAuthInitiationData();
Expand Down Expand Up @@ -274,9 +275,8 @@ private String getErrorMessage(AuthServiceResponseWrapper response) throws AuthS
return queryParams.get(AuthServiceConstants.AUTH_FAILURE_MSG_PARAM);
}

private List<AuthenticatorData> getAuthenticatorBasicData(String authenticatorList,
List<AuthenticatorData> authInitiationData)
throws AuthServiceException {
private List<AuthenticatorData> getAuthenticatorBasicData(AuthServiceRequestWrapper request,
String authenticatorList, List<AuthenticatorData> authInitiationData) throws AuthServiceException {

List<AuthenticatorData> authenticatorDataList = new ArrayList<>();
String[] authenticatorAndIdpsArr = StringUtils.split(authenticatorList,
Expand All @@ -293,7 +293,8 @@ private List<AuthenticatorData> getAuthenticatorBasicData(String authenticatorLi
continue;
}

ApplicationAuthenticator authenticator = FrameworkUtils.getAppAuthenticatorByName(name);
ApplicationAuthenticator authenticator = ApplicationAuthenticatorManager.getInstance()
.getAppAuthenticatorByName(name, getTenantDomain((HttpServletRequest) request.getRequest()));
if (authenticator == null) {
throw new AuthServiceException(AuthServiceConstants.ErrorMessage.ERROR_AUTHENTICATOR_NOT_FOUND.code(),
String.format(AuthServiceConstants.ErrorMessage.ERROR_AUTHENTICATOR_NOT_FOUND.description(),
Expand Down Expand Up @@ -413,7 +414,7 @@ private void validateRequest(AuthServiceRequest authServiceRequest) throws AuthS
}

// Validate all configured authenticators support API based authentication.
Set<ApplicationAuthenticator> authenticators = getConfiguredAuthenticators(serviceProvider);
Set<ApplicationAuthenticator> authenticators = getConfiguredAuthenticators(serviceProvider, tenantDomain);
for (ApplicationAuthenticator authenticator : authenticators) {
if (!authenticator.isAPIBasedAuthenticationSupported()) {
throw new AuthServiceClientException(
Expand All @@ -425,7 +426,8 @@ private void validateRequest(AuthServiceRequest authServiceRequest) throws AuthS

}

private Set<ApplicationAuthenticator> getConfiguredAuthenticators(ServiceProvider serviceProvider) {
private Set<ApplicationAuthenticator> getConfiguredAuthenticators(ServiceProvider serviceProvider,
String tenantDomain) {

LocalAndOutboundAuthenticationConfig authenticationConfig = serviceProvider
.getLocalAndOutBoundAuthenticationConfig();
Expand All @@ -435,40 +437,42 @@ private Set<ApplicationAuthenticator> getConfiguredAuthenticators(ServiceProvide

Set<ApplicationAuthenticator> authenticators = new HashSet<>();
for (AuthenticationStep authenticationStep : authenticationConfig.getAuthenticationSteps()) {
processLocalAuthenticators(authenticationStep, authenticators);
processFederatedAuthenticators(authenticationStep, authenticators);
processLocalAuthenticators(authenticationStep, authenticators, tenantDomain);
processFederatedAuthenticators(authenticationStep, authenticators, tenantDomain);
}

return authenticators;
}

private void processLocalAuthenticators(AuthenticationStep authenticationStep,
Set<ApplicationAuthenticator> authenticators) {
Set<ApplicationAuthenticator> authenticators, String tenantDomain) {

if (authenticationStep.getLocalAuthenticatorConfigs() != null) {
for (LocalAuthenticatorConfig localAuthenticatorConfig :
authenticationStep.getLocalAuthenticatorConfigs()) {
addAuthenticator(authenticators, localAuthenticatorConfig.getName());
addAuthenticator(authenticators, localAuthenticatorConfig.getName(), tenantDomain);
}
}
}

private void processFederatedAuthenticators(AuthenticationStep authenticationStep,
Set<ApplicationAuthenticator> authenticators) {
Set<ApplicationAuthenticator> authenticators, String tenantDomain) {

if (authenticationStep.getFederatedIdentityProviders() != null) {
for (IdentityProvider federatedIdP : authenticationStep.getFederatedIdentityProviders()) {
FederatedAuthenticatorConfig fedAuthenticatorConfig = federatedIdP.getDefaultAuthenticatorConfig();
if (fedAuthenticatorConfig != null) {
addAuthenticator(authenticators, fedAuthenticatorConfig.getName());
addAuthenticator(authenticators, fedAuthenticatorConfig.getName(), tenantDomain);
}
}
}
}

private void addAuthenticator(Set<ApplicationAuthenticator> authenticators, String authenticatorName) {
private void addAuthenticator(Set<ApplicationAuthenticator> authenticators, String authenticatorName,
String tenantDomain) {

ApplicationAuthenticator authenticator = FrameworkUtils.getAppAuthenticatorByName(authenticatorName);
ApplicationAuthenticator authenticator = ApplicationAuthenticatorManager.getInstance()
.getAppAuthenticatorByName(authenticatorName, tenantDomain);
if (authenticator != null) {
authenticators.add(authenticator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.wso2.carbon.identity.application.authentication.framework.exception.session.storage.SessionDataStorageOptimizationException;
import org.wso2.carbon.identity.application.authentication.framework.exception.session.storage.SessionDataStorageOptimizationServerException;
import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils;
import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException;
Expand Down Expand Up @@ -175,8 +175,8 @@ private void loadAuthenticatorConfig(AuthenticationContext context)
StepConfig stepConfig = entry.getValue();
for (AuthenticatorConfig authenticatorConfig : stepConfig.getAuthenticatorList()) {
if (authenticatorConfig.getApplicationAuthenticator() == null) {
authenticatorConfig.setApplicationAuthenticator(FrameworkUtils.
getAppAuthenticatorByName(authenticatorConfig.getName()));
authenticatorConfig.setApplicationAuthenticator(ApplicationAuthenticatorManager.getInstance()
.getAppAuthenticatorByName(authenticatorConfig.getName(), context.getTenantDomain()));
}
if (authenticatorConfig.getIdps() == null && authenticatorConfig.getIdpNames() == null) {
authenticatorConfig.setIdPs(Collections.emptyMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.wso2.carbon.identity.application.authentication.framework.config.model.ExternalIdPConfig;
import org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig;
import org.wso2.carbon.identity.application.authentication.framework.config.model.StepConfig;
import org.wso2.carbon.identity.application.authentication.framework.internal.core.ApplicationAuthenticatorManager;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;
import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkUtils;
import org.wso2.carbon.identity.application.common.model.IdentityProvider;
import org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants;
import org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil;
Expand Down Expand Up @@ -1047,7 +1047,8 @@ private AuthenticatorConfig processAuthenticatorConfigElement(OMElement authenti
}

AuthenticatorConfig authenticatorConfig = new AuthenticatorConfig(authenticatorName, enabled, parameterMap);
authenticatorConfig.setApplicationAuthenticator(FrameworkUtils.getAppAuthenticatorByName(authenticatorName));
authenticatorConfig.setApplicationAuthenticator(ApplicationAuthenticatorManager.getInstance()
.getSystemDefinedAuthenticatorByName(authenticatorName));

return authenticatorConfig;
}
Expand Down
Loading

0 comments on commit 3935c78

Please sign in to comment.