Skip to content

Commit

Permalink
Add AuthenticatorAdapterService,
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Jan 22, 2025
1 parent 26eb2d9 commit fc029ea
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.application.authentication.framework;

import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;

/**
* Authenticator Adapter Service which responsible for creating ApplicationAuthenticator for the provided user defined
* authenticator configs.
*/
public interface AuthenticatorAdapterService {

/**
* Get the ApplicationAuthenticator for the given user defined federated authenticator config.
*
* @param config Federated Authenticator Config.
* @return FederatedApplicationAuthenticator instance.
*/
FederatedApplicationAuthenticator getFederatedAuthenticatorAdapter(FederatedAuthenticatorConfig config);

/**
* Get the ApplicationAuthenticator for the given user defined local authenticator config.
*
* @param config Local Authenticator Config.
* @return LocalApplicationAuthenticator instance.
*/
LocalApplicationAuthenticator getLocalAuthenticatorAdapter(LocalAuthenticatorConfig config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.carbon.identity.application.authentication.framework.config.loader;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator;
Expand All @@ -34,6 +35,8 @@
import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder;
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.ApplicationAuthenticatorService;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
import org.wso2.carbon.identity.application.common.model.AuthenticationStep;
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.IdentityProvider;
Expand All @@ -42,6 +45,8 @@
import org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.common.model.script.AuthenticationScriptConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedFederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.mgt.ApplicationConstants;
import org.wso2.carbon.idp.mgt.IdentityProviderManagementException;
import org.wso2.carbon.idp.mgt.IdentityProviderManager;
Expand Down Expand Up @@ -189,7 +194,7 @@ public SequenceConfig getSequence(ServiceProvider serviceProvider, String tenant
loadFederatedAuthenticators(authenticationStep, stepConfig, tenantDomain);

// loading local authenticators
loadLocalAuthenticators(authenticationStep, stepConfig);
loadLocalAuthenticators(authenticationStep, stepConfig, tenantDomain);

sequenceConfig.getStepMap().put(stepOrder, stepConfig);
}
Expand Down Expand Up @@ -264,12 +269,13 @@ protected void loadFederatedAuthenticators(AuthenticationStep authenticationStep

String actualAuthenticatorName = federatedAuthenticator.getName();
// assign it to the step
loadStepAuthenticator(stepConfig, federatedIDP, actualAuthenticatorName);
loadStepAuthenticator(stepConfig, federatedIDP, actualAuthenticatorName, tenantDomain);
}
}
}

