Skip to content

Commit 8ba2cce

Browse files
committed
/authenticators/system
1 parent 04abbb6 commit 8ba2cce

File tree

10 files changed

+247
-65
lines changed

10 files changed

+247
-65
lines changed

components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/ApplicationAuthenticatorService.java

+50-16
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package org.wso2.carbon.identity.application.common;
2020

21-
import jdk.vm.ci.meta.Local;
2221
import org.apache.commons.logging.Log;
2322
import org.apache.commons.logging.LogFactory;
2423
import org.wso2.carbon.identity.application.common.dao.AuthenticatorManagementDAO;
@@ -128,6 +127,7 @@ public List<RequestPathAuthenticatorConfig> getRequestPathAuthenticators() {
128127
* @param name The name of the Local Application Authenticator configuration.
129128
* @return Retrieved LocalAuthenticatorConfig.
130129
*
130+
* @deprecated It is recommended to use {@link #getLocalAuthenticatorByName(String, String)},
131131
* which supports retrieving both USER and SYSTEM defined Local Application Authenticator configuration by name.
132132
*/
133133
@Deprecated
@@ -300,6 +300,24 @@ public UserDefinedLocalAuthenticatorConfig updateUserDefinedLocalAuthenticator(
300300
existingConfig, authenticatorConfig, IdentityTenantUtil.getTenantId(tenantDomain));
301301
}
302302

303+
/**
304+
* Add a system defined Local Application Authenticator configuration.
305+
*
306+
* @param authenticatorConfig Local Application Authenticator configuration.
307+
* @param tenantDomain Tenant domain.
308+
* @throws AuthenticatorMgtException If an error occurs while adding the authenticator configuration.
309+
*/
310+
public LocalAuthenticatorConfig addSystemDefinedLocalAuthenticator(LocalAuthenticatorConfig authenticatorConfig, String tenantDomain)
311+
throws AuthenticatorMgtException {
312+
313+
if (isExistingAuthenticatorNameDB(authenticatorConfig.getName(), tenantDomain)) {
314+
throw buildClientException(AuthenticatorMgtError.ERROR_AUTHENTICATOR_ALREADY_EXIST,
315+
authenticatorConfig.getName());
316+
}
317+
return dao.addSystemLocalAuthenticator(
318+
authenticatorConfig, IdentityTenantUtil.getTenantId(tenantDomain));
319+
}
320+
303321
/**
304322
* Update a Local Application Authenticator configuration.
305323
*
@@ -311,8 +329,18 @@ public LocalAuthenticatorConfig updateAuthenticatorAmrValue(LocalAuthenticatorCo
311329
LocalAuthenticatorConfig existingConfig = resolveExistingSystemLocalAuthenticator(authenticatorConfig.getName(),
312330
tenantDomain);
313331
if (existingConfig == null) {
314-
throw buildClientException(AuthenticatorMgtError.ERROR_NOT_FOUND_AUTHENTICATOR,
315-
authenticatorConfig.getName());
332+
for (LocalAuthenticatorConfig localAuthenticator : localAuthenticators) {
333+
if (localAuthenticator.getName().equals(authenticatorConfig.getName())) {
334+
existingConfig = addSystemDefinedLocalAuthenticator(authenticatorConfig, tenantDomain);
335+
break;
336+
}
337+
}
338+
//calling the add method if the authenticator is not found. If it is not even in the file throw this error
339+
if(existingConfig == null){
340+
throw buildClientException(AuthenticatorMgtError.ERROR_NOT_FOUND_AUTHENTICATOR,
341+
authenticatorConfig.getName());
342+
}
343+
316344
}
317345
return dao.updateSystemLocalAuthenticatorAmrValue(
318346
existingConfig, authenticatorConfig, IdentityTenantUtil.getTenantId(tenantDomain));
@@ -353,19 +381,19 @@ public UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticator(Stri
353381
authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain));
354382
}
355383

356-
/**
357-
* Retrieve a Local Application Authenticator configuration by name.
358-
*
359-
* @param authenticatorName Name of Local Application Authenticator configuration to be deleted.
360-
* @param tenantDomain Tenant domain.
361-
* @return Retrieved LocalAuthenticatorConfig.
362-
* @throws AuthenticatorMgtException If an error occurs while retrieving the authenticator configuration.
363-
*/
364-
public LocalAuthenticatorConfig getSystemLocalAuthenticator(String authenticatorName, String tenantDomain)
365-
throws AuthenticatorMgtException {
366-
367-
return dao.getSystemLocalAuthenticator(authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain));
368-
}
384+
// /**
385+
// * Retrieve a Local Application Authenticator configuration by name.
386+
// *
387+
// * @param authenticatorName Name of Local Application Authenticator configuration to be deleted.
388+
// * @param tenantDomain Tenant domain.
389+
// * @return Retrieved LocalAuthenticatorConfig.
390+
// * @throws AuthenticatorMgtException If an error occurs while retrieving the authenticator configuration.
391+
// */
392+
// public LocalAuthenticatorConfig getSystemLocalAuthenticator(String authenticatorName, String tenantDomain)
393+
// throws AuthenticatorMgtException {
394+
//
395+
// return dao.getSystemLocalAuthenticator(authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain));
396+
// }
369397

