Skip to content

Commit

Permalink
Improve authentication mgt
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla committed Jan 14, 2025
1 parent 0e2f2c9 commit cdb7e1c
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,41 @@ public static ApplicationAuthenticatorService getInstance() {
return instance;
}

/**
* This method is used to get the list of SYSTEM defined local authenticator configurations.
*
* @deprecated It is recommended to use {@link #getAllSystemDefinedLocalAuthenticators()},
* which returning the SYSTEM defined local application authenticator configurations.
*/
@Deprecated
public List<LocalAuthenticatorConfig> getLocalAuthenticators() {
return this.localAuthenticators;
}

/**
* This returns list of all SYSTEM defined local authenticator configurations.
*
* @return Retrieved LocalAuthenticatorConfig.
*/
public List<LocalAuthenticatorConfig> getAllSystemDefinedLocalAuthenticators() {

return this.localAuthenticators;
}

/**
* This returns list of all SYSTEM and USER defined local authenticator configurations.
*
* @param tenantDomain Tenant domain.
* @return Retrieved LocalAuthenticatorConfig.
*/
public List<LocalAuthenticatorConfig> getAllLocalAuthenticators(String tenantDomain)
throws AuthenticatorMgtException {

List<LocalAuthenticatorConfig> configList = new ArrayList<>(getAllUserDefinedLocalAuthenticators(tenantDomain));
configList.addAll(localAuthenticators);
return configList;
}

/**
* This returns user defined local authenticators.
*
Expand Down Expand Up @@ -206,8 +237,7 @@ public UserDefinedLocalAuthenticatorConfig addUserDefinedLocalAuthenticator(
UserDefinedLocalAuthenticatorConfig authenticatorConfig, String tenantDomain)
throws AuthenticatorMgtException {

LocalAuthenticatorConfig config = getLocalAuthenticatorByName(authenticatorConfig.getName(), tenantDomain);
if (config != null) {
if (isExistingAuthenticatorName(authenticatorConfig.getName(), tenantDomain)) {
throw buildClientException(AuthenticatorMgtError.ERROR_AUTHENTICATOR_ALREADY_EXIST,
authenticatorConfig.getName());
}
Expand Down Expand Up @@ -278,6 +308,14 @@ public UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticator(Stri
authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain));
}

/**
* Check whether an any local of federated authenticator configuration with the given name exists.
*
* @param authenticatorName Name of the authenticator.
* @param tenantDomain Tenant domain.
* @return True if an authenticator with the given name exists.
* @throws AuthenticatorMgtException If an error occurs while checking the existence of the authenticator.
*/
public boolean isExistingAuthenticatorName(String authenticatorName, String tenantDomain)
throws AuthenticatorMgtException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void deleteUserDefinedLocalAuthenticator(String authenticatorConfigName, UserDef
authenticatorConfig, int tenantId) throws AuthenticatorMgtException;

/**
* Check whether an authenticator with the given name exists.
* Check whether an any local of federated authenticator configuration with the given name exists.
*
* @param authenticatorName Name of the authenticator.
* @param tenantId Tenant Id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType;
import org.wso2.carbon.identity.core.util.IdentityDatabaseUtil;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -179,16 +180,17 @@ public boolean isExistingAuthenticatorName(String authenticatorName, int tenantI

NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
try {
jdbcTemplate.withTransaction(template ->
ResultSet results = jdbcTemplate.withTransaction(template ->
template.fetchSingleRecord(Query.IS_AUTHENTICATOR_EXISTS_BY_NAME_SQL,
(resultSet, rowNumber) -> true,
(resultSet, rowNumber) -> resultSet,
statement -> {
statement.setString(Column.NAME, authenticatorName);
statement.setInt(Column.TENANT_ID, tenantId);
}));
return false;
return results != null;
} catch (TransactionException e) {
throw buildServerException(AuthenticatorMgtError.ERROR_WHILE_ADDING_AUTHENTICATOR, e);
throw buildServerException(AuthenticatorMgtError.ERROR_WHILE_CHECKING_FOR_EXISTING_AUTHENTICATOR_BY_NAME, e,
authenticatorName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ public boolean isExistingAuthenticatorName(String authenticatorName, int tenantI
return jdbcTemplate.withTransaction(
template -> dao.isExistingAuthenticatorName(authenticatorName, tenantId));
} catch (TransactionException e) {
throw handleAuthenticatorMgtException(AuthenticatorMgtError.ERROR_AUTHENTICATOR_ALREADY_EXIST, e,
authenticatorName);
throw handleAuthenticatorMgtException(AuthenticatorMgtError
.ERROR_WHILE_CHECKING_FOR_EXISTING_AUTHENTICATOR_BY_NAME, e, authenticatorName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void setDefinedByType(DefinedByType type) {
}

/**
* Get the image of the Local authenticator config.
* Get the image url of the local authenticator config.
*
* @return Image
*/
Expand All @@ -282,7 +282,7 @@ public String getImageUrl() {
}

