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 29, 2024
1 parent 4a85b28 commit baefb68
Show file tree
Hide file tree
Showing 16 changed files with 483 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
org.wso2.carbon.identity.core.util; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.core.cache; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.central.log.mgt.*; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.action.management.*; version="${carbon.identity.package.import.version.range}",

com.fasterxml.jackson.annotation; version="${com.fasterxml.jackson.annotation.version.range}"
</Import-Package>
Expand Down
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 All @@ -36,6 +37,8 @@
import java.util.ArrayList;
import java.util.List;

import static org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages.ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED;

/**
* Application authenticator service.
*/
Expand Down Expand Up @@ -63,28 +66,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 +93,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 +113,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 @@ -153,7 +148,12 @@ public RequestPathAuthenticatorConfig getRequestPathAuthenticatorByName(String n
}

public void addLocalAuthenticator(LocalAuthenticatorConfig authenticator) {

if (authenticator != null) {
if (authenticator.getDefinedByType() != DefinedByType.SYSTEM) {
throw new AuthenticatorMgtServerRuntimeException(
ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED.getMessage());
}
localAuthenticators.add(authenticator);
}
}
Expand Down Expand Up @@ -194,12 +194,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 +220,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 +241,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 +259,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 +276,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,11 @@ 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."),
ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED("65006", "Error while adding local authenticator.",
"Only system defined authenticators are allowed to add via this method.");

private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ public AuthenticatorMgtException(String message) {
super(message);
}

public AuthenticatorMgtException(String message, String errorCode) {

super(message);
this.errorCode = errorCode;
}

public AuthenticatorMgtException(String message, String errorCode, Throwable cause) {

super(message, cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,12 @@
*/
public class AuthenticatorMgtServerException extends AuthenticatorMgtException {

public AuthenticatorMgtServerException(String message, String errorCode) {

super(message, errorCode);
}

public AuthenticatorMgtServerException(String message, String description, String errorCode) {

super(message, description, errorCode);
}

public AuthenticatorMgtServerException(String message, String errorCode, Throwable cause) {
public AuthenticatorMgtServerException(String errorCode, String message, Throwable cause) {

super(message, errorCode, cause);
}

public AuthenticatorMgtServerException(String message, String description, String errorCode,
public AuthenticatorMgtServerException(String errorCode, String message, String description,
Throwable cause) {

super(message, description, errorCode, cause);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.application.common.exception;

/**
* Authenticator configuration management server runtime exception.
*/
public class AuthenticatorMgtServerRuntimeException extends RuntimeException {

private final String errorCode;
private final String description;

public AuthenticatorMgtServerRuntimeException(String message, String description, String errorCode) {

super(message);
this.errorCode = errorCode;
this.description = description;
}

public String getErrorCode() {

return this.errorCode;
}

public String getDescription() {

return this.description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.wso2.carbon.identity.application.common.internal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
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;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;

/**
* 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);

@Activate
protected void activate(ComponentContext context) {

try {
BundleContext bundleCtx = context.getBundleContext();
bundleCtx.registerService(ApplicationAuthenticatorService.class.getName(),
ApplicationAuthenticatorService.getInstance(),
null);
LOG.debug("Application Authenticator Service is activated.");
} catch (Throwable e) {
LOG.error("Error while initializing Application Authenticator Service component.", e);
}
}

@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);
}
}
}
Loading

0 comments on commit baefb68

Please sign in to comment.