wallaby-cash 2.0.1-alpha.26 → 2.0.1-alpha.28

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.cjs.js CHANGED
@@ -7,8 +7,6 @@ const axios = require('axios');
7
7
  const log = require('loglevel');
8
8
  const cryptoJs = require('crypto-js');
9
9
  const jwtDecode = require('jwt-decode');
10
- const jsBase64 = require('js-base64');
11
- const base64url = require('base64url');
12
10
  const lodash = require('lodash');
13
11
  const browser$2 = require('@simplewebauthn/browser');
14
12
 
@@ -16,7 +14,6 @@ const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e
16
14
 
17
15
  const axios__default = /*#__PURE__*/_interopDefaultCompat(axios);
18
16
  const log__default = /*#__PURE__*/_interopDefaultCompat(log);
19
- const base64url__default = /*#__PURE__*/_interopDefaultCompat(base64url);
20
17
 
21
18
  /******************************************************************************
22
19
  Copyright (c) Microsoft Corporation.
@@ -514,7 +511,6 @@ var defaultEnvironment = (_a = browser$1.env.NODE_ENV) !== null && _a !== void 0
514
511
  */
515
512
  var defaultNetwork = 'testnet';
516
513
  var isTest = browser$1.env.NODE_ENV === 'test';
517
- var PUBLIC_KEY_CREDENTIAL_TIMEOUT = 60000;
518
514
  var DEFAULT_ENTROPY_BYTES$1 = 32;
519
515
  var PBKDF2_ITERATIONS = 131072;
520
516
  var DEFAULT_PRIMARY_SHARE_INDEX = 0;
@@ -531,7 +527,7 @@ var DEFAULT_HOSTNAME = 'https://www.republic.com';
531
527
 
532
528
  https://chromium.googlesource.com/chromium/src/+/main/content/browser/webauth/pub_key_cred_params.md
533
529
  */