370398
/**
371399
* Check whether any local or federated authenticator configuration exists with the given name.
@@ -411,4 +439,10 @@ private LocalAuthenticatorConfig resolveExistingSystemLocalAuthenticator(String
411439
String tenantDomain) throws AuthenticatorMgtException{
412440
return dao.getSystemLocalAuthenticator(authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain));
413441
}
442+
443+
private boolean isExistingAuthenticatorNameDB(String authenticatorName, String tenantDomain)
444+
throws AuthenticatorMgtException {
445+
446+
return dao.isExistingAuthenticatorNameDB(authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain));
447+
}
414448
}

components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/cache/AuthenticatorCacheEntry.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package org.wso2.carbon.identity.application.common.cache;
2020

21+
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
2122
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
2223
import org.wso2.carbon.identity.core.cache.CacheEntry;
2324

@@ -28,19 +29,19 @@ public class AuthenticatorCacheEntry extends CacheEntry {
2829

2930
private static final long serialVersionUID = -6234723984328871924L;
3031

31-
private UserDefinedLocalAuthenticatorConfig authenticatorConfig;
32+
private LocalAuthenticatorConfig authenticatorConfig;
3233

33-
public AuthenticatorCacheEntry(UserDefinedLocalAuthenticatorConfig authenticatorConfig) {
34+
public AuthenticatorCacheEntry(LocalAuthenticatorConfig authenticatorConfig) {
3435

3536
this.authenticatorConfig = authenticatorConfig;
3637
}
3738

38-
public UserDefinedLocalAuthenticatorConfig getAuthenticatorConfig() {
39+
public LocalAuthenticatorConfig getAuthenticatorConfig() {
3940

4041
return authenticatorConfig;
4142
}
4243

43-
public void setAuthenticatorConfig(UserDefinedLocalAuthenticatorConfig authenticatorConfig) {
44+
public void setAuthenticatorConfig(LocalAuthenticatorConfig authenticatorConfig) {
4445

4546
this.authenticatorConfig = authenticatorConfig;
4647
}

components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/constant/AuthenticatorMgtSQLConstants.java

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public static class Query {
6565
" (:TENANT_ID;, (SELECT ID FROM IDP WHERE IDP.NAME = :IDP_NAME; AND IDP.TENANT_ID = :TENANT_ID;), " +
6666
":NAME;, :IS_ENABLED;, :DEFINED_BY;, :AUTHENTICATION_TYPE;, :DISPLAY_NAME;, " +
6767
":IMAGE_URL;, :DESCRIPTION;)";
68+
public static final String ADD_SYSTEM_LOCAL_AUTHENTICATOR_SQL = "INSERT INTO IDP_AUTHENTICATOR " +
69+
"(TENANT_ID, IDP_ID, NAME, IS_ENABLED, DEFINED_BY, AMR_VALUE, DISPLAY_NAME) " +
70+
"VALUES " +
71+
"(:TENANT_ID, " +
72+
"(SELECT ID FROM IDP WHERE NAME = :IDP_NAME; AND TENANT_ID = :TENANT_ID;), " +
73+
":NAME;, :IS_ENABLED;, :DEFINED_BY;, :AMR_VALUE;, :DISPLAY_NAME;)";
6874
public static final String UPDATE_AUTHENTICATOR_SQL = "UPDATE IDP_AUTHENTICATOR SET IS_ENABLED = " +
6975
":IS_ENABLED;, DISPLAY_NAME = :DISPLAY_NAME;, IMAGE_URL = :IMAGE_URL;, DESCRIPTION = :DESCRIPTION;, " +
7076
"AMR_VALUE = :AMR_VALUE" +

components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/AuthenticatorManagementDAO.java

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.wso2.carbon.identity.application.common.dao;
2020

2121
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException;
22+
import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtServerException;
2223
import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig;
2324
import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig;
2425

@@ -115,4 +116,22 @@ void deleteUserDefinedLocalAuthenticator(String authenticatorConfigName, UserDef
115116
* @throws AuthenticatorMgtException If an error occurs while checking the existence of the authenticator.
116117
*/
117118
boolean isExistingAuthenticatorName(String authenticatorName, int tenantId) throws AuthenticatorMgtException;
119+
120+
/**
121+
* Add a new system local authenticator configuration.
122+
*
123+
* @param authenticatorConfig Local application authenticator configuration.
124+
* @param tenantId Tenant Id.
125+
* @return Created LocalAuthenticatorConfig.
126+
*/
127+
LocalAuthenticatorConfig addSystemLocalAuthenticator(LocalAuthenticatorConfig authenticatorConfig, int tenantId) throws AuthenticatorMgtException;
128+
129+
/**
130+
* Update a system local authenticator configuration.
131+
*
132+
* @param authenticatorName Name of the authenticator
133+
* @param tenantId Tenant Id.
134+
* @return Updated LocalAuthenticatorConfig.
135+
*/
136+
boolean isExistingAuthenticatorNameDB(String authenticatorName, int tenantId) throws AuthenticatorMgtException;
118137
}

