-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiframe-manager.effects.ts
More file actions
173 lines (151 loc) · 5.69 KB
/
iframe-manager.effects.ts
File metadata and controls
173 lines (151 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (c) 2025 Ping Identity Corporation. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
/* eslint-disable @typescript-eslint/no-empty-function */
export interface GetParamsFromIFrameOptions {
/** The URL to load in the iframe. */
url: string;
/** Timeout in milliseconds for the entire operation. */
timeout: number;
/** Array of query parameter keys expected upon successful completion. */
successParams: string[];
/** Array of query parameter keys indicating an error occurred. */
errorParams: string[];
}
export type ResolvedParams = Record<string, string>;
type Noop = () => void;
function hasErrorParams(params: URLSearchParams, errorParams: string[]): boolean {
for (const key of errorParams) {
if (params.has(key)) {
return true;
}
}
return false;
}
// Helper function to check if all required success params are present
function hasSomeSuccessParams(params: URLSearchParams, successParams: string[]): boolean {
return successParams.some((key) => params.has(key));
}
function searchParamsToRecord(params: URLSearchParams): ResolvedParams {
const result: ResolvedParams = {};
params.forEach((value, key) => {
result[key] = value;
});
return result;
}
/**
* Initializes the Iframe Manager effect.
* @returns An object containing the API for managing iframe requests.
*/
export function iFrameManager() {
/**
* Creates a hidden iframe to navigate to the specified URL,
* waits for a redirect back to the application's origin,
* and resolves/rejects based on the query parameters found in the redirect URL.
* IMPORTANT: This relies on the final redirect target being on the SAME ORIGIN
* as the parent window due to browser security restrictions (Same-Origin Policy).
* Accessing contentWindow.location of a cross-origin iframe will fail.
*
* @param options - The options for the iframe request (URL, timeout, success/error params).
* @returns A Promise that resolves with the parsed query parameters on success or
* when error params are present; rejects on timeout or if unable to access iframe content.
*/
return {
getParamsByRedirect: (options: GetParamsFromIFrameOptions): Promise<ResolvedParams> => {
const { url, timeout, successParams, errorParams } = options;
if (
!successParams ||
!errorParams ||
successParams.length === 0 ||
errorParams.length === 0
) {
const error = new Error('successParams and errorParams must be provided');
throw error;
}
return new Promise<ResolvedParams>((resolve, reject) => {
let iframe: HTMLIFrameElement | null = null;
let timerId: ReturnType<typeof setTimeout> | null = null;
let onLoadHandler: () => void = () => {};
let cleanup: Noop = () => {};
cleanup = (): void => {
if (!iframe && !timerId) return;
if (timerId) {
clearTimeout(timerId);
timerId = null;
}
if (iframe) {
iframe.removeEventListener('load', onLoadHandler);
if (iframe.parentNode) {
iframe.remove();
}
iframe = null;
}
onLoadHandler = () => {};
cleanup = () => {};
};
onLoadHandler = (): void => {
try {
if (iframe && iframe.contentWindow) {
const currentIframeHref = iframe.contentWindow.location.href;
if (currentIframeHref === 'about:blank' || !currentIframeHref) {
return; // Wait for actual navigation
}
const redirectUrl = new URL(currentIframeHref);
const searchParams = redirectUrl.searchParams;
const parsedParams = searchParamsToRecord(searchParams);
// 1. Check for Error Parameters
if (hasErrorParams(searchParams, errorParams)) {
cleanup();
resolve(parsedParams); // Resolve with all parsed params for context
return;
}
// 2. Check for Success Parameters
if (hasSomeSuccessParams(searchParams, successParams)) {
cleanup();
resolve(parsedParams); // Resolve with all parsed params
return;
}
/*
* 3. Neither Error nor Success: Intermediate Redirect?
* If neither error nor all required success params are found,
* assume it's an intermediate step in the redirect flow.
* Do nothing, let the timeout eventually handle non-resolving states
* or wait for the next 'load' event.
*/
}
} catch {
// This likely means a cross-origin navigation occurred where access is denied.
cleanup();
reject({
type: 'internal_error',
message: 'unexpected failure',
});
}
};
try {
iframe = document.createElement('iframe');
iframe.style.display = 'none'; // Hide the iframe
iframe.addEventListener('load', onLoadHandler);
document.body.appendChild(iframe);
timerId = setTimeout(() => {
cleanup();
reject({
type: 'internal_error',
message: 'iframe timed out',
});
}, timeout);
iframe.src = url;
} catch {
cleanup(); // Attempt cleanup even if setup failed partially
reject({
type: 'internal_error',
message: 'error setting up iframe',
});
}
});
},
};
}