Skip to content

Commit

Permalink
Add new VerificationAuthenticatorConfig for verification authenticators
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Oct 22, 2024
1 parent cfd33b1 commit b6248b3
Show file tree
Hide file tree
Showing 17 changed files with 2,687 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* Action management exception.
*/
public class AuthenticatorMgtServerExceptionActionMgtException extends Exception {
public class ActionMgtException extends Exception {

private String errorCode;
private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.central.log.mgt</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.testutil</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
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.base.AuthenticatorPropertyConstants.AuthenticatorType;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;

import java.util.ArrayList;
Expand Down Expand Up @@ -134,52 +136,66 @@ public void removeRequestPathAuthenticator(RequestPathAuthenticatorConfig authen
}

public LocalAuthenticatorConfig createUserDefinedLocalAuthenticator(LocalAuthenticatorConfig authenticatorConfig,
String tenantDomain) throws AuthenticatorMgtException {
AuthenticatorType type, String tenantDomain) throws AuthenticatorMgtException {

LocalAuthenticatorConfig config = getLocalAuthenticatorByName(authenticatorConfig.getName());
if (config != null) {
ErrorMessages error = ErrorMessages.ERROR_AUTHENTICATOR_ALREADY_EXIST;
throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription(), authenticatorConfig.getName()));
}
validateAuthenticatorDefinedByType(authenticatorConfig);

// set definedby type to USER.
LocalAuthenticatorConfig createdConfig = CACHE_BACKED_DAO.addUserDefinedLocalAuthenticator(
authenticatorConfig, IdentityTenantUtil.getTenantId(tenantDomain));
authenticatorConfig, IdentityTenantUtil.getTenantId(tenantDomain), type);

localAuthenticators.add(createdConfig);
return authenticatorConfig;
return createdConfig;
}

public LocalAuthenticatorConfig updateUserDefinedLocalAuthenticator(LocalAuthenticatorConfig authenticatorConfig,
String tenantDomain) throws AuthenticatorMgtException {

LocalAuthenticatorConfig existingAuthenticatorConfig = getLocalAuthenticatorByName(authenticatorConfig.getName());
if (existingAuthenticatorConfig == null) {
ErrorMessages error = ErrorMessages.ERROR_NOT_FOUND_AUTHENTICATOR;
throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription(), authenticatorConfig.getName()));
}
validateAuthenticatorDefinedByType(authenticatorConfig);
LocalAuthenticatorConfig existingAuthenticatorConfig = getExistingAuthenticatorConfig(
authenticatorConfig.getName());

LocalAuthenticatorConfig updatedConfig = CACHE_BACKED_DAO.updateUserDefinedLocalAuthenticator(
existingAuthenticatorConfig, authenticatorConfig, IdentityTenantUtil.getTenantId(tenantDomain));

localAuthenticators.remove(existingAuthenticatorConfig);
localAuthenticators.add(updatedConfig);
return authenticatorConfig;
return updatedConfig;
}

public void deleteUserDefinedLocalAuthenticator(String authenticatorName, String tenantDomain)
throws AuthenticatorMgtException {

LocalAuthenticatorConfig authenticatorConfig = getExistingAuthenticatorConfig(authenticatorName);

CACHE_BACKED_DAO.deleteUserDefinedLocalAuthenticator(authenticatorName,
IdentityTenantUtil.getTenantId(tenantDomain));
localAuthenticators.remove(authenticatorConfig);
}

private LocalAuthenticatorConfig getExistingAuthenticatorConfig(String authenticatorName)
throws AuthenticatorMgtClientException {

LocalAuthenticatorConfig authenticatorConfig = getLocalAuthenticatorByName(authenticatorName);
if (authenticatorConfig == null) {
ErrorMessages error = ErrorMessages.ERROR_NOT_FOUND_AUTHENTICATOR;
throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(),
String.format(error.getDescription(), authenticatorName));
}
validateAuthenticatorDefinedByType(authenticatorConfig);

return authenticatorConfig;

CACHE_BACKED_DAO.deleteUserDefinedLocalAuthenticator(authenticatorName,
IdentityTenantUtil.getTenantId(tenantDomain));
localAuthenticators.remove(authenticatorConfig);
}

