|
| 1 | +import { createAuthorizeUrl, GetAuthorizationUrlOptions } from '@forgerock/sdk-oidc'; |
| 2 | + |
| 3 | +import { AuthorizeErrorResponse, AuthorizeSuccessResponse } from './authorize.request.types.js'; |
| 4 | +import { OidcConfig } from './config.types.js'; |
| 5 | + |
| 6 | +type OptionalAuthorizeOptions = Partial<GetAuthorizationUrlOptions>; |
| 7 | + |
| 8 | +export function createAuthorizeOptions( |
| 9 | + config: OidcConfig, |
| 10 | + options?: OptionalAuthorizeOptions, |
| 11 | +): GetAuthorizationUrlOptions { |
| 12 | + return { |
| 13 | + clientId: config.clientId, |
| 14 | + redirectUri: config.redirectUri, |
| 15 | + scope: config.scope || 'openid', |
| 16 | + responseType: config.responseType || 'code', |
| 17 | + ...options, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +export async function handleResponse( |
| 22 | + response: Record<string, unknown>, |
| 23 | + authorizePath: string, |
| 24 | + options: GetAuthorizationUrlOptions, |
| 25 | +): Promise<AuthorizeSuccessResponse | AuthorizeErrorResponse> { |
| 26 | + // Test if response is from a fetch to PingOne |
| 27 | + if ('authorizeResponse' in response) { |
| 28 | + const authorizeResponse = response.authorizeResponse as AuthorizeSuccessResponse; |
| 29 | + return authorizeResponse; |
| 30 | + } |
| 31 | + // Test if response is from an iframe |
| 32 | + if ('code' in response && 'state' in response) { |
| 33 | + const authorizeResponse = response as unknown as AuthorizeSuccessResponse; |
| 34 | + return authorizeResponse; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * If we reach here, it means the response is missing code or state. |
| 39 | + * Let's create a new authorize URL with `prompt=login` to redirect the user to the |
| 40 | + * authorization endpoint provide it to the application so it can handle the redirect. |
| 41 | + */ |
| 42 | + const newAuthorizeUrl = await createAuthorizeUrl(authorizePath, options); |
| 43 | + |
| 44 | + if ('error' in response && 'error_description' in response) { |
| 45 | + const errorResponse = { |
| 46 | + error: response.error as string, |
| 47 | + error_description: response.error_description as string, |
| 48 | + type: 'auth_error', |
| 49 | + redirectUrl: newAuthorizeUrl, |
| 50 | + } as AuthorizeErrorResponse; |
| 51 | + |
| 52 | + return errorResponse; |
| 53 | + } else { |
| 54 | + return { |
| 55 | + error_description: 'Unknown authorization error', |
| 56 | + redirectUrl: newAuthorizeUrl, |
| 57 | + type: 'auth_error', |
| 58 | + } as AuthorizeErrorResponse; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export async function handleError( |
| 63 | + error: unknown, |
| 64 | + authorizePath: string, |
| 65 | + options: GetAuthorizationUrlOptions, |
| 66 | +): Promise<AuthorizeErrorResponse> { |
| 67 | + const message = error instanceof Error ? error.message : ''; |
| 68 | + /** |
| 69 | + * Let's create a new authorize URL with `prompt=login` to redirect the user to the |
| 70 | + * authorization endpoint provide it to the application so it can handle the redirect. |
| 71 | + */ |
| 72 | + const newAuthorizeUrl = await createAuthorizeUrl(authorizePath, options); |
| 73 | + |
| 74 | + return { |
| 75 | + error: 'network_error', |
| 76 | + error_description: message || 'An error occurred while fetching the authorization URL', |
| 77 | + redirectUrl: newAuthorizeUrl, |
| 78 | + type: 'auth_error', |
| 79 | + }; |
| 80 | +} |
0 commit comments