|
| 1 | +/** |
| 2 | + * Copyright (c) 2025 Ping Identity Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * This software may be modified and distributed under the terms |
| 5 | + * of the MIT license. See the LICENSE file for details. |
| 6 | + */ |
| 7 | +import { Console, Effect, pipe } from 'effect'; |
| 8 | +import { MockApi } from '../spec.js'; |
| 9 | +import { |
| 10 | + HttpApiBuilder, |
| 11 | + HttpApiError, |
| 12 | + HttpBody, |
| 13 | + HttpServerRequest, |
| 14 | + HttpServerResponse, |
| 15 | +} from '@effect/platform'; |
| 16 | +import { responseMap } from '../responses/index.js'; |
| 17 | +import { validator } from '../helpers/match.js'; |
| 18 | +import { returnSuccessResponseRedirect } from '../responses/return-success-redirect.js'; |
| 19 | + |
| 20 | +const CapabilitiesHandlerMock = HttpApiBuilder.group(MockApi, 'Capabilities', (handlers) => |
| 21 | + handlers.handle('capabilities', ({ urlParams, payload }) => |
| 22 | + Effect.gen(function* () { |
| 23 | + /** |
| 24 | + * We expect an acr_value query parameter to be present in the request. |
| 25 | + * If it is not present, we return a 404 Not Found error. |
| 26 | + */ |
| 27 | + const acr_value = urlParams?.acr_values ?? ''; |
| 28 | + console.log('acr_value', acr_value); |
| 29 | + |
| 30 | + if (!acr_value) { |
| 31 | + return yield* Effect.fail(new HttpApiError.NotFound()); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * We need a step index cookie to determine which step of the authentication process we are on. |
| 36 | + * If the cookie is not present, we return a 404 Not Found error. |
| 37 | + */ |
| 38 | + |
| 39 | + const req = yield* HttpServerRequest.HttpServerRequest; |
| 40 | + |
| 41 | + const stepIndexCookie = req.cookies['stepIndex']; |
| 42 | + console.log(req.cookies); |
| 43 | + |
| 44 | + /** |
| 45 | + * If we are here with no step index that means we can't continue through a flow. |
| 46 | + * We should error |
| 47 | + */ |
| 48 | + if (!stepIndexCookie) { |
| 49 | + console.log('no step index'); |
| 50 | + return yield* Effect.fail(new HttpApiError.NotFound()); |
| 51 | + } |
| 52 | + |
| 53 | + const stepIndex = parseInt(stepIndexCookie); |
| 54 | + |
| 55 | + /** |
| 56 | + * If we have no step index, we should error or if its an invalid number |
| 57 | + */ |
| 58 | + |
| 59 | + if (isNaN(stepIndex) || stepIndex < 0) { |
| 60 | + return yield* Effect.fail(new HttpApiError.NotFound()); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Match the body against validators now |
| 65 | + * if the body has no match, we are defaulting to a successful response. |
| 66 | + */ |
| 67 | + const result = yield* validator(payload); |
| 68 | + |
| 69 | + if (result === false) { |
| 70 | + return yield* Effect.fail(new HttpApiError.Unauthorized()); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * We use the step index to find the next step in the response map. |
| 75 | + * If the step index is out of bounds, we return a 404 Not Found error. |
| 76 | + */ |
| 77 | + const steps = responseMap[acr_value]; |
| 78 | + |
| 79 | + /** |
| 80 | + * This may not be the best way to write this. |
| 81 | + * An alternative option would be for us to include the success response we want to return, |
| 82 | + * in the response map. |
| 83 | + * |
| 84 | + * then we can check if we are at the last step. if we are we write the cookie |
| 85 | + * and then we return the success response (last item in array) |
| 86 | + * |
| 87 | + * for now, this returns a default success response and writes cookies. |
| 88 | + */ |
| 89 | + if (stepIndex + 1 >= steps.length) { |
| 90 | + /** |
| 91 | + * we need to return a success because we have not failed yet, |
| 92 | + * and we have no more steps to process. |
| 93 | + */ |
| 94 | + const body = yield* HttpBody.json(returnSuccessResponseRedirect).pipe( |
| 95 | + Effect.tap(Console.log(`here stepIndex: ${stepIndex}`)), |
| 96 | + /** |
| 97 | + * Decide on a better way to handle this error possibiltiy |
| 98 | + */ |
| 99 | + Effect.catchTag('HttpBodyError', () => |
| 100 | + Effect.fail( |
| 101 | + new HttpApiError.HttpApiDecodeError({ |
| 102 | + message: 'Failed to encode body', |
| 103 | + issues: [], |
| 104 | + }), |
| 105 | + ), |
| 106 | + ), |
| 107 | + ); |
| 108 | + return pipe( |
| 109 | + HttpServerResponse.json(body), |
| 110 | + HttpServerResponse.setCookie('ST', 'MockApiCookie123'), |
| 111 | + HttpServerResponse.setCookie( |
| 112 | + 'interactionId', |
| 113 | + returnSuccessResponseRedirect.interactionId, |
| 114 | + { |
| 115 | + httpOnly: true, |
| 116 | + secure: true, |
| 117 | + sameSite: 'strict', |
| 118 | + }, |
| 119 | + ), |
| 120 | + HttpServerResponse.setCookie( |
| 121 | + 'interactionToken', |
| 122 | + returnSuccessResponseRedirect.interactionToken, |
| 123 | + { |
| 124 | + httpOnly: true, |
| 125 | + secure: true, |
| 126 | + sameSite: 'strict', |
| 127 | + }, |
| 128 | + ), |
| 129 | + HttpServerResponse.removeCookie('stepIndex'), |
| 130 | + HttpServerResponse.setStatus(200), |
| 131 | + HttpServerResponse.setHeader('Content-Type', 'application/json'), |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * The stepIndex middleware is used to auto-increment the step index |
| 137 | + * based on the request type. If the step index is out of bounds, |
| 138 | + * we return a 404 Not Found error. so we won't increment it, but we check for the next step |
| 139 | + * in the flow. |
| 140 | + */ |
| 141 | + const nextStep = steps[stepIndex + 1]; |
| 142 | + |
| 143 | + return nextStep; |
| 144 | + }).pipe(Effect.withSpan('Capabilities Handler Mock')), |
| 145 | + ), |
| 146 | +); |
| 147 | + |
| 148 | +export { CapabilitiesHandlerMock }; |
0 commit comments