@nibssplc/cams-sdk 0.0.1-beta.37 → 0.0.1-beta.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth.d.ts +12 -0
- package/dist/index.cjs.js +260 -170
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +260 -170
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +260 -170
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/auth.d.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { CAMSConfig } from "./types";
|
|
2
2
|
import { Profile } from "./validators";
|
|
3
|
+
/**
|
|
4
|
+
* Generate a secure random window name to prevent targeting
|
|
5
|
+
*/
|
|
3
6
|
export declare const generateSecureWindowName: () => string;
|
|
7
|
+
/**
|
|
8
|
+
* Detect if current window is a popup to prevent recursive popups
|
|
9
|
+
*/
|
|
4
10
|
export declare const isPopupWindow: () => boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Validate configuration and return parsed URL
|
|
13
|
+
*/
|
|
5
14
|
export declare function validateConfig(config: CAMSConfig): URL;
|
|
15
|
+
/**
|
|
16
|
+
* Open CAMS authentication popup and handle the authentication flow
|
|
17
|
+
*/
|
|
6
18
|
export declare function openCAMSPopUpLogin(config: CAMSConfig): Promise<Profile>;
|
package/dist/index.cjs.js
CHANGED
|
@@ -85,13 +85,33 @@ class Logger {
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
// Capture original functions to prevent tampering (client-side only)
|
|
88
|
-
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
//
|
|
88
|
+
// Constants
|
|
89
|
+
const DEFAULT_AUTH_TIMEOUT = 300000; // 5 minutes
|
|
90
|
+
const DEFAULT_IDLE_TIMEOUT = 120000; // 2 minutes
|
|
91
|
+
const INITIAL_POLL_INTERVAL = 1000;
|
|
92
|
+
const MAX_POLL_INTERVAL = 5000;
|
|
93
|
+
const WINDOW_FEATURES = "scrollbars=yes,resizable=yes";
|
|
94
|
+
// Secure reference to native functions
|
|
95
|
+
const NativeFunctions = {
|
|
96
|
+
addEventListener: typeof window !== "undefined" ? window.addEventListener : null,
|
|
97
|
+
removeEventListener: typeof window !== "undefined" ? window.removeEventListener : null,
|
|
98
|
+
setTimeout: typeof window !== "undefined" ? window.setTimeout.bind(window) : setTimeout,
|
|
99
|
+
clearTimeout: typeof window !== "undefined"
|
|
100
|
+
? window.clearTimeout.bind(window)
|
|
101
|
+
: clearTimeout,
|
|
102
|
+
};
|
|
103
|
+
const defaultMicrosoftDomains = [
|
|
104
|
+
".microsoft.com",
|
|
105
|
+
".microsoftonline.com",
|
|
106
|
+
".live.com",
|
|
107
|
+
];
|
|
108
|
+
/**
|
|
109
|
+
* Generate a secure random window name to prevent targeting
|
|
110
|
+
*/
|
|
111
|
+
const generateSecureWindowName = () => `NINSS_CAS_AUTH_${Math.random().toString(36).slice(2, 9)}_${Date.now()}`;
|
|
112
|
+
/**
|
|
113
|
+
* Detect if current window is a popup to prevent recursive popups
|
|
114
|
+
*/
|
|
95
115
|
const isPopupWindow = () => {
|
|
96
116
|
if (typeof window === "undefined")
|
|
97
117
|
return false;
|
|
@@ -102,256 +122,326 @@ const isPopupWindow = () => {
|
|
|
102
122
|
return false;
|
|
103
123
|
}
|
|
104
124
|
};
|
|
125
|
+
/**
|
|
126
|
+
* Safely close a window with error handling for cross-origin restrictions
|
|
127
|
+
*/
|
|
128
|
+
const safeWindowClose = (window) => {
|
|
129
|
+
if (!window)
|
|
130
|
+
return;
|
|
131
|
+
try {
|
|
132
|
+
if (!window.closed) {
|
|
133
|
+
window.close();
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
Logger.debug("Error closing window (cross-origin restriction)", {
|
|
138
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* Center popup window on screen
|
|
144
|
+
*/
|
|
145
|
+
const getPopupPosition = (width, height) => {
|
|
146
|
+
if (typeof window === "undefined")
|
|
147
|
+
return { left: 0, top: 0 };
|
|
148
|
+
const left = Math.max(0, (window.screen.width - width) / 2);
|
|
149
|
+
const top = Math.max(0, (window.screen.height - height) / 2);
|
|
150
|
+
return { left, top };
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* Validate origin against expected domain with support for subdomains and Microsoft redirects
|
|
154
|
+
*/
|
|
155
|
+
const isValidOrigin = (eventOrigin, expectedOrigin, microsoftDomains = defaultMicrosoftDomains) => {
|
|
156
|
+
try {
|
|
157
|
+
const originUrl = new URL(eventOrigin);
|
|
158
|
+
const expectedHostname = expectedOrigin.startsWith("http")
|
|
159
|
+
? new URL(expectedOrigin).hostname
|
|
160
|
+
: expectedOrigin;
|
|
161
|
+
// Check Microsoft domains for Azure AD redirects
|
|
162
|
+
const isMicrosoftDomain = microsoftDomains.some((domain) => originUrl.hostname.endsWith(domain));
|
|
163
|
+
// Allow exact match, subdomains, or Microsoft domains
|
|
164
|
+
return (originUrl.hostname === expectedHostname ||
|
|
165
|
+
originUrl.hostname.endsWith("." + expectedHostname) ||
|
|
166
|
+
isMicrosoftDomain);
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Safely add message event listener with error handling
|
|
174
|
+
*/
|
|
175
|
+
const addMessageListener = (listener) => {
|
|
176
|
+
if (!NativeFunctions.addEventListener) {
|
|
177
|
+
throw new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Message listener not available in current environment");
|
|
178
|
+
}
|
|
179
|
+
const wrappedListener = (event) => {
|
|
180
|
+
try {
|
|
181
|
+
listener(event);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
Logger.error("Error in message listener", {
|
|
185
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
NativeFunctions.addEventListener.call(window, "message", wrappedListener);
|
|
190
|
+
return () => {
|
|
191
|
+
if (NativeFunctions.removeEventListener) {
|
|
192
|
+
NativeFunctions.removeEventListener.call(window, "message", wrappedListener);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* Validate configuration and return parsed URL
|
|
198
|
+
*/
|
|
105
199
|
function validateConfig(config) {
|
|
106
|
-
// Validate URL format and parse once
|
|
107
200
|
const urlValidation = URLSchema.safeParse(config.camsUrl);
|
|
108
201
|
if (!urlValidation.success) {
|
|
109
202
|
throw new CAMSError(exports.CAMSErrorType.INVALID_URL, "Invalid CAMS URL format");
|
|
110
203
|
}
|
|
111
|
-
// Domain validation is mandatory for security
|
|
112
204
|
if (!config.messageOrigin) {
|
|
113
205
|
throw new CAMSError(exports.CAMSErrorType.INVALID_URL, "Message Origin must be specified for security");
|
|
114
206
|
}
|
|
115
|
-
|
|
116
|
-
return url;
|
|
207
|
+
return new URL(config.camsUrl);
|
|
117
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Open CAMS authentication popup and handle the authentication flow
|
|
211
|
+
*/
|
|
118
212
|
function openCAMSPopUpLogin(config) {
|
|
119
213
|
return new Promise((resolve, reject) => {
|
|
214
|
+
// Environment validation
|
|
120
215
|
if (typeof window === "undefined") {
|
|
121
216
|
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Cannot open popup in server-side environment"));
|
|
122
217
|
return;
|
|
123
218
|
}
|
|
124
|
-
if (config.debug)
|
|
125
|
-
Logger.setLevel(exports.LogLevel.DEBUG);
|
|
126
|
-
// If this is a popup window, don't open another popup
|
|
127
219
|
if (isPopupWindow()) {
|
|
128
|
-
Logger.warn("
|
|
220
|
+
Logger.warn("Attempted to open popup from within popup window");
|
|
129
221
|
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Cannot open authentication popup from within popup window"));
|
|
130
222
|
return;
|
|
131
223
|
}
|
|
224
|
+
// Initialize logging
|
|
225
|
+
if (config.debug)
|
|
226
|
+
Logger.setLevel(exports.LogLevel.DEBUG);
|
|
132
227
|
Logger.info("Starting CAMS authentication", { url: config.camsUrl });
|
|
228
|
+
// Validate configuration
|
|
133
229
|
let validatedUrl;
|
|
134
230
|
try {
|
|
135
231
|
validatedUrl = validateConfig(config);
|
|
136
|
-
Logger.debug("
|
|
232
|
+
Logger.debug("Configuration validation passed", validatedUrl);
|
|
137
233
|
}
|
|
138
234
|
catch (error) {
|
|
139
|
-
Logger.error("
|
|
235
|
+
Logger.error("Configuration validation failed", {
|
|
140
236
|
error: error instanceof Error ? error.message : "Unknown error",
|
|
141
237
|
});
|
|
142
238
|
reject(error);
|
|
143
239
|
return;
|
|
144
240
|
}
|
|
145
|
-
|
|
146
|
-
|
|
241
|
+
// Extract configuration with defaults
|
|
242
|
+
const { messageOrigin, windowHeight = 600, windowWidth = 500, authTimeout = DEFAULT_AUTH_TIMEOUT, idleTimeout = DEFAULT_IDLE_TIMEOUT,
|
|
243
|
+
// allowedRedirectDomains = defaultMicrosoftDomains,
|
|
147
244
|
} = config;
|
|
148
|
-
|
|
245
|
+
// State management
|
|
246
|
+
let authWindow = null;
|
|
149
247
|
let authTimeoutId = null;
|
|
150
248
|
let idleTimeoutId = null;
|
|
151
249
|
let checkClosedInterval = null;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
});
|
|
157
|
-
// Center the popup window
|
|
158
|
-
const left = (window.screen.width - windowWidth) / 2;
|
|
159
|
-
const top = (window.screen.height - windowHeight) / 2;
|
|
160
|
-
const secureWindowName = generateSecureWindowName();
|
|
161
|
-
authWindow = window.open(validatedUrl.toString(), secureWindowName, `width=${windowWidth},height=${windowHeight},left=${left},top=${top},scrollbars=yes,resizable=yes`);
|
|
162
|
-
}
|
|
163
|
-
catch (error) {
|
|
164
|
-
Logger.error("Failed to open popup window", {
|
|
165
|
-
error: error instanceof Error ? error.message : "Unknown error",
|
|
166
|
-
});
|
|
167
|
-
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Popup blocked by browser. Please allow popups and try again."));
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
if (!authWindow) {
|
|
171
|
-
Logger.error("Popup window blocked");
|
|
172
|
-
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Popup blocked by browser. Please allow popups and try again."));
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
// Only check for immediate closure (true popup blocking)
|
|
176
|
-
if (authWindow.closed) {
|
|
177
|
-
Logger.error("Popup immediately closed by blocker");
|
|
178
|
-
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Popup blocked by browser. Please allow popups and try again."));
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
Logger.debug("Popup window opened successfully");
|
|
250
|
+
let removeMessageListener = null;
|
|
251
|
+
/**
|
|
252
|
+
* Clean up all resources and timeouts
|
|
253
|
+
*/
|
|
182
254
|
const cleanup = () => {
|
|
183
|
-
if (
|
|
184
|
-
|
|
255
|
+
if (removeMessageListener) {
|
|
256
|
+
removeMessageListener();
|
|
257
|
+
removeMessageListener = null;
|
|
185
258
|
}
|
|
186
259
|
if (idleTimeoutId)
|
|
187
|
-
|
|
260
|
+
NativeFunctions.clearTimeout(idleTimeoutId);
|
|
188
261
|
if (authTimeoutId)
|
|
189
|
-
|
|
262
|
+
NativeFunctions.clearTimeout(authTimeoutId);
|
|
190
263
|
if (checkClosedInterval)
|
|
191
|
-
|
|
264
|
+
NativeFunctions.clearTimeout(checkClosedInterval);
|
|
192
265
|
idleTimeoutId = null;
|
|
193
266
|
authTimeoutId = null;
|
|
194
267
|
checkClosedInterval = null;
|
|
195
268
|
};
|
|
196
|
-
|
|
197
|
-
|
|
269
|
+
/**
|
|
270
|
+
* Clean up and close the popup window, then reject with error
|
|
271
|
+
*/
|
|
272
|
+
const cleanupAndClose = (error) => {
|
|
273
|
+
cleanup();
|
|
274
|
+
safeWindowClose(authWindow);
|
|
275
|
+
if (error) {
|
|
276
|
+
reject(error);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* Reset idle timeout on user activity
|
|
281
|
+
*/
|
|
282
|
+
const resetIdleTimeout = () => {
|
|
283
|
+
if (idleTimeoutId)
|
|
284
|
+
NativeFunctions.clearTimeout(idleTimeoutId);
|
|
285
|
+
Logger.debug("Resetting idle timeout", { idleTimeout });
|
|
286
|
+
idleTimeoutId = NativeFunctions.setTimeout(() => {
|
|
287
|
+
Logger.warn("Authentication idle timeout reached", { idleTimeout });
|
|
288
|
+
cleanupAndClose(new CAMSError(exports.CAMSErrorType.TIMEOUT, `Authentication timed out due to inactivity after ${idleTimeout}ms`));
|
|
289
|
+
}, idleTimeout);
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Set up popup window closure detection with exponential backoff
|
|
293
|
+
*/
|
|
294
|
+
const setupClosureDetection = () => {
|
|
295
|
+
let pollInterval = INITIAL_POLL_INTERVAL;
|
|
296
|
+
const checkClosed = () => {
|
|
297
|
+
try {
|
|
298
|
+
if (authWindow?.closed) {
|
|
299
|
+
Logger.info("Authentication window closed by user");
|
|
300
|
+
cleanupAndClose(new CAMSError(exports.CAMSErrorType.USER_CANCELLED, "Authentication window was closed"));
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
catch (error) {
|
|
305
|
+
// Cross-origin access error means window is still open but on different domain
|
|
306
|
+
Logger.debug("Cannot access window.closed due to cross-origin restrictions", error);
|
|
307
|
+
}
|
|
308
|
+
// Exponential backoff with maximum limit
|
|
309
|
+
pollInterval = Math.min(pollInterval * 1.2, MAX_POLL_INTERVAL);
|
|
310
|
+
checkClosedInterval = NativeFunctions.setTimeout(checkClosed, pollInterval);
|
|
311
|
+
};
|
|
312
|
+
checkClosedInterval = NativeFunctions.setTimeout(checkClosed, pollInterval);
|
|
313
|
+
};
|
|
314
|
+
/**
|
|
315
|
+
* Handle incoming message events from the popup
|
|
316
|
+
*/
|
|
317
|
+
const handleMessage = (event) => {
|
|
198
318
|
Logger.debug("Received message", {
|
|
199
|
-
origin:
|
|
319
|
+
origin: event.origin,
|
|
200
320
|
expectedOrigin: messageOrigin,
|
|
201
|
-
data:
|
|
321
|
+
data: event.data,
|
|
202
322
|
});
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
? new URL(messageOrigin).hostname
|
|
206
|
-
: messageOrigin;
|
|
207
|
-
// Allow Microsoft redirect domains for Azure AD authentication
|
|
208
|
-
const isMicrosoftDomain = originUrl.hostname.endsWith(".microsoft.com") ||
|
|
209
|
-
originUrl.hostname.endsWith(".microsoftonline.com") ||
|
|
210
|
-
originUrl.hostname.endsWith(".live.com");
|
|
211
|
-
const isAllowedOrigin = originUrl.hostname === expectedHostname ||
|
|
212
|
-
originUrl.hostname.endsWith("." + expectedHostname) ||
|
|
213
|
-
isMicrosoftDomain;
|
|
214
|
-
if (!isAllowedOrigin) {
|
|
323
|
+
// Security: Validate origin
|
|
324
|
+
if (!isValidOrigin(event.origin, messageOrigin, defaultMicrosoftDomains)) {
|
|
215
325
|
Logger.warn("Blocked message from unauthorized origin", {
|
|
216
|
-
origin:
|
|
326
|
+
origin: event.origin,
|
|
217
327
|
expected: messageOrigin,
|
|
218
|
-
data:
|
|
328
|
+
data: event.data,
|
|
219
329
|
});
|
|
220
330
|
return;
|
|
221
331
|
}
|
|
222
|
-
// Validate
|
|
223
|
-
if (
|
|
332
|
+
// Security: Validate message source
|
|
333
|
+
if (event.source !== authWindow) {
|
|
224
334
|
Logger.warn("Message from unexpected source", {
|
|
225
|
-
source:
|
|
226
|
-
origin:
|
|
335
|
+
source: event.source,
|
|
336
|
+
origin: event.origin,
|
|
227
337
|
expected: messageOrigin,
|
|
228
|
-
data:
|
|
338
|
+
data: event.data,
|
|
229
339
|
});
|
|
230
340
|
return;
|
|
231
341
|
}
|
|
232
|
-
//
|
|
233
|
-
if (
|
|
234
|
-
Logger.warn("Message from unexpected source", {
|
|
235
|
-
source: messageEvent.source,
|
|
236
|
-
origin: messageEvent.origin,
|
|
237
|
-
expected: messageOrigin,
|
|
238
|
-
data: messageEvent.data,
|
|
239
|
-
});
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
// Ignore heartbeat messages
|
|
243
|
-
if (messageEvent.data?.type === "heartbeat") {
|
|
342
|
+
// Handle heartbeat messages (reset idle timeout)
|
|
343
|
+
if (event.data?.type === "heartbeat") {
|
|
244
344
|
Logger.debug("Received heartbeat message");
|
|
245
345
|
resetIdleTimeout();
|
|
246
346
|
return;
|
|
247
347
|
}
|
|
248
|
-
//
|
|
249
|
-
|
|
250
|
-
|
|
348
|
+
// Process authentication messages
|
|
349
|
+
processAuthenticationMessage(event.data);
|
|
350
|
+
};
|
|
351
|
+
/**
|
|
352
|
+
* Process and validate authentication message payloads
|
|
353
|
+
*/
|
|
354
|
+
const processAuthenticationMessage = (data) => {
|
|
355
|
+
// Success case: Valid profile received
|
|
356
|
+
const profileValidation = ProfileSchema.safeParse(data);
|
|
357
|
+
if (profileValidation.success) {
|
|
251
358
|
Logger.info("Authentication successful");
|
|
252
359
|
cleanupAndClose();
|
|
253
|
-
resolve(
|
|
360
|
+
resolve(profileValidation.data);
|
|
254
361
|
return;
|
|
255
362
|
}
|
|
256
|
-
|
|
257
|
-
|
|
363
|
+
// Authentication failure case
|
|
364
|
+
const authFailureValidation = AuthFailureMessageSchema.safeParse(data);
|
|
365
|
+
if (authFailureValidation.success) {
|
|
366
|
+
const { error, errorCode, errorDetails } = authFailureValidation.data;
|
|
258
367
|
Logger.warn("Authentication failure received", {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
368
|
+
error,
|
|
369
|
+
errorCode,
|
|
370
|
+
errorDetails,
|
|
262
371
|
});
|
|
263
|
-
cleanupAndClose(new CAMSError(exports.CAMSErrorType.API_VALIDATION_ERROR,
|
|
372
|
+
cleanupAndClose(new CAMSError(exports.CAMSErrorType.API_VALIDATION_ERROR, `Authentication failed: ${error}${errorCode ? ` (${errorCode})` : ""}`));
|
|
264
373
|
return;
|
|
265
374
|
}
|
|
266
|
-
|
|
267
|
-
|
|
375
|
+
// Generic error case
|
|
376
|
+
const errorValidation = ErrorMessageSchema.safeParse(data);
|
|
377
|
+
if (errorValidation.success) {
|
|
268
378
|
Logger.warn("Authentication error received", {
|
|
269
|
-
error:
|
|
379
|
+
error: errorValidation.data.error,
|
|
270
380
|
});
|
|
271
|
-
cleanupAndClose(new CAMSError(exports.CAMSErrorType.USER_CANCELLED,
|
|
381
|
+
cleanupAndClose(new CAMSError(exports.CAMSErrorType.USER_CANCELLED, `Authentication failed: ${errorValidation.data.error}`));
|
|
272
382
|
return;
|
|
273
383
|
}
|
|
384
|
+
// Invalid message format
|
|
274
385
|
Logger.error("Invalid message format received", {
|
|
275
|
-
messageData:
|
|
276
|
-
dataType: typeof
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
errorValidationError: errorMsg.error
|
|
281
|
-
? errorMsg.error.issues
|
|
282
|
-
: undefined,
|
|
386
|
+
messageData: data,
|
|
387
|
+
dataType: typeof data,
|
|
388
|
+
profileValidationError: profileValidation.error?.issues,
|
|
389
|
+
authFailureValidationError: authFailureValidation.error?.issues,
|
|
390
|
+
errorValidationError: errorValidation.error?.issues,
|
|
283
391
|
});
|
|
284
392
|
cleanupAndClose(new CAMSError(exports.CAMSErrorType.INVALID_ORIGIN, "Invalid message format"));
|
|
285
393
|
};
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
394
|
+
// Main authentication flow execution
|
|
395
|
+
try {
|
|
396
|
+
// Open popup window
|
|
397
|
+
const { left, top } = getPopupPosition(windowWidth, windowHeight);
|
|
398
|
+
const secureWindowName = generateSecureWindowName();
|
|
399
|
+
Logger.debug("Opening popup window", {
|
|
400
|
+
width: windowWidth,
|
|
401
|
+
height: windowHeight,
|
|
402
|
+
left,
|
|
403
|
+
top,
|
|
404
|
+
});
|
|
405
|
+
authWindow = window.open(validatedUrl.toString(), secureWindowName, `width=${windowWidth},height=${windowHeight},left=${left},top=${top},${WINDOW_FEATURES}`);
|
|
406
|
+
// Check for popup blocking
|
|
407
|
+
if (!authWindow) {
|
|
408
|
+
Logger.error("Popup window blocked or failed to open");
|
|
409
|
+
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Popup blocked by browser. Please allow popups and try again."));
|
|
410
|
+
return;
|
|
295
411
|
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
// Timeout handler
|
|
301
|
-
if (idleTimeoutId)
|
|
302
|
-
originalClearTimeout(idleTimeoutId);
|
|
303
|
-
Logger.debug("Resetting idle timeout", { idleTimeout });
|
|
304
|
-
idleTimeoutId = originalSetTimeout(() => {
|
|
305
|
-
Logger.warn("Authentication idle timeout reached", { idleTimeout });
|
|
306
|
-
cleanupAndClose(new CAMSError(exports.CAMSErrorType.TIMEOUT, `Authentication timed out due to inactivity after ${idleTimeout}ms`));
|
|
307
|
-
}, idleTimeout);
|
|
308
|
-
};
|
|
309
|
-
Logger.debug("Setting auth timeout", { authTimeout });
|
|
310
|
-
authTimeoutId = originalSetTimeout(() => {
|
|
311
|
-
Logger.warn("Authentication auth timeout reached", { authTimeout });
|
|
312
|
-
cleanupAndClose(new CAMSError(exports.CAMSErrorType.TIMEOUT, `Authentication process exceeded auth time limit of ${authTimeout}ms`));
|
|
313
|
-
}, authTimeout);
|
|
314
|
-
resetIdleTimeout();
|
|
315
|
-
// Use exponential backoff for popup closure checking
|
|
316
|
-
let pollInterval = 1000; // Start with 1 second
|
|
317
|
-
const maxPollInterval = 5000; // Max 5 seconds
|
|
318
|
-
const checkClosed = () => {
|
|
319
|
-
if (authWindow?.closed) {
|
|
320
|
-
Logger.info("Authentication window closed by user");
|
|
321
|
-
cleanupAndClose(new CAMSError(exports.CAMSErrorType.USER_CANCELLED, "Authentication window was closed"));
|
|
412
|
+
// Check for immediate closure (aggressive popup blockers)
|
|
413
|
+
if (authWindow.closed) {
|
|
414
|
+
Logger.error("Popup immediately closed by blocker");
|
|
415
|
+
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Popup blocked by browser. Please allow popups and try again."));
|
|
322
416
|
return;
|
|
323
417
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
418
|
+
Logger.debug("Popup window opened successfully");
|
|
419
|
+
// Set up message listener
|
|
420
|
+
removeMessageListener = addMessageListener(handleMessage);
|
|
421
|
+
Logger.debug("Message listener attached");
|
|
422
|
+
// Set up timeouts
|
|
423
|
+
Logger.debug("Setting authentication timeout", { authTimeout });
|
|
424
|
+
authTimeoutId = NativeFunctions.setTimeout(() => {
|
|
425
|
+
Logger.warn("Authentication timeout reached", { authTimeout });
|
|
426
|
+
cleanupAndClose(new CAMSError(exports.CAMSErrorType.TIMEOUT, `Authentication process exceeded time limit of ${authTimeout}ms`));
|
|
427
|
+
}, authTimeout);
|
|
428
|
+
resetIdleTimeout();
|
|
429
|
+
setupClosureDetection();
|
|
430
|
+
// Focus popup for better UX
|
|
431
|
+
try {
|
|
432
|
+
authWindow.focus();
|
|
333
433
|
}
|
|
334
|
-
|
|
335
|
-
|
|
434
|
+
catch (error) {
|
|
435
|
+
Logger.debug("Could not focus popup window (cross-origin restriction)", error);
|
|
336
436
|
}
|
|
337
437
|
}
|
|
338
438
|
catch (error) {
|
|
339
|
-
Logger.error("Failed to
|
|
439
|
+
Logger.error("Failed to initialize authentication popup", {
|
|
340
440
|
error: error instanceof Error ? error.message : "Unknown error",
|
|
341
441
|
});
|
|
342
442
|
cleanup();
|
|
343
|
-
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Failed to
|
|
443
|
+
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Failed to initialize authentication: " +
|
|
344
444
|
(error instanceof Error ? error.message : "Unknown error")));
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
// Add focus to the popup for better UX
|
|
348
|
-
try {
|
|
349
|
-
if (authWindow && !authWindow.closed) {
|
|
350
|
-
authWindow.focus();
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
catch {
|
|
354
|
-
// Ignore focus errors (cross-origin restrictions)
|
|
355
445
|
}
|
|
356
446
|
});
|
|
357
447
|
}
|