/**
* Set the image of the Local authenticator config.
* Set the image url of the local authenticator config.
*
* @param imageUrl The image of the local authenticator config.
*/
Expand All @@ -292,7 +292,7 @@ public void setImageUrl(String imageUrl) {
}

/**
* Get the description of the Local authenticator config.
* Get the description of the local authenticator config.
*
* @return Description
*/
Expand All @@ -302,7 +302,7 @@ public String getDescription() {
}

/**
* Set the description of the Local authenticator config.
* Set the description of the local authenticator config.
*
* @param description The description of the local authenticator config.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ public enum AuthenticatorMgtError {
ERROR_CODE_DELETING_ENDPOINT_CONFIG("65012", "Error while managing endpoint configurations.",
"Error while managing endpoint configurations for the user defined local authenticator %s."),
ERROR_CODE_HAVING_MULTIPLE_PROP("65013", "Multiple properties found", "Only actionId " +
"property is allowed for authenticator: %s.");
"property is allowed for authenticator: %s."),
ERROR_WHILE_CHECKING_FOR_EXISTING_AUTHENTICATOR_BY_NAME("65014", "Error while retrieving " +
"authenticator.", "Error while check any authenticator exists by given name: %s.");

private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public void testCreateUserDefinedLocalAuthenticatorWithExistingAuthenticator(

@Test(priority = 3, expectedExceptions = AuthenticatorMgtException.class,
expectedExceptionsMessageRegExp = "Authenticator display name is invalid.")
public void testCreateUserDefinedLocalAuthenticatorWithBlankDisplayName() throws AuthenticatorMgtException {
public void testCreateUserDefinedLocalAuthenticatorWithInvalidDisplayName() throws AuthenticatorMgtException {

UserDefinedLocalAuthenticatorConfig config = createUserDefinedAuthenticatorConfig(
"custom_withBlankDisplayName", AuthenticationType.IDENTIFICATION);
Expand Down Expand Up @@ -351,6 +351,22 @@ public void testGetUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticatorCo
}

@Test(priority = 16)
public void testIsExistingAuthenticatorName() throws AuthenticatorMgtException {

Assert.assertTrue(ApplicationCommonServiceDataHolder.getInstance().
getApplicationAuthenticatorService().isExistingAuthenticatorName(
authenticatorConfig1.getName(), tenantDomain));
}

@Test(priority = 17)
public void testIsExistingAuthenticatorNameForNonExistName() throws AuthenticatorMgtException {

Assert.assertFalse(ApplicationCommonServiceDataHolder.getInstance().
getApplicationAuthenticatorService().isExistingAuthenticatorName(
NON_EXIST_AUTHENTICATOR_NAME, tenantDomain));
}

@Test(priority = 18)
public void testDeleteUserDefinedLocalAuthenticatorWithActionException() throws Exception {

ActionManagementService actionManagementServiceForException = mock(ActionManagementService.class);
Expand All @@ -368,7 +384,7 @@ public void testDeleteUserDefinedLocalAuthenticatorWithActionException() throws
authenticatorConfigForException.getName(), tenantDomain));
}

@Test(priority = 17, dataProvider = "authenticatorConfigToModify")
@Test(priority = 19, dataProvider = "authenticatorConfigToModify")
public void testDeleteUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticatorConfig config)
throws AuthenticatorMgtException {

Expand All @@ -378,7 +394,7 @@ public void testDeleteUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticato
.getLocalAuthenticatorByName(config.getName()));
}

@Test(priority = 18)
@Test(priority = 20)
public void testDeleteUserDefinedLocalAuthenticatorWithNonExistingAuthenticator() throws AuthenticatorMgtException {

// Assert that no exception is thrown when trying to delete a non-existing authenticator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,21 @@ public void testGetUserDefinedLocalAuthenticatorForNonExist() throws Authenticat
NON_EXIST_AUTHENTICATOR_NAME, tenantId));
}

@Test(dataProvider = "authenticatorConfig", priority = 9)
@Test(priority = 9)
public void testIsExistingAuthenticatorName() throws AuthenticatorMgtException {

Assert.assertTrue(authenticatorManagementDAO.isExistingAuthenticatorName(
authenticatorConfig1.getName(), tenantId));
}

@Test(priority = 10)
public void testIsExistingAuthenticatorNameForNonExistName() throws AuthenticatorMgtException {

Assert.assertFalse(authenticatorManagementDAO.isExistingAuthenticatorName(
NON_EXIST_AUTHENTICATOR_NAME, tenantId));
}

@Test(dataProvider = "authenticatorConfig", priority = 11)
public void testDeleteUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticatorConfig config)
throws AuthenticatorMgtException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementClientException;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementServerException;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementValidationException;
import org.wso2.carbon.identity.application.common.IdentityApplicationRegistrationFailureException;
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
import org.wso2.carbon.identity.application.common.model.ApplicationBasicInfo;
import org.wso2.carbon.identity.application.common.model.AssociatedRolesConfig;
import org.wso2.carbon.identity.application.common.model.AuthenticationStep;
Expand Down Expand Up @@ -997,8 +999,8 @@ public LocalAuthenticatorConfig[] getAllLocalAuthenticators(String tenantDomain)

try {
startTenantFlow(tenantDomain);
IdentityProviderDAO idpdao = ApplicationMgtSystemConfig.getInstance().getIdentityProviderDAO();
List<LocalAuthenticatorConfig> localAuthenticators = idpdao.getAllLocalAuthenticators();
List<LocalAuthenticatorConfig> localAuthenticators = ApplicationAuthenticatorService.getInstance()
.getAllLocalAuthenticators(tenantDomain);
if (localAuthenticators != null) {
return localAuthenticators.toArray(new LocalAuthenticatorConfig[localAuthenticators.size()]);
}
Expand Down Expand Up @@ -1506,15 +1508,15 @@ private void validateResourceId(String resourceId, String tenantDomain)
int filteredCount = 0;
try {
startTenantFlow(tenantDomain);
IdentityProviderDAO idpdao = ApplicationMgtSystemConfig.getInstance().getIdentityProviderDAO();
List<LocalAuthenticatorConfig> localAuthenticators = idpdao.getAllLocalAuthenticators();
List<LocalAuthenticatorConfig> localAuthenticators = ApplicationAuthenticatorService.getInstance()
.getAllLocalAuthenticators(tenantDomain);
if (localAuthenticators != null) {
filteredCount = (int) localAuthenticators.stream()
.filter(authenticatorConfig ->
authenticatorConfig.getName()
.equals(authenticatorName)).count();
}
} catch (IdentityApplicationManagementException e) {
} catch (IdentityApplicationManagementException | AuthenticatorMgtException e) {
throw new IdentityApplicationManagementException(
String.format(IdPManagementConstants.ErrorMessage
.ERROR_CODE_GET_CONNECTED_APPS_REQUEST_INVALID.getMessage(), resourceId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.carbon.identity.application.mgt.dao;

import org.wso2.carbon.identity.application.common.ApplicationAuthenticatorService;
import org.wso2.carbon.identity.application.common.IdentityApplicationManagementException;
import org.wso2.carbon.identity.application.common.model.IdentityProvider;
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
Expand Down Expand Up @@ -51,7 +52,9 @@ public interface IdentityProviderDAO {
* Returns all the local authenticators available on the system.
* @return
* @throws IdentityApplicationManagementException
* @deprecated use {@link ApplicationAuthenticatorService#getInstance().getAllLocalAuthenticators(String)} instead.
*/
@Deprecated
List<LocalAuthenticatorConfig> getAllLocalAuthenticators() throws IdentityApplicationManagementException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ public List<IdentityProvider> getAllIdentityProviders()
return federatedIdentityProviders;
}