private void validateAuthenticatorDefinedByType(LocalAuthenticatorConfig authenticatorConfig)
throws AuthenticatorMgtClientException {

if (authenticatorConfig.getDefinedByType()) {
if (authenticatorConfig.getDefinedByType() == DefinedByType.USER) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@

package org.wso2.carbon.identity.application.common.constant;

/**
* Constants for authenticator configuration management service.
*/
public class AuthenticatorMgtConstants {

public static final String IS_TRUE_VALUE = "1";
public static final String IS_FALSE_VALUE = "0";
public static final String LOCAL_IDP_NAME = "LOCAL";

/**
* Error messages.
*/
Expand All @@ -28,8 +35,10 @@ public enum ErrorMessages {
// Client errors.
ERROR_NOT_FOUND_AUTHENTICATOR("60001", "No Authenticator is found.",
"No authenticator is found by given authenticator name: %s."),
ERROR_OP_ON_SYSTEM_AUTHENTICATOR("60002", "No operations allowed",
ERROR_OP_ON_SYSTEM_AUTHENTICATOR("60002", "No operations allowed on system authenticators.",
"Do not allow to perform any operation on system defined authenticator: %s."),
ERROR_AUTHENTICATOR_ALREADY_EXIST("60003", "There is already an authenticator.",
"There is already an authenticator by the given name: %s."),

// Server errors.
ERROR_WHILE_ADDING_AUTHENTICATOR("65001", "Error while adding authenticator.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

package org.wso2.carbon.identity.application.common.constant;

/**
* SQL constants for authenticator configuration management service.
*/
public class AuthenticatorMgtSQLConstants {

private AuthenticatorMgtSQLConstants() {
Expand All @@ -29,13 +32,19 @@ private AuthenticatorMgtSQLConstants() {
*/
public static class Column {

public static final String IDP_ID = "IDP_ID";
public static final String IDP_ID = "ID";
public static final String IDP_NAME = "IDP_NAME";
public static final String TENANT_ID = "TENANT_ID";
public static final String NAME = "NAME";
public static final String IS_ENABLED = "IS_ENABLED";
public static final String DEFINED_BY = "DEFINED_BY";
public static final String AUTHENTICATOR_TYPE = "AUTHENTICATOR_TYPE";
public static final String DISPLAY_NAME = "DISPLAY_NAME";
public static final String ID = "ID";
public static final String AUTHENTICATOR_ID = "AUTHENTICATOR_ID";
public static final String PROPERTY_KEY = "PROPERTY_KEY";
public static final String PROPERTY_VALUE = "PROPERTY_VALUE";
public static final String IS_SECRET = "IS_SECRET";

private Column() {

Expand All @@ -47,10 +56,26 @@ private Column() {
*/
public static class Query {

public static final String ADD_USER_DEFINED_AUTHENTICATOR= "INSERT INTO IDP_AUTHENTICATOR " +
public static final String ADD_AUTHENTICATOR_SQL = "INSERT INTO IDP_AUTHENTICATOR " +
"(TENANT_ID, IDP_ID, NAME, IS_ENABLED, DEFINED_BY, AUTHENTICATOR_TYPE, DISPLAY_NAME) VALUES" +
" (:TENANT_ID, (SELECT ID FROM IDP WHERE IDP.NAME=? AND IDP.TENANT_ID =?), " +
":NAME, :IS_ENABLED, :DEFINED_BY, :AUTHENTICATOR_TYPE, :DISPLAY_NAME)";
" (:TENANT_ID;, (SELECT ID FROM IDP WHERE IDP.NAME = :IDP_NAME; AND IDP.TENANT_ID = :TENANT_ID;), " +
":NAME;, :IS_ENABLED;, :DEFINED_BY;, :AUTHENTICATOR_TYPE;, :DISPLAY_NAME;);";
public static final String UPDATE_AUTHENTICATOR_SQL = "UPDATE IDP_AUTHENTICATOR SET IS_ENABLED = " +
":IS_ENABLED;, DISPLAY_NAME = :DISPLAY_NAME; WHERE NAME = :NAME; AND TENANT_ID = :TENANT_ID;";
public static final String GET_AUTHENTICATOR_SQL = "SELECT * FROM IDP_AUTHENTICATOR WHERE NAME = :NAME; " +
" AND TENANT_ID = :TENANT_ID;";
public static final String DELETE_AUTHENTICATOR_SQL = "DELETE FROM IDP_AUTHENTICATOR WHERE NAME = :NAME; " +
" AND TENANT_ID = :TENANT_ID;";
public static final String GET_AUTHENTICATOR_ID_SQL = "SELECT ID FROM IDP_AUTHENTICATOR " +
"WHERE NAME = :NAME; AND TENANT_ID = :TENANT_ID;";
public static final String ADD_AUTHENTICATOR_PROP_SQL = "INSERT INTO IDP_AUTHENTICATOR_PROPERTY " +
"(AUTHENTICATOR_ID, TENANT_ID, PROPERTY_KEY, PROPERTY_VALUE, IS_SECRET) VALUES " +
"(:AUTHENTICATOR_ID;, :TENANT_ID;, :PROPERTY_KEY;, :PROPERTY_VALUE;, :IS_SECRET;);";
public static final String DELETE_AUTHENTICATOR_PROP_SQL = "DELETE FROM IDP_AUTHENTICATOR_PROPERTY " +
"WHERE AUTHENTICATOR_ID = :AUTHENTICATOR_ID; AND TENANT_ID = :TENANT_ID;";
public static final String GET_AUTHENTICATOR_PROP_SQL = "SELECT PROPERTY_KEY, PROPERTY_VALUE, IS_SECRET" +
" FROM IDP_AUTHENTICATOR_PROPERTY " +
"WHERE AUTHENTICATOR_ID = :AUTHENTICATOR_ID; AND TENANT_ID = :TENANT_ID;";

private Query() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.AuthenticatorType;

/**
* This interface performs CRUD operations for the Local Application Authenticator configurations.
Expand All @@ -36,7 +37,7 @@ public interface AuthenticatorManagementDAO {
* @throws AuthenticatorMgtException If an error occurs while adding the authenticator configuration.
*/
LocalAuthenticatorConfig addUserDefinedLocalAuthenticator(
LocalAuthenticatorConfig authenticatorConfig, Integer tenantId)
LocalAuthenticatorConfig authenticatorConfig, Integer tenantId, AuthenticatorType type)
throws AuthenticatorMgtException;

/**
Expand Down
Loading

0 comments on commit b6248b3

Please sign in to comment.