534
- var PUBLIC_KEY_CREDENTIAL_PARAM = [
530
+ [
535
531
  { type: 'public-key', alg: exports.PublicKeyCredentialAlgorithm.RS256 },
536
532
  { type: 'public-key', alg: exports.PublicKeyCredentialAlgorithm.ES256 },
537
533
  ];
@@ -4888,7 +4884,7 @@ var Base = /** @class */ (function () {
4888
4884
  if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.storage) {
4889
4885
  return this.options.storage;
4890
4886
  }
4891
- if (window && window.localStorage) {
4887
+ if (window && (window === null || window === void 0 ? void 0 : window.localStorage)) {
4892
4888
  return window.localStorage;
4893
4889
  }
4894
4890
  throw new Error('Your device storage is not supported natively yet, please provide a custom storage in the config options.');
@@ -5722,48 +5718,6 @@ var Cipher = /** @class */ (function () {
5722
5718
  return Cipher;
5723
5719
  }());
5724
5720
 
5725
- function base64ToUint8Array(base64) {
5726
- try {
5727
- var binaryString = jsBase64.atob(base64);
5728
- var len = binaryString.length;
5729
- var bytes = new Uint8Array(len);
5730
- for (var i = 0; i < len; i++) {
5731
- bytes[i] = binaryString.charCodeAt(i);
5732
- }
5733
- return bytes;
5734
- }
5735
- catch (error) {
5736
- logger.error('SDK::helpers::base64ToUint8Array', error);
5737
- throw error;
5738
- }
5739
- }
5740
- // export function base64ToUint8Array(base64: string): Uint8Array | undefined {
5741
- // try {
5742
- // // encode the base64 string first
5743
- // const encodedBase64 = window.btoa(base64);
5744
- // // decode the base64 string to get the binary string
5745
- // const binaryString = window.atob(encodedBase64);
5746
- // const len = binaryString.length;
5747
- // const bytes = new Uint8Array(len);
5748
- // for (let i = 0; i < len; i++) {
5749
- // bytes[i] = binaryString.charCodeAt(i);
5750
- // }
5751
- // return bytes;
5752
- // } catch (error) {
5753
- // logger.error('error in base64ToUint8Array', error);
5754
- // return;
5755
- // }
5756
- // }
5757
- // export function arrayBufferToBase64(buffer: ArrayBuffer): string {
5758
- // let binary = '';
5759
- // const bytes = new Uint8Array(buffer);
5760
- // const len = bytes.byteLength;
5761
- // for (let i = 0; i < len; i++) {
5762
- // binary += String.fromCharCode(bytes[i]);
5763
- // }
5764
- // return binary;
5765
- // }
5766
-
5767
5721
  var PasskeyService = /** @class */ (function () {
5768
5722
  function PasskeyService(userId, storageService, request) {
5769
5723
  this.cipher = new Cipher();
@@ -6169,7 +6123,7 @@ var AuthService = /** @class */ (function (_super) {
6169
6123
  * @description Checks the current user Wallaby Access Token.
6170
6124
  * @returns { boolean } Returns true if the user is authenticated, false otherwise.
6171
6125
  */
6172
- AuthService.prototype.isAuthenticated = function () {
6126
+ AuthService.prototype.validateSession = function () {
6173
6127
  return __awaiter(this, void 0, void 0, function () {
6174
6128
  var accessToken;
6175
6129
  return __generator(this, function (_a) {
@@ -6223,7 +6177,8 @@ var AuthService = /** @class */ (function (_super) {
6223
6177
  * @description
6224
6178
  * It registers the client's device public key with Wallaby server and gets back a random message string.
6225
6179
  * The random string will be hashed and signed by the client's device private key and sent back to Wallaby for verification.
6226
- * @returns a JWT encoded with users's externalUserId
6180
+ * @param { publicKey: string; privateKey: string } deviceAuthKeys the device key pair to register with wallaby.
6181
+ * @returns the messaged, the hash, and the signature using the passed private key.
6227
6182
  */
6228
6183
  AuthService.prototype.getSignature = function (deviceAuthKeys) {
6229
6184
  return __awaiter(this, void 0, void 0, function () {
@@ -6258,6 +6213,14 @@ var AuthService = /** @class */ (function (_super) {
6258
6213
  });
6259
6214
  });
6260
6215
  };
6216
+ /**
6217
+ * Signs up a new user.
6218
+ *
6219
+ * @param {AuthenticationRequest} input - The authentication request containing user details and encryption mechanism.
6220
+ * @returns {Promise<AuthResponse>} - The authentication response containing user details and access token.
6221
+ *
6222
+ * @throws {Error} - Throws an error if the sign-up process fails.
6223
+ */
6261
6224
  AuthService.prototype.signUp = function (input) {
6262
6225
  return __awaiter(this, void 0, void 0, function () {
6263
6226
  var deviceKeyPair, signatureMessageResponse, authentication, _a, error_3;
@@ -6313,6 +6276,14 @@ var AuthService = /** @class */ (function (_super) {
6313
6276
  });
6314
6277
  });
6315
6278
  };
6279
+ /**
6280
+ * Signs in the user.
6281
+ *
6282
+ * @param {AuthenticationRequest} input - The authentication request containing user details and encryption mechanism.
6283
+ * @returns {Promise<AuthResponse>} - The authentication response containing user details and access token.
6284
+ *
6285
+ * @throws {Error} - Throws an error if the sign-up process fails.
6286
+ */
6316
6287
  AuthService.prototype.signIn = function (input) {
6317
6288
  return __awaiter(this, void 0, void 0, function () {
6318
6289
  var authentication, _a;
@@ -6354,42 +6325,14 @@ var AuthService = /** @class */ (function (_super) {
6354
6325
  });
6355
6326
  });
6356
6327
  };
6357
- AuthService.prototype.checkPrfCompatibility = function (input) {
6358
- return __awaiter(this, void 0, void 0, function () {
6359
- var firstSalt, randomMessage, prfExtensionEnabled, error_4;
6360
- return __generator(this, function (_a) {
6361
- switch (_a.label) {
6362
- case 0:
6363
- _a.trys.push([0, 2, , 3]);
6364
- firstSalt = new Uint8Array(32);
6365
- randomMessage = new Uint8Array(64);
6366
- return [4 /*yield*/, navigator.credentials
6367
- .create({
6368
- publicKey: this.constructPasskeyRegistrationPublicKey({
6369
- nickname: input.nickname,
6370
- displayName: input.displayName,
6371
- randomSignatureMessage: bytesToUtf8(randomMessage),
6372
- firstSalt: firstSalt,
6373
- }),
6374
- })
6375
- .then(function (registrationCredential) {
6376
- var _a;
6377
- var pubKeyCredential = registrationCredential;
6378
- var clientExtensions = pubKeyCredential.getClientExtensionResults();
6379
- return (_a = clientExtensions.prf) === null || _a === void 0 ? void 0 : _a.enabled;
6380
- })];
6381
- case 1:
6382
- prfExtensionEnabled = _a.sent();
6383
- return [2 /*return*/, prfExtensionEnabled !== null && prfExtensionEnabled !== void 0 ? prfExtensionEnabled : false];
6384
- case 2:
6385
- error_4 = _a.sent();
6386
- logger.error('SDK::AuthService::checkPRFCompatibility::error', error_4);
6387
- return [2 /*return*/, Promise.reject(error_4)];
6388
- case 3: return [2 /*return*/];
6389
- }
6390
- });
6391
- });
6392
- };
6328
+ /**
6329
+ *
6330
+ * @private
6331
+ * Signs in the user using the password encryption mechanism.
6332
+ * @param {AuthenticationRequest} input - The authentication request containing user details and encryption mechanism.
6333
+ * @returns {Promise<AuthResponse>} - The authentication response containing user details and access token.
6334
+ * @throws Throws an error if the passkey creation fails.
6335
+ */
6393
6336
  AuthService.prototype.handleSignInWithPassword = function (_a) {
6394
6337
  return __awaiter(this, arguments, void 0, function (_b) {
6395
6338
  var clientSalt, credentialsKeyPair, encryptedPriv, decryptedClientAuthPrivKey, encryptedDevicePrivateKey, newIdentity, clientPublicKey, signatureMessage, authentication;
@@ -6464,7 +6407,7 @@ var AuthService = /** @class */ (function (_super) {
6464
6407
  };
6465
6408
  AuthService.prototype.handleSignInWithPasskey = function (_a) {
6466
6409
  return __awaiter(this, arguments, void 0, function (_b) {
6467
- var authentication, storedCredentials, encryptedDevicePrivateKey, devicePublicKey, deviceKeyPair, signatureMessage, challengeResponse, error_5;
6410
+ var authentication, storedCredentials, encryptedDevicePrivateKey, devicePublicKey, deviceKeyPair, signatureMessage, challengeResponse, error_4;
6468
6411
  var input = _b.input;
6469
6412
  return __generator(this, function (_c) {
6470
6413
  switch (_c.label) {
@@ -6522,9 +6465,9 @@ var AuthService = /** @class */ (function (_super) {
6522
6465
  throw new Error(authentication.error);
6523
6466
  return [2 /*return*/, __assign(__assign(__assign({}, authentication), { prfValue: authentication.prfValue }), input)];
6524
6467
  case 11:
6525
- error_5 = _c.sent();
6526
- logger.error('SDK::AuthService::handleSignInWithPasskey::error ', error_5);
6527
- throw error_5;
6468
+ error_4 = _c.sent();
6469
+ logger.error('SDK::AuthService::handleSignInWithPasskey::error ', error_4);
6470
+ throw error_4;
6528
6471
  case 12: return [2 /*return*/];
6529
6472
  }
6530
6473
  });
@@ -6565,7 +6508,7 @@ var AuthService = /** @class */ (function (_super) {
6565
6508
  */
6566
6509
  AuthService.prototype.refreshAuthTokens = function () {
6567
6510
  return __awaiter(this, void 0, void 0, function () {
6568
- var response, accessToken, idToken, error_6;
6511
+ var response, accessToken, idToken, error_5;
6569
6512
  return __generator(this, function (_a) {
6570
6513
  switch (_a.label) {
6571
6514
  case 0:
@@ -6589,9 +6532,9 @@ var AuthService = /** @class */ (function (_super) {
6589
6532
  _a.sent();
6590
6533
  return [2 /*return*/, response];
6591
6534
  case 3:
6592
- error_6 = _a.sent();
6593
- logger.error('SDK::AuthService::RefreshAccessToken::error', error_6);
6594
- return [2 /*return*/, Promise.reject(error_6)];
6535
+ error_5 = _a.sent();
6536
+ logger.error('SDK::AuthService::RefreshAccessToken::error', error_5);
6537
+ return [2 /*return*/, Promise.reject(error_5)];
6595
6538
  case 4: return [2 /*return*/];
6596
6539
  }
6597
6540
  });
@@ -6604,7 +6547,7 @@ var AuthService = /** @class */ (function (_super) {
6604
6547
  */
6605
6548
  AuthService.prototype.updateClientAuthPubKey = function (_a) {
6606
6549
  return __awaiter(this, arguments, void 0, function (_b) {
6607
- var response, error_7;
6550
+ var response, error_6;
6608
6551
  var _this = this;
6609
6552
  var password = _b.password;
6610
6553
  return __generator(this, function (_c) {
@@ -6694,14 +6637,24 @@ var AuthService = /** @class */ (function (_super) {
6694
6637
  response = _c.sent();
6695
6638
  return [2 /*return*/, response];
6696
6639
  case 2:
6697
- error_7 = _c.sent();
6698
- logger.error('SDK::AuthService::clientAuthPubKey::error', error_7);
6699
- return [2 /*return*/, Promise.reject(error_7)];
6640
+ error_6 = _c.sent();
6641
+ logger.error('SDK::AuthService::clientAuthPubKey::error', error_6);
6642
+ return [2 /*return*/, Promise.reject(error_6)];
6700
6643
  case 3: return [2 /*return*/];
6701
6644
  }
6702
6645
  });
6703
6646
  });
6704
6647
  };
6648
+ /**
6649
+ *
6650
+ * @private
6651
+ * Signs up the user using the password encryption mechanism.
6652
+ * @param { SignatureMessageResponse } signatureMessage - signature message response from `this.getSignature`.
6653
+ * @param { AuthenticationRequest } input - The authentication request containing user details and encryption mechanism.
6654
+ * @param { publicKey: string; privateKey: string } deviceAuthKeys - The device key pair to register with Wallaby.
6655
+ * @returns {Promise<AuthResponse>} - The authentication response containing user details and access token.
6656
+ * @throws Throws an error if the passkey creation fails.
6657
+ */
6705
6658
  AuthService.prototype.handleSignUpWithPassword = function (_a) {
6706
6659
  return __awaiter(this, arguments, void 0, function (_b) {
6707
6660
  var authentication;
@@ -6780,131 +6733,6 @@ var AuthService = /** @class */ (function (_super) {
6780
6733
  });
6781
6734
  });
6782
6735
  };
6783
- /**
6784
- * @description
6785
- * Constructs the public key object for passkey authentication.
6786
- * @param {PublicKeyCredential} credential User's nickname
6787
- * @param {string} randomSignatureMessage Random signature message
6788
- * @param {Uint8Array} firstSalt First salt
6789
- * @throws {Error} If any of the required parameters are missing
6790
- * @throws {Error} If the credential rawId is not valid
6791
- * @throws {Error} If the relying party ID is not a valid domain, so ensure to pass `hostname` to the sdk's constructor
6792
- * @returns {PublicKeyCredentialCreationOptions} The public key credential creation options
6793
- */
6794
- AuthService.prototype.constructPasskeyAuthenticationPublicKey = function (_a) {
6795
- var _b;
6796
- var credential = _a.credential, firstSalt = _a.firstSalt, randomSignatureMessage = _a.randomSignatureMessage, skipVerification = _a.skipVerification;
6797
- if (!credential || !firstSalt || !randomSignatureMessage) {
6798
- throw new Error('Missing required parameters for passkey public key object');
6799
- }
6800
- var rawId = credential.rawId instanceof Uint8Array || credential.rawId instanceof ArrayBuffer
6801
- ? credential.rawId
6802
- : ((_b = base64ToUint8Array(credential.rawId)) !== null && _b !== void 0 ? _b : new Uint8Array([0]));
6803
- if (!rawId) {
6804
- throw new Error('Error getting passkey public key raw ID');
6805
- }
6806
- // TODO Revisit this, need to check if the this is how to get the challenge.
6807
- // TODO Need to add to script to decode the challenge and compare.
6808
- var someChallenge = base64url__default.default.encode(randomSignatureMessage);
6809
- var someChallengeBuffer = Uint8Array.from(someChallenge, function (c) { return c.charCodeAt(0); });
6810
- return {
6811
- // allowCredentials: [
6812
- // {
6813
- // id: rawId,
6814
- // type: 'public-key',
6815
- // transports: ['internal', 'hybrid'],
6816
- // },
6817
- // ],
6818
- challenge: someChallengeBuffer,
6819
- rpId: this.getRelyingPartyId(),
6820
- userVerification: skipVerification ? 'discouraged' : 'required',
6821
- extensions: {
6822
- prf: {
6823
- eval: {
6824
- first: firstSalt,
6825
- },
6826
- },
6827
- },
6828
- };
6829
- };
6830
- /**
6831
- * @description
6832
- * Constructs the public key object for passkey registration.
6833
- * @param {string} nickname User's nickname
6834
- * @param {string} displayName User's display name
6835
- * @param {string} randomSignatureMessage Random signature message
6836
- * @param {Uint8Array} firstSalt First salt
6837
- * @throws {Error} If any of the required parameters are missing
6838
- * @throws {Error} If the relying party ID is not a valid domain, so ensure to pass `hostname` to the sdk's constructor
6839
- * @returns {PublicKeyCredentialCreationOptions} The public key credential creation options
6840
- */
6841
- AuthService.prototype.constructPasskeyRegistrationPublicKey = function (_a) {
6842
- var nickname = _a.nickname, displayName = _a.displayName, randomSignatureMessage = _a.randomSignatureMessage, firstSalt = _a.firstSalt;
6843
- if (!nickname || !displayName || !randomSignatureMessage || !firstSalt) {
6844
- throw new Error('Missing required parameters for passkey public key object');
6845
- }
6846
- var relyingParty = {
6847
- id: this.getRelyingPartyId(),
6848
- name: typeof window === 'undefined' ? 'republic.com' : window.location.hostname,
6849
- };
6850
- logger.log('SDK::AuthService::constructPasskeyRegistrationPublicKey::relyingParty', relyingParty);
6851
- return {
6852
- rp: relyingParty,
6853
- user: {
6854
- id: utf8ToBytes$2(this.userId),
6855
- name: nickname,
6856
- displayName: displayName,
6857
- },
6858
- pubKeyCredParams: PUBLIC_KEY_CREDENTIAL_PARAM,
6859
- timeout: PUBLIC_KEY_CREDENTIAL_TIMEOUT,
6860
- authenticatorSelection: {
6861
- residentKey: 'preferred', // Must be a resident key (discoverable credential)
6862
- requireResidentKey: false, // Ensures the credential is stored on the authenticator
6863
- userVerification: 'required', // for stricter security
6864
- // triggering a passkey to save on the same device.
6865
- // authenticatorAttachment: 'platform' as AuthenticatorAttachment,
6866
- // triggering a passkey to save on the browser.
6867
- authenticatorAttachment: 'platform',
6868
- },
6869
- attestation: 'none',
6870
- challenge: utf8ToBytes$2(randomSignatureMessage),
6871
- extensions: {
6872
- credProps: true, // Enables credential properties like signatures.
6873
- prf: {
6874
- eval: {
6875
- first: firstSalt,
6876
- },
6877
- },
6878
- },
6879
- };
6880
- };
6881
- /**
6882
- *
6883
- * basically we need to send just localhost instead of window.location.hostname which will return `http://localhost:3000`
6884
- * The authenticator will reject the request if the rpId is not a valid domain, the following error is thrown:
6885
- * SecurityError: The relying party ID is not a registrable domain suffix of, nor equal to the current domain. Subsequently, an attempt to fetch the .well-known/webauthn resource of the claimed RP ID failed.
6886
- *
6887
- * For more on this, see:
6888
- * https://www.w3.org/TR/webauthn-2/#webauthn-relying-party
6889
- *
6890
- * @returns {string} The relying party ID
6891
- */
6892
- AuthService.prototype.getRelyingPartyId = function () {
6893
- var _a;
6894
- if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.hostname) {
6895
- return this.options.hostname;
6896
- }
6897
- if (typeof window === 'undefined') {
6898
- return DEFAULT_HOSTNAME;
6899
- }
6900
- var hostname = window.location.hostname;
6901
- if (!hostname)
6902
- throw new Error('Hostname is required when using this feature');
6903
- if (hostname.includes('localhost')) {
6904
- return DEFAULT_HOSTNAME;
6905
- }
6906
- return hostname;
6907
- };
6908
6736
  return AuthService;
6909
6737
  }(Base));
6910
6738