|
1 | 1 | import { iFrameManager } from '@forgerock/iframe-manager'; |
2 | 2 | import { createAuthorizeUrl, GetAuthorizationUrlOptions } from '@forgerock/sdk-oidc'; |
| 3 | +import { Micro } from 'effect'; |
3 | 4 |
|
4 | 5 | import { createAuthorizeOptions, handleError, handleResponse } from './authorize.request.utils.js'; |
5 | 6 |
|
@@ -28,32 +29,51 @@ export async function authorize( |
28 | 29 | * We need to make a post (or a get) request and both are supported by |
29 | 30 | * PingOne. |
30 | 31 | */ |
31 | | - const authorizeUrl = await createAuthorizeUrl(authorizePath, { |
32 | | - ...optionsWithDefaults, |
33 | | - prompt: 'none', |
34 | | - responseMode: 'pi.flow', |
35 | | - }); |
36 | | - const res = await fetch(authorizeUrl, { |
37 | | - method: 'POST', |
38 | | - credentials: 'include', |
39 | | - }); |
| 32 | + const authorizeUrlMicro = Micro.promise(() => |
| 33 | + createAuthorizeUrl(authorizePath, { |
| 34 | + ...optionsWithDefaults, |
| 35 | + prompt: 'none', |
| 36 | + responseMode: 'pi.flow', |
| 37 | + }), |
| 38 | + ); |
40 | 39 |
|
41 | | - response = await res.json(); |
| 40 | + const fetchMicro = (url: string) => |
| 41 | + Micro.promise(() => |
| 42 | + fetch(url, { |
| 43 | + method: 'POST', |
| 44 | + credentials: 'include', |
| 45 | + }), |
| 46 | + ); |
| 47 | + |
| 48 | + const authorizeRequest = authorizeUrlMicro.pipe( |
| 49 | + Micro.flatMap(fetchMicro), |
| 50 | + Micro.flatMap((response) => Micro.promise(response.json)), |
| 51 | + ); |
| 52 | + response = await Micro.runPromise(authorizeRequest); |
42 | 53 | } else { |
43 | | - const authorizeUrl = await createAuthorizeUrl(authorizePath, { |
44 | | - ...optionsWithDefaults, |
45 | | - prompt: 'none', |
46 | | - }); |
47 | | - response = await iFrameManager().getParamsByRedirect({ |
48 | | - url: authorizeUrl, |
49 | | - /*** |
50 | | - * https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2 |
51 | | - * The client MUST ignore unrecognized response parameters. |
52 | | - */ |
53 | | - successParams: ['code', 'state'], |
54 | | - errorParams: ['error', 'error_description'], |
55 | | - timeout: config.serverConfig.timeout || 3000, |
56 | | - }); |
| 54 | + const authorizeUrlMicro = Micro.promise(() => |
| 55 | + createAuthorizeUrl(authorizePath, { |
| 56 | + ...optionsWithDefaults, |
| 57 | + prompt: 'none', |
| 58 | + }), |
| 59 | + ); |
| 60 | + |
| 61 | + const iframeMicro = (url: string) => |
| 62 | + Micro.promise(() => |
| 63 | + iFrameManager().getParamsByRedirect({ |
| 64 | + url, |
| 65 | + /*** |
| 66 | + * https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2 |
| 67 | + * The client MUST ignore unrecognized response parameters. |
| 68 | + */ |
| 69 | + successParams: ['code', 'state'], |
| 70 | + errorParams: ['error', 'error_description'], |
| 71 | + timeout: config.serverConfig.timeout || 3000, |
| 72 | + }), |
| 73 | + ); |
| 74 | + |
| 75 | + const authorizeRequest = authorizeUrlMicro.pipe(Micro.flatMap(iframeMicro)); |
| 76 | + response = await Micro.runPromise(authorizeRequest); |
57 | 77 | } |
58 | 78 |
|
59 | 79 | // Normalize response, for both success and failure, to handle both |
|
0 commit comments