Skip to content

Commit 42b48fb

Browse files
committed
feat(oidc-client): introduce Effect in authorize
1 parent 297bf2a commit 42b48fb

3 files changed

Lines changed: 60 additions & 39 deletions

File tree

packages/oidc-client/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"@forgerock/sdk-oidc": "workspace:*",
3232
"@forgerock/sdk-request-middleware": "workspace:*",
3333
"@forgerock/sdk-types": "workspace:*",
34-
"@reduxjs/toolkit": "catalog:"
34+
"@reduxjs/toolkit": "catalog:",
35+
"effect": "^3.12.7"
3536
},
3637
"nx": {
3738
"tags": ["scope:package"]

packages/oidc-client/src/lib/authorize.request.ts

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { iFrameManager } from '@forgerock/iframe-manager';
22
import { createAuthorizeUrl, GetAuthorizationUrlOptions } from '@forgerock/sdk-oidc';
3+
import { Micro } from 'effect';
34

45
import { createAuthorizeOptions, handleError, handleResponse } from './authorize.request.utils.js';
56

@@ -28,32 +29,51 @@ export async function authorize(
2829
* We need to make a post (or a get) request and both are supported by
2930
* PingOne.
3031
*/
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+
);
4039

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);
4253
} 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);
5777
}
5878

5979
// Normalize response, for both success and failure, to handle both

pnpm-lock.yaml

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)