Skip to content

Commit

Permalink
Update loginflow-AI API contract
Browse files Browse the repository at this point in the history
  • Loading branch information
sahandilshan committed Jan 17, 2025
1 parent 9751286 commit ddf97f9
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ public enum ErrorMessage {
"Error occurred while getting the Login Flow AI result status."),
ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT("65601",
"Error occurred while getting the Login Flow AI result.",
"Error occurred while getting the Login Flow AI result.");
"Error occurred while getting the Login Flow AI result."),
ERROR_WHILE_CONVERTING_LOGINFLOW_AI_SERVER_RESPONSE("65602",
"Error occurred while converting the AI server response.",
"Could not convert the AI server response to a valid response.");

private final String code;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023-2024, WSO2 LLC. (http://www.wso2.com).
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
Expand Down Expand Up @@ -419,7 +419,7 @@ public Response exportApplicationAsFile(@ApiParam(value = "ID of the application
@Path("/loginflow/generate")
@Consumes({ "application/json" })
@Produces({ "application/json" })
@ApiOperation(value = "Initiate login flow generation", notes = "This API provides the capability to initiate the generation of a login flow. <br> <b>Permission required:</b> * /permission/admin/manage/identity/applicationmgt/update <br> <b>Scope required:</b> * internal_application_mgt_update ", response = LoginFlowGenerateResponse.class, authorizations = {
@ApiOperation(value = "Initiate login flow generation", notes = "This API provides the capability to initiate the generation of a login flow. <br> <b>Scope required:</b> * internal_application_mgt_update ", response = LoginFlowGenerateResponse.class, authorizations = {
@Authorization(value = "BasicAuth"),
@Authorization(value = "OAuth2", scopes = {

Expand Down Expand Up @@ -754,10 +754,10 @@ public Response getInboundSAMLConfiguration(@ApiParam(value = "ID of the applica

@Valid
@GET
@Path("/loginflow/result/{operation_id}")
@Path("/loginflow/result/{operationId}")

@Produces({ "application/json" })
@ApiOperation(value = "Get the final login flow result", notes = "", response = LoginFlowResultResponse.class, authorizations = {
@ApiOperation(value = "Get the final login flow result", notes = "This API provides the capability to retrieve the final login flow result. <br> <b>Scope required:</b> * internal_application_mgt_update ", response = LoginFlowResultResponse.class, authorizations = {
@Authorization(value = "BasicAuth"),
@Authorization(value = "OAuth2", scopes = {

Expand All @@ -766,19 +766,21 @@ public Response getInboundSAMLConfiguration(@ApiParam(value = "ID of the applica
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK", response = LoginFlowResultResponse.class),
@ApiResponse(code = 400, message = "Bad Request", response = Error.class),
@ApiResponse(code = 401, message = "Unauthorized", response = Error.class),
@ApiResponse(code = 403, message = "Forbidden", response = Error.class),
@ApiResponse(code = 404, message = "Not Found", response = Error.class)
})
public Response getLoginFlowResult(@ApiParam(value = "",required=true) @PathParam("operation_id") String operationId) {
public Response getLoginFlowResult(@ApiParam(value = "",required=true) @PathParam("operationId") String operationId) {

return delegate.getLoginFlowResult(operationId );
}

@Valid
@GET
@Path("/loginflow/status/{operation_id}")
@Path("/loginflow/status/{operationId}")

@Produces({ "application/json" })
@ApiOperation(value = "Get the status of the login flow generation process", notes = "", response = LoginFlowStatusResponse.class, authorizations = {
@ApiOperation(value = "Get the status of the login flow generation process", notes = "This API provides the capability to retrieve the status of the login flow generation process. <br> <b>Scope required:</b> * internal_application_mgt_update ", response = LoginFlowStatusResponse.class, authorizations = {
@Authorization(value = "BasicAuth"),
@Authorization(value = "OAuth2", scopes = {

Expand All @@ -787,9 +789,11 @@ public Response getLoginFlowResult(@ApiParam(value = "",required=true) @PathPara
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK", response = LoginFlowStatusResponse.class),
@ApiResponse(code = 400, message = "Bad Request", response = Error.class),
@ApiResponse(code = 401, message = "Unauthorized", response = Error.class),
@ApiResponse(code = 403, message = "Forbidden", response = Error.class),
@ApiResponse(code = 404, message = "Not Found", response = Error.class)
})
public Response getLoginFlowStatus(@ApiParam(value = "",required=true) @PathParam("operation_id") String operationId) {
public Response getLoginFlowStatus(@ApiParam(value = "",required=true) @PathParam("operationId") String operationId) {

return delegate.getLoginFlowStatus(operationId );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public LoginFlowGenerateRequest availableAuthenticators(Map<String, List<Map<Str
}

@ApiModelProperty(value = "")
@JsonProperty("available_authenticators")
@JsonProperty("availableAuthenticators")
@Valid
public Map<String, List<Map<String, Object>>> getAvailableAuthenticators() {
return availableAuthenticators;
Expand All @@ -78,7 +78,7 @@ public LoginFlowGenerateRequest userClaims(List<Map<String, Object>> userClaims)
}

@ApiModelProperty(value = "")
@JsonProperty("user_claims")
@JsonProperty("userClaims")
@Valid
public List<Map<String, Object>> getUserClaims() {
return userClaims;
Expand All @@ -104,7 +104,7 @@ public LoginFlowGenerateRequest userQuery(String userQuery) {
}

@ApiModelProperty(value = "")
@JsonProperty("user_query")
@JsonProperty("userQuery")
@Valid
public String getUserQuery() {
return userQuery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public LoginFlowGenerateResponse operationId(String operationId) {
}

@ApiModelProperty(value = "")
@JsonProperty("operation_id")
@JsonProperty("operationId")
@Valid
public String getOperationId() {
return operationId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public LoginFlowStatusResponse operationId(String operationId) {
}

@ApiModelProperty(value = "")
@JsonProperty("operation_id")
@JsonProperty("operationId")
@Valid
public String getOperationId() {
return operationId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static org.wso2.carbon.identity.api.server.application.management.common.ApplicationManagementConstants.DESCRIPTION_KEY;
import static org.wso2.carbon.identity.api.server.application.management.common.ApplicationManagementConstants.ErrorMessage.ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT;
import static org.wso2.carbon.identity.api.server.application.management.common.ApplicationManagementConstants.ErrorMessage.ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT_STATUS;
import static org.wso2.carbon.identity.api.server.application.management.common.ApplicationManagementConstants.ErrorMessage.ERROR_WHILE_CONVERTING_LOGINFLOW_AI_SERVER_RESPONSE;

/**
* Service class for login flow AI related operations.
Expand All @@ -56,7 +57,8 @@ public class LoginFlowAIService {
private static final Log log = LogFactory.getLog(LoginFlowAIService.class);

/**
* Generate authentication sequence.
* Generate authentication sequence using login flow AI. Here we generate the authentication sequence based on the
* available user claims metadata and authenticators.
*
* @param loginFlowGenerateRequest LoginFlowGenerateRequest.
* @return LoginFlowGenerateResponse.
Expand Down Expand Up @@ -116,7 +118,7 @@ public LoginFlowResultResponse getLoginFlowAIGenerationResult(String operationId
LoginFlowResultResponse response = new LoginFlowResultResponse();
Map<String, Object> generationResultMap = (Map<String, Object>) generationResult;
response.setStatus(getStatusFromResult(generationResultMap));
if (!((Map<?, ?>) generationResult).containsKey(AI_RESPONSE_DATA_KEY)) {
if (!generationResultMap.containsKey(AI_RESPONSE_DATA_KEY)) {
throw new AIServerException(ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT_STATUS.getMessage(),
ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT_STATUS.getCode());
}
Expand Down Expand Up @@ -158,12 +160,11 @@ private StatusEnum getStatusFromResult(Map<String, Object> resultMap)

if (resultMap.containsKey(AI_RESPONSE_STATUS_KEY)) {
String status = (String) resultMap.get(AI_RESPONSE_STATUS_KEY);
if ("IN_PROGRESS".equals(status)) {
return StatusEnum.IN_PROGRESS;
} else if ("COMPLETED".equals(status)) {
return StatusEnum.COMPLETED;
} else if ("FAILED".equals(status)) {
return StatusEnum.FAILED;
try {
return StatusEnum.fromValue(status);
} catch (IllegalArgumentException e) {
throw new AIServerException(ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT.getMessage(),
ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT.getCode());
}
}
throw new AIServerException(ERROR_CODE_ERROR_GETTING_LOGINFLOW_AI_RESULT.getMessage(),
Expand All @@ -172,6 +173,7 @@ private StatusEnum getStatusFromResult(Map<String, Object> resultMap)

private APIError handleClientException(AIClientException error) {

log.debug("Client error occurred while invoking Loginflow-ai service.", error);
ErrorResponse.Builder errorResponseBuilder = new ErrorResponse.Builder()
.withCode(error.getErrorCode())
.withMessage(error.getMessage());
Expand All @@ -185,6 +187,7 @@ private APIError handleClientException(AIClientException error) {

private APIError handleServerException(AIServerException error) {

log.error("Server error occurred while invoking Loginflow-ai service.", error);
ErrorResponse.Builder errorResponseBuilder = new ErrorResponse.Builder()
.withCode(error.getErrorCode())
.withMessage(error.getMessage());
Expand All @@ -196,7 +199,7 @@ private APIError handleServerException(AIServerException error) {
return new APIError(Response.Status.INTERNAL_SERVER_ERROR, errorResponseBuilder.build());
}

private static Map<String, Object> convertObjectToMap(Object object) {
private static Map<String, Object> convertObjectToMap(Object object) throws AIServerException {

if (object instanceof Map) {
Map<String, Object> map = new HashMap<>();
Expand All @@ -216,11 +219,11 @@ private static Map<String, Object> convertObjectToMap(Object object) {
}
return map;
}
log.warn("Object is not an instance of Map. Returning an empty map.");
return new HashMap<>();
throw new AIServerException(ERROR_WHILE_CONVERTING_LOGINFLOW_AI_SERVER_RESPONSE.getMessage(),
ERROR_WHILE_CONVERTING_LOGINFLOW_AI_SERVER_RESPONSE.getCode());
}

private static Object[] convertListToArray(List<?> list) {
private static Object[] convertListToArray(List<?> list) throws AIServerException {

Object[] array = new Object[list.size()];
for (int i = 0; i < list.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2624,9 +2624,6 @@ paths:
description: >
This API provides the capability to initiate the generation of a login flow.
<br>
<b>Permission required:</b>
* /permission/admin/manage/identity/applicationmgt/update
<br>
<b>Scope required:</b>
* internal_application_mgt_update
operationId: generateLoginFlow
Expand Down Expand Up @@ -2666,14 +2663,19 @@ paths:
schema:
$ref: '#/components/schemas/Error'

/applications/loginflow/status/{operation_id}:
/applications/loginflow/status/{operationId}:
get:
tags:
- LoginFlow
summary: Get the status of the login flow generation process
description: >
This API provides the capability to retrieve the status of the login flow generation process.
<br>
<b>Scope required:</b>
* internal_application_mgt_update
operationId: getLoginFlowStatus
parameters:
- name: operation_id
- name: operationId
in: path
required: true
schema:
Expand All @@ -2691,21 +2693,38 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'403':
description: Forbidden
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'404':
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/Error'

/applications/loginflow/result/{operation_id}:
/applications/loginflow/result/{operationId}:
get:
tags:
- LoginFlow
summary: Get the final login flow result
description: >
This API provides the capability to retrieve the final login flow result.
<br>
<b>Scope required:</b>
* internal_application_mgt_update
operationId: getLoginFlowResult
parameters:
- name: operation_id
- name: operationId
in: path
required: true
schema:
Expand All @@ -2723,6 +2742,18 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Error'
'401':
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'403':
description: Forbidden
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
'404':
description: Not Found
content:
Expand Down Expand Up @@ -4461,25 +4492,25 @@ components:
LoginFlowGenerateRequest:
type: object
properties:
available_authenticators:
availableAuthenticators:
type: object
additionalProperties:
type: array
items:
type: object
additionalProperties: true
user_claims:
userClaims:
type: array
items:
type: object
additionalProperties: true
user_query:
userQuery:
type: string

LoginFlowGenerateResponse:
type: object
properties:
operation_id:
operationId:
type: string

StatusEnum:
Expand All @@ -4496,7 +4527,7 @@ components:
LoginFlowStatusResponse:
type: object
properties:
operation_id:
operationId:
type: string
status:
type: object
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@
<maven.buildnumber.plugin.version>1.4</maven.buildnumber.plugin.version>
<org.apache.felix.annotations.version>1.2.4</org.apache.felix.annotations.version>
<identity.governance.version>1.11.21</identity.governance.version>
<carbon.identity.framework.version>7.7.85</carbon.identity.framework.version>
<carbon.identity.framework.version>7.7.93</carbon.identity.framework.version>
<maven.findbugsplugin.version>3.0.5</maven.findbugsplugin.version>
<findsecbugs-plugin.version>1.12.0</findsecbugs-plugin.version>
<maven.checkstyleplugin.excludes>**/gen/**/*</maven.checkstyleplugin.excludes>
Expand Down

0 comments on commit ddf97f9

Please sign in to comment.