Skip to content

Commit

Permalink
Add definedBy type property.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Oct 4, 2024
1 parent 64eea15 commit 07cc31a
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import org.apache.axiom.om.OMElement;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.identity.base.AuthenticatorPropertiesConstant.DefinedByType;

import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -46,6 +49,7 @@
public class FederatedAuthenticatorConfig implements Serializable {

private static final long serialVersionUID = -2361107623257323257L;
private static final Logger LOG = LoggerFactory.getLogger(LocalAuthenticatorConfig.class);

@XmlElement(name = "Name")
protected String name;
Expand All @@ -63,6 +67,9 @@ public class FederatedAuthenticatorConfig implements Serializable {
@XmlElement(name = "Tags")
protected String[] tags;

@XmlElement(name = "DefinedBy")
protected DefinedByType definedByType;

public static FederatedAuthenticatorConfig build(OMElement federatedAuthenticatorConfigOM) {

if (federatedAuthenticatorConfigOM == null) {
Expand Down Expand Up @@ -101,9 +108,17 @@ public static FederatedAuthenticatorConfig build(OMElement federatedAuthenticato
Property[] propertiesArr = propertiesArrList.toArray(new Property[propertiesArrList.size()]);
federatedAuthenticatorConfig.setProperties(propertiesArr);
}
} else if ("DefinedBy".equals(elementName)) {
federatedAuthenticatorConfig.setDefinedByType(DefinedByType.valueOf(element.getText()));
}
}

if (federatedAuthenticatorConfig.getDefinedByType() == null) {
federatedAuthenticatorConfig.setDefinedByType(DefinedByType.SYSTEM);
LOG.debug("The defined by type is not set for the {}. Hence setting default SYSTEM value.",
federatedAuthenticatorConfig.getName());
}

return federatedAuthenticatorConfig;
}

Expand Down Expand Up @@ -230,4 +245,24 @@ public void setTags(String[] tagList) {

tags = tagList;
}

/**
* Get the defined by type of the federated authenticator config.
*
* @return DefinedByType
*/
public DefinedByType getDefinedByType() {

return definedByType;
}

/**
* Set the defined by type of the federated authenticator config.
*
* @param type The defined by type of the federated authenticator config.
*/
public void setDefinedByType(DefinedByType type) {

definedByType = type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import org.apache.axiom.om.OMElement;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.identity.base.IdentityConstants;
import org.wso2.carbon.identity.base.AuthenticatorPropertiesConstant.DefinedByType;

import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -46,6 +49,7 @@
public class LocalAuthenticatorConfig implements Serializable {

private static final long serialVersionUID = 3363298518257599291L;
private static final Logger LOG = LoggerFactory.getLogger(LocalAuthenticatorConfig.class);

@XmlElement(name = "Name")
protected String name;
Expand All @@ -63,6 +67,9 @@ public class LocalAuthenticatorConfig implements Serializable {
@XmlElement(name = "Tags")
protected String[] tags;

@XmlElement(name = "DefinedBy")
protected DefinedByType definedByType;

/*
* <LocalAuthenticatorConfig> <Name></Name> <DisplayName></DisplayName> <IsEnabled></IsEnabled>
* <Properties></Properties> </LocalAuthenticatorConfig>
Expand Down Expand Up @@ -111,8 +118,17 @@ public static LocalAuthenticatorConfig build(OMElement localAuthenticatorConfigO
Property[] propertiesArr = propertiesArrList.toArray(new Property[0]);
localAuthenticatorConfig.setProperties(propertiesArr);
}
} else if ("DefinedBy".equals(member.getLocalName())) {
localAuthenticatorConfig.setDefinedByType(DefinedByType.valueOf(member.getText()));
}
}

if (localAuthenticatorConfig.getDefinedByType() == null) {
localAuthenticatorConfig.setDefinedByType(DefinedByType.SYSTEM);
LOG.debug("The defined by type is not set for the {}. Hence setting default SYSTEM value.",
localAuthenticatorConfig.getName());
}

return localAuthenticatorConfig;
}

Expand Down Expand Up @@ -224,4 +240,24 @@ public void setTags(String[] tagList) {

tags = tagList;
}

/**
* Get the defined by type of the Local authenticator config.
*
* @return DefinedByType
*/
public DefinedByType getDefinedByType() {

return definedByType;
}

/**
* Set the defined by type of the Local authenticator config.
*
* @param type The defined by type of the local authenticator config.
*/
public void setDefinedByType(DefinedByType type) {

definedByType = type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.apache.axiom.om.OMElement;
import org.apache.commons.collections.CollectionUtils;
import org.wso2.carbon.identity.base.AuthenticatorPropertiesConstant;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -74,6 +75,10 @@ public static RequestPathAuthenticatorConfig build(OMElement requestPathAuthenti
}
}
}

// Since custom request path authenticators are not allowed, the definedBy type will always be set to SYSTEM.
requestPathAuthenticatorConfig.setDefinedByType(AuthenticatorPropertiesConstant.DefinedByType.SYSTEM);

return requestPathAuthenticatorConfig;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private ApplicationConstants() {
public static final String IDP_NAME = "idpName";
public static final String IDP_AUTHENTICATOR_NAME = "authenticatorName";
public static final String IDP_AUTHENTICATOR_DISPLAY_NAME = "authenticatorDisplayName";
public static final String IDP_AUTHENTICATOR_DEFINED_BY_TYPE = "definedByType";
public static final String APPLICATION_DOMAIN = "Application";
// Regex for validating application name.
public static final String APP_NAME_VALIDATING_REGEX = "^[a-zA-Z0-9 ._-]*$";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.wso2.carbon.identity.application.authentication.framework.exception.LogoutFailedException;
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatorData;
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.base.AuthenticatorPropertiesConstant.DefinedByType;

import java.io.Serializable;
import java.util.List;
Expand Down Expand Up @@ -171,4 +172,13 @@ default String getI18nKey() {
return StringUtils.EMPTY;
}

/**
* Get the authenticator type. Default value will be SYSTEM.
*
* @return Authenticator Type.
*/
default DefinedByType getDefinedByType() {

return DefinedByType.SYSTEM;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig;
import org.wso2.carbon.identity.application.mgt.ApplicationManagementService;
import org.wso2.carbon.identity.base.AuthenticatorPropertiesConstant.DefinedByType;
import org.wso2.carbon.identity.claim.metadata.mgt.ClaimMetadataManagementService;
import org.wso2.carbon.identity.configuration.mgt.core.ConfigurationManager;
import org.wso2.carbon.identity.core.handler.HandlerComparator;
Expand Down Expand Up @@ -508,13 +509,15 @@ protected void setAuthenticator(ApplicationAuthenticator authenticator) {
localAuthenticatorConfig.setTags(getTags(authenticator));
AuthenticatorConfig fileBasedConfig = getAuthenticatorConfig(authenticator.getName());
localAuthenticatorConfig.setEnabled(fileBasedConfig.isEnabled());
localAuthenticatorConfig.setDefinedByType(DefinedByType.SYSTEM);
ApplicationAuthenticatorService.getInstance().addLocalAuthenticator(localAuthenticatorConfig);
} else if (authenticator instanceof FederatedApplicationAuthenticator) {
FederatedAuthenticatorConfig federatedAuthenticatorConfig = new FederatedAuthenticatorConfig();
federatedAuthenticatorConfig.setName(authenticator.getName());
federatedAuthenticatorConfig.setProperties(configProperties);
federatedAuthenticatorConfig.setDisplayName(authenticator.getFriendlyName());
federatedAuthenticatorConfig.setTags(getTags(authenticator));
federatedAuthenticatorConfig.setDefinedByType(DefinedByType.SYSTEM);
ApplicationAuthenticatorService.getInstance().addFederatedAuthenticator(federatedAuthenticatorConfig);
} else if (authenticator instanceof RequestPathApplicationAuthenticator) {
RequestPathAuthenticatorConfig reqPathAuthenticatorConfig = new RequestPathAuthenticatorConfig();
Expand All @@ -524,6 +527,7 @@ protected void setAuthenticator(ApplicationAuthenticator authenticator) {
reqPathAuthenticatorConfig.setTags(getTags(authenticator));
AuthenticatorConfig fileBasedConfig = getAuthenticatorConfig(authenticator.getName());
reqPathAuthenticatorConfig.setEnabled(fileBasedConfig.isEnabled());
reqPathAuthenticatorConfig.setDefinedByType(DefinedByType.SYSTEM);
ApplicationAuthenticatorService.getInstance().addRequestPathAuthenticator(reqPathAuthenticatorConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
import org.wso2.carbon.identity.application.common.model.Property;
import org.wso2.carbon.identity.application.common.model.ServiceProvider;
import org.wso2.carbon.identity.application.mgt.ApplicationConstants;
import org.wso2.carbon.identity.base.AuthenticatorPropertiesConstant.DefinedByType;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.base.IdentityRuntimeException;
import org.wso2.carbon.identity.central.log.mgt.utils.LoggerUtils;
Expand Down Expand Up @@ -4190,4 +4191,21 @@ public static boolean isURLRelative(String uriString) throws URISyntaxException

return !new URI(uriString).isAbsolute();
}

/**
* This method return defined by type for the given authenticator name.
*
* @param authenticatorName Name of the authenticator.
* @return The defined by type.
*/
public static DefinedByType getAuthenticatorDefinedByType(String authenticatorName) {

for (ApplicationAuthenticator authenticator: FrameworkServiceComponent.getAuthenticators()) {
if (authenticator.getName().equals(authenticatorName)) {
return authenticator.getDefinedByType();
}
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2024, 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.base;

public class AuthenticatorPropertiesConstant {

/**
* The Defined by Types - SYSTEM: system define authenticator, USER: user defined authentication extension.
*/
public enum DefinedByType {

SYSTEM,
USER
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.wso2.carbon.identity.application.common.model.SubProperty;
import org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants;
import org.wso2.carbon.identity.application.common.util.IdentityApplicationManagementUtil;
import org.wso2.carbon.identity.base.AuthenticatorPropertiesConstant.DefinedByType;
import org.wso2.carbon.identity.base.IdentityConstants;
import org.wso2.carbon.identity.base.IdentityException;
import org.wso2.carbon.identity.core.ServiceURLBuilder;
Expand Down Expand Up @@ -171,6 +172,7 @@ public void addResidentIdP(IdentityProvider identityProvider, String tenantDomai
if (saml2SSOResidentAuthenticatorConfig == null) {
saml2SSOResidentAuthenticatorConfig = new FederatedAuthenticatorConfig();
saml2SSOResidentAuthenticatorConfig.setName(IdentityApplicationConstants.Authenticator.SAML2SSO.NAME);
saml2SSOResidentAuthenticatorConfig.setDefinedByType(DefinedByType.SYSTEM);
}
if (saml2SSOResidentAuthenticatorConfig.getProperties() == null) {
saml2SSOResidentAuthenticatorConfig.setProperties(new Property[0]);
Expand Down Expand Up @@ -255,6 +257,7 @@ public void addResidentIdP(IdentityProvider identityProvider, String tenantDomai
FederatedAuthenticatorConfig oidcAuthenticationConfig = new FederatedAuthenticatorConfig();
oidcAuthenticationConfig.setProperties(new Property[]{oidcProperty});
oidcAuthenticationConfig.setName(IdentityApplicationConstants.Authenticator.OIDC.NAME);
oidcAuthenticationConfig.setDefinedByType(DefinedByType.SYSTEM);

Property passiveStsProperty = new Property();
passiveStsProperty.setName(IdentityApplicationConstants.Authenticator.PassiveSTS.IDENTITY_PROVIDER_ENTITY_ID);
Expand All @@ -263,6 +266,7 @@ public void addResidentIdP(IdentityProvider identityProvider, String tenantDomai
FederatedAuthenticatorConfig passiveStsAuthenticationConfig = new FederatedAuthenticatorConfig();
passiveStsAuthenticationConfig.setProperties(new Property[]{passiveStsProperty});
passiveStsAuthenticationConfig.setName(IdentityApplicationConstants.Authenticator.PassiveSTS.NAME);
passiveStsAuthenticationConfig.setDefinedByType(DefinedByType.SYSTEM);

FederatedAuthenticatorConfig[] federatedAuthenticatorConfigs = {saml2SSOResidentAuthenticatorConfig,
passiveStsAuthenticationConfig, oidcAuthenticationConfig};
Expand Down

0 comments on commit 07cc31a

Please sign in to comment.