|
| 1 | +/** |
| 2 | + * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import AsgardeoAPIError from '../../errors/AsgardeoAPIError'; |
| 20 | +import {EmbeddedFlowExecuteRequestConfig as EmbeddedFlowExecuteRequestConfigV2} from '../../models/v2/embedded-flow-v2'; |
| 21 | +import {EmbeddedRecoveryFlowResponse} from '../../models/v2/embedded-recovery-flow-v2'; |
| 22 | + |
| 23 | +/** |
| 24 | + * Executes an embedded recovery flow by sending a request to the flow execution endpoint. |
| 25 | + * |
| 26 | + * This function handles password-recovery and account-recovery flows driven by the |
| 27 | + * Asgardeo server. The server returns UI components for each step (e.g. username |
| 28 | + * collection, OTP verification, password reset) and this function forwards the |
| 29 | + * user's responses back to the server. |
| 30 | + * |
| 31 | + * @param requestConfig - Request configuration containing URL, payload, and optional headers. |
| 32 | + * @returns A promise that resolves with the flow execution response. |
| 33 | + * @throws AsgardeoAPIError when the request fails or a payload is missing. |
| 34 | + * |
| 35 | + * @example |
| 36 | + * ```typescript |
| 37 | + * // Initiate recovery flow |
| 38 | + * const response = await executeEmbeddedRecoveryFlowV2({ |
| 39 | + * baseUrl: 'https://api.asgardeo.io/t/myorg', |
| 40 | + * payload: { |
| 41 | + * flowType: 'RECOVERY', |
| 42 | + * applicationId: 'my-app-id', |
| 43 | + * }, |
| 44 | + * }); |
| 45 | + * |
| 46 | + * // Continue recovery flow with user input |
| 47 | + * const nextResponse = await executeEmbeddedRecoveryFlowV2({ |
| 48 | + * baseUrl: 'https://api.asgardeo.io/t/myorg', |
| 49 | + * payload: { |
| 50 | + * executionId: response.executionId, |
| 51 | + * action: 'submit', |
| 52 | + * inputs: { username: 'user@example.com' }, |
| 53 | + * challengeToken: response.challengeToken, |
| 54 | + * }, |
| 55 | + * }); |
| 56 | + * ``` |
| 57 | + */ |
| 58 | +const executeEmbeddedRecoveryFlowV2 = async ({ |
| 59 | + url, |
| 60 | + baseUrl, |
| 61 | + payload, |
| 62 | + ...requestConfig |
| 63 | +}: EmbeddedFlowExecuteRequestConfigV2): Promise<EmbeddedRecoveryFlowResponse> => { |
| 64 | + if (!payload) { |
| 65 | + throw new AsgardeoAPIError( |
| 66 | + 'Recovery payload is required', |
| 67 | + 'executeEmbeddedRecoveryFlow-ValidationError-002', |
| 68 | + 'javascript', |
| 69 | + 400, |
| 70 | + 'If a recovery payload is not provided, the request cannot be constructed correctly.', |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + const endpoint: string = url ?? `${baseUrl}/flow/execute`; |
| 75 | + |
| 76 | + // Strip any user-provided 'verbose' parameter as it should only be used internally |
| 77 | + const cleanPayload: typeof payload = |
| 78 | + typeof payload === 'object' && payload !== null |
| 79 | + ? Object.fromEntries(Object.entries(payload).filter(([key]: [string, unknown]) => key !== 'verbose')) |
| 80 | + : payload; |
| 81 | + |
| 82 | + // `verbose: true` is required to get the `meta` field in the response that includes component details. |
| 83 | + // Add verbose:true if: |
| 84 | + // 1. payload contains only applicationId and flowType (initial request) |
| 85 | + // 2. payload contains only executionId (flow continuation without inputs) |
| 86 | + const hasOnlyAppIdAndFlowType: boolean = |
| 87 | + typeof cleanPayload === 'object' && |
| 88 | + cleanPayload !== null && |
| 89 | + 'applicationId' in cleanPayload && |
| 90 | + 'flowType' in cleanPayload && |
| 91 | + Object.keys(cleanPayload).length === 2; |
| 92 | + const hasOnlyFlowId: boolean = |
| 93 | + typeof cleanPayload === 'object' && |
| 94 | + cleanPayload !== null && |
| 95 | + 'executionId' in cleanPayload && |
| 96 | + Object.keys(cleanPayload).length === 1; |
| 97 | + |
| 98 | + const requestPayload: Record<string, unknown> = |
| 99 | + hasOnlyAppIdAndFlowType || hasOnlyFlowId |
| 100 | + ? { |
| 101 | + ...cleanPayload, |
| 102 | + verbose: true, |
| 103 | + } |
| 104 | + : cleanPayload; |
| 105 | + |
| 106 | + const response: Response = await fetch(endpoint, { |
| 107 | + ...requestConfig, |
| 108 | + body: JSON.stringify(requestPayload), |
| 109 | + headers: { |
| 110 | + Accept: 'application/json', |
| 111 | + 'Content-Type': 'application/json', |
| 112 | + ...requestConfig.headers, |
| 113 | + }, |
| 114 | + method: requestConfig.method || 'POST', |
| 115 | + }); |
| 116 | + |
| 117 | + if (!response.ok) { |
| 118 | + const errorText: string = await response.text(); |
| 119 | + |
| 120 | + throw new AsgardeoAPIError( |
| 121 | + `Recovery request failed: ${errorText}`, |
| 122 | + 'executeEmbeddedRecoveryFlow-ResponseError-001', |
| 123 | + 'javascript', |
| 124 | + response.status, |
| 125 | + response.statusText, |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + const flowResponse: EmbeddedRecoveryFlowResponse = await response.json(); |
| 130 | + |
| 131 | + return flowResponse; |
| 132 | +}; |
| 133 | + |
| 134 | +export default executeEmbeddedRecoveryFlowV2; |
0 commit comments