Skip to content

Commit

Permalink
Addressed comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Dec 13, 2024
1 parent 664de74 commit c7f35b4
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@

import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildClientException;
import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildRuntimeServerException;
import static org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants.Authenticator.DISPLAY_NAME;

/**
* Application authenticator service.
*/
public class ApplicationAuthenticatorService {

private static final String DISPLAY_NAME = "Display name";
private static volatile ApplicationAuthenticatorService instance;
private static final Log LOG = LogFactory.getLog(ApplicationAuthenticatorService.class);
private static final AuthenticatorManagementDAO dao =
Expand Down Expand Up @@ -74,6 +74,7 @@ public List<LocalAuthenticatorConfig> getLocalAuthenticators() {
/**
* This returns user defined local authenticators.
*
* @param tenantDomain Tenant domain.
* @return Retrieved LocalAuthenticatorConfig.
*/
public List<UserDefinedLocalAuthenticatorConfig> getAllUserDefinedLocalAuthenticators(String tenantDomain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
public class AuthenticatorCacheEntry extends CacheEntry {

private static final long serialVersionUID = -6234723984328871924L;

private UserDefinedLocalAuthenticatorConfig authenticatorConfig;

public AuthenticatorCacheEntry(UserDefinedLocalAuthenticatorConfig authenticatorConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
public class AuthenticatorCacheKey extends CacheKey {

private static final long serialVersionUID = -2897123859023483921L;

private final String authenticatorName;

public AuthenticatorCacheKey(String authenticatorName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public interface AuthenticatorManagementDAO {
*
* @param authenticatorConfig Local application authenticator configuration.
* @param tenantId Tenant Id.
*
* @return Created UserDefinedLocalAuthenticatorConfig.
* @throws AuthenticatorMgtException If an error occurs while adding the authenticator configuration.
*/
Expand All @@ -46,7 +45,6 @@ UserDefinedLocalAuthenticatorConfig addUserDefinedLocalAuthenticator(
* @param existingAuthenticatorConfig Existing Local application authenticator configuration.
* @param updatedAuthenticatorConfig New local application authenticator configuration.
* @param tenantId Tenant Id.
*
* @return Updated UserDefinedLocalAuthenticatorConfig.
* @throws AuthenticatorMgtException If an error occurs while updating the authenticator configuration.
*/
Expand All @@ -56,11 +54,10 @@ UserDefinedLocalAuthenticatorConfig updateUserDefinedLocalAuthenticator(
throws AuthenticatorMgtException;

/**
* Retrieve a Local user defined Application Authenticator configuration by name.
* Retrieve a local user defined application authenticator configuration by name.
*
* @param authenticatorConfigName Name of the local application authenticator configuration.
* @param tenantId Tenant Id.
*
* @return Retrieved UserDefinedLocalAuthenticatorConfig
* @throws AuthenticatorMgtException If an error occurs while retrieving the authenticator configuration.
*/
Expand All @@ -70,8 +67,7 @@ UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticator(
/**
* Retrieve all user defined local application authenticator configurations.
*
* @param tenantId Tenant Id.
*
* @param tenantId Tenant Id.
* @return Retrieved UserDefinedLocalAuthenticatorConfig
* @throws AuthenticatorMgtException If an error occurs while retrieving the authenticator configurations.
*/
Expand All @@ -83,7 +79,6 @@ List<UserDefinedLocalAuthenticatorConfig> getAllUserDefinedLocalAuthenticators(i
*
* @param authenticatorConfigName Name of the local application authenticator configuration.
* @param tenantId Tenant Id.
*
* @throws AuthenticatorMgtException If an error occurs while deleting the authenticator configuration.
*/
void deleteUserDefinedLocalAuthenticator(String authenticatorConfigName, UserDefinedLocalAuthenticatorConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

package org.wso2.carbon.identity.application.common.dao.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate;
import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException;
import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtSQLConstants.Column;
Expand All @@ -33,20 +31,17 @@
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.AuthenticationType;
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;

import java.util.ArrayList;
import java.util.List;

import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildServerException;
import static org.wso2.carbon.identity.application.common.util.IdentityApplicationConstants.Authenticator.ACTION_ID_PROPERTY;

/**
* This class implements the AuthenticatorManagementDAO interface which perform CRUD operation on database.
*/
public class AuthenticatorManagementDAOImpl implements AuthenticatorManagementDAO {

private static final Log LOG = LogFactory.getLog(AuthenticatorManagementDAOImpl.class);
public static final String IS_TRUE_VALUE = "1";
public static final String IS_FALSE_VALUE = "0";
public static final String LOCAL_IDP_NAME = "LOCAL";
Expand All @@ -69,17 +64,11 @@ public UserDefinedLocalAuthenticatorConfig addUserDefinedLocalAuthenticator(
statement.setInt(Column.TENANT_ID, tenantId);
}), null, false);

int authenticatorConfigID = getAuthenticatorIdentifier(authenticatorConfig.getName(), tenantId);
addAuthenticatorProperties(authenticatorConfig.getName(), authenticatorConfigID,
authenticatorConfig.getProperties(), tenantId);
int authenticatorConfigID = getAuthenticatorEntryId(authenticatorConfig.getName(), tenantId);
addAuthenticatorProperty(authenticatorConfigID, authenticatorConfig.getProperties(), tenantId);

return getUserDefinedLocalAuthenticatorByName(authenticatorConfig.getName(), tenantId);
} catch (DataAccessException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Error while adding the authenticator: %s in tenant domain: %s. " +
"Rolling back added Authenticator information.", authenticatorConfig.getName(),
IdentityTenantUtil.getTenantDomain(tenantId)));
}
throw buildServerException(AuthenticatorMgtError.ERROR_WHILE_ADDING_AUTHENTICATOR, e);
}
}
Expand All @@ -103,11 +92,6 @@ public UserDefinedLocalAuthenticatorConfig updateUserDefinedLocalAuthenticator(

return getUserDefinedLocalAuthenticatorByName(updatedAuthenticatorConfig.getName(), tenantId);
} catch (DataAccessException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Error while updating the authenticator: %s in tenant domain: %s. " +
"Rolling back updated Authenticator information.",
existingAuthenticatorConfig.getName(), IdentityTenantUtil.getTenantDomain(tenantId)));
}
throw buildServerException(AuthenticatorMgtError.ERROR_WHILE_UPDATING_AUTHENTICATOR, e);
}
}
Expand All @@ -119,10 +103,6 @@ public UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticator(
try {
return getUserDefinedLocalAuthenticatorByName(authenticatorConfigName, tenantId);
} catch (DataAccessException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Error while retrieving the user defined local authenticator:%s in tenant " +
"domain: %s.", authenticatorConfigName, IdentityTenantUtil.getTenantDomain(tenantId)));
}
throw buildServerException(AuthenticatorMgtError.ERROR_WHILE_RETRIEVING_AUTHENTICATOR_BY_NAME, e);
}
}
Expand Down Expand Up @@ -151,15 +131,11 @@ public List<UserDefinedLocalAuthenticatorConfig> getAllUserDefinedLocalAuthentic
});