protected void loadLocalAuthenticators(AuthenticationStep authenticationStep, StepConfig stepConfig) {
protected void loadLocalAuthenticators(AuthenticationStep authenticationStep, StepConfig stepConfig,
String tenantDomain) throws FrameworkException {

LocalAuthenticatorConfig[] localAuthenticators = authenticationStep.getLocalAuthenticatorConfigs();
if (localAuthenticators != null) {
Expand All @@ -278,12 +284,13 @@ protected void loadLocalAuthenticators(AuthenticationStep authenticationStep, St
// assign it to the step
for (LocalAuthenticatorConfig localAuthenticator : localAuthenticators) {
String actualAuthenticatorName = localAuthenticator.getName();
loadStepAuthenticator(stepConfig, localIdp, actualAuthenticatorName);
loadStepAuthenticator(stepConfig, localIdp, actualAuthenticatorName, tenantDomain);
}
}
}

private void loadStepAuthenticator(StepConfig stepConfig, IdentityProvider idp, String authenticatorName) {
private void loadStepAuthenticator(StepConfig stepConfig, IdentityProvider idp, String authenticatorName,
String tenantDomain) throws FrameworkException {

AuthenticatorConfig authenticatorConfig = null;

Expand All @@ -300,14 +307,18 @@ private void loadStepAuthenticator(StepConfig stepConfig, IdentityProvider idp,
authenticatorConfig = new AuthenticatorConfig();
authenticatorConfig.setName(authenticatorName);

ApplicationAuthenticator appAuthenticatorForConfig = null;
for (ApplicationAuthenticator appAuthenticator : FrameworkServiceComponent.getAuthenticators()) {

if (authenticatorName.equalsIgnoreCase(appAuthenticator.getName())) {
authenticatorConfig.setApplicationAuthenticator(appAuthenticator);
appAuthenticatorForConfig = appAuthenticator;
break;
}
}

if (appAuthenticatorForConfig == null) {
appAuthenticatorForConfig = resolveUserDefinedAuthenticator(authenticatorName, idp, tenantDomain);
}
authenticatorConfig.setApplicationAuthenticator(appAuthenticatorForConfig);
stepConfig.getAuthenticatorList().add(authenticatorConfig);
}

Expand All @@ -321,4 +332,35 @@ private void loadStepAuthenticator(StepConfig stepConfig, IdentityProvider idp,
stepConfig.setMultiOption(true);
}
}

private ApplicationAuthenticator resolveUserDefinedAuthenticator(
String authenticatorName, IdentityProvider idp, String tenantDomain) throws FrameworkException {

try {
if (StringUtils.equals(idp.getIdentityProviderName(), FrameworkConstants.LOCAL_IDP_NAME)) {

UserDefinedLocalAuthenticatorConfig config = ApplicationAuthenticatorService.getInstance()
.getUserDefinedLocalAuthenticator(authenticatorName, tenantDomain);
if (config != null) {
return FrameworkServiceDataHolder.getInstance().getAuthenticatorAdapterService()
.getLocalAuthenticatorAdapter(config);
}
} else {
UserDefinedFederatedAuthenticatorConfig config = (UserDefinedFederatedAuthenticatorConfig)
IdentityProviderManager.getInstance().getIdPByName(idp.getIdentityProviderName(), tenantDomain)
.getDefaultAuthenticatorConfig();

if (config != null) {
return FrameworkServiceDataHolder.getInstance().getAuthenticatorAdapterService()
.getFederatedAuthenticatorAdapter(config);
}
}

throw new FrameworkException(String.format(
"The authenticator %s not found for the user defined authenticator: %s", authenticatorName));
} catch (AuthenticatorMgtException | IdentityProviderManagementException e) {
throw new FrameworkException(String.format(
"An error occurred when retrieving user defined authenticator: %s", authenticatorName), e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationDataPublisher;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationFlowHandler;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationMethodNameTranslator;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticatorAdapterService;
import org.wso2.carbon.identity.application.authentication.framework.FederatedApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry;
import org.wso2.carbon.identity.application.authentication.framework.LocalApplicationAuthenticator;
Expand Down Expand Up @@ -1109,4 +1110,24 @@ protected void unsetRoleManagementServiceV2(RoleManagementService roleManagement
FrameworkServiceDataHolder.getInstance().setRoleManagementServiceV2(null);
log.debug("RoleManagementServiceV2 unset in FrameworkServiceComponent bundle.");
}

/* TODO: The cardinality is set to OPTIONAL until AuthenticatorAdapterService implements. Update it to MANDATORY,
one adapter service implementation done. */
@Reference(
name = "org.wso2.carbon.identity.application.authentication.framework.AuthenticatorAdapterService",
service = org.wso2.carbon.identity.application.authentication.framework.AuthenticatorAdapterService.class,
cardinality = ReferenceCardinality.OPTIONAL,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetAuthenticatorAdapterService")
protected void setAuthenticatorAdapterService(AuthenticatorAdapterService adapterService) {

FrameworkServiceDataHolder.getInstance().setAuthenticatorAdapterService(adapterService);
log.debug("AuthenticatorAdapterService set in FrameworkServiceComponent bundle.");
}

protected void unsetAuthenticatorAdapterService(AuthenticatorAdapterService adapterService) {

FrameworkServiceDataHolder.getInstance().setAuthenticatorAdapterService(adapterService);
log.debug("AuthenticatorAdapterService unset in FrameworkServiceComponent bundle.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.wso2.carbon.identity.application.authentication.framework.ApplicationAuthenticator;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationDataPublisher;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticationMethodNameTranslator;
import org.wso2.carbon.identity.application.authentication.framework.AuthenticatorAdapterService;
import org.wso2.carbon.identity.application.authentication.framework.JsFunctionRegistry;
import org.wso2.carbon.identity.application.authentication.framework.ServerSessionManagementService;
import org.wso2.carbon.identity.application.authentication.framework.config.loader.SequenceLoader;
Expand Down Expand Up @@ -102,6 +103,7 @@ public class FrameworkServiceDataHolder {
private String requireCode = "";
private String secretsCode = StringUtils.EMPTY;
private boolean userSessionMappingEnabled;
private AuthenticatorAdapterService authenticatorAdapterService;

/*
This property restricts unnecessary user local search during federated authentication flow for authentication
Expand Down Expand Up @@ -827,4 +829,25 @@ public void setRoleManagementServiceV2(RoleManagementService roleManagementServi

this.roleManagementServiceV2 = roleManagementServiceV2;
}

/**
* Set {@link AuthenticatorAdapterService}.
*
* @param authenticatorAdapterService Instance of {@link AuthenticatorAdapterService}.
*/
public void setAuthenticatorAdapterService(AuthenticatorAdapterService authenticatorAdapterService) {

this.authenticatorAdapterService = authenticatorAdapterService;
}

/**
* Get {@link AuthenticatorAdapterService}.
*
* @return Instance of {@link AuthenticatorAdapterService}.
*/
public AuthenticatorAdapterService getAuthenticatorAdapterService() {

return authenticatorAdapterService;
}

}

0 comments on commit fc029ea

Please sign in to comment.