@nibssplc/cams-sdk 0.0.1-beta.36 → 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/README.md +155 -0
- package/dist/auth.d.ts +12 -2
- package/dist/index.cjs.js +253 -270
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.esm.js +254 -265
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +253 -270
- package/dist/index.umd.js.map +1 -1
- package/package.json +2 -1
- package/dist/popupAuth.d.ts +0 -20
- /package/dist/{mfaAuth.d.ts → MFAAuth.d.ts} +0 -0
package/dist/index.umd.js
CHANGED
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
LogLevel[LogLevel["DEBUG"] = 3] = "DEBUG";
|
|
59
59
|
})(exports.LogLevel || (exports.LogLevel = {}));
|
|
60
60
|
class Logger {
|
|
61
|
-
static level = exports.LogLevel.
|
|
61
|
+
static level = exports.LogLevel.DEBUG;
|
|
62
62
|
static prefix = '[CAMS-SDK]';
|
|
63
63
|
static isTestEnv = typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'test';
|
|
64
64
|
static setLevel(level) {
|
|
@@ -87,16 +87,31 @@
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
// Capture original functions to prevent tampering (client-side only)
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
90
|
+
// Constants
|
|
91
|
+
const DEFAULT_AUTH_TIMEOUT = 300000; // 5 minutes
|
|
92
|
+
const DEFAULT_IDLE_TIMEOUT = 120000; // 2 minutes
|
|
93
|
+
const INITIAL_POLL_INTERVAL = 1000;
|
|
94
|
+
const MAX_POLL_INTERVAL = 5000;
|
|
95
|
+
const WINDOW_FEATURES = "scrollbars=yes,resizable=yes";
|
|
96
|
+
// Secure reference to native functions
|
|
97
|
+
const NativeFunctions = {
|
|
98
|
+
addEventListener: typeof window !== "undefined" ? window.addEventListener : null,
|
|
99
|
+
removeEventListener: typeof window !== "undefined" ? window.removeEventListener : null,
|
|
100
|
+
setTimeout: typeof window !== "undefined" ? window.setTimeout : setTimeout,
|
|
101
|
+
clearTimeout: typeof window !== "undefined" ? window.clearTimeout : 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
|
+
*/
|
|
100
115
|
const isPopupWindow = () => {
|
|
101
116
|
if (typeof window === "undefined")
|
|
102
117
|
return false;
|
|
@@ -107,354 +122,330 @@
|
|
|
107
122
|
return false;
|
|
108
123
|
}
|
|
109
124
|
};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
125
|
+
/**
|
|
126
|
+
* Safely close a window with error handling for cross-origin restrictions
|
|
127
|
+
*/
|
|
128
|
+
const safeWindowClose = (window) => {
|
|
129
|
+
if (!window)
|
|
113
130
|
return;
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
window.opener.postMessage(data, targetOrigin);
|
|
131
|
+
try {
|
|
132
|
+
if (!window.closed) {
|
|
117
133
|
window.close();
|
|
118
134
|
}
|
|
119
|
-
catch (error) {
|
|
120
|
-
console.error("Failed to forward auth to parent:", error);
|
|
121
|
-
}
|
|
122
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 };
|
|
123
151
|
};
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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);
|
|
128
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
|
+
};
|
|
129
195
|
};
|
|
196
|
+
/**
|
|
197
|
+
* Validate configuration and return parsed URL
|
|
198
|
+
*/
|
|
130
199
|
function validateConfig(config) {
|
|
131
|
-
// Validate URL format and parse once
|
|
132
200
|
const urlValidation = URLSchema.safeParse(config.camsUrl);
|
|
133
201
|
if (!urlValidation.success) {
|
|
134
202
|
throw new CAMSError(exports.CAMSErrorType.INVALID_URL, "Invalid CAMS URL format");
|
|
135
203
|
}
|
|
136
|
-
// Domain validation is mandatory for security
|
|
137
204
|
if (!config.messageOrigin) {
|
|
138
205
|
throw new CAMSError(exports.CAMSErrorType.INVALID_URL, "Message Origin must be specified for security");
|
|
139
206
|
}
|
|
140
|
-
|
|
141
|
-
return url;
|
|
207
|
+
return new URL(config.camsUrl);
|
|
142
208
|
}
|
|
209
|
+
/**
|
|
210
|
+
* Open CAMS authentication popup and handle the authentication flow
|
|
211
|
+
*/
|
|
143
212
|
function openCAMSPopUpLogin(config) {
|
|
144
213
|
return new Promise((resolve, reject) => {
|
|
214
|
+
// Environment validation
|
|
145
215
|
if (typeof window === "undefined") {
|
|
146
216
|
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Cannot open popup in server-side environment"));
|
|
147
217
|
return;
|
|
148
218
|
}
|
|
149
|
-
if (config.debug)
|
|
150
|
-
Logger.setLevel(exports.LogLevel.DEBUG);
|
|
151
|
-
// If this is a popup window, don't open another popup
|
|
152
219
|
if (isPopupWindow()) {
|
|
153
|
-
Logger.warn("
|
|
220
|
+
Logger.warn("Attempted to open popup from within popup window");
|
|
154
221
|
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Cannot open authentication popup from within popup window"));
|
|
155
222
|
return;
|
|
156
223
|
}
|
|
224
|
+
// Initialize logging
|
|
225
|
+
if (config.debug)
|
|
226
|
+
Logger.setLevel(exports.LogLevel.DEBUG);
|
|
157
227
|
Logger.info("Starting CAMS authentication", { url: config.camsUrl });
|
|
228
|
+
// Validate configuration
|
|
158
229
|
let validatedUrl;
|
|
159
230
|
try {
|
|
160
231
|
validatedUrl = validateConfig(config);
|
|
161
|
-
Logger.debug("
|
|
232
|
+
Logger.debug("Configuration validation passed");
|
|
162
233
|
}
|
|
163
234
|
catch (error) {
|
|
164
|
-
Logger.error("
|
|
235
|
+
Logger.error("Configuration validation failed", {
|
|
165
236
|
error: error instanceof Error ? error.message : "Unknown error",
|
|
166
237
|
});
|
|
167
238
|
reject(error);
|
|
168
239
|
return;
|
|
169
240
|
}
|
|
170
|
-
|
|
171
|
-
|
|
241
|
+
// Extract configuration with defaults
|
|
242
|
+
const { messageOrigin, windowHeight = 600, windowWidth = 500, authTimeout = DEFAULT_AUTH_TIMEOUT, idleTimeout = DEFAULT_IDLE_TIMEOUT,
|
|
243
|
+
// allowedRedirectDomains = defaultMicrosoftDomains,
|
|
172
244
|
} = config;
|
|
173
|
-
|
|
245
|
+
// State management
|
|
246
|
+
let authWindow = null;
|
|
174
247
|
let authTimeoutId = null;
|
|
175
248
|
let idleTimeoutId = null;
|
|
176
249
|
let checkClosedInterval = null;
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
});
|
|
182
|
-
// Center the popup window
|
|
183
|
-
const left = (window.screen.width - windowWidth) / 2;
|
|
184
|
-
const top = (window.screen.height - windowHeight) / 2;
|
|
185
|
-
const secureWindowName = generateSecureWindowName();
|
|
186
|
-
authWindow = originalWindowOpen
|
|
187
|
-
? originalWindowOpen.call(window, validatedUrl.toString(), secureWindowName, `width=${windowWidth},height=${windowHeight},left=${left},top=${top},scrollbars=yes,resizable=yes`)
|
|
188
|
-
: null;
|
|
189
|
-
// Track active window for manual closure
|
|
190
|
-
activeAuthWindow = authWindow;
|
|
191
|
-
}
|
|
192
|
-
catch (error) {
|
|
193
|
-
Logger.error("Failed to open popup window", {
|
|
194
|
-
error: error instanceof Error ? error.message : "Unknown error",
|
|
195
|
-
});
|
|
196
|
-
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Popup blocked by browser. Please allow popups and try again."));
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
if (!authWindow) {
|
|
200
|
-
Logger.error("Popup window blocked");
|
|
201
|
-
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Popup blocked by browser. Please allow popups and try again."));
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
// Only check for immediate closure (true popup blocking)
|
|
205
|
-
if (authWindow.closed) {
|
|
206
|
-
Logger.error("Popup immediately closed by blocker");
|
|
207
|
-
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Popup blocked by browser. Please allow popups and try again."));
|
|
208
|
-
return;
|
|
209
|
-
}
|
|
210
|
-
Logger.debug("Popup window opened successfully");
|
|
250
|
+
let removeMessageListener = null;
|
|
251
|
+
/**
|
|
252
|
+
* Clean up all resources and timeouts
|
|
253
|
+
*/
|
|
211
254
|
const cleanup = () => {
|
|
212
|
-
if (
|
|
213
|
-
|
|
255
|
+
if (removeMessageListener) {
|
|
256
|
+
removeMessageListener();
|
|
257
|
+
removeMessageListener = null;
|
|
214
258
|
}
|
|
215
259
|
if (idleTimeoutId)
|
|
216
|
-
|
|
260
|
+
NativeFunctions.clearTimeout(idleTimeoutId);
|
|
217
261
|
if (authTimeoutId)
|
|
218
|
-
|
|
262
|
+
NativeFunctions.clearTimeout(authTimeoutId);
|
|
219
263
|
if (checkClosedInterval)
|
|
220
|
-
|
|
264
|
+
NativeFunctions.clearTimeout(checkClosedInterval);
|
|
221
265
|
idleTimeoutId = null;
|
|
222
266
|
authTimeoutId = null;
|
|
223
267
|
checkClosedInterval = null;
|
|
224
268
|
};
|
|
225
|
-
|
|
226
|
-
|
|
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) => {
|
|
227
318
|
Logger.debug("Received message", {
|
|
228
|
-
origin:
|
|
319
|
+
origin: event.origin,
|
|
229
320
|
expectedOrigin: messageOrigin,
|
|
230
|
-
data:
|
|
321
|
+
data: event.data,
|
|
231
322
|
});
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
? new URL(messageOrigin).hostname
|
|
235
|
-
: messageOrigin;
|
|
236
|
-
// Allow Microsoft redirect domains for Azure AD authentication
|
|
237
|
-
const isMicrosoftDomain = originUrl.hostname.endsWith(".microsoft.com") ||
|
|
238
|
-
originUrl.hostname.endsWith(".microsoftonline.com") ||
|
|
239
|
-
originUrl.hostname.endsWith(".live.com");
|
|
240
|
-
const isAllowedOrigin = originUrl.hostname === expectedHostname ||
|
|
241
|
-
originUrl.hostname.endsWith("." + expectedHostname) ||
|
|
242
|
-
isMicrosoftDomain;
|
|
243
|
-
if (!isAllowedOrigin) {
|
|
323
|
+
// Security: Validate origin
|
|
324
|
+
if (!isValidOrigin(event.origin, messageOrigin, defaultMicrosoftDomains)) {
|
|
244
325
|
Logger.warn("Blocked message from unauthorized origin", {
|
|
245
|
-
origin:
|
|
326
|
+
origin: event.origin,
|
|
246
327
|
expected: messageOrigin,
|
|
247
|
-
data:
|
|
328
|
+
data: event.data,
|
|
248
329
|
});
|
|
249
330
|
return;
|
|
250
331
|
}
|
|
251
|
-
// Validate
|
|
252
|
-
if (
|
|
332
|
+
// Security: Validate message source
|
|
333
|
+
if (event.source !== authWindow) {
|
|
253
334
|
Logger.warn("Message from unexpected source", {
|
|
254
|
-
source:
|
|
255
|
-
origin:
|
|
335
|
+
source: event.source,
|
|
336
|
+
origin: event.origin,
|
|
256
337
|
expected: messageOrigin,
|
|
257
|
-
data:
|
|
338
|
+
data: event.data,
|
|
258
339
|
});
|
|
259
340
|
return;
|
|
260
341
|
}
|
|
261
|
-
//
|
|
262
|
-
if (
|
|
263
|
-
Logger.warn("Message from unexpected source", {
|
|
264
|
-
source: messageEvent.source,
|
|
265
|
-
origin: messageEvent.origin,
|
|
266
|
-
expected: messageOrigin,
|
|
267
|
-
data: messageEvent.data,
|
|
268
|
-
});
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
// Ignore heartbeat messages
|
|
272
|
-
if (messageEvent.data?.type === "heartbeat") {
|
|
342
|
+
// Handle heartbeat messages (reset idle timeout)
|
|
343
|
+
if (event.data?.type === "heartbeat") {
|
|
273
344
|
Logger.debug("Received heartbeat message");
|
|
274
345
|
resetIdleTimeout();
|
|
275
346
|
return;
|
|
276
347
|
}
|
|
277
|
-
//
|
|
278
|
-
|
|
279
|
-
|
|
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) {
|
|
280
358
|
Logger.info("Authentication successful");
|
|
281
359
|
cleanupAndClose();
|
|
282
|
-
resolve(
|
|
360
|
+
resolve(profileValidation.data);
|
|
283
361
|
return;
|
|
284
362
|
}
|
|
285
|
-
|
|
286
|
-
|
|
363
|
+
// Authentication failure case
|
|
364
|
+
const authFailureValidation = AuthFailureMessageSchema.safeParse(data);
|
|
365
|
+
if (authFailureValidation.success) {
|
|
366
|
+
const { error, errorCode, errorDetails } = authFailureValidation.data;
|
|
287
367
|
Logger.warn("Authentication failure received", {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
368
|
+
error,
|
|
369
|
+
errorCode,
|
|
370
|
+
errorDetails,
|
|
291
371
|
});
|
|
292
|
-
cleanupAndClose(new CAMSError(exports.CAMSErrorType.API_VALIDATION_ERROR,
|
|
372
|
+
cleanupAndClose(new CAMSError(exports.CAMSErrorType.API_VALIDATION_ERROR, `Authentication failed: ${error}${errorCode ? ` (${errorCode})` : ""}`));
|
|
293
373
|
return;
|
|
294
374
|
}
|
|
295
|
-
|
|
296
|
-
|
|
375
|
+
// Generic error case
|
|
376
|
+
const errorValidation = ErrorMessageSchema.safeParse(data);
|
|
377
|
+
if (errorValidation.success) {
|
|
297
378
|
Logger.warn("Authentication error received", {
|
|
298
|
-
error:
|
|
379
|
+
error: errorValidation.data.error,
|
|
299
380
|
});
|
|
300
|
-
cleanupAndClose(new CAMSError(exports.CAMSErrorType.USER_CANCELLED,
|
|
381
|
+
cleanupAndClose(new CAMSError(exports.CAMSErrorType.USER_CANCELLED, `Authentication failed: ${errorValidation.data.error}`));
|
|
301
382
|
return;
|
|
302
383
|
}
|
|
384
|
+
// Invalid message format
|
|
303
385
|
Logger.error("Invalid message format received", {
|
|
304
|
-
messageData:
|
|
305
|
-
dataType: typeof
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
errorValidationError: errorMsg.error
|
|
310
|
-
? errorMsg.error.issues
|
|
311
|
-
: undefined,
|
|
386
|
+
messageData: data,
|
|
387
|
+
dataType: typeof data,
|
|
388
|
+
profileValidationError: profileValidation.error?.issues,
|
|
389
|
+
authFailureValidationError: authFailureValidation.error?.issues,
|
|
390
|
+
errorValidationError: errorValidation.error?.issues,
|
|
312
391
|
});
|
|
313
392
|
cleanupAndClose(new CAMSError(exports.CAMSErrorType.INVALID_ORIGIN, "Invalid message format"));
|
|
314
393
|
};
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
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;
|
|
325
411
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
// Timeout handler
|
|
331
|
-
if (idleTimeoutId)
|
|
332
|
-
originalClearTimeout(idleTimeoutId);
|
|
333
|
-
Logger.debug("Resetting idle timeout", { idleTimeout });
|
|
334
|
-
idleTimeoutId = originalSetTimeout(() => {
|
|
335
|
-
Logger.warn("Authentication idle timeout reached", { idleTimeout });
|
|
336
|
-
cleanupAndClose(new CAMSError(exports.CAMSErrorType.TIMEOUT, `Authentication timed out due to inactivity after ${idleTimeout}ms`));
|
|
337
|
-
}, idleTimeout);
|
|
338
|
-
};
|
|
339
|
-
Logger.debug("Setting auth timeout", { authTimeout });
|
|
340
|
-
authTimeoutId = originalSetTimeout(() => {
|
|
341
|
-
Logger.warn("Authentication auth timeout reached", { authTimeout });
|
|
342
|
-
cleanupAndClose(new CAMSError(exports.CAMSErrorType.TIMEOUT, `Authentication process exceeded auth time limit of ${authTimeout}ms`));
|
|
343
|
-
}, authTimeout);
|
|
344
|
-
resetIdleTimeout();
|
|
345
|
-
// Use exponential backoff for popup closure checking
|
|
346
|
-
let pollInterval = 1000; // Start with 1 second
|
|
347
|
-
const maxPollInterval = 5000; // Max 5 seconds
|
|
348
|
-
const checkClosed = () => {
|
|
349
|
-
if (authWindow?.closed) {
|
|
350
|
-
Logger.info("Authentication window closed by user");
|
|
351
|
-
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."));
|
|
352
416
|
return;
|
|
353
417
|
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
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();
|
|
363
433
|
}
|
|
364
|
-
|
|
365
|
-
|
|
434
|
+
catch (error) {
|
|
435
|
+
Logger.debug("Could not focus popup window (cross-origin restriction)", error);
|
|
366
436
|
}
|
|
367
437
|
}
|
|
368
438
|
catch (error) {
|
|
369
|
-
Logger.error("Failed to
|
|
439
|
+
Logger.error("Failed to initialize authentication popup", {
|
|
370
440
|
error: error instanceof Error ? error.message : "Unknown error",
|
|
371
441
|
});
|
|
372
442
|
cleanup();
|
|
373
|
-
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Failed to
|
|
443
|
+
reject(new CAMSError(exports.CAMSErrorType.POPUP_BLOCKED, "Failed to initialize authentication: " +
|
|
374
444
|
(error instanceof Error ? error.message : "Unknown error")));
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
// Add focus to the popup for better UX
|
|
378
|
-
try {
|
|
379
|
-
if (authWindow && !authWindow.closed) {
|
|
380
|
-
authWindow.focus();
|
|
381
|
-
}
|
|
382
|
-
}
|
|
383
|
-
catch {
|
|
384
|
-
// Ignore focus errors (cross-origin restrictions)
|
|
385
445
|
}
|
|
386
446
|
});
|
|
387
447
|
}
|
|
388
448
|
|
|
389
|
-
/**
|
|
390
|
-
* Initialize popup authentication handler
|
|
391
|
-
* This should be called in the popup window to handle auth completion
|
|
392
|
-
*/
|
|
393
|
-
function initializePopupAuth(targetOrigin) {
|
|
394
|
-
if (!isPopupWindow()) {
|
|
395
|
-
Logger.debug("Not in popup window, skipping popup auth initialization");
|
|
396
|
-
return;
|
|
397
|
-
}
|
|
398
|
-
Logger.info("Initializing popup authentication handler");
|
|
399
|
-
// Listen for auth completion events from the popup app
|
|
400
|
-
const handleAuthComplete = (profile) => {
|
|
401
|
-
Logger.info("Authentication completed in popup, forwarding to parent");
|
|
402
|
-
forwardAuthToParent(profile, targetOrigin);
|
|
403
|
-
};
|
|
404
|
-
// Listen for auth errors
|
|
405
|
-
const handleAuthError = (error) => {
|
|
406
|
-
Logger.warn("Authentication error in popup, forwarding to parent");
|
|
407
|
-
forwardAuthToParent({
|
|
408
|
-
type: "AUTH_FAILURE",
|
|
409
|
-
error: error.message || "Authentication failed",
|
|
410
|
-
errorCode: error.code,
|
|
411
|
-
errorDetails: error
|
|
412
|
-
}, targetOrigin);
|
|
413
|
-
};
|
|
414
|
-
// Expose global handlers for the popup app to use
|
|
415
|
-
window.__CAMS_POPUP_AUTH__ = {
|
|
416
|
-
completeAuth: handleAuthComplete,
|
|
417
|
-
errorAuth: handleAuthError,
|
|
418
|
-
isPopup: true
|
|
419
|
-
};
|
|
420
|
-
Logger.debug("Popup authentication handlers registered");
|
|
421
|
-
}
|
|
422
|
-
/**
|
|
423
|
-
* Complete authentication in popup window
|
|
424
|
-
* Call this when authentication is successful in the popup
|
|
425
|
-
*/
|
|
426
|
-
function completePopupAuth(profile, targetOrigin) {
|
|
427
|
-
if (!isPopupWindow()) {
|
|
428
|
-
Logger.warn("completePopupAuth called outside of popup window");
|
|
429
|
-
return;
|
|
430
|
-
}
|
|
431
|
-
Logger.info("Completing popup authentication");
|
|
432
|
-
forwardAuthToParent(profile, targetOrigin);
|
|
433
|
-
}
|
|
434
|
-
/**
|
|
435
|
-
* Report authentication error in popup window
|
|
436
|
-
* Call this when authentication fails in the popup
|
|
437
|
-
*/
|
|
438
|
-
function errorPopupAuth(error, targetOrigin) {
|
|
439
|
-
if (!isPopupWindow()) {
|
|
440
|
-
Logger.warn("errorPopupAuth called outside of popup window");
|
|
441
|
-
return;
|
|
442
|
-
}
|
|
443
|
-
Logger.warn("Reporting popup authentication error");
|
|
444
|
-
forwardAuthToParent({
|
|
445
|
-
type: "AUTH_FAILURE",
|
|
446
|
-
error: error.message || "Authentication failed",
|
|
447
|
-
errorCode: error.code,
|
|
448
|
-
errorDetails: error
|
|
449
|
-
}, targetOrigin);
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Check if current window is a CAMS popup
|
|
453
|
-
*/
|
|
454
|
-
function isCAMSPopup() {
|
|
455
|
-
return isPopupWindow() && !!window.__CAMS_POPUP_AUTH__;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
449
|
class CAMSSessionManager {
|
|
459
450
|
storage;
|
|
460
451
|
keyPrefix;
|
|
@@ -663,13 +654,11 @@
|
|
|
663
654
|
this.profile = profile;
|
|
664
655
|
this.setToken(profile.userProfile.tokens.Bearer);
|
|
665
656
|
Logger.info("Authentication completed in popup, forwarding to parent");
|
|
666
|
-
completePopupAuth(profile, targetOrigin);
|
|
667
657
|
}
|
|
668
658
|
catch (error) {
|
|
669
659
|
Logger.error("Failed to complete popup authentication", {
|
|
670
660
|
error: error instanceof Error ? error.message : "Unknown error"
|
|
671
661
|
});
|
|
672
|
-
errorPopupAuth(error, targetOrigin);
|
|
673
662
|
}
|
|
674
663
|
}
|
|
675
664
|
/**
|
|
@@ -681,7 +670,6 @@
|
|
|
681
670
|
return;
|
|
682
671
|
}
|
|
683
672
|
Logger.warn("Reporting authentication error from popup");
|
|
684
|
-
errorPopupAuth(error, targetOrigin);
|
|
685
673
|
}
|
|
686
674
|
}
|
|
687
675
|
// 🔹 Safe fallback for Node/SSR
|
|
@@ -750,6 +738,7 @@
|
|
|
750
738
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
751
739
|
}
|
|
752
740
|
const data = await response.json();
|
|
741
|
+
console.log("MFA Response >>>", data);
|
|
753
742
|
if (data.isAuthenticated) {
|
|
754
743
|
Logger.info("MFA verification successful");
|
|
755
744
|
this.resetAttempts();
|
|
@@ -783,13 +772,7 @@
|
|
|
783
772
|
exports.Logger = Logger;
|
|
784
773
|
exports.ProfileSchema = ProfileSchema;
|
|
785
774
|
exports.URLSchema = URLSchema;
|
|
786
|
-
exports.closeWindow = closeWindow;
|
|
787
|
-
exports.completePopupAuth = completePopupAuth;
|
|
788
|
-
exports.errorPopupAuth = errorPopupAuth;
|
|
789
|
-
exports.forwardAuthToParent = forwardAuthToParent;
|
|
790
775
|
exports.generateSecureWindowName = generateSecureWindowName;
|
|
791
|
-
exports.initializePopupAuth = initializePopupAuth;
|
|
792
|
-
exports.isCAMSPopup = isCAMSPopup;
|
|
793
776
|
exports.isPopupWindow = isPopupWindow;
|
|
794
777
|
exports.openCAMSPopUpLogin = openCAMSPopUpLogin;
|
|
795
778
|
exports.validateConfig = validateConfig;
|