for (UserDefinedLocalAuthenticatorConfig retrievedConfigs: allUserDefinedLocalConfigs) {
int authenticatorConfigID = getAuthenticatorIdentifier(retrievedConfigs.getName(), tenantId);
int authenticatorConfigID = getAuthenticatorEntryId(retrievedConfigs.getName(), tenantId);
retrievedConfigs.setProperties(getAuthenticatorProperties(authenticatorConfigID, tenantId));
}
return allUserDefinedLocalConfigs;
} catch (DataAccessException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Error while retrieving the all user defined local authenticators in tenant " +
"domain: %s.", IdentityTenantUtil.getTenantDomain(tenantId)));
}
throw buildServerException(AuthenticatorMgtError.ERROR_WHILE_RETRIEVING_AUTHENTICATOR_BY_NAME, e);
}
}
Expand All @@ -170,19 +146,13 @@ public void deleteUserDefinedLocalAuthenticator(String authenticatorConfigName,

NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
try {

jdbcTemplate.executeUpdate(Query.DELETE_AUTHENTICATOR_SQL,
statement -> {
statement.setString(Column.NAME, authenticatorConfigName);
statement.setInt(Column.TENANT_ID, tenantId);
statement.executeUpdate();
});
} catch (DataAccessException e) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Error while deleting the authenticator: %s in tenant domain: %s. " +
"Rolling back deleted Authenticator information.", authenticatorConfigName,
IdentityTenantUtil.getTenantDomain(tenantId)));
}
throw buildServerException(AuthenticatorMgtError.ERROR_WHILE_DELETING_AUTHENTICATOR, e);
}
}
Expand Down Expand Up @@ -211,7 +181,7 @@ private UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticatorByNa
return null;
}

