|
| 1 | +import { iFrameManager } from '@forgerock/iframe-manager'; |
| 2 | +import { createAuthorizeUrl, GetAuthorizationUrlOptions } from '@forgerock/sdk-oidc'; |
| 3 | +import { createOidcStore } from './store.js'; |
| 4 | +import { fetchWellKnownConfig } from './wellknown.api.js'; |
| 5 | +import { RequestMiddleware } from '@forgerock/sdk-request-middleware'; |
| 6 | +import { handleAuthorize, recreateAuthorizeUrl } from './client.store.utils.js'; |
| 7 | + |
| 8 | +interface OIDCConfig { |
| 9 | + prefix?: string; |
| 10 | + serverConfig: { |
| 11 | + wellknown: string; |
| 12 | + }; |
| 13 | + middleware?: RequestMiddleware[]; |
| 14 | + logger?: ReturnType<typeof import('@forgerock/sdk-logger').logger>; |
| 15 | +} |
| 16 | + |
| 17 | +interface AuthorizeSuccessResponse { |
| 18 | + code: string; |
| 19 | + state: string; |
| 20 | + redirectUrl?: string; // Optional, used when the response is from a P1 server |
| 21 | +} |
| 22 | + |
| 23 | +interface AuthorizeUrlResponse { |
| 24 | + authorizeUrl: string; |
| 25 | +} |
| 26 | + |
| 27 | +interface AuthorizeErrorResponse { |
| 28 | + error: string; |
| 29 | + error_description?: string; |
| 30 | + state?: string; |
| 31 | +} |
| 32 | + |
| 33 | +type AuthorizeResponse = AuthorizeSuccessResponse | AuthorizeUrlResponse | AuthorizeErrorResponse; |
| 34 | + |
| 35 | +export type { AuthorizeSuccessResponse, AuthorizeErrorResponse, AuthorizeResponse, OIDCConfig }; |
| 36 | + |
| 37 | +export async function oidc(config: OIDCConfig) { |
| 38 | + const store = createOidcStore({ requestMiddleware: config.middleware, logger: config.logger }); |
| 39 | + const iframeMgr = iFrameManager(); |
| 40 | + |
| 41 | + if (!config?.serverConfig?.wellknown) { |
| 42 | + return { |
| 43 | + error: 'requires a wellknown url initializing this factory.', |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + const wellKnownUrl = config.serverConfig.wellknown; |
| 48 | + const { data, error } = await store.dispatch( |
| 49 | + fetchWellKnownConfig.endpoints.fetchWellKnownConfig.initiate(wellKnownUrl), |
| 50 | + ); |
| 51 | + if (error || !data) { |
| 52 | + return { |
| 53 | + error: `Error fetching wellknown config`, |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * if true, then we need to use GET or POST (p1?) |
| 59 | + * if false, we use iframe. |
| 60 | + */ |
| 61 | + const supportsPiFlow = data.response_modes_supported?.includes('pi.flow'); |
| 62 | + |
| 63 | + return { |
| 64 | + createAuthorizeUrl: createAuthorizeUrl, |
| 65 | + authorizeSilently: async ( |
| 66 | + options: GetAuthorizationUrlOptions, |
| 67 | + timeout = 3000, |
| 68 | + ): Promise<AuthorizeResponse | { error: string }> => { |
| 69 | + const authorizePath = data.authorization_endpoint; |
| 70 | + |
| 71 | + const authorizeUrl = await createAuthorizeUrl(authorizePath, options); |
| 72 | + |
| 73 | + try { |
| 74 | + /** |
| 75 | + * If we support the pi flow field, |
| 76 | + * this means we are using a p1 server. p1 servers |
| 77 | + * do not support redirection through iframes because they |
| 78 | + * set iframe's to DENY. |
| 79 | + */ |
| 80 | + if (supportsPiFlow) { |
| 81 | + /** |
| 82 | + * We need to make a post (or a get) request |
| 83 | + * and both are supported by p1. |
| 84 | + */ |
| 85 | + return handleAuthorize(authorizeUrl); |
| 86 | + } |
| 87 | + |
| 88 | + const resolvedParams = await iframeMgr.getParamsByRedirect({ |
| 89 | + url: authorizeUrl, |
| 90 | + /*** |
| 91 | + * https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.2 |
| 92 | + * The client MUST ignore unrecognized response parameters. |
| 93 | + * The authorization code string size is left undefined by this specification. |
| 94 | + * The client should avoid making assumptions about code value sizes. |
| 95 | + * The authorization server SHOULD document the size of any value it issues. |
| 96 | + */ |
| 97 | + successParams: ['code', 'state'], |
| 98 | + errorParams: ['error', 'error_description'], |
| 99 | + timeout, |
| 100 | + }); |
| 101 | + |
| 102 | + if ('error' in resolvedParams) { |
| 103 | + return recreateAuthorizeUrl(resolvedParams, authorizePath, options); |
| 104 | + } |
| 105 | + |
| 106 | + const { code, state } = resolvedParams; |
| 107 | + |
| 108 | + return { |
| 109 | + code, |
| 110 | + state, |
| 111 | + }; |
| 112 | + } catch (iframeError: unknown) { |
| 113 | + return { |
| 114 | + error: 'iframe_error', |
| 115 | + error_description: (iframeError as Error)?.message || 'Authorization iframe failed', |
| 116 | + }; |
| 117 | + } |
| 118 | + }, |
| 119 | + }; |
| 120 | +} |
0 commit comments