@metamask-previews/seedless-onboarding-controller 6.0.0-preview-578568f → 6.0.0-preview-950f30f7

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/CHANGELOG.md CHANGED
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Changed
11
+
12
+ - Revert `revokeToken` value as optional in `authenticate` method. ([#7012](https://github.com/MetaMask/core/pull/7012))
13
+ - Renamed, `checkIsSeedlessOnboardingUserAuthenticated` to `getIsUserAuthenticated` and added an optional parameter to skip the vault creation check. ([#7012](https://github.com/MetaMask/core/pull/7012))
14
+
15
+ ### Fixed
16
+
17
+ - Fixed `InvalidRevokeToken` issue in `refreshAuthTokens` method. ([#7012](https://github.com/MetaMask/core/pull/7012))
18
+
10
19
  ## [6.0.0]
11
20
 
12
21
  ### Added
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _SeedlessOnboardingController_instances, _SeedlessOnboardingController_vaultEncryptor, _SeedlessOnboardingController_controllerOperationMutex, _SeedlessOnboardingController_vaultOperationMutex, _SeedlessOnboardingController_refreshJWTToken, _SeedlessOnboardingController_revokeRefreshToken, _SeedlessOnboardingController_renewRefreshToken, _SeedlessOnboardingController_passwordOutdatedCacheTTL, _SeedlessOnboardingController_isUnlocked, _SeedlessOnboardingController_cachedDecryptedVaultData, _SeedlessOnboardingController_submitGlobalPassword, _SeedlessOnboardingController_setUnlocked, _SeedlessOnboardingController_persistOprfKey, _SeedlessOnboardingController_persistAuthPubKey, _SeedlessOnboardingController_storeKeyringEncryptionKey, _SeedlessOnboardingController_loadKeyringEncryptionKey, _SeedlessOnboardingController_loadSeedlessEncryptionKey, _SeedlessOnboardingController_recoverAuthPubKey, _SeedlessOnboardingController_recoverEncKey, _SeedlessOnboardingController_fetchAllSecretDataFromMetadataStore, _SeedlessOnboardingController_changeEncryptionKey, _SeedlessOnboardingController_encryptAndStoreSecretData, _SeedlessOnboardingController_unlockVaultAndGetVaultData, _SeedlessOnboardingController_decryptAndParseVaultData, _SeedlessOnboardingController_withPersistedSecretMetadataBackupsState, _SeedlessOnboardingController_filterDupesAndUpdateSocialBackupsMetadata, _SeedlessOnboardingController_createNewVaultWithAuthData, _SeedlessOnboardingController_updateVault, _SeedlessOnboardingController_withControllerLock, _SeedlessOnboardingController_withVaultLock, _SeedlessOnboardingController_parseVaultData, _SeedlessOnboardingController_assertIsUnlocked, _SeedlessOnboardingController_assertIsAuthenticatedUser, _SeedlessOnboardingController_assertIsSRPBackedUpUser, _SeedlessOnboardingController_assertPasswordInSync, _SeedlessOnboardingController_resetPasswordOutdatedCache, _SeedlessOnboardingController_addRefreshTokenToRevokeList, _SeedlessOnboardingController_isAuthTokenError, _SeedlessOnboardingController_isMaxKeyChainLengthError, _SeedlessOnboardingController_executeWithTokenRefresh;
13
+ var _SeedlessOnboardingController_instances, _SeedlessOnboardingController_vaultEncryptor, _SeedlessOnboardingController_controllerOperationMutex, _SeedlessOnboardingController_vaultOperationMutex, _SeedlessOnboardingController_refreshJWTToken, _SeedlessOnboardingController_revokeRefreshToken, _SeedlessOnboardingController_renewRefreshToken, _SeedlessOnboardingController_passwordOutdatedCacheTTL, _SeedlessOnboardingController_isUnlocked, _SeedlessOnboardingController_cachedDecryptedVaultData, _SeedlessOnboardingController_submitGlobalPassword, _SeedlessOnboardingController_setUnlocked, _SeedlessOnboardingController_persistOprfKey, _SeedlessOnboardingController_persistAuthPubKey, _SeedlessOnboardingController_storeKeyringEncryptionKey, _SeedlessOnboardingController_loadKeyringEncryptionKey, _SeedlessOnboardingController_loadSeedlessEncryptionKey, _SeedlessOnboardingController_recoverAuthPubKey, _SeedlessOnboardingController_recoverEncKey, _SeedlessOnboardingController_fetchAllSecretDataFromMetadataStore, _SeedlessOnboardingController_changeEncryptionKey, _SeedlessOnboardingController_encryptAndStoreSecretData, _SeedlessOnboardingController_unlockVaultAndGetVaultData, _SeedlessOnboardingController_decryptAndParseVaultData, _SeedlessOnboardingController_withPersistedSecretMetadataBackupsState, _SeedlessOnboardingController_filterDupesAndUpdateSocialBackupsMetadata, _SeedlessOnboardingController_createNewVaultWithAuthData, _SeedlessOnboardingController_updateVault, _SeedlessOnboardingController_getAccessTokenAndRevokeToken, _SeedlessOnboardingController_withControllerLock, _SeedlessOnboardingController_withVaultLock, _SeedlessOnboardingController_parseVaultData, _SeedlessOnboardingController_assertIsUnlocked, _SeedlessOnboardingController_assertIsAuthenticatedUser, _SeedlessOnboardingController_assertPasswordInSync, _SeedlessOnboardingController_resetPasswordOutdatedCache, _SeedlessOnboardingController_addRefreshTokenToRevokeList, _SeedlessOnboardingController_isAuthTokenError, _SeedlessOnboardingController_isMaxKeyChainLengthError, _SeedlessOnboardingController_executeWithTokenRefresh;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.SeedlessOnboardingController = exports.getInitialSeedlessOnboardingControllerStateWithDefaults = void 0;
16
16
  const auth_network_utils_1 = require("@metamask/auth-network-utils");
@@ -298,8 +298,10 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
298
298
  state.socialLoginEmail = socialLoginEmail;
299
299
  state.metadataAccessToken = metadataAccessToken;
300
300
  state.refreshToken = refreshToken;
301
- // Temporarily store revoke token & access token in state for later vault creation
302
- state.revokeToken = revokeToken;
301
+ if (revokeToken) {
302
+ // Temporarily store revoke token & access token in state for later vault creation
303
+ state.revokeToken = revokeToken;
304
+ }
303
305
  state.accessToken = accessToken;
304
306
  // we will check if the controller state is properly set with the authenticated user info
305
307
  // before setting the isSeedlessOnboardingUserAuthenticated to true
@@ -641,7 +643,7 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
641
643
  return passwordOutdatedCache.isExpiredPwd;
642
644
  }
643
645
  }
644
- (0, assertions_1.assertIsAuthUserInfoValid)(this.state);
646
+ __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsAuthenticatedUser).call(this, this.state);
645
647
  const { nodeAuthTokens, authConnectionId, groupedAuthConnectionId, userId, } = this.state;
646
648
  const currentDeviceAuthPubKey = __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverAuthPubKey).call(this);
647
649
  let globalAuthPubKey = options?.globalAuthPubKey;
@@ -674,13 +676,19 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
674
676
  /**
675
677
  * Check if the user is authenticated with the seedless onboarding flow by checking the token values in the state.
676
678
  *
679
+ * @param skipVaultCreationCheck - An optional flag to skip the vault creation check.
680
+ * If false, the method will check all the required state values which are necessary for vault creation.
681
+ * If true, the method will only check the persisted authentication state values which are accessible regardless of the vault lock status.
677
682
  * @returns True if the user is authenticated, false otherwise.
678
683
  */
679
- async checkIsSeedlessOnboardingUserAuthenticated() {
684
+ async getIsUserAuthenticated(skipVaultCreationCheck = false) {
680
685
  let isAuthenticated = false;
681
686
  try {
682
- (0, assertions_1.assertIsSeedlessOnboardingUserAuthenticated)(this.state);
683
- isAuthenticated = true;
687
+ __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsAuthenticatedUser).call(this, this.state);
688
+ console.log('skipVaultCreationCheck', skipVaultCreationCheck);
689
+ isAuthenticated =
690
+ skipVaultCreationCheck || // if the `vaultCreationCheck` is skipped, we won't check the `accessToken` and `revokeToken`
691
+ (Boolean(this.state.accessToken) && Boolean(this.state.revokeToken));
684
692
  }
685
693
  catch {
686
694
  isAuthenticated = false;
@@ -729,7 +737,7 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
729
737
  */
730
738
  async refreshAuthTokens() {
731
739
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsAuthenticatedUser).call(this, this.state);
732
- const { refreshToken, revokeToken } = this.state;
740
+ const { refreshToken } = this.state;
733
741
  const res = await __classPrivateFieldGet(this, _SeedlessOnboardingController_refreshJWTToken, "f").call(this, {
734
742
  connection: this.state.authConnection,
735
743
  refreshToken,
@@ -740,6 +748,7 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
740
748
  try {
741
749
  const { idTokens, accessToken, metadataAccessToken } = res;
742
750
  // re-authenticate with the new id tokens to set new node auth tokens
751
+ // NOTE: here we can't provide the `revokeToken` value to the `authenticate` method because `refreshAuthTokens` method can be called when the wallet (vault) is locked
743
752
  await this.authenticate({
744
753
  idTokens,
745
754
  accessToken,
@@ -749,7 +758,6 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
749
758
  groupedAuthConnectionId: this.state.groupedAuthConnectionId,
750
759
  userId: this.state.userId,
751
760
  refreshToken,
752
- revokeToken,
753
761
  skipLock: true,
754
762
  });
755
763
  }
@@ -774,9 +782,6 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
774
782
  password,
775
783
  encryptionKey: vaultEncryptionKey,
776
784
  });
777
- if (!revokeToken) {
778
- throw new Error(constants_1.SeedlessOnboardingControllerErrorMessage.InvalidRevokeToken);
779
- }
780
785
  const { newRevokeToken, newRefreshToken } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_renewRefreshToken, "f").call(this, {
781
786
  connection: this.state.authConnection,
782
787
  revokeToken,
@@ -887,6 +892,9 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
887
892
  try {
888
893
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsAuthenticatedUser).call(this, this.state);
889
894
  const { accessToken } = this.state;
895
+ if (!accessToken) {
896
+ return true; // Consider missing token as expired
897
+ }
890
898
  const decodedToken = (0, utils_3.decodeJWTToken)(accessToken);
891
899
  return decodedToken.exp < Math.floor(Date.now() / 1000);
892
900
  }
@@ -1012,8 +1020,10 @@ async function _SeedlessOnboardingController_loadSeedlessEncryptionKey(encKey) {
1012
1020
  const seedlessEncryptionKey = aes.decrypt(encryptedKeyBytes);
1013
1021
  return (0, utils_2.bytesToUtf8)(seedlessEncryptionKey);
1014
1022
  }, _SeedlessOnboardingController_recoverAuthPubKey = function _SeedlessOnboardingController_recoverAuthPubKey() {
1015
- __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsSRPBackedUpUser).call(this, this.state);
1016
1023
  const { authPubKey } = this.state;
1024
+ if (!authPubKey) {
1025
+ throw new Error(constants_1.SeedlessOnboardingControllerErrorMessage.SRPNotBackedUpError);
1026
+ }
1017
1027
  return (0, utils_1.base64ToBytes)(authPubKey);
1018
1028
  }, _SeedlessOnboardingController_recoverEncKey =
1019
1029
  /**
@@ -1024,7 +1034,7 @@ async function _SeedlessOnboardingController_loadSeedlessEncryptionKey(encKey) {
1024
1034
  * @throws RecoveryError - If failed to recover the encryption key.
1025
1035
  */
1026
1036
  async function _SeedlessOnboardingController_recoverEncKey(password) {
1027
- (0, assertions_1.assertIsAuthUserInfoValid)(this.state);
1037
+ __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsAuthenticatedUser).call(this, this.state);
1028
1038
  const { nodeAuthTokens, authConnectionId, groupedAuthConnectionId, userId, } = this.state;
1029
1039
  try {
1030
1040
  const recoverEncKeyResult = await this.toprfClient.recoverEncKey({
@@ -1315,7 +1325,7 @@ async function _SeedlessOnboardingController_withPersistedSecretMetadataBackupsS
1315
1325
  */
1316
1326
  async function _SeedlessOnboardingController_createNewVaultWithAuthData({ password, rawToprfEncryptionKey, rawToprfPwEncryptionKey, rawToprfAuthKeyPair, }) {
1317
1327
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsAuthenticatedUser).call(this, this.state);
1318
- const { revokeToken, accessToken } = this.state;
1328
+ const { accessToken, revokeToken } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_getAccessTokenAndRevokeToken).call(this, password);
1319
1329
  const vaultData = {
1320
1330
  toprfAuthKeyPair: rawToprfAuthKeyPair,
1321
1331
  toprfEncryptionKey: rawToprfEncryptionKey,
@@ -1363,6 +1373,36 @@ async function _SeedlessOnboardingController_updateVault({ password, vaultData,
1363
1373
  state.encryptedSeedlessEncryptionKey = (0, utils_1.bytesToBase64)(encryptedKey);
1364
1374
  });
1365
1375
  });
1376
+ }, _SeedlessOnboardingController_getAccessTokenAndRevokeToken =
1377
+ /**
1378
+ * Get the access token and revoke token from the state or the vault.
1379
+ *
1380
+ * @param password - The password to decrypt the vault.
1381
+ * @returns The access token and revoke token.
1382
+ */
1383
+ async function _SeedlessOnboardingController_getAccessTokenAndRevokeToken(password) {
1384
+ let { accessToken, revokeToken } = this.state;
1385
+ // `accessToken` and `revokeToken` are both available in the state, `ONLY` when the wallet (vault) is unlocked
1386
+ // or during the period between the social authentication and the vault creation during the onboarding flow.
1387
+ if (accessToken && revokeToken) {
1388
+ return { accessToken, revokeToken };
1389
+ }
1390
+ // if `password` is provided to decrypt the vault, decrypt the vault and get the access token and revoke token from the vault
1391
+ if (this.state.vault) {
1392
+ // if the access token or revoke token is not available in the state, decrypt the vault and get the access token and revoke token from the vault
1393
+ const { vaultData } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_decryptAndParseVaultData).call(this, { password });
1394
+ accessToken = accessToken || vaultData.accessToken;
1395
+ revokeToken = revokeToken || vaultData.revokeToken;
1396
+ }
1397
+ // we should always throw an error if the access token or revoke token is not available
1398
+ // to prevent the caller from using the controller in an invalid state
1399
+ if (!accessToken) {
1400
+ throw new Error(constants_1.SeedlessOnboardingControllerErrorMessage.InvalidAccessToken);
1401
+ }
1402
+ if (!revokeToken) {
1403
+ throw new Error(constants_1.SeedlessOnboardingControllerErrorMessage.InvalidRevokeToken);
1404
+ }
1405
+ return { accessToken, revokeToken };
1366
1406
  }, _SeedlessOnboardingController_withControllerLock =
1367
1407
  /**
1368
1408
  * Lock the controller mutex before executing the given function,
@@ -1419,10 +1459,6 @@ async function _SeedlessOnboardingController_withVaultLock(callback) {
1419
1459
  });
1420
1460
  throw error;
1421
1461
  }
1422
- }, _SeedlessOnboardingController_assertIsSRPBackedUpUser = function _SeedlessOnboardingController_assertIsSRPBackedUpUser(value) {
1423
- if (!this.state.authPubKey) {
1424
- throw new Error(constants_1.SeedlessOnboardingControllerErrorMessage.SRPNotBackedUpError);
1425
- }
1426
1462
  }, _SeedlessOnboardingController_assertPasswordInSync =
1427
1463
  /**
1428
1464
  * Assert that the password is in sync with the global password.