int authenticatorConfigID = getAuthenticatorIdentifier(authenticatorConfigName, tenantId);
int authenticatorConfigID = getAuthenticatorEntryId(authenticatorConfigName, tenantId);
List<Property> properties = new ArrayList<>();
jdbcTemplate.fetchSingleRecord(Query.GET_AUTHENTICATOR_PROP_SQL,
(resultSet, rowNumber) -> {
Expand All @@ -238,31 +208,26 @@ private UserDefinedLocalAuthenticatorConfig getLocalAuthenticatorConfigBasedOnTy
return new UserDefinedLocalAuthenticatorConfig(AuthenticationType.IDENTIFICATION);
}

private int getAuthenticatorIdentifier(String authenticatorConfigName, int tenantId)
private int getAuthenticatorEntryId(String authenticatorConfigName, int tenantId)
throws AuthenticatorMgtServerException, DataAccessException {

NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
String id = jdbcTemplate.fetchSingleRecord(Query.GET_AUTHENTICATOR_ID_SQL,
(resultSet, rowNumber) -> resultSet.getString(Column.ID),
int id = jdbcTemplate.fetchSingleRecord(Query.GET_AUTHENTICATOR_ID_SQL,
(resultSet, rowNumber) -> resultSet.getInt(Column.ID),
statement -> {
statement.setString(Column.NAME, authenticatorConfigName);
statement.setInt(Column.TENANT_ID, tenantId);
});

if (id != null) {
return Integer.parseInt(id);
if (id != 0) {
return id;
}
throw buildServerException(AuthenticatorMgtError.ERROR_CODE_NO_AUTHENTICATOR_FOUND,
authenticatorConfigName);
}

private void addAuthenticatorProperties(String authenticatorName, int authenticatorConfigID, Property[] properties,
int tenantId) throws DataAccessException, AuthenticatorMgtServerException {

if (!(properties.length == 1 && ACTION_ID_PROPERTY.equals(properties[0].getName()))) {
throw buildServerException(AuthenticatorMgtError.ERROR_CODE_HAVING_MULTIPLE_PROP,
authenticatorName);
}
private void addAuthenticatorProperty(int authenticatorConfigID, Property[] properties, int tenantId)
throws DataAccessException {

Property prop = properties[0];
NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
Expand All @@ -272,16 +237,11 @@ private void addAuthenticatorProperties(String authenticatorName, int authentica
statementProp.setInt(Column.TENANT_ID, tenantId);
statementProp.setString(Column.PROPERTY_KEY, prop.getName());
statementProp.setString(Column.PROPERTY_VALUE, prop.getValue());
if (prop.isConfidential()) {
statementProp.setString(Column.IS_SECRET, IS_TRUE_VALUE);
} else {
statementProp.setString(Column.IS_SECRET, IS_FALSE_VALUE);
}
statementProp.setString(Column.IS_SECRET, IS_FALSE_VALUE);
}), null, false);
}

private Property[] getAuthenticatorProperties(int authenticatorConfigID,
int tenantId) throws DataAccessException {
private Property[] getAuthenticatorProperties(int authenticatorConfigID, int tenantId) throws DataAccessException {

List<Property> properties = new ArrayList<>();
NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
Expand Down
Loading

0 comments on commit c7f35b4

Please sign in to comment.