From cdad525b731920fd6cd0205a187a9f4a3967a315 Mon Sep 17 00:00:00 2001 From: sahandilshan Date: Mon, 6 Jan 2025 11:00:44 +0530 Subject: [PATCH] Modify LoginFlowAI Implementation --- .../mgt/ai/LoginFlowAIManagerImpl.java | 19 +++++++++++++------ .../mgt/ai/constant/LoginFlowAIConstants.java | 6 ++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ai/LoginFlowAIManagerImpl.java b/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ai/LoginFlowAIManagerImpl.java index 11d901c531c5..b446386fdfcf 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ai/LoginFlowAIManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ai/LoginFlowAIManagerImpl.java @@ -36,7 +36,12 @@ import java.util.Map; import static org.wso2.carbon.ai.service.mgt.util.AIHttpClientUtil.executeRequest; +import static org.wso2.carbon.identity.application.mgt.ai.constant.LoginFlowAIConstants.AUTHENTICATORS_PROPERTY; import static org.wso2.carbon.identity.application.mgt.ai.constant.LoginFlowAIConstants.ErrorMessages.SERVER_ERROR_WHILE_CONNECTING_TO_LOGINFLOW_AI_SERVICE; +import static org.wso2.carbon.identity.application.mgt.ai.constant.LoginFlowAIConstants.OPERATION_ID_PROPERTY; +import static org.wso2.carbon.identity.application.mgt.ai.constant.LoginFlowAIConstants.USER_CLAIM_PROPERTY; +import static org.wso2.carbon.identity.application.mgt.ai.constant.LoginFlowAIConstants.USER_QUERY_PROPERTY; +import static org.wso2.carbon.registry.core.RegistryConstants.PATH_SEPARATOR; /** * Implementation of the LoginFlowAIManager interface to communicate with the LoginFlowAI service. @@ -71,16 +76,16 @@ public String generateAuthenticationSequence(String userQuery, JSONArray userCla ObjectMapper objectMapper = new ObjectMapper(); Map requestBody = new HashMap<>(); - requestBody.put("user_query", userQuery); + requestBody.put(USER_QUERY_PROPERTY, userQuery); try { // Convert JSONArray to List. List userClaimsList = objectMapper.readValue(userClaims.toString(), List.class); - requestBody.put("user_claims", userClaimsList); + requestBody.put(USER_CLAIM_PROPERTY, userClaimsList); // Convert JSONObject to Map. Map authenticatorsMap = objectMapper.readValue(availableAuthenticators.toString(), Map.class); - requestBody.put("available_authenticators", authenticatorsMap); + requestBody.put(AUTHENTICATORS_PROPERTY, authenticatorsMap); } catch (JsonSyntaxException | IOException e) { throw new AIClientException("Error occurred while parsing the user claims or available " + "authenticators.", SERVER_ERROR_WHILE_CONNECTING_TO_LOGINFLOW_AI_SERVICE.getCode(), e); @@ -88,7 +93,7 @@ public String generateAuthenticationSequence(String userQuery, JSONArray userCla Map stringObjectMap = executeRequest(LOGINFLOW_AI_ENDPOINT, LOGINFLOW_AI_GENERATE_PATH, HttpPost.class, requestBody); - return (String) stringObjectMap.get("operation_id"); + return (String) stringObjectMap.get(OPERATION_ID_PROPERTY); } /** @@ -104,7 +109,8 @@ public String generateAuthenticationSequence(String userQuery, JSONArray userCla public Map getAuthenticationSequenceGenerationStatus(String operationId) throws AIServerException, AIClientException { - return executeRequest(LOGINFLOW_AI_ENDPOINT, LOGINFLOW_AI_STATUS_PATH + "/" + operationId, HttpGet.class, null); + return executeRequest(LOGINFLOW_AI_ENDPOINT, LOGINFLOW_AI_STATUS_PATH + PATH_SEPARATOR + operationId, + HttpGet.class, null); } /** @@ -120,6 +126,7 @@ public Map getAuthenticationSequenceGenerationStatus(String oper public Map getAuthenticationSequenceGenerationResult(String operationId) throws AIServerException, AIClientException { - return executeRequest(LOGINFLOW_AI_ENDPOINT, LOGINFLOW_AI_RESULT_PATH + "/" + operationId, HttpGet.class, null); + return executeRequest(LOGINFLOW_AI_ENDPOINT, LOGINFLOW_AI_RESULT_PATH + PATH_SEPARATOR + operationId, + HttpGet.class, null); } } diff --git a/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ai/constant/LoginFlowAIConstants.java b/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ai/constant/LoginFlowAIConstants.java index 1da6ca348d5e..5e164d0f86fd 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ai/constant/LoginFlowAIConstants.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.mgt/src/main/java/org/wso2/carbon/identity/application/mgt/ai/constant/LoginFlowAIConstants.java @@ -23,6 +23,12 @@ */ public class LoginFlowAIConstants { + public static final String OPERATION_ID_PROPERTY = "operation_id"; + public static final String USER_CLAIM_PROPERTY = "user_claims"; + public static final String USER_QUERY_PROPERTY = "user_query"; + public static final String AUTHENTICATORS_PROPERTY = "available_authenticators"; + + /** * Enums for error messages. */