Skip to content

Commit

Permalink
Add service layer support to manage the user defined local authentica…
Browse files Browse the repository at this point in the history
…tors
  • Loading branch information
Thisara-Welmilla committed Dec 1, 2024
1 parent 20e9f48 commit 2450e90
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages;
import org.wso2.carbon.identity.application.common.dao.impl.AuthenticatorManagementDAOImpl;
import org.wso2.carbon.identity.application.common.dao.impl.CacheBackedAuthenticatorMgtDAO;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtClientException;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
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.AuthenticatorMgtExceptionBuilder.AuthenticatorMgtError;
import org.wso2.carbon.identity.application.common.util.UserDefinedLocalAuthenticatorValidator;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;

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;
import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.*;

/**
* Application authenticator service.
Expand Down Expand Up @@ -150,8 +149,8 @@ public void addLocalAuthenticator(LocalAuthenticatorConfig authenticator) {

if (authenticator != null) {
if (authenticator.getDefinedByType() != DefinedByType.SYSTEM) {
throw new AuthenticatorMgtServerRuntimeException(
ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED.getMessage());
throw buildRuntimeServerException(
AuthenticatorMgtError.ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED, null);
}
localAuthenticators.add(authenticator);
}
Expand Down Expand Up @@ -201,9 +200,8 @@ public UserDefinedLocalAuthenticatorConfig addUserDefinedLocalAuthenticator(

LocalAuthenticatorConfig config = getLocalAuthenticatorByName(authenticatorConfig.getName(), tenantDomain);
if (config != null) {
ErrorMessages error = ErrorMessages.ERROR_AUTHENTICATOR_ALREADY_EXIST;
throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription(), authenticatorConfig.getName()));
throw buildClientException(AuthenticatorMgtError.ERROR_AUTHENTICATOR_ALREADY_EXIST,
authenticatorConfig.getName());
}
authenticatorValidator.validateAuthenticatorName(authenticatorConfig.getName());
authenticatorValidator.validateForBlank("Display name", authenticatorConfig.getDisplayName());
Expand Down Expand Up @@ -281,9 +279,7 @@ private UserDefinedLocalAuthenticatorConfig resolveExistingAuthenticator(String
getUserDefinedLocalAuthenticator(authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain));

if (existingAuthenticatorConfig == null) {
ErrorMessages error = ErrorMessages.ERROR_NOT_FOUND_AUTHENTICATOR;
throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription(), authenticatorName));
throw buildClientException(AuthenticatorMgtError.ERROR_NOT_FOUND_AUTHENTICATOR, authenticatorName);
}

return existingAuthenticatorConfig;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AuthenticatorMgtServerRuntimeException extends RuntimeException {
private final String errorCode;
private final String description;

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

super(message);
this.errorCode = errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
package org.wso2.carbon.identity.application.common.util;

import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtClientException;
import org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.AuthenticatorMgtError;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;

import java.util.regex.Pattern;

import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildClientException;

/**
* User Defined Local Authenticator Validator class.
*/
Expand All @@ -43,9 +45,7 @@ public class UserDefinedLocalAuthenticatorValidator {
public void validateForBlank(String fieldName, String fieldValue) throws AuthenticatorMgtClientException {

if (StringUtils.isBlank(fieldValue)) {
ErrorMessages error = ErrorMessages.ERROR_BLANK_FIELD_VALUE;
throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription(), fieldName));
throw buildClientException(AuthenticatorMgtError.ERROR_BLANK_FIELD_VALUE, fieldName);
}
}

Expand All @@ -59,9 +59,8 @@ public void validateAuthenticatorName(String name) throws AuthenticatorMgtClient

boolean isValidName = authenticatorNameRegexPattern.matcher(name).matches();
if (!isValidName) {
ErrorMessages error = ErrorMessages.ERROR_INVALID_AUTHENTICATOR_NAME;
throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription(), name, AUTHENTICATOR_NAME_REGEX));
throw buildClientException(AuthenticatorMgtError.ERROR_INVALID_AUTHENTICATOR_NAME,
name, AUTHENTICATOR_NAME_REGEX);
}
}

Expand All @@ -75,8 +74,7 @@ public void validateDefinedByType(DefinedByType definedByType)
throws AuthenticatorMgtClientException {

if (definedByType != DefinedByType.USER) {
ErrorMessages error = ErrorMessages.ERROR_OP_ON_SYSTEM_AUTHENTICATOR;
throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), error.getDescription());
throw buildClientException(AuthenticatorMgtError.ERROR_OP_ON_SYSTEM_AUTHENTICATOR);
}
}
}

0 comments on commit 2450e90

Please sign in to comment.