Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AuthenticatorAdapterService to get authentiactor instance for user defined authenticators. #6319

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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;
Expand All @@ -33,6 +33,7 @@ public class ApplicationAuthenticationService {

private static final Log log = LogFactory.getLog(ApplicationAuthenticationService.class);

@Deprecated
public ApplicationAuthenticator getAuthenticator(String name) throws ApplicationAuthenticationException {

if (name == null) {
Expand All @@ -43,7 +44,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 @@ -53,15 +55,47 @@ public ApplicationAuthenticator getAuthenticator(String name) throws Application
return appAuthenticator;
}

@Deprecated
public List<ApplicationAuthenticator> getAllAuthenticators() throws ApplicationAuthenticationException {
return FrameworkServiceComponent.getAuthenticators();

return ApplicationAuthenticatorManager.getInstance().getSystemDefinedAuthenticators();
}

@Deprecated
public ApplicationAuthenticator getAllAuthenticator(
String name, String tenantDomain) throws ApplicationAuthenticationException {

if (name == null) {
String errMsg = "Authenticator name cannot be null";
log.error(errMsg);
throw new ApplicationAuthenticationException(errMsg);
}

ApplicationAuthenticator appAuthenticator = null;

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

if (authenticator.getName().equals(name)) {
appAuthenticator = authenticator;
}
}

return appAuthenticator;
}

@Deprecated
public List<ApplicationAuthenticator> getAllSystemAuthenticators() throws ApplicationAuthenticationException {

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 +109,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 +124,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
@@ -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 @@ -32,13 +32,14 @@
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;
import org.wso2.carbon.identity.application.common.model.IdentityProvider;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.mgt.ApplicationManagementServiceImpl;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants;
import org.wso2.carbon.idp.mgt.IdentityProviderManagementClientException;
import org.wso2.carbon.idp.mgt.IdentityProviderManagementException;
import org.wso2.carbon.idp.mgt.IdentityProviderManagementServerException;
Expand Down Expand Up @@ -140,7 +141,10 @@ private void optimizeAuthenticatorConfig(AuthenticationContext context)
List<AuthenticatorConfig> authenticatorList = stepConfig.getAuthenticatorList();
for (AuthenticatorConfig authenticatorConfig : authenticatorList) {
authenticatorConfig.setIdPResourceIds(Collections.emptyList());
authenticatorConfig.setApplicationAuthenticator(null);
if (AuthenticatorPropertyConstants.DefinedByType.SYSTEM.equals(
authenticatorConfig.getApplicationAuthenticator().getDefinedByType())) {
authenticatorConfig.setApplicationAuthenticator(null);
}
List<String> idPResourceId = new ArrayList<>();
if (authenticatorConfig.getIdps() != null) {
for (Map.Entry<String, IdentityProvider> entry : authenticatorConfig.getIdps().entrySet()) {
Expand Down Expand Up @@ -171,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 @@ -901,7 +901,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
Loading