Skip to content

Commit

Permalink
Modify LoginFlowAI Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sahandilshan committed Jan 6, 2025
1 parent 998b94f commit cdad525
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -71,24 +76,24 @@ public String generateAuthenticationSequence(String userQuery, JSONArray userCla

ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("user_query", userQuery);
requestBody.put(USER_QUERY_PROPERTY, userQuery);
try {
// Convert JSONArray to List.
List<Object> userClaimsList = objectMapper.readValue(userClaims.toString(), List.class);
requestBody.put("user_claims", userClaimsList);
requestBody.put(USER_CLAIM_PROPERTY, userClaimsList);

// Convert JSONObject to Map.
Map<String, Object> 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);
}

Map<String, Object> 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);
}

/**
Expand All @@ -104,7 +109,8 @@ public String generateAuthenticationSequence(String userQuery, JSONArray userCla
public Map<String, Object> 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);
}

/**
Expand All @@ -120,6 +126,7 @@ public Map<String, Object> getAuthenticationSequenceGenerationStatus(String oper
public Map<String, Object> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit cdad525

Please sign in to comment.