Skip to content

Commit

Permalink
Add service layer support for the custom local auth extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Nov 22, 2024
1 parent 4a85b28 commit c096f8d
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
import org.wso2.carbon.identity.application.common.util.UserDefinedLocalAuthenticatorValidator;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.AuthenticationType;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;
Expand Down Expand Up @@ -63,28 +64,19 @@ public static ApplicationAuthenticatorService getInstance() {
return instance;
}

/**
* This returns only SYSTEM defined local authenticators.
*
* @return Retrieved LocalAuthenticatorConfig.
*/
@Deprecated
public List<LocalAuthenticatorConfig> getLocalAuthenticators() {
return this.localAuthenticators;
}

/**
* This returns both SYSTEM and USER defined local authenticators.
* This returns User defined local authenticators.
*
* @return Retrieved LocalAuthenticatorConfig.
*/
public List<LocalAuthenticatorConfig> getLocalAuthenticators(String tenantDomain)
public List<UserDefinedLocalAuthenticatorConfig> getUserDefinedLocalAuthenticators(String tenantDomain)
throws AuthenticatorMgtException {

List<LocalAuthenticatorConfig> userDefinedAuthenticators =
CACHE_BACKED_DAO.getAllUserDefinedLocalAuthenticator(IdentityTenantUtil.getTenantId(tenantDomain));
userDefinedAuthenticators.addAll(localAuthenticators);
return userDefinedAuthenticators;
return CACHE_BACKED_DAO.getAllUserDefinedLocalAuthenticator(IdentityTenantUtil.getTenantId(tenantDomain));
}

