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