|
1 | | -# iframe-manager |
| 1 | +# @forgerock/iframe-manager |
2 | 2 |
|
3 | | -This library was generated with [Nx](https://nx.dev). |
| 3 | +This package provides a utility for making background requests using hidden iframes, primarily intended for scenarios like silent authentication or token renewal in OAuth/OIDC flows. It extracts specified URL parameters from the final redirect URL within the iframe. |
4 | 4 |
|
5 | | -## Building |
| 5 | +## Overview |
6 | 6 |
|
7 | | -Run `nx build iframe-manager` to build the library. |
| 7 | +The package exports a single initialization function, `iframeManagerInit`. Calling this function returns an object containing the core API method: `getParamsFromIFrame`. |
8 | 8 |
|
9 | | -## Running unit tests |
| 9 | +## Installation |
10 | 10 |
|
11 | | -Run `nx test iframe-manager` to execute the unit tests via [Vitest](https://vitest.dev/). |
| 11 | +This package is typically used within the Ping Identity JavaScript SDK monorepo. If used standalone, you would install it via npm or yarn: |
| 12 | + |
| 13 | +```bash |
| 14 | +npm install @forgerock/iframe-manager |
| 15 | +# or |
| 16 | +yarn add @forgerock/iframe-manager |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +Import the initializer, call it, and then use the returned `getParamsFromIFrame` method. |
| 22 | + |
| 23 | +```typescript |
| 24 | +import iframeManagerInit from '@forgerock/iframe-manager'; |
| 25 | +import type { AuthorizeUrl } from '@forgerock/sdk-types'; |
| 26 | + |
| 27 | +// Initialize the iframe manager |
| 28 | +const iframeManager = iframeManagerInit(); |
| 29 | + |
| 30 | +// Define the request options |
| 31 | +const options = { |
| 32 | + url: 'https://authorization-server.com/authorize?client_id=...' as AuthorizeUrl, // Replace with your actual authorize URL |
| 33 | + requestTimeout: 5000, // Timeout in milliseconds (e.g., 5 seconds) |
| 34 | + urlParams: ['code', 'state', 'error', 'error_description'], // Specify the parameters you expect to parse |
| 35 | +}; |
| 36 | + |
| 37 | +// Make the request |
| 38 | +iframeManager |
| 39 | + .getParamsFromIFrame(options) |
| 40 | + .then((params) => { |
| 41 | + console.log('Successfully received parameters:', params); |
| 42 | + // Example: { code: '...', state: '...' } |
| 43 | + // Handle the received parameters (e.g., exchange code for tokens) |
| 44 | + }) |
| 45 | + .catch((error) => { |
| 46 | + console.error('Iframe request failed:', error.message); |
| 47 | + // Handle the error (e.g., timeout, authorization error) |
| 48 | + }); |
| 49 | +``` |
| 50 | + |
| 51 | +## API |
| 52 | + |
| 53 | +### `iframeManagerInit()` |
| 54 | + |
| 55 | +- **Description:** Initializes the iframe manager effect. Currently, it takes no configuration, but this might change in future versions. |
| 56 | +- **Returns:** `object` - An object containing the API methods. |
| 57 | + - `getParamsFromIFrame`: `(options: { urlParams: string[]; url: AuthorizeUrl; requestTimeout: number; }) => Promise<Record<string, string>>` |
| 58 | + |
| 59 | +### `getParamsFromIFrame(options)` |
| 60 | + |
| 61 | +- **Description:** Creates a hidden iframe, navigates it to the specified `url`, and waits for it to redirect. It then attempts to parse specified query parameters (`urlParams`) from the final URL loaded in the iframe. It rejects if the request times out, if specific OAuth error parameters (`error`, `error_description`) are found in the redirect URL, or if there's an issue accessing the iframe's content (e.g., cross-origin errors before the final expected redirect). |
| 62 | +- **Parameters:** |
| 63 | + - `options`: `object` |
| 64 | + - `url`: `AuthorizeUrl` (string) - The initial URL to load in the iframe. |
| 65 | + - `requestTimeout`: `number` - The maximum time in milliseconds to wait for the iframe to load and provide parameters before rejecting with a timeout error. |
| 66 | + - `urlParams`: `string[]` - An array of query parameter keys to extract from the final redirect URL. Only parameters listed here will be included in the resolved object. |
| 67 | +- **Returns:** `Promise<Record<string, string>>` - A promise that resolves with an object containing the key-value pairs of the extracted query parameters. It rejects with an `Error` if the operation times out, encounters an OAuth error in the URL parameters, or fails for other reasons (like cross-origin access errors). |
0 commit comments