public List<FederatedAuthenticatorConfig> getFederatedAuthenticators() {
Expand All @@ -99,8 +91,10 @@ public List<RequestPathAuthenticatorConfig> getRequestPathAuthenticators() {
* This returns only SYSTEM defined local authenticator by name.
*
* @param name The name of the Local Application Authenticator configuration.
*
* @return Retrieved LocalAuthenticatorConfig.
*
* @deprecated It is recommended to use {@link #getLocalAuthenticatorByName(String, String)},
* which supports retrieving both USER and SYSTEM defined Local Application Authenticator configuration by name.
*/
@Deprecated
public LocalAuthenticatorConfig getLocalAuthenticatorByName(String name) {
Expand All @@ -117,7 +111,6 @@ public LocalAuthenticatorConfig getLocalAuthenticatorByName(String name) {
*
* @param name The name of the Local Application Authenticator configuration.
* @param tenantDomain Tenant domain.
*
* @return Retrieved LocalAuthenticatorConfig.
* @throws AuthenticatorMgtException If an error occurs while retrieving the authenticator configuration by name.
*/
Expand Down Expand Up @@ -194,12 +187,12 @@ public void removeRequestPathAuthenticator(RequestPathAuthenticatorConfig authen
* @param authenticatorConfig The Local Application Authenticator configuration.
* @param type Authentication type of the authenticator.
* @param tenantDomain Tenant domain.
*
* @return Updated LocalAuthenticatorConfig.
* @throws AuthenticatorMgtException If an error occurs while creating the authenticator configuration.
*/
public LocalAuthenticatorConfig createUserDefinedLocalAuthenticator(LocalAuthenticatorConfig authenticatorConfig,
AuthenticationType type, String tenantDomain) throws AuthenticatorMgtException {
public UserDefinedLocalAuthenticatorConfig addUserDefinedLocalAuthenticator(
UserDefinedLocalAuthenticatorConfig authenticatorConfig, AuthenticationType type, String tenantDomain)
throws AuthenticatorMgtException {

LocalAuthenticatorConfig config = getLocalAuthenticatorByName(authenticatorConfig.getName(), tenantDomain);
if (config != null) {
Expand All @@ -220,14 +213,14 @@ public LocalAuthenticatorConfig createUserDefinedLocalAuthenticator(LocalAuthent
*
* @param authenticatorConfig The Local Application Authenticator configuration.
* @param tenantDomain Tenant Domain.
*
* @return Updated LocalAuthenticatorConfig.
* @return Updated UserDefinedLocalAuthenticatorConfig.
* @throws AuthenticatorMgtException If an error occurs while updating the authenticator configuration.
*/
public LocalAuthenticatorConfig updateUserDefinedLocalAuthenticator(LocalAuthenticatorConfig authenticatorConfig,
String tenantDomain) throws AuthenticatorMgtException {
public UserDefinedLocalAuthenticatorConfig updateUserDefinedLocalAuthenticator(
UserDefinedLocalAuthenticatorConfig authenticatorConfig, String tenantDomain)
throws AuthenticatorMgtException {

LocalAuthenticatorConfig existingConfig = resolveExistingAuthenticator(
UserDefinedLocalAuthenticatorConfig existingConfig = resolveExistingAuthenticator(
authenticatorConfig.getName(), tenantDomain);
authenticatorValidator.validateDefinedByType(existingConfig.getDefinedByType());
authenticatorValidator.validateForBlank("Display name", authenticatorConfig.getDisplayName());
Expand All @@ -241,16 +234,16 @@ public LocalAuthenticatorConfig updateUserDefinedLocalAuthenticator(LocalAuthent
*
* @param authenticatorName Name of Local Application Authenticator configuration to be deleted.
* @param tenantDomain Tenant domain.
*
* @throws AuthenticatorMgtException If an error occurs while deleting the authenticator configuration.
*/
public void deleteUserDefinedLocalAuthenticator(String authenticatorName, String tenantDomain)
throws AuthenticatorMgtException {

LocalAuthenticatorConfig existingConfig = resolveExistingAuthenticator(authenticatorName, tenantDomain);
UserDefinedLocalAuthenticatorConfig existingConfig = resolveExistingAuthenticator(
authenticatorName, tenantDomain);
authenticatorValidator.validateDefinedByType(existingConfig.getDefinedByType());

CACHE_BACKED_DAO.deleteUserDefinedLocalAuthenticator(authenticatorName,
CACHE_BACKED_DAO.deleteUserDefinedLocalAuthenticator(authenticatorName, existingConfig,
IdentityTenantUtil.getTenantId(tenantDomain));
}

Expand All @@ -259,15 +252,14 @@ public void deleteUserDefinedLocalAuthenticator(String authenticatorName, String
*
* @param authenticatorName Name of Local Application Authenticator configuration to be deleted.
* @param tenantDomain Tenant domain.
*
* @return Retrieved LocalAuthenticatorConfig.
* @return Retrieved UserDefinedLocalAuthenticatorConfig.
* @throws AuthenticatorMgtException If an error occurs while retrieving the authenticator configuration.
*/
public LocalAuthenticatorConfig getUserDefinedLocalAuthenticator(String authenticatorName, String tenantDomain)
throws AuthenticatorMgtException {
public UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticator(String authenticatorName,
String tenantDomain) throws AuthenticatorMgtException {

LocalAuthenticatorConfig config = CACHE_BACKED_DAO.getUserDefinedLocalAuthenticator(authenticatorName,
IdentityTenantUtil.getTenantId(tenantDomain));
UserDefinedLocalAuthenticatorConfig config = CACHE_BACKED_DAO.getUserDefinedLocalAuthenticator(
authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain));

if (config != null && !config.getDefinedByType().equals(DefinedByType.USER)) {
return null;
Expand All @@ -277,11 +269,11 @@ public LocalAuthenticatorConfig getUserDefinedLocalAuthenticator(String authenti

}

private LocalAuthenticatorConfig resolveExistingAuthenticator(String authenticatorName, String tenantDomain)
throws AuthenticatorMgtException {
private UserDefinedLocalAuthenticatorConfig resolveExistingAuthenticator(String authenticatorName,
String tenantDomain) throws AuthenticatorMgtException {

LocalAuthenticatorConfig existingAuthenticatorConfig = CACHE_BACKED_DAO.getUserDefinedLocalAuthenticator(
authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain));
UserDefinedLocalAuthenticatorConfig existingAuthenticatorConfig = CACHE_BACKED_DAO.
getUserDefinedLocalAuthenticator(authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain));

if (existingAuthenticatorConfig == null) {
ErrorMessages error = ErrorMessages.ERROR_NOT_FOUND_AUTHENTICATOR;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public enum ErrorMessages {
ERROR_WHILE_RETRIEVING_AUTHENTICATOR_BY_NAME("65003", "Error while retrieving authenticator.",
"Error while retrieving authenticator from the system."),
ERROR_WHILE_DELETING_AUTHENTICATOR("65004", "Error while deleting authenticator.",
"Error while deleting authenticator from the system."),;
"Error while deleting authenticator from the system."),
ERROR_CODE_ENDPOINT_CONFIG_MGT("65005", "Error while managing endpoint configurations.",
"Error while managing endpoint configurations for the user defined local authenticator %s."),;

private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.wso2.carbon.identity.application.common.internal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.identity.action.management.ActionManagementService;

/**
* OSGI service component for the Application Common Service Component.
*/
@Component(
name = "application.common.service.component",
immediate = true
)
public class ApplicationCommonServiceComponent {

private static final Log LOG = LogFactory.getLog(ApplicationCommonServiceComponent.class);

@Reference(
name = "action.management.service",
service = ActionManagementService.class,
cardinality = ReferenceCardinality.MANDATORY,
policy = ReferencePolicy.DYNAMIC,
unbind = "unsetActionManagementService"
)
protected void setActionManagementService(ActionManagementService actionManagementService) {

if (LOG.isDebugEnabled()) {
LOG.debug(
"Registering a reference for ActionManagementService in the ApplicationCommonServiceComponent.");
}
ApplicationCommonServiceDataHolder.getInstance().setActionManagementService(actionManagementService);
}

protected void unsetActionManagementService(ActionManagementService actionManagementService) {

if (LOG.isDebugEnabled()) {
LOG.debug("Unregistering the reference for ActionManagementService in the " +
"ApplicationCommonServiceComponent.");
}
if (ApplicationCommonServiceDataHolder.getInstance().getActionManagementService()
.equals(actionManagementService)) {
ApplicationCommonServiceDataHolder.getInstance().setActionManagementService(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.wso2.carbon.identity.application.common.internal;

import org.wso2.carbon.identity.action.management.ActionManagementService;

/**
* The data holder for the Application Common Service Component.
*/
public class ApplicationCommonServiceDataHolder {

private static final ApplicationCommonServiceDataHolder INSTANCE = new ApplicationCommonServiceDataHolder();

private ActionManagementService actionManagementService;

private ApplicationCommonServiceDataHolder() {

}

public static ApplicationCommonServiceDataHolder getInstance() {

return INSTANCE;
}

public ActionManagementService getActionManagementService() {

return actionManagementService;
}

public void setActionManagementService(ActionManagementService actionManagementService) {

this.actionManagementService = actionManagementService;
}
}

This file was deleted.

Loading

0 comments on commit c096f8d

Please sign in to comment.