components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/impl/AuthenticatorManagementDAOImpl.java

+74-3
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,29 @@ private LocalAuthenticatorConfig getSystemLocalAuthenticatorByName(String authen
137137
throws TransactionException {
138138

139139
NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
140-
LocalAuthenticatorConfig config = jdbcTemplate.withTransaction(template ->
140+
LocalAuthenticatorConfigDaoModel configDaoModel = jdbcTemplate.withTransaction(template ->
141141
template.fetchSingleRecord(Query.GET_USER_DEFINED_LOCAL_AUTHENTICATOR_SQL,
142142
(resultSet, rowNumber) -> {
143143
LocalAuthenticatorConfig localAuthenticatorConfig = new LocalAuthenticatorConfig();
144144
localAuthenticatorConfig.setName(resultSet.getString(Column.NAME));
145145
localAuthenticatorConfig.setDisplayName(resultSet.getString(Column.DISPLAY_NAME));
146146
localAuthenticatorConfig.setAmrValue(resultSet.getString(Column.AMR_VALUE));
147147
localAuthenticatorConfig.setEnabled(resultSet.getString(Column.IS_ENABLED).equals(IS_TRUE_VALUE));
148-
return localAuthenticatorConfig;
148+
return new LocalAuthenticatorConfigDaoModel(resultSet.getInt(Column.ID), localAuthenticatorConfig);
149149
},
150150
statement -> {
151151
statement.setString(Column.NAME, authenticatorConfigName);
152152
statement.setInt(Column.TENANT_ID, tenantId);
153153
statement.setString(Column.DEFINED_BY, DefinedByType.SYSTEM.toString());
154+
statement.setString(Column.IDP_NAME, LOCAL_IDP_NAME);
154155
}));
155156

156-
if (config == null) {
157+
if (configDaoModel == null) {
157158
return null;
158159
}
160+
161+
LocalAuthenticatorConfig config = configDaoModel.getConfig();
162+
config.setProperties(getAuthenticatorProperties(configDaoModel.getEntryId(), tenantId));
159163
return config;
160164
}
161165

@@ -258,6 +262,53 @@ public boolean isExistingAuthenticatorName(String authenticatorName, int tenantI
258262
}
259263
}
260264

265+
@Override
266+
public LocalAuthenticatorConfig addSystemLocalAuthenticator(LocalAuthenticatorConfig authenticatorConfig, int tenantId) throws AuthenticatorMgtServerException {
267+
NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
268+
try {
269+
int authenticatorConfigID = jdbcTemplate.withTransaction(template ->
270+
template.executeInsert(Query.ADD_SYSTEM_LOCAL_AUTHENTICATOR_SQL,
271+
statement -> {
272+
statement.setString(Column.NAME, authenticatorConfig.getName());
273+
statement.setString(Column.DISPLAY_NAME, authenticatorConfig.getDisplayName());
274+
statement.setString(Column.DEFINED_BY, DefinedByType.SYSTEM.toString());
275+
statement.setString(Column.AMR_VALUE, authenticatorConfig.getAmrValue());
276+
statement.setString(Column.IS_ENABLED,
277+
authenticatorConfig.isEnabled() ? IS_TRUE_VALUE : IS_FALSE_VALUE);
278+
statement.setString(Column.AUTHENTICATION_TYPE, "IDENTIFICATION");
279+
statement.setString(Column.IDP_NAME, LOCAL_IDP_NAME);
280+
statement.setInt(Column.TENANT_ID, tenantId);
281+
}, null, true));
282+
283+
if (authenticatorConfigID == 0) {
284+
authenticatorConfigID = getAuthenticatorEntryId(authenticatorConfig.getName(), tenantId);
285+
}
286+
addAuthenticatorProperty(authenticatorConfigID, authenticatorConfig.getProperties(), tenantId);
287+
288+
return getSystemLocalAuthenticatorByName(authenticatorConfig.getName(), tenantId);
289+
} catch (TransactionException e) {
290+
throw buildServerException(AuthenticatorMgtError.ERROR_WHILE_ADDING_AUTHENTICATOR, e);
291+
}
292+
}
293+
294+
@Override
295+
public boolean isExistingAuthenticatorNameDB(String authenticatorName, int tenantId) throws AuthenticatorMgtException {
296+
NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
297+
try {
298+
ResultSet results = jdbcTemplate.withTransaction(template ->
299+
template.fetchSingleRecord(Query.IS_AUTHENTICATOR_EXISTS_BY_NAME_SQL,
300+
(resultSet, rowNumber) -> resultSet,
301+
statement -> {
302+
statement.setString(Column.NAME, authenticatorName);
303+
statement.setInt(Column.TENANT_ID, tenantId);
304+
}));
305+
return results != null;
306+
} catch (TransactionException e) {
307+
throw buildServerException(AuthenticatorMgtError.ERROR_WHILE_CHECKING_FOR_EXISTING_AUTHENTICATOR_BY_NAME, e,
308+
authenticatorName);
309+
}
310+
}
311+
261312
private UserDefinedLocalAuthenticatorConfig getUserDefinedLocalAuthenticatorByName(String authenticatorConfigName,
262313
int tenantId) throws TransactionException {
263314

@@ -375,4 +426,24 @@ public UserDefinedLocalAuthenticatorConfig getConfig() {
375426
return config;
376427
}
377428
}
429+
430+
private static class LocalAuthenticatorConfigDaoModel{
431+
432+
private final int entryId;
433+
private final LocalAuthenticatorConfig config;
434+
435+
436+
private LocalAuthenticatorConfigDaoModel(int entryId, LocalAuthenticatorConfig config){
437+
this.entryId = entryId;
438+
this.config = config;
439+
}
440+
441+
public int getEntryId() {
442+
return entryId;
443+
}
444+
445+
public LocalAuthenticatorConfig getConfig() {
446+
return config;
447+
}
448+
}
378449
}

components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/dao/impl/AuthenticatorManagementFacade.java

+23
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,29 @@ public boolean isExistingAuthenticatorName(String authenticatorName, int tenantI
240240
}
241241
}
242242

243+
@Override
244+
public LocalAuthenticatorConfig addSystemLocalAuthenticator(LocalAuthenticatorConfig authenticatorConfig, int tenantId) throws AuthenticatorMgtException {
245+
NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
246+
try {
247+
return jdbcTemplate.withTransaction(template -> dao.addSystemLocalAuthenticator(authenticatorConfig, tenantId));
248+
} catch (TransactionException e) {
249+
throw handleAuthenticatorMgtException(AuthenticatorMgtError.ERROR_WHILE_ADDING_AUTHENTICATOR, e,
250+
authenticatorConfig.getName());
251+
}
252+
}
253+
254+
@Override
255+
public boolean isExistingAuthenticatorNameDB(String authenticatorName, int tenantId) throws AuthenticatorMgtException {
256+
NamedJdbcTemplate jdbcTemplate = new NamedJdbcTemplate(IdentityDatabaseUtil.getDataSource());
257+
try {
258+
return jdbcTemplate.withTransaction(
259+
template -> dao.isExistingAuthenticatorNameDB(authenticatorName, tenantId));
260+
} catch (TransactionException e) {
261+
throw handleAuthenticatorMgtException(AuthenticatorMgtError
262+
.ERROR_WHILE_CHECKING_FOR_EXISTING_AUTHENTICATOR_BY_NAME, e, authenticatorName);
263+
}
264+
}
265+
243266
/**
244267
* Handle the authenticator management client exception.
245268
*

0 commit comments

Comments
 (0)