From 942975cd43d5abc6da230e4c218c9af87cbc474b Mon Sep 17 00:00:00 2001 From: Thisara-Welmilla Date: Mon, 2 Dec 2024 15:17:02 +0530 Subject: [PATCH] Update APIs to support custom authentication management. --- .../ServerAuthenticatorManagementService.java | 17 ++++++++++++----- .../LocalAuthenticatorConfigBuilderFactory.java | 13 +++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java index 1bb986ba81..988745d9db 100644 --- a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java +++ b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/core/ServerAuthenticatorManagementService.java @@ -115,7 +115,13 @@ public List getAuthenticators(String filter, Integer limit, Integ LocalAuthenticatorConfig[] localAuthenticatorConfigs = AuthenticatorsServiceHolder.getInstance() .getApplicationManagementService().getAllLocalAuthenticators(ContextLoader .getTenantDomainFromContext()); - + List userDefinedLocalAuthConfigs = + AuthenticatorsServiceHolder.getInstance().getApplicationAuthenticatorService() + .getAllUserDefinedLocalAuthenticators(ContextLoader.getTenantDomainFromContext()); + if (CollectionUtils.isNotEmpty(userDefinedLocalAuthConfigs)) { + localAuthenticatorConfigs = (LocalAuthenticatorConfig[]) ArrayUtils.addAll(localAuthenticatorConfigs, + userDefinedLocalAuthConfigs.toArray(new LocalAuthenticatorConfig[0])); + } int localAuthenticatorsCount = localAuthenticatorConfigs.length; RequestPathAuthenticatorConfig[] requestPathAuthenticatorConfigs = new RequestPathAuthenticatorConfig[0]; @@ -155,6 +161,8 @@ public List getAuthenticators(String filter, Integer limit, Integ null); } catch (IdentityProviderManagementException e) { throw handleIdPException(e, Constants.ErrorMessage.ERROR_CODE_ERROR_LISTING_IDPS, null); + } catch (AuthenticatorMgtException e) { + throw handleAuthenticatorException(e); } } @@ -207,8 +215,7 @@ public ConnectedApps getConnectedAppsOfLocalAuthenticator(String authenticatorId * @param config The user defined local authenticator update request. * @return The created authenticator. */ - public Authenticator addUserDefinedLocalAuthenticator( - UserDefinedLocalAuthenticatorCreation config) { + public Authenticator addUserDefinedLocalAuthenticator(UserDefinedLocalAuthenticatorCreation config) { try { UserDefinedLocalAuthenticatorConfig createdConfig = AuthenticatorsServiceHolder.getInstance() @@ -1002,7 +1009,7 @@ private APIError handleAuthenticatorException(AuthenticatorMgtException e) { errorCode : Constants.AUTHENTICATOR_ERROR_PREFIX + errorCode; errorResponse.setCode(errorCode); } - errorResponse.setDescription(e.getMessage()); + errorResponse.setDescription(e.getDescription()); status = Response.Status.BAD_REQUEST; } else if (e instanceof AuthenticatorMgtServerException) { if (e.getErrorCode() != null) { @@ -1012,7 +1019,7 @@ private APIError handleAuthenticatorException(AuthenticatorMgtException e) { errorCode : Constants.AUTHENTICATOR_ERROR_PREFIX + errorCode; errorResponse.setCode(errorCode); } - errorResponse.setDescription(e.getMessage()); + errorResponse.setDescription(e.getDescription()); status = Response.Status.INTERNAL_SERVER_ERROR; } else { status = Response.Status.INTERNAL_SERVER_ERROR; diff --git a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/impl/LocalAuthenticatorConfigBuilderFactory.java b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/impl/LocalAuthenticatorConfigBuilderFactory.java index f3c27fcb67..bae00ad0af 100644 --- a/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/impl/LocalAuthenticatorConfigBuilderFactory.java +++ b/components/org.wso2.carbon.identity.api.server.authenticators/org.wso2.carbon.identity.api.server.authenticators.v1/src/main/java/org/wso2/carbon/identity/api/server/authenticators/v1/impl/LocalAuthenticatorConfigBuilderFactory.java @@ -77,11 +77,12 @@ public static Authenticator build(UserDefinedLocalAuthenticatorConfig config) { public static UserDefinedLocalAuthenticatorConfig build(UserDefinedLocalAuthenticatorCreation config) throws AuthenticatorMgtClientException { + String authenticationType = AuthenticatorPropertyConstants.AuthenticationType.IDENTIFICATION.toString(); + if (config.getAuthenticationType() != null) { + authenticationType = config.getAuthenticationType().toString(); + } UserDefinedLocalAuthenticatorConfig authConfig = new UserDefinedLocalAuthenticatorConfig( - AuthenticatorPropertyConstants.AuthenticationType.valueOf( - config.getAuthenticationType().toString())); - authConfig.setAuthenticationType(AuthenticatorPropertyConstants.AuthenticationType.valueOf( - config.getAuthenticationType().toString())); + AuthenticatorPropertyConstants.AuthenticationType.valueOf(authenticationType)); authConfig.setName(config.getName()); authConfig.setDisplayName(config.getDisplayName()); authConfig.setEnabled(config.getIsEnabled()); @@ -123,9 +124,9 @@ private static UserDefinedAuthenticatorEndpointConfig buildEndpointConfig(Endpoi .entrySet().stream().collect(Collectors.toMap( Map.Entry::getKey, entry -> entry.getValue().toString()))); return endpointConfigBuilder.build(); - } catch (NoSuchElementException e) { + } catch (NoSuchElementException | IllegalArgumentException e) { Constants.ErrorMessage error = ERROR_CODE_INVALID_ENDPOINT_CONFIG; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), error.getMessage()); + throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), e.getMessage()); } }