@nibssplc/cams-sdk-react 1.0.0-rc.23 → 1.0.0-rc.25

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.
@@ -11,6 +11,7 @@ export interface UseCAMSMSALAuthOptions {
11
11
  appCode: string;
12
12
  allowedOrigins?: string[];
13
13
  MFAEndpoint?: string;
14
+ activeCookiePeriod?: number;
14
15
  }
15
16
  export interface UseCAMSMSALAuthReturn {
16
17
  login: () => Promise<void>;
@@ -27,5 +28,6 @@ export interface UseCAMSMSALAuthReturn {
27
28
  completeMFA: (data: UserValidatedResponse) => Promise<MFAResponse>;
28
29
  setRequiresMFA: React.Dispatch<React.SetStateAction<boolean>>;
29
30
  sendEmailOTP: () => Promise<boolean>;
31
+ activeCookiePeriod: number;
30
32
  }
31
33
  export declare function useCAMSMSALAuth(options: UseCAMSMSALAuthOptions): UseCAMSMSALAuthReturn;
package/dist/index.cjs.js CHANGED
@@ -45,20 +45,38 @@ var DialogPrimitive__namespace = /*#__PURE__*/_interopNamespaceDefault(DialogPri
45
45
  if (typeof window !== 'undefined') {
46
46
  // Ensure crypto object exists
47
47
  if (!window.crypto) {
48
- window.crypto = {};
48
+ try {
49
+ Object.defineProperty(window, 'crypto', {
50
+ value: {},
51
+ writable: true,
52
+ configurable: true
53
+ });
54
+ }
55
+ catch (e) {
56
+ // Ignore if crypto cannot be defined
57
+ }
49
58
  }
50
59
  // Polyfill getRandomValues
51
- if (!window.crypto.getRandomValues) {
52
- window.crypto.getRandomValues = function (array) {
53
- var bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
54
- for (var i = 0; i < bytes.length; i++) {
55
- bytes[i] = Math.floor(Math.random() * 256);
56
- }
57
- return array;
58
- };
60
+ if (window.crypto && !window.crypto.getRandomValues) {
61
+ try {
62
+ Object.defineProperty(window.crypto, 'getRandomValues', {
63
+ value: function (array) {
64
+ var bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
65
+ for (var i = 0; i < bytes.length; i++) {
66
+ bytes[i] = Math.floor(Math.random() * 256);
67
+ }
68
+ return array;
69
+ },
70
+ writable: true,
71
+ configurable: true
72
+ });
73
+ }
74
+ catch (e) {
75
+ // Ignore if getRandomValues cannot be defined
76
+ }
59
77
  }
60
78
  // Polyfill crypto.subtle with SHA-256 (handles both undefined and null in non-HTTPS contexts)
61
- if (!window.crypto.subtle || window.location.protocol === 'http:') {
79
+ if (window.crypto && (!window.crypto.subtle || window.location.protocol === 'http:')) {
62
80
  var sha256_1 = function (data) { return __awaiter(void 0, void 0, void 0, function () {
63
81
  var bytes, hash, k, ml, msg, i, w, j, j, s0, s1, a, b, c, d, e, f, g, h, j, S1, ch, temp1, S0, maj, temp2, result, i;
64
82
  return __generator(this, function (_a) {
@@ -128,28 +146,46 @@ if (typeof window !== 'undefined') {
128
146
  return [2 /*return*/, result.buffer];
129
147
  });
130
148
  }); };
131
- window.crypto.subtle = {
132
- digest: function (algorithm, data) { return __awaiter(void 0, void 0, void 0, function () {
133
- var alg;
134
- return __generator(this, function (_a) {
135
- alg = typeof algorithm === 'string' ? algorithm : algorithm.name;
136
- if (alg === 'SHA-256')
137
- return [2 /*return*/, sha256_1(data)];
138
- console.error("Unsupported crypto algorithm: ".concat(alg));
139
- throw new Error("Unsupported algorithm: ".concat(alg));
140
- });
141
- }); }
142
- };
149
+ try {
150
+ Object.defineProperty(window.crypto, 'subtle', {
151
+ value: {
152
+ digest: function (algorithm, data) { return __awaiter(void 0, void 0, void 0, function () {
153
+ var alg;
154
+ return __generator(this, function (_a) {
155
+ alg = typeof algorithm === 'string' ? algorithm : algorithm.name;
156
+ if (alg === 'SHA-256')
157
+ return [2 /*return*/, sha256_1(data)];
158
+ console.error("Unsupported crypto algorithm: ".concat(alg));
159
+ throw new Error("Unsupported algorithm: ".concat(alg));
160
+ });
161
+ }); }
162
+ },
163
+ writable: true,
164
+ configurable: true
165
+ });
166
+ }
167
+ catch (e) {
168
+ // Ignore if subtle cannot be defined
169
+ }
143
170
  }
144
171
  // Polyfill randomUUID
145
- if (!window.crypto.randomUUID) {
146
- window.crypto.randomUUID = function () {
147
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
148
- var r = (Math.random() * 16) | 0;
149
- var v = c === 'x' ? r : (r & 0x3) | 0x8;
150
- return v.toString(16);
172
+ if (window.crypto && !window.crypto.randomUUID) {
173
+ try {
174
+ Object.defineProperty(window.crypto, 'randomUUID', {
175
+ value: function () {
176
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
177
+ var r = (Math.random() * 16) | 0;
178
+ var v = c === 'x' ? r : (r & 0x3) | 0x8;
179
+ return v.toString(16);
180
+ });
181
+ },
182
+ writable: true,
183
+ configurable: true
151
184
  });
152
- };
185
+ }
186
+ catch (e) {
187
+ // Ignore if randomUUID cannot be defined
188
+ }
153
189
  }
154
190
  }
155
191
 
@@ -383,6 +419,7 @@ function useCAMSAuth(options) {
383
419
  }
384
420
 
385
421
  var setCookie = function (name, value, days) {
422
+ if (days === void 0) { days = 1; }
386
423
  var expires = new Date(Date.now() + days * 24 * 60 * 60 * 1000).toUTCString();
387
424
  console.log("Auth Cookie Expires >>>", { name: name, value: value, expires: expires });
388
425
  document.cookie = "".concat(name, "=").concat(encodeURIComponent(value), "; expires=").concat(expires, "; path=/; samesite=Lax");
@@ -404,15 +441,15 @@ var deleteCookie = function (name) {
404
441
 
405
442
  function useCAMSMSALAuth(options) {
406
443
  var _this = this;
407
- var optStorageKey = options.storageKey, optScopes = options.scopes, prompt = options.prompt, appCode = options.appCode, MFAEndpoint = options.MFAEndpoint, onAuthSuccess = options.onAuthSuccess, onAuthError = options.onAuthError;
444
+ var optStorageKey = options.storageKey, optScopes = options.scopes, prompt = options.prompt, appCode = options.appCode, MFAEndpoint = options.MFAEndpoint, onAuthSuccess = options.onAuthSuccess, onAuthError = options.onAuthError, _a = options.activeCookiePeriod, activeCookiePeriod = _a === void 0 ? 1 : _a;
408
445
  var storageKey = optStorageKey || "CAMS-MSAL-AUTH-SDK";
409
- var _a = msalReact.useMsal(), instance = _a.instance, inProgress = _a.inProgress, accounts = _a.accounts;
446
+ var _b = msalReact.useMsal(), instance = _b.instance, inProgress = _b.inProgress, accounts = _b.accounts;
410
447
  var account = msalReact.useAccount(accounts[0] || {});
411
- var _b = React.useState(null), error = _b[0], setError = _b[1];
412
- var _c = React.useState(""), idToken = _c[0], setIdToken = _c[1];
413
- var _d = React.useState(""), accessToken = _d[0], setAccessToken = _d[1];
414
- var _e = React.useState(null), mfaAuthenticator = _e[0], setMfaAuthenticator = _e[1];
415
- var _f = React.useState(false), requiresMFA = _f[0], setRequiresMFA = _f[1];
448
+ var _c = React.useState(null), error = _c[0], setError = _c[1];
449
+ var _d = React.useState(""), idToken = _d[0], setIdToken = _d[1];
450
+ var _e = React.useState(""), accessToken = _e[0], setAccessToken = _e[1];
451
+ var _f = React.useState(null), mfaAuthenticator = _f[0], setMfaAuthenticator = _f[1];
452
+ var _g = React.useState(false), requiresMFA = _g[0], setRequiresMFA = _g[1];
416
453
  var isLoading = inProgress !== msalBrowser.InteractionStatus.None;
417
454
  var isAuthenticated = !!account && !!accessToken && !requiresMFA;
418
455
  var scopes = optScopes || ["openid", "profile", "email"];
@@ -579,7 +616,7 @@ function useCAMSMSALAuth(options) {
579
616
  accessToken: accessToken,
580
617
  idToken: idToken,
581
618
  }));
582
- setCookie("CAMS-MSAL-AUTH-SDK-PROFILE", JSON.stringify({ type: "AUTH_SUCCESS", userProfile: __assign({}, data) }), 1);
619
+ setCookie("CAMS-MSAL-AUTH-SDK-PROFILE", JSON.stringify({ type: "AUTH_SUCCESS", userProfile: __assign({}, data) }), activeCookiePeriod);
583
620
  setRequiresMFA(false);
584
621
  // Set requiresMFA to false after storage update
585
622
  camsSdk.Logger.debug("MFA completed successfully, storage updated", {
@@ -598,7 +635,7 @@ function useCAMSMSALAuth(options) {
598
635
  }
599
636
  return [2 /*return*/];
600
637
  });
601
- }); }, [mfaAuthenticator, accessToken, idToken, storageKey]);
638
+ }); }, [mfaAuthenticator, accessToken, idToken, storageKey, activeCookiePeriod]);
602
639
  var sendEmailOTP = React.useCallback(function () { return __awaiter$1(_this, void 0, void 0, function () {
603
640
  return __generator$1(this, function (_a) {
604
641
  switch (_a.label) {
@@ -654,7 +691,8 @@ function useCAMSMSALAuth(options) {
654
691
  requiresMFA: requiresMFA,
655
692
  completeMFA: completeMFA,
656
693
  sendEmailOTP: sendEmailOTP,
657
- setRequiresMFA: setRequiresMFA
694
+ setRequiresMFA: setRequiresMFA,
695
+ activeCookiePeriod: activeCookiePeriod,
658
696
  };
659
697
  }
660
698