@Override
/**
*
* @deprecated use {@link ApplicationAuthenticatorService#getInstance().getAllLocalAuthenticators(String)} instead.
*/
@Override
@Deprecated
public List<LocalAuthenticatorConfig> getAllLocalAuthenticators()
throws IdentityApplicationManagementException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public List<ApplicationAuthenticator> getAllAuthenticators() throws ApplicationA
return FrameworkServiceComponent.getAuthenticators();
}

public List<ApplicationAuthenticator> getLocalAuthenticators() throws ApplicationAuthenticationException {
public List<ApplicationAuthenticator> getAllSystemDefinedLocalAuthenticators() {

List<ApplicationAuthenticator> localAuthenticators = new ArrayList<ApplicationAuthenticator>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ protected void filterOptions(Map<String, Map<String, String>> authenticationOpti

if (FrameworkConstants.LOCAL_IDP_NAME.equals(idpName)) {
List<LocalAuthenticatorConfig> localAuthenticators = ApplicationAuthenticatorService
.getInstance().getLocalAuthenticators();
.getInstance().getAllSystemDefinedLocalAuthenticators();
for (LocalAuthenticatorConfig localAuthenticatorConfig : localAuthenticators) {
if (FrameworkUtils.isAuthenticatorNameInAuthConfigEnabled()) {
if (authenticatorConfig.getName().equals(localAuthenticatorConfig.getName()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ protected void filterOptions(Map<String, Map<String, String>> authenticationOpti

if (FrameworkConstants.LOCAL_IDP_NAME.equals(idpName)) {
List<LocalAuthenticatorConfig> localAuthenticators = ApplicationAuthenticatorService
.getInstance().getLocalAuthenticators();
.getInstance().getAllSystemDefinedLocalAuthenticators();
for (LocalAuthenticatorConfig localAuthenticatorConfig : localAuthenticators) {
if (FrameworkUtils.isAuthenticatorNameInAuthConfigEnabled()) {
if (authenticatorConfig.getName().equals(localAuthenticatorConfig.getName()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ protected void filterOptions(Map<String, Map<String, String>> authenticationOpti

if (FrameworkConstants.LOCAL_IDP_NAME.equals(idpName)) {
List<LocalAuthenticatorConfig> localAuthenticators = ApplicationAuthenticatorService
.getInstance().getLocalAuthenticators();
.getInstance().getAllSystemDefinedLocalAuthenticators();
for (LocalAuthenticatorConfig localAuthenticatorConfig : localAuthenticators) {
if (FrameworkUtils.isAuthenticatorNameInAuthConfigEnabled()) {
if (authenticatorConfig.getName().equals(localAuthenticatorConfig.getName()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ public void testGetAllAuthenticators() throws Exception {
}

@Test
public void testGetLocalAuthenticators() throws Exception {
public void testGetLocalAuthenticators() {

List<ApplicationAuthenticator> localAuthenticators = applicationAuthenticationService.getLocalAuthenticators();
List<ApplicationAuthenticator> localAuthenticators =
applicationAuthenticationService.getAllSystemDefinedLocalAuthenticators();
assertEquals(localAuthenticators.size(), 1);
assertEquals(localAuthenticators.get(0).getName(), LOCAL_AUTHENTICATOR_NAME);
}
Expand Down
Loading

0 comments on commit cdb7e1c

Please sign in to comment.