@metamask-previews/seedless-onboarding-controller 7.1.0-preview-77d255e6a → 7.1.0-preview-fb7aa07ff

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,14 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
- ### Added
11
-
12
- - **BREAKING** The `encryptor` constructor param requires `encryptWithKey` method. ([#7800](https://github.com/MetaMask/core/pull/7800))
13
- - The method is to encrypt the vault with cached encryption key while the wallet is unlocked.
14
- - Added new public method, `getAccessToken`. ([#7800](https://github.com/MetaMask/core/pull/7800))
15
- - Clients can use this method to get `accessToken` from the controller, instead of directly accessing from the state.
16
- - This method also adds refresh token mechanism when `accessToken` is expired, hence preventing expired token usage in the clients.
17
-
18
10
  ### Changed
19
11
 
20
12
  - Update StateMetadata's `includeInStateLogs` property. ([#7750](https://github.com/MetaMask/core/pull/7750))
@@ -22,10 +14,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
22
14
  - Bump `@metamask/keyring-controller` from `^25.0.0` to `^25.1.0` ([#7713](https://github.com/MetaMask/core/pull/7713))
23
15
  - Upgrade `@metamask/utils` from `^11.8.1` to `^11.9.0` ([#7511](https://github.com/MetaMask/core/pull/7511))
24
16
 
25
- ### Fixed
26
-
27
- - Fixed new `accessToken` not being persisted in the vault after the token refresh. ([#7800](https://github.com/MetaMask/core/pull/7800))
28
-
29
17
  ## [7.1.0]
30
18
 
31
19
  ### Added
@@ -10,12 +10,11 @@ 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_pickLatestAccessToken, _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, _SeedlessOnboardingController_updateVaultAfterAuthTokenRefresh, _SeedlessOnboardingController_checkTokensExpired;
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");
17
17
  const base_controller_1 = require("@metamask/base-controller");
18
- const messenger_1 = require("@metamask/messenger");
19
18
  const toprf_secure_backup_1 = require("@metamask/toprf-secure-backup");
20
19
  const utils_1 = require("@metamask/utils");
21
20
  const aes_1 = require("@noble/ciphers/aes");
@@ -243,7 +242,6 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
243
242
  __classPrivateFieldSet(this, _SeedlessOnboardingController_refreshJWTToken, refreshJWTToken, "f");
244
243
  __classPrivateFieldSet(this, _SeedlessOnboardingController_revokeRefreshToken, revokeRefreshToken, "f");
245
244
  __classPrivateFieldSet(this, _SeedlessOnboardingController_renewRefreshToken, renewRefreshToken, "f");
246
- this.messenger.registerActionHandler('SeedlessOnboardingController:getAccessToken', this.getAccessToken.bind(this));
247
245
  }
248
246
  async fetchMetadataAccessCreds() {
249
247
  const { metadataAccessToken } = this.state;
@@ -554,7 +552,7 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
554
552
  /**
555
553
  * Submit the password to the controller, verify the password validity and unlock the controller.
556
554
  *
557
- * This method will be used especially when user unlock the wallet.
555
+ * This method will be used especially when user rehydrate/unlock the wallet.
558
556
  * The provided password will be verified against the encrypted vault, encryption key will be derived and saved in the controller state.
559
557
  *
560
558
  * This operation is useful when user performs some actions that requires the user password/encryption key. e.g. add new srp backup
@@ -564,27 +562,7 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
564
562
  */
565
563
  async submitPassword(password) {
566
564
  return await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_withControllerLock).call(this, async () => {
567
- // get the access token from the state before unlocking, it might be the new token set from the `refreshAuthTokens` method.
568
- const { accessToken: accessTokenBeforeUnlock } = this.state;
569
- const deserializedVaultData = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_unlockVaultAndGetVaultData).call(this, {
570
- password,
571
- });
572
- const accessTokenFromDecryptedVault = deserializedVaultData.accessToken;
573
- // Pick the latest access token - the token from state might be newer (from refreshAuthTokens)
574
- // than the token stored in the vault.
575
- const latestAccessToken = __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_pickLatestAccessToken).call(this, accessTokenBeforeUnlock, accessTokenFromDecryptedVault);
576
- // update the state and vault with the latest access token `ONLY` if it's different from the current access token in the state.
577
- if (latestAccessToken !== accessTokenFromDecryptedVault) {
578
- const updatedVaultData = {
579
- ...deserializedVaultData,
580
- accessToken: latestAccessToken,
581
- };
582
- await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_updateVault).call(this, {
583
- password,
584
- vaultData: updatedVaultData,
585
- pwEncKey: deserializedVaultData.toprfPwEncryptionKey,
586
- });
587
- }
565
+ await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_unlockVaultAndGetVaultData).call(this, { password });
588
566
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_setUnlocked).call(this);
589
567
  });
590
568
  }
@@ -790,8 +768,6 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
790
768
  refreshToken,
791
769
  skipLock: true,
792
770
  });
793
- // update the vault with new access token
794
- await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_updateVaultAfterAuthTokenRefresh).call(this, accessToken);
795
771
  }
796
772
  catch (error) {
797
773
  log('Error refreshing node auth tokens', error);
@@ -882,21 +858,6 @@ class SeedlessOnboardingController extends base_controller_1.BaseController {
882
858
  }
883
859
  });
884
860
  }
885
- /**
886
- * Get the access token from the state.
887
- *
888
- * If the tokens are expired, the method will refresh them and return the new access token.
889
- *
890
- * @returns The access token.
891
- */
892
- async getAccessToken() {
893
- return __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_withControllerLock).call(this, async () => {
894
- __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_assertIsAuthenticatedUser).call(this, this.state);
895
- return __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_executeWithTokenRefresh).call(this, async () => {
896
- return this.state.accessToken;
897
- }, 'getAccessToken');
898
- });
899
- }
900
861
  /**
901
862
  * Check if the current node auth token is expired.
902
863
  *
@@ -965,28 +926,22 @@ _SeedlessOnboardingController_vaultEncryptor = new WeakMap(), _SeedlessOnboardin
965
926
  * corresponding to the current authPubKey in state.
966
927
  */
967
928
  async function _SeedlessOnboardingController_submitGlobalPassword({ targetAuthPubKey, globalPassword, maxKeyChainLength, }) {
968
- const { pwEncKey: globalPwEncKey, authKeyPair: globalAuthKeyPair } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverEncKey).call(this, globalPassword);
929
+ const { pwEncKey: curPwEncKey, authKeyPair: curAuthKeyPair } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_recoverEncKey).call(this, globalPassword);
969
930
  try {
970
931
  // Recover vault encryption key.
971
932
  const res = await this.toprfClient.recoverPwEncKey({
972
933
  targetAuthPubKey,
973
- curPwEncKey: globalPwEncKey,
974
- curAuthKeyPair: globalAuthKeyPair,
934
+ curPwEncKey,
935
+ curAuthKeyPair,
975
936
  maxPwChainLength: maxKeyChainLength,
976
937
  });
977
938
  const { pwEncKey } = res;
978
939
  const vaultKey = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_loadSeedlessEncryptionKey).call(this, pwEncKey);
979
- // accessToken before unlocking vault and flooding the state with values from the decrypted vault
980
- // it might be the new token set from the `refreshAuthTokens` method.
981
- const { accessToken: accessTokenBeforeUnlock } = this.state;
982
940
  // Unlock the controller
983
- const decryptedVaultData = await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_unlockVaultAndGetVaultData).call(this, {
941
+ await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_unlockVaultAndGetVaultData).call(this, {
984
942
  encryptionKey: vaultKey,
985
943
  });
986
944
  __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_setUnlocked).call(this);
987
- // Pick the latest access token - the token from state might be newer (from refreshAuthTokens)
988
- // than the token stored in the vault. The vault will be updated later by syncLatestGlobalPassword.
989
- __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_pickLatestAccessToken).call(this, accessTokenBeforeUnlock, decryptedVaultData.accessToken);
990
945
  }
991
946
  catch (error) {
992
947
  if (__classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_isAuthTokenError).call(this, error)) {
@@ -999,18 +954,6 @@ async function _SeedlessOnboardingController_submitGlobalPassword({ targetAuthPu
999
954
  }
1000
955
  }, _SeedlessOnboardingController_setUnlocked = function _SeedlessOnboardingController_setUnlocked() {
1001
956
  __classPrivateFieldSet(this, _SeedlessOnboardingController_isUnlocked, true, "f");
1002
- }, _SeedlessOnboardingController_pickLatestAccessToken = function _SeedlessOnboardingController_pickLatestAccessToken(tokenBeforeUnlock, tokenAfterUnlock) {
1003
- let latestToken = tokenAfterUnlock;
1004
- if (tokenBeforeUnlock &&
1005
- tokenAfterUnlock &&
1006
- tokenBeforeUnlock !== tokenAfterUnlock) {
1007
- latestToken = (0, utils_3.compareAndGetLatestToken)(tokenBeforeUnlock, tokenAfterUnlock);
1008
- // Update the access token in the state with the latest access token
1009
- this.update((state) => {
1010
- state.accessToken = latestToken;
1011
- });
1012
- }
1013
- return latestToken;
1014
957
  }, _SeedlessOnboardingController_persistOprfKey =
1015
958
  /**
1016
959
  * Persist the encryption key for the seedless onboarding flow.
@@ -1428,14 +1371,14 @@ async function _SeedlessOnboardingController_updateVault({ password, vaultData,
1428
1371
  // from the password using an intentionally slow key derivation function.
1429
1372
  // We should make sure that we only call it very intentionally.
1430
1373
  const { vault, exportedKeyString } = await __classPrivateFieldGet(this, _SeedlessOnboardingController_vaultEncryptor, "f").encryptWithDetail(password, serializedVaultData);
1374
+ // Encrypt vault key.
1431
1375
  const aes = (0, webcrypto_1.managedNonce)(aes_1.gcm)(pwEncKey);
1432
1376
  const encryptedKey = aes.encrypt((0, utils_2.utf8ToBytes)(exportedKeyString));
1433
- const encryptedSeedlessEncryptionKey = (0, utils_1.bytesToBase64)(encryptedKey);
1434
1377
  this.update((state) => {
1435
1378
  state.vault = vault;
1436
1379
  state.vaultEncryptionKey = exportedKeyString;
1437
1380
  state.vaultEncryptionSalt = JSON.parse(vault).salt;
1438
- state.encryptedSeedlessEncryptionKey = encryptedSeedlessEncryptionKey;
1381
+ state.encryptedSeedlessEncryptionKey = (0, utils_1.bytesToBase64)(encryptedKey);
1439
1382
  });
1440
1383
  });
1441
1384
  }, _SeedlessOnboardingController_getAccessTokenAndRevokeToken =
@@ -1594,7 +1537,18 @@ async function _SeedlessOnboardingController_assertPasswordInSync(options) {
1594
1537
  */
1595
1538
  async function _SeedlessOnboardingController_executeWithTokenRefresh(operation, operationName) {
1596
1539
  try {
1597
- if (__classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_checkTokensExpired).call(this)) {
1540
+ // proactively check for expired tokens and refresh them if needed
1541
+ const isNodeAuthTokenExpired = this.checkNodeAuthTokenExpired();
1542
+ const isMetadataAccessTokenExpired = this.checkMetadataAccessTokenExpired();
1543
+ // access token is only accessible when the vault is unlocked
1544
+ // so skip the check if the vault is locked
1545
+ let isAccessTokenExpired = false;
1546
+ if (__classPrivateFieldGet(this, _SeedlessOnboardingController_isUnlocked, "f")) {
1547
+ isAccessTokenExpired = this.checkAccessTokenExpired();
1548
+ }
1549
+ if (isNodeAuthTokenExpired ||
1550
+ isMetadataAccessTokenExpired ||
1551
+ isAccessTokenExpired) {
1598
1552
  log(`JWT token expired during ${operationName}, attempting to refresh tokens`, 'node auth token exp check');
1599
1553
  await this.refreshAuthTokens();
1600
1554
  }
@@ -1620,58 +1574,6 @@ async function _SeedlessOnboardingController_executeWithTokenRefresh(operation,
1620
1574
  throw error;
1621
1575
  }
1622
1576
  }
1623
- }, _SeedlessOnboardingController_updateVaultAfterAuthTokenRefresh = async function _SeedlessOnboardingController_updateVaultAfterAuthTokenRefresh(accessToken) {
1624
- await __classPrivateFieldGet(this, _SeedlessOnboardingController_instances, "m", _SeedlessOnboardingController_withVaultLock).call(this, async () => {
1625
- if (!__classPrivateFieldGet(this, _SeedlessOnboardingController_isUnlocked, "f")) {
1626
- // we just temporarily store the access token in the state
1627
- // when user attempts to unlock the vault, we will use this access token to update the vault
1628
- this.update((state) => {
1629
- state.accessToken = accessToken;
1630
- });
1631
- return;
1632
- }
1633
- const { vaultEncryptionKey, vaultEncryptionSalt } = this.state;
1634
- if (!vaultEncryptionKey ||
1635
- !vaultEncryptionSalt ||
1636
- !__classPrivateFieldGet(this, _SeedlessOnboardingController_cachedDecryptedVaultData, "f")) {
1637
- throw new Error(constants_1.SeedlessOnboardingControllerErrorMessage.MissingCredentials);
1638
- }
1639
- const serializedVaultData = (0, utils_3.serializeVaultData)({
1640
- ...__classPrivateFieldGet(this, _SeedlessOnboardingController_cachedDecryptedVaultData, "f"),
1641
- accessToken,
1642
- });
1643
- const encryptionKey = await __classPrivateFieldGet(this, _SeedlessOnboardingController_vaultEncryptor, "f").importKey(vaultEncryptionKey);
1644
- const updatedEncVault = await __classPrivateFieldGet(this, _SeedlessOnboardingController_vaultEncryptor, "f").encryptWithKey(encryptionKey, serializedVaultData);
1645
- // NOTE: Referenced from keyring-controller!
1646
- // We need to include the salt used to derive
1647
- // the encryption key, to be able to derive it
1648
- // from password again.
1649
- updatedEncVault.salt = vaultEncryptionSalt;
1650
- this.update((state) => {
1651
- state.vault = JSON.stringify(updatedEncVault);
1652
- state.vaultEncryptionSalt = vaultEncryptionSalt;
1653
- state.accessToken = accessToken;
1654
- state.vaultEncryptionKey = vaultEncryptionKey;
1655
- });
1656
- // update the cached decrypted vault data with the new access token
1657
- __classPrivateFieldSet(this, _SeedlessOnboardingController_cachedDecryptedVaultData, {
1658
- ...__classPrivateFieldGet(this, _SeedlessOnboardingController_cachedDecryptedVaultData, "f"),
1659
- accessToken,
1660
- }, "f");
1661
- });
1662
- }, _SeedlessOnboardingController_checkTokensExpired = function _SeedlessOnboardingController_checkTokensExpired() {
1663
- // proactively check for expired tokens and refresh them if needed
1664
- const isNodeAuthTokenExpired = this.checkNodeAuthTokenExpired();
1665
- const isMetadataAccessTokenExpired = this.checkMetadataAccessTokenExpired();
1666
- // access token is only accessible when the vault is unlocked
1667
- // so skip the check if the vault is locked
1668
- let isAccessTokenExpired = false;
1669
- if (__classPrivateFieldGet(this, _SeedlessOnboardingController_isUnlocked, "f")) {
1670
- isAccessTokenExpired = this.checkAccessTokenExpired();
1671
- }
1672
- return (isNodeAuthTokenExpired ||
1673
- isMetadataAccessTokenExpired ||
1674
- isAccessTokenExpired);
1675
1577
  };
1676
1578
  /**
1677
1579
  * Assert that the provided password is a valid non-empty string.