@nibssplc/cams-sdk-react 0.0.1-beta.95 → 0.0.1-beta.97

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/index.esm.js CHANGED
@@ -536,11 +536,11 @@ function base64urlToArrayBuffer(base64url) {
536
536
  */
537
537
  function arrayBufferToBase64url(buffer) {
538
538
  var bytes = new Uint8Array(buffer);
539
- var binaryStr = String.fromCharCode.apply(null, bytes);
539
+ var binaryStr = Array.from(bytes, function (b) { return String.fromCharCode(b); }).join("");
540
540
  return btoa(binaryStr)
541
541
  .replace(/\+/g, "-")
542
542
  .replace(/\//g, "_")
543
- .replace(/=/g, "");
543
+ .replace(/=+$/, ""); // use regex to remove all trailing '='
544
544
  }
545
545
 
546
546
  /**
@@ -554,21 +554,23 @@ function arrayBufferToBase64url(buffer) {
554
554
  */
555
555
  function register(options) {
556
556
  return __awaiter(this, void 0, void 0, function () {
557
- var createOptions, credential, publicKeyCredential, attestationResponse;
558
- return __generator(this, function (_a) {
559
- switch (_a.label) {
557
+ var createOptions, credential, publicKeyCredential, attestationResponse, transports, err_1;
558
+ var _a, _b, _c;
559
+ return __generator(this, function (_d) {
560
+ switch (_d.label) {
560
561
  case 0:
562
+ _d.trys.push([0, 2, , 3]);
561
563
  createOptions = __assign(__assign({}, options), { challenge: base64urlToArrayBuffer(options.challenge), user: __assign(__assign({}, options.user), { id: base64urlToArrayBuffer(options.user.id) }) });
562
564
  return [4 /*yield*/, navigator.credentials.create({
563
565
  publicKey: createOptions,
564
566
  })];
565
567
  case 1:
566
- credential = _a.sent();
567
- if (!credential) {
568
- throw new Error("Failed to create credential.");
569
- }
568
+ credential = _d.sent();
569
+ if (!credential)
570
+ throw new Error("No credential created.");
570
571
  publicKeyCredential = credential;
571
572
  attestationResponse = publicKeyCredential.response;
573
+ transports = (_c = (_b = (_a = publicKeyCredential.response).getTransports) === null || _b === void 0 ? void 0 : _b.call(_a)) !== null && _c !== void 0 ? _c : [];
572
574
  return [2 /*return*/, {
573
575
  id: publicKeyCredential.id,
574
576
  rawId: arrayBufferToBase64url(publicKeyCredential.rawId),
@@ -576,8 +578,19 @@ function register(options) {
576
578
  response: {
577
579
  clientDataJSON: arrayBufferToBase64url(attestationResponse.clientDataJSON),
578
580
  attestationObject: arrayBufferToBase64url(attestationResponse.attestationObject),
581
+ transports: transports,
579
582
  },
580
583
  }];
584
+ case 2:
585
+ err_1 = _d.sent();
586
+ if (err_1.name === "NotAllowedError") {
587
+ throw new Error("Registration cancelled by user.");
588
+ }
589
+ if (err_1.name === "InvalidStateError") {
590
+ throw new Error("Passkey already registered for this user.");
591
+ }
592
+ throw err_1;
593
+ case 3: return [2 /*return*/];
581
594
  }
582
595
  });
583
596
  });
@@ -1844,10 +1857,11 @@ var MFAOptions = function (_a) {
1844
1857
  var authenticate = useWebAuthn().authenticate;
1845
1858
  var handleFIDOLogin = function () { return __awaiter(void 0, void 0, void 0, function () {
1846
1859
  var options, assertionResponse, error_1;
1847
- return __generator(this, function (_a) {
1848
- switch (_a.label) {
1860
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
1861
+ return __generator(this, function (_o) {
1862
+ switch (_o.label) {
1849
1863
  case 0:
1850
- _a.trys.push([0, 4, , 5]);
1864
+ _o.trys.push([0, 4, , 5]);
1851
1865
  // 1. Fetch authentication challenge from your server
1852
1866
  console.log("Requesting authentication challenge from server...");
1853
1867
  return [4 /*yield*/, axios.post(MFAEndpoints.RetrieveAuthChallenge, {}, {
@@ -1857,13 +1871,20 @@ var MFAOptions = function (_a) {
1857
1871
  },
1858
1872
  })];
1859
1873
  case 1:
1860
- options = (_a.sent()).data;
1874
+ options = (_o.sent()).data;
1861
1875
  console.log("Received challenge:", options);
1876
+ __assign(__assign({}, options), { pubKeyCredParams: (_a = options.pubKeyCredParams) === null || _a === void 0 ? void 0 : _a.map(function (param) { return ({
1877
+ type: "public-key",
1878
+ alg: param.alg
1879
+ }); }), attestation: ((_b = options.attestation) === null || _b === void 0 ? void 0 : _b.toLowerCase()) || "none", authenticatorSelection: __assign(__assign({}, options.authenticatorSelection), { residentKey: ((_d = (_c = options.authenticatorSelection) === null || _c === void 0 ? void 0 : _c.residentKey) === null || _d === void 0 ? void 0 : _d.toLowerCase()) || "discouraged", userVerification: ((_f = (_e = options.authenticatorSelection) === null || _e === void 0 ? void 0 : _e.userVerification) === null || _f === void 0 ? void 0 : _f.toLowerCase()) || "preferred" }) });
1862
1880
  // 2. Call the SDK to trigger the browser's passkey authentication UI
1863
1881
  console.log("Calling SDK authenticate function...");
1864
- return [4 /*yield*/, authenticate(options)];
1882
+ return [4 /*yield*/, authenticate(__assign(__assign({}, options), { pubKeyCredParams: (_g = options.pubKeyCredParams) === null || _g === void 0 ? void 0 : _g.map(function (param) { return ({
1883
+ type: "public-key",
1884
+ alg: param.alg
1885
+ }); }), attestation: ((_h = options.attestation) === null || _h === void 0 ? void 0 : _h.toLowerCase()) || "none", authenticatorSelection: __assign(__assign({}, options.authenticatorSelection), { residentKey: ((_k = (_j = options.authenticatorSelection) === null || _j === void 0 ? void 0 : _j.residentKey) === null || _k === void 0 ? void 0 : _k.toLowerCase()) || "discouraged", userVerification: ((_m = (_l = options.authenticatorSelection) === null || _l === void 0 ? void 0 : _l.userVerification) === null || _m === void 0 ? void 0 : _m.toLowerCase()) || "preferred" }) }))];
1865
1886
  case 2:
1866
- assertionResponse = _a.sent();
1887
+ assertionResponse = _o.sent();
1867
1888
  console.log("Authentication assertion received from client:", assertionResponse);
1868
1889
  // 3. Send the assertion back to the server for verification
1869
1890
  console.log("Sending assertion to server for verification...");
@@ -1875,11 +1896,11 @@ var MFAOptions = function (_a) {
1875
1896
  withCredentials: true, // credentials: 'include'
1876
1897
  })];
1877
1898
  case 3:
1878
- _a.sent();
1899
+ _o.sent();
1879
1900
  toast.success("🔑 Sign-in successful!");
1880
1901
  return [3 /*break*/, 5];
1881
1902
  case 4:
1882
- error_1 = _a.sent();
1903
+ error_1 = _o.sent();
1883
1904
  console.error("Authentication failed:", error_1);
1884
1905
  toast.error("❌ Could not sign in.");
1885
1906
  return [3 /*break*/, 5];