@metamask-previews/profile-sync-controller 21.0.0-preview-243e64f7 → 21.0.0-preview-34e39226

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.
Files changed (64) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/dist/controllers/authentication/AuthenticationController.cjs +53 -2
  3. package/dist/controllers/authentication/AuthenticationController.cjs.map +1 -1
  4. package/dist/controllers/authentication/AuthenticationController.d.cts +4 -1
  5. package/dist/controllers/authentication/AuthenticationController.d.cts.map +1 -1
  6. package/dist/controllers/authentication/AuthenticationController.d.mts +4 -1
  7. package/dist/controllers/authentication/AuthenticationController.d.mts.map +1 -1
  8. package/dist/controllers/authentication/AuthenticationController.mjs +54 -3
  9. package/dist/controllers/authentication/AuthenticationController.mjs.map +1 -1
  10. package/dist/controllers/authentication/mocks/mockResponses.cjs +21 -8
  11. package/dist/controllers/authentication/mocks/mockResponses.cjs.map +1 -1
  12. package/dist/controllers/authentication/mocks/mockResponses.d.cts +10 -3
  13. package/dist/controllers/authentication/mocks/mockResponses.d.cts.map +1 -1
  14. package/dist/controllers/authentication/mocks/mockResponses.d.mts +10 -3
  15. package/dist/controllers/authentication/mocks/mockResponses.d.mts.map +1 -1
  16. package/dist/controllers/authentication/mocks/mockResponses.mjs +20 -8
  17. package/dist/controllers/authentication/mocks/mockResponses.mjs.map +1 -1
  18. package/dist/controllers/user-storage/UserStorageController.cjs +1 -1
  19. package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
  20. package/dist/controllers/user-storage/UserStorageController.d.cts +1 -1
  21. package/dist/controllers/user-storage/UserStorageController.d.mts +1 -1
  22. package/dist/controllers/user-storage/UserStorageController.mjs +1 -1
  23. package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
  24. package/dist/controllers/user-storage/mocks/mockResponses.cjs +3 -3
  25. package/dist/controllers/user-storage/mocks/mockResponses.cjs.map +1 -1
  26. package/dist/controllers/user-storage/mocks/mockResponses.d.cts +2 -1
  27. package/dist/controllers/user-storage/mocks/mockResponses.d.cts.map +1 -1
  28. package/dist/controllers/user-storage/mocks/mockResponses.d.mts +2 -1
  29. package/dist/controllers/user-storage/mocks/mockResponses.d.mts.map +1 -1
  30. package/dist/controllers/user-storage/mocks/mockResponses.mjs +3 -3
  31. package/dist/controllers/user-storage/mocks/mockResponses.mjs.map +1 -1
  32. package/dist/sdk/authentication-jwt-bearer/flow-srp.cjs +36 -0
  33. package/dist/sdk/authentication-jwt-bearer/flow-srp.cjs.map +1 -1
  34. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.cts +3 -1
  35. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.cts.map +1 -1
  36. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.mts +3 -1
  37. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.mts.map +1 -1
  38. package/dist/sdk/authentication-jwt-bearer/flow-srp.mjs +38 -2
  39. package/dist/sdk/authentication-jwt-bearer/flow-srp.mjs.map +1 -1
  40. package/dist/sdk/authentication-jwt-bearer/services.cjs +3 -1
  41. package/dist/sdk/authentication-jwt-bearer/services.cjs.map +1 -1
  42. package/dist/sdk/authentication-jwt-bearer/services.d.cts +1 -0
  43. package/dist/sdk/authentication-jwt-bearer/services.d.cts.map +1 -1
  44. package/dist/sdk/authentication-jwt-bearer/services.d.mts +1 -0
  45. package/dist/sdk/authentication-jwt-bearer/services.d.mts.map +1 -1
  46. package/dist/sdk/authentication-jwt-bearer/services.mjs +1 -0
  47. package/dist/sdk/authentication-jwt-bearer/services.mjs.map +1 -1
  48. package/dist/sdk/authentication.cjs +4 -0
  49. package/dist/sdk/authentication.cjs.map +1 -1
  50. package/dist/sdk/authentication.d.cts +1 -0
  51. package/dist/sdk/authentication.d.cts.map +1 -1
  52. package/dist/sdk/authentication.d.mts +1 -0
  53. package/dist/sdk/authentication.d.mts.map +1 -1
  54. package/dist/sdk/authentication.mjs +4 -0
  55. package/dist/sdk/authentication.mjs.map +1 -1
  56. package/dist/sdk/mocks/auth.cjs +8 -3
  57. package/dist/sdk/mocks/auth.cjs.map +1 -1
  58. package/dist/sdk/mocks/auth.d.cts +5 -0
  59. package/dist/sdk/mocks/auth.d.cts.map +1 -1
  60. package/dist/sdk/mocks/auth.d.mts +5 -0
  61. package/dist/sdk/mocks/auth.d.mts.map +1 -1
  62. package/dist/sdk/mocks/auth.mjs +8 -3
  63. package/dist/sdk/mocks/auth.mjs.map +1 -1
  64. package/package.json +5 -3
package/CHANGELOG.md CHANGED
@@ -7,6 +7,11 @@ 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** Automatically pair the SeedlessOnboarding profileID with the SRP based profileID ([#6048](https://github.com/MetaMask/core/pull/6048))
13
+ - this adds `@metamask/seedless-onboarding-controller` as a peer dependency and requires clients to change their initialization of the controllers to allow `SeedlessOnboardingControllerGetStateAction` as well as forward the build type to the `config.env` in the controller constructors.
14
+
10
15
  ## [21.0.0]
11
16
 
12
17
  ### Added
@@ -10,7 +10,7 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
11
11
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
12
12
  };
13
- var _AuthenticationController_instances, _AuthenticationController_metametrics, _AuthenticationController_auth, _AuthenticationController_config, _AuthenticationController_isUnlocked, _AuthenticationController_keyringController, _AuthenticationController_registerMessageHandlers, _AuthenticationController_getLoginResponseFromState, _AuthenticationController_setLoginResponseToState, _AuthenticationController_assertIsUnlocked, _AuthenticationController_snapGetPublicKey, _AuthenticationController_snapGetAllPublicKeys, _AuthenticationController__snapSignMessageCache, _AuthenticationController_snapSignMessage;
13
+ var _AuthenticationController_instances, _AuthenticationController_metametrics, _AuthenticationController_auth, _AuthenticationController_config, _AuthenticationController_isUnlocked, _AuthenticationController_keyringController, _AuthenticationController_registerMessageHandlers, _AuthenticationController_getLoginResponseFromState, _AuthenticationController_setLoginResponseToState, _AuthenticationController_assertIsUnlocked, _AuthenticationController_tryPairingWithSocialToken, _AuthenticationController_snapGetPublicKey, _AuthenticationController_snapGetAllPublicKeys, _AuthenticationController__snapSignMessageCache, _AuthenticationController_snapSignMessage;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.defaultState = void 0;
16
16
  const base_controller_1 = require("@metamask/base-controller");
@@ -29,6 +29,14 @@ const metadata = {
29
29
  persist: true,
30
30
  anonymous: false,
31
31
  },
32
+ socialPairingDone: {
33
+ persist: true,
34
+ anonymous: true,
35
+ },
36
+ pairingInProgress: {
37
+ persist: false,
38
+ anonymous: true,
39
+ },
32
40
  };
33
41
  /**
34
42
  * Controller that enables authentication for restricted endpoints.
@@ -92,18 +100,23 @@ class AuthenticationController extends base_controller_1.BaseController {
92
100
  __classPrivateFieldGet(this, _AuthenticationController_instances, "m", _AuthenticationController_assertIsUnlocked).call(this, 'performSignIn');
93
101
  const allPublicKeys = await __classPrivateFieldGet(this, _AuthenticationController_instances, "m", _AuthenticationController_snapGetAllPublicKeys).call(this);
94
102
  const accessTokens = [];
95
- // We iterate sequentially in order to be sure that the first entry
103
+ // We iterate sequentially to be sure that the first entry
96
104
  // is the primary SRP LoginResponse.
97
105
  for (const [entropySourceId] of allPublicKeys) {
98
106
  const accessToken = await __classPrivateFieldGet(this, _AuthenticationController_auth, "f").getAccessToken(entropySourceId);
99
107
  accessTokens.push(accessToken);
100
108
  }
109
+ // don't await for the pairing to finish
110
+ __classPrivateFieldGet(this, _AuthenticationController_instances, "m", _AuthenticationController_tryPairingWithSocialToken).call(this).catch(() => {
111
+ // no-op. failures must not interfere with the sign-in flow
112
+ });
101
113
  return accessTokens;
102
114
  }
103
115
  performSignOut() {
104
116
  this.update((state) => {
105
117
  state.isSignedIn = false;
106
118
  state.srpSessionData = undefined;
119
+ state.socialPairingDone = false;
107
120
  });
108
121
  }
109
122
  /**
@@ -176,6 +189,44 @@ _AuthenticationController_metametrics = new WeakMap(), _AuthenticationController
176
189
  if (!__classPrivateFieldGet(this, _AuthenticationController_isUnlocked, "f")) {
177
190
  throw new Error(`${methodName} - unable to proceed, wallet is locked`);
178
191
  }
192
+ }, _AuthenticationController_tryPairingWithSocialToken = async function _AuthenticationController_tryPairingWithSocialToken() {
193
+ const { accessToken: socialPairingToken } = this.messagingSystem.call('SeedlessOnboardingController:getState');
194
+ // Early return if no social pairing token
195
+ if (!socialPairingToken) {
196
+ this.update((state) => {
197
+ // set this to false when undefined to signal that an attempt was made.
198
+ state.socialPairingDone = state.socialPairingDone ?? false;
199
+ });
200
+ return;
201
+ }
202
+ // Atomically check and set pairingInProgress to prevent race conditions
203
+ let conditionsMet = false;
204
+ this.update((state) => {
205
+ if (state.socialPairingDone || state.pairingInProgress) {
206
+ return;
207
+ }
208
+ state.pairingInProgress = true;
209
+ conditionsMet = true;
210
+ });
211
+ if (!conditionsMet) {
212
+ return;
213
+ }
214
+ try {
215
+ const paired = await __classPrivateFieldGet(this, _AuthenticationController_auth, "f").pairSocialIdentifier(socialPairingToken);
216
+ if (paired) {
217
+ this.update((state) => {
218
+ // Prevents a race condition when sign-out is performed before pairing completes
219
+ if (state.isSignedIn) {
220
+ state.socialPairingDone = true;
221
+ }
222
+ });
223
+ }
224
+ }
225
+ finally {
226
+ this.update((state) => {
227
+ state.pairingInProgress = false;
228
+ });
229
+ }
179
230
  }, _AuthenticationController_snapGetPublicKey =
180
231
  /**
181
232
  * Returns the auth snap public key.
@@ -1 +1 @@
1
- {"version":3,"file":"AuthenticationController.cjs","sourceRoot":"","sources":["../../../src/controllers/authentication/AuthenticationController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAMA,+DAA2D;AAQ3D,iEAI8B;AAO9B,6CAKmB;AAGnB,MAAM,cAAc,GAAG,0BAA0B,CAAC;AAOrC,QAAA,YAAY,GAAkC;IAC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AA4B,EAAC,OAAO,EAAE,eAAe,CAAC,CACvD,CAAW,CAAC;IAEb,uBAAA,IAAI,uDAAuB,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC;IAE9C,OAAO,MAAM,CAAC;AAChB,CAAC;kBAxSkB,wBAAwB","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n KeyringControllerGetStateAction,\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { HandleSnapRequest } from '@metamask/snaps-controllers';\n\nimport {\n createSnapPublicKeyRequest,\n createSnapAllPublicKeysRequest,\n createSnapSignMessageRequest,\n} from './auth-snap-requests';\nimport type {\n LoginResponse,\n SRPInterface,\n UserProfile,\n UserProfileMetaMetrics,\n} from '../../sdk';\nimport {\n assertMessageStartsWithMetamask,\n AuthType,\n Env,\n JwtBearerAuth,\n} from '../../sdk';\nimport type { MetaMetricsAuth } from '../../shared/types/services';\n\nconst controllerName = 'AuthenticationController';\n\n// State\nexport type AuthenticationControllerState = {\n isSignedIn: boolean;\n srpSessionData?: Record<string, LoginResponse>;\n};\nexport const defaultState: AuthenticationControllerState = {\n isSignedIn: false,\n};\nconst metadata: StateMetadata<AuthenticationControllerState> = {\n isSignedIn: {\n persist: true,\n anonymous: true,\n },\n srpSessionData: {\n persist: true,\n anonymous: false,\n },\n};\n\ntype ControllerConfig = {\n env: Env;\n};\n\n// Messenger Actions\ntype CreateActionsObj<Controller extends keyof AuthenticationController> = {\n [K in Controller]: {\n type: `${typeof controllerName}:${K}`;\n handler: AuthenticationController[K];\n };\n};\ntype ActionsObj = CreateActionsObj<\n | 'performSignIn'\n | 'performSignOut'\n | 'getBearerToken'\n | 'getSessionProfile'\n | 'getUserProfileMetaMetrics'\n | 'isSignedIn'\n>;\nexport type Actions =\n | ActionsObj[keyof ActionsObj]\n | AuthenticationControllerGetStateAction;\nexport type AuthenticationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AuthenticationControllerState\n>;\nexport type AuthenticationControllerPerformSignIn = ActionsObj['performSignIn'];\nexport type AuthenticationControllerPerformSignOut =\n ActionsObj['performSignOut'];\nexport type AuthenticationControllerGetBearerToken =\n ActionsObj['getBearerToken'];\nexport type AuthenticationControllerGetSessionProfile =\n ActionsObj['getSessionProfile'];\nexport type AuthenticationControllerGetUserProfileMetaMetrics =\n ActionsObj['getUserProfileMetaMetrics'];\nexport type AuthenticationControllerIsSignedIn = ActionsObj['isSignedIn'];\n\nexport type AuthenticationControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n AuthenticationControllerState\n >;\n\nexport type Events = AuthenticationControllerStateChangeEvent;\n\n// Allowed Actions\nexport type AllowedActions =\n | HandleSnapRequest\n | KeyringControllerGetStateAction;\n\nexport type AllowedEvents =\n | KeyringControllerLockEvent\n | KeyringControllerUnlockEvent;\n\n// Messenger\nexport type AuthenticationControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Controller that enables authentication for restricted endpoints.\n * Used for Backup & Sync, Notifications, and other services.\n */\nexport default class AuthenticationController extends BaseController<\n typeof controllerName,\n AuthenticationControllerState,\n AuthenticationControllerMessenger\n> {\n readonly #metametrics: MetaMetricsAuth;\n\n readonly #auth: SRPInterface;\n\n readonly #config: ControllerConfig = {\n env: Env.PRD,\n };\n\n #isUnlocked = false;\n\n readonly #keyringController = {\n setupLockedStateSubscriptions: () => {\n const { isUnlocked } = this.messagingSystem.call(\n 'KeyringController:getState',\n );\n this.#isUnlocked = isUnlocked;\n\n this.messagingSystem.subscribe('KeyringController:unlock', () => {\n this.#isUnlocked = true;\n });\n\n this.messagingSystem.subscribe('KeyringController:lock', () => {\n this.#isUnlocked = false;\n });\n },\n };\n\n constructor({\n messenger,\n state,\n config,\n metametrics,\n }: {\n messenger: AuthenticationControllerMessenger;\n state?: AuthenticationControllerState;\n config?: Partial<ControllerConfig>;\n /**\n * Not using the Messaging System as we\n * do not want to tie this strictly to extension\n */\n metametrics: MetaMetricsAuth;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n if (!metametrics) {\n throw new Error('`metametrics` field is required');\n }\n\n this.#config = {\n ...this.#config,\n ...config,\n };\n\n this.#metametrics = metametrics;\n\n this.#auth = new JwtBearerAuth(\n {\n env: this.#config.env,\n platform: metametrics.agent,\n type: AuthType.SRP,\n },\n {\n storage: {\n getLoginResponse: this.#getLoginResponseFromState.bind(this),\n setLoginResponse: this.#setLoginResponseToState.bind(this),\n },\n signing: {\n getIdentifier: this.#snapGetPublicKey.bind(this),\n signMessage: this.#snapSignMessage.bind(this),\n },\n metametrics: this.#metametrics,\n },\n );\n\n this.#keyringController.setupLockedStateSubscriptions();\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getBearerToken',\n this.getBearerToken.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getSessionProfile',\n this.getSessionProfile.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:isSignedIn',\n this.isSignedIn.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:performSignIn',\n this.performSignIn.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:performSignOut',\n this.performSignOut.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getUserProfileMetaMetrics',\n this.getUserProfileMetaMetrics.bind(this),\n );\n }\n\n async #getLoginResponseFromState(\n entropySourceId?: string,\n ): Promise<LoginResponse | null> {\n if (entropySourceId) {\n if (!this.state.srpSessionData?.[entropySourceId]) {\n return null;\n }\n return this.state.srpSessionData[entropySourceId];\n }\n\n const primarySrpLoginResponse = Object.values(\n this.state.srpSessionData || {},\n )?.[0];\n\n if (!primarySrpLoginResponse) {\n return null;\n }\n\n return primarySrpLoginResponse;\n }\n\n async #setLoginResponseToState(\n loginResponse: LoginResponse,\n entropySourceId?: string,\n ) {\n const metaMetricsId = await this.#metametrics.getMetaMetricsId();\n this.update((state) => {\n if (entropySourceId) {\n state.isSignedIn = true;\n if (!state.srpSessionData) {\n state.srpSessionData = {};\n }\n state.srpSessionData[entropySourceId] = {\n ...loginResponse,\n profile: {\n ...loginResponse.profile,\n metaMetricsId,\n },\n };\n }\n });\n }\n\n #assertIsUnlocked(methodName: string): void {\n if (!this.#isUnlocked) {\n throw new Error(`${methodName} - unable to proceed, wallet is locked`);\n }\n }\n\n public async performSignIn(): Promise<string[]> {\n this.#assertIsUnlocked('performSignIn');\n\n const allPublicKeys = await this.#snapGetAllPublicKeys();\n const accessTokens = [];\n\n // We iterate sequentially in order to be sure that the first entry\n // is the primary SRP LoginResponse.\n for (const [entropySourceId] of allPublicKeys) {\n const accessToken = await this.#auth.getAccessToken(entropySourceId);\n accessTokens.push(accessToken);\n }\n\n return accessTokens;\n }\n\n public performSignOut(): void {\n this.update((state) => {\n state.isSignedIn = false;\n state.srpSessionData = undefined;\n });\n }\n\n /**\n * Will return a bearer token.\n * Logs a user in if a user is not logged in.\n *\n * @returns profile for the session.\n */\n\n public async getBearerToken(entropySourceId?: string): Promise<string> {\n this.#assertIsUnlocked('getBearerToken');\n return await this.#auth.getAccessToken(entropySourceId);\n }\n\n /**\n * Will return a session profile.\n * Logs a user in if a user is not logged in.\n *\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns profile for the session.\n */\n public async getSessionProfile(\n entropySourceId?: string,\n ): Promise<UserProfile> {\n this.#assertIsUnlocked('getSessionProfile');\n return await this.#auth.getUserProfile(entropySourceId);\n }\n\n public async getUserProfileMetaMetrics(): Promise<UserProfileMetaMetrics> {\n this.#assertIsUnlocked('getUserProfileMetaMetrics');\n return await this.#auth.getUserProfileMetaMetrics();\n }\n\n public isSignedIn(): boolean {\n return this.state.isSignedIn;\n }\n\n /**\n * Returns the auth snap public key.\n *\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns The snap public key.\n */\n async #snapGetPublicKey(entropySourceId?: string): Promise<string> {\n this.#assertIsUnlocked('#snapGetPublicKey');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapPublicKeyRequest(entropySourceId),\n )) as string;\n\n return result;\n }\n\n /**\n * Returns a mapping of entropy source IDs to auth snap public keys.\n *\n * @returns A mapping of entropy source IDs to public keys.\n */\n async #snapGetAllPublicKeys(): Promise<[string, string][]> {\n this.#assertIsUnlocked('#snapGetAllPublicKeys');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapAllPublicKeysRequest(),\n )) as [string, string][];\n\n return result;\n }\n\n #_snapSignMessageCache: Record<`metamask:${string}`, string> = {};\n\n /**\n * Signs a specific message using an underlying auth snap.\n *\n * @param message - A specific tagged message to sign.\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns A Signature created by the snap.\n */\n async #snapSignMessage(\n message: string,\n entropySourceId?: string,\n ): Promise<string> {\n assertMessageStartsWithMetamask(message);\n\n if (this.#_snapSignMessageCache[message]) {\n return this.#_snapSignMessageCache[message];\n }\n\n this.#assertIsUnlocked('#snapSignMessage');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapSignMessageRequest(message, entropySourceId),\n )) as string;\n\n this.#_snapSignMessageCache[message] = result;\n\n return result;\n }\n}\n"]}
1
+ {"version":3,"file":"AuthenticationController.cjs","sourceRoot":"","sources":["../../../src/controllers/authentication/AuthenticationController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAMA,+DAA2D;AAS3D,iEAI8B;AAO9B,6CAKmB;A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C,CAAC;IAEzC,IAAI,uBAAA,IAAI,uDAAuB,CAAC,OAAO,CAAC,EAAE;QACxC,OAAO,uBAAA,IAAI,uDAAuB,CAAC,OAAO,CAAC,CAAC;KAC7C;IAED,uBAAA,IAAI,uFAAkB,MAAtB,IAAI,EAAmB,kBAAkB,CAAC,CAAC;IAE3C,MAAM,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7C,8BAA8B,EAC9B,IAAA,iDAA4B,EAAC,OAAO,EAAE,eAAe,CAAC,CACvD,CAAW,CAAC;IAEb,uBAAA,IAAI,uDAAuB,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC;IAE9C,OAAO,MAAM,CAAC;AAChB,CAAC;kBA3VkB,wBAAwB","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n KeyringControllerGetStateAction,\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { SeedlessOnboardingControllerGetStateAction } from '@metamask/seedless-onboarding-controller';\nimport type { HandleSnapRequest } from '@metamask/snaps-controllers';\n\nimport {\n createSnapAllPublicKeysRequest,\n createSnapPublicKeyRequest,\n createSnapSignMessageRequest,\n} from './auth-snap-requests';\nimport type {\n LoginResponse,\n SRPInterface,\n UserProfile,\n UserProfileMetaMetrics,\n} from '../../sdk';\nimport {\n assertMessageStartsWithMetamask,\n AuthType,\n Env,\n JwtBearerAuth,\n} from '../../sdk';\nimport type { MetaMetricsAuth } from '../../shared/types/services';\n\nconst controllerName = 'AuthenticationController';\n\n// State\nexport type AuthenticationControllerState = {\n isSignedIn: boolean;\n srpSessionData?: Record<string, LoginResponse>;\n socialPairingDone?: boolean;\n pairingInProgress?: boolean;\n};\nexport const defaultState: AuthenticationControllerState = {\n isSignedIn: false,\n};\nconst metadata: StateMetadata<AuthenticationControllerState> = {\n isSignedIn: {\n persist: true,\n anonymous: true,\n },\n srpSessionData: {\n persist: true,\n anonymous: false,\n },\n socialPairingDone: {\n persist: true,\n anonymous: true,\n },\n pairingInProgress: {\n persist: false,\n anonymous: true,\n },\n};\n\ntype ControllerConfig = {\n env: Env;\n};\n\n// Messenger Actions\ntype CreateActionsObj<Controller extends keyof AuthenticationController> = {\n [K in Controller]: {\n type: `${typeof controllerName}:${K}`;\n handler: AuthenticationController[K];\n };\n};\ntype ActionsObj = CreateActionsObj<\n | 'performSignIn'\n | 'performSignOut'\n | 'getBearerToken'\n | 'getSessionProfile'\n | 'getUserProfileMetaMetrics'\n | 'isSignedIn'\n>;\nexport type Actions =\n | ActionsObj[keyof ActionsObj]\n | AuthenticationControllerGetStateAction;\nexport type AuthenticationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AuthenticationControllerState\n>;\nexport type AuthenticationControllerPerformSignIn = ActionsObj['performSignIn'];\nexport type AuthenticationControllerPerformSignOut =\n ActionsObj['performSignOut'];\nexport type AuthenticationControllerGetBearerToken =\n ActionsObj['getBearerToken'];\nexport type AuthenticationControllerGetSessionProfile =\n ActionsObj['getSessionProfile'];\nexport type AuthenticationControllerGetUserProfileMetaMetrics =\n ActionsObj['getUserProfileMetaMetrics'];\nexport type AuthenticationControllerIsSignedIn = ActionsObj['isSignedIn'];\n\nexport type AuthenticationControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n AuthenticationControllerState\n >;\n\nexport type Events = AuthenticationControllerStateChangeEvent;\n\n// Allowed Actions\nexport type AllowedActions =\n | HandleSnapRequest\n | KeyringControllerGetStateAction\n | SeedlessOnboardingControllerGetStateAction;\n\nexport type AllowedEvents =\n | KeyringControllerLockEvent\n | KeyringControllerUnlockEvent;\n\n// Messenger\nexport type AuthenticationControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Controller that enables authentication for restricted endpoints.\n * Used for Backup & Sync, Notifications, and other services.\n */\nexport default class AuthenticationController extends BaseController<\n typeof controllerName,\n AuthenticationControllerState,\n AuthenticationControllerMessenger\n> {\n readonly #metametrics: MetaMetricsAuth;\n\n readonly #auth: SRPInterface;\n\n readonly #config: ControllerConfig = {\n env: Env.PRD,\n };\n\n #isUnlocked = false;\n\n readonly #keyringController = {\n setupLockedStateSubscriptions: () => {\n const { isUnlocked } = this.messagingSystem.call(\n 'KeyringController:getState',\n );\n this.#isUnlocked = isUnlocked;\n\n this.messagingSystem.subscribe('KeyringController:unlock', () => {\n this.#isUnlocked = true;\n });\n\n this.messagingSystem.subscribe('KeyringController:lock', () => {\n this.#isUnlocked = false;\n });\n },\n };\n\n constructor({\n messenger,\n state,\n config,\n metametrics,\n }: {\n messenger: AuthenticationControllerMessenger;\n state?: AuthenticationControllerState;\n config?: Partial<ControllerConfig>;\n /**\n * Not using the Messaging System as we\n * do not want to tie this strictly to extension\n */\n metametrics: MetaMetricsAuth;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n if (!metametrics) {\n throw new Error('`metametrics` field is required');\n }\n\n this.#config = {\n ...this.#config,\n ...config,\n };\n\n this.#metametrics = metametrics;\n\n this.#auth = new JwtBearerAuth(\n {\n env: this.#config.env,\n platform: metametrics.agent,\n type: AuthType.SRP,\n },\n {\n storage: {\n getLoginResponse: this.#getLoginResponseFromState.bind(this),\n setLoginResponse: this.#setLoginResponseToState.bind(this),\n },\n signing: {\n getIdentifier: this.#snapGetPublicKey.bind(this),\n signMessage: this.#snapSignMessage.bind(this),\n },\n metametrics: this.#metametrics,\n },\n );\n\n this.#keyringController.setupLockedStateSubscriptions();\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getBearerToken',\n this.getBearerToken.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getSessionProfile',\n this.getSessionProfile.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:isSignedIn',\n this.isSignedIn.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:performSignIn',\n this.performSignIn.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:performSignOut',\n this.performSignOut.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getUserProfileMetaMetrics',\n this.getUserProfileMetaMetrics.bind(this),\n );\n }\n\n async #getLoginResponseFromState(\n entropySourceId?: string,\n ): Promise<LoginResponse | null> {\n if (entropySourceId) {\n if (!this.state.srpSessionData?.[entropySourceId]) {\n return null;\n }\n return this.state.srpSessionData[entropySourceId];\n }\n\n const primarySrpLoginResponse = Object.values(\n this.state.srpSessionData || {},\n )?.[0];\n\n if (!primarySrpLoginResponse) {\n return null;\n }\n\n return primarySrpLoginResponse;\n }\n\n async #setLoginResponseToState(\n loginResponse: LoginResponse,\n entropySourceId?: string,\n ) {\n const metaMetricsId = await this.#metametrics.getMetaMetricsId();\n this.update((state) => {\n if (entropySourceId) {\n state.isSignedIn = true;\n if (!state.srpSessionData) {\n state.srpSessionData = {};\n }\n state.srpSessionData[entropySourceId] = {\n ...loginResponse,\n profile: {\n ...loginResponse.profile,\n metaMetricsId,\n },\n };\n }\n });\n }\n\n #assertIsUnlocked(methodName: string): void {\n if (!this.#isUnlocked) {\n throw new Error(`${methodName} - unable to proceed, wallet is locked`);\n }\n }\n\n public async performSignIn(): Promise<string[]> {\n this.#assertIsUnlocked('performSignIn');\n\n const allPublicKeys = await this.#snapGetAllPublicKeys();\n const accessTokens = [];\n\n // We iterate sequentially to be sure that the first entry\n // is the primary SRP LoginResponse.\n for (const [entropySourceId] of allPublicKeys) {\n const accessToken = await this.#auth.getAccessToken(entropySourceId);\n accessTokens.push(accessToken);\n }\n\n // don't await for the pairing to finish\n this.#tryPairingWithSocialToken().catch(() => {\n // no-op. failures must not interfere with the sign-in flow\n });\n\n return accessTokens;\n }\n\n public performSignOut(): void {\n this.update((state) => {\n state.isSignedIn = false;\n state.srpSessionData = undefined;\n state.socialPairingDone = false;\n });\n }\n\n /**\n * Will return a bearer token.\n * Logs a user in if a user is not logged in.\n *\n * @returns profile for the session.\n */\n\n public async getBearerToken(entropySourceId?: string): Promise<string> {\n this.#assertIsUnlocked('getBearerToken');\n return await this.#auth.getAccessToken(entropySourceId);\n }\n\n /**\n * Will return a session profile.\n * Logs a user in if a user is not logged in.\n *\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns profile for the session.\n */\n public async getSessionProfile(\n entropySourceId?: string,\n ): Promise<UserProfile> {\n this.#assertIsUnlocked('getSessionProfile');\n return await this.#auth.getUserProfile(entropySourceId);\n }\n\n public async getUserProfileMetaMetrics(): Promise<UserProfileMetaMetrics> {\n this.#assertIsUnlocked('getUserProfileMetaMetrics');\n return await this.#auth.getUserProfileMetaMetrics();\n }\n\n public isSignedIn(): boolean {\n return this.state.isSignedIn;\n }\n\n async #tryPairingWithSocialToken(): Promise<void> {\n const { accessToken: socialPairingToken } = this.messagingSystem.call(\n 'SeedlessOnboardingController:getState',\n );\n\n // Early return if no social pairing token\n if (!socialPairingToken) {\n this.update((state) => {\n // set this to false when undefined to signal that an attempt was made.\n state.socialPairingDone = state.socialPairingDone ?? false;\n });\n return;\n }\n\n // Atomically check and set pairingInProgress to prevent race conditions\n let conditionsMet = false;\n this.update((state) => {\n if (state.socialPairingDone || state.pairingInProgress) {\n return;\n }\n state.pairingInProgress = true;\n conditionsMet = true;\n });\n\n if (!conditionsMet) {\n return;\n }\n\n try {\n const paired = await this.#auth.pairSocialIdentifier(socialPairingToken);\n if (paired) {\n this.update((state) => {\n // Prevents a race condition when sign-out is performed before pairing completes\n if (state.isSignedIn) {\n state.socialPairingDone = true;\n }\n });\n }\n } finally {\n this.update((state) => {\n state.pairingInProgress = false;\n });\n }\n }\n\n /**\n * Returns the auth snap public key.\n *\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns The snap public key.\n */\n async #snapGetPublicKey(entropySourceId?: string): Promise<string> {\n this.#assertIsUnlocked('#snapGetPublicKey');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapPublicKeyRequest(entropySourceId),\n )) as string;\n\n return result;\n }\n\n /**\n * Returns a mapping of entropy source IDs to auth snap public keys.\n *\n * @returns A mapping of entropy source IDs to public keys.\n */\n async #snapGetAllPublicKeys(): Promise<[string, string][]> {\n this.#assertIsUnlocked('#snapGetAllPublicKeys');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapAllPublicKeysRequest(),\n )) as [string, string][];\n\n return result;\n }\n\n #_snapSignMessageCache: Record<`metamask:${string}`, string> = {};\n\n /**\n * Signs a specific message using an underlying auth snap.\n *\n * @param message - A specific tagged message to sign.\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns A Signature created by the snap.\n */\n async #snapSignMessage(\n message: string,\n entropySourceId?: string,\n ): Promise<string> {\n assertMessageStartsWithMetamask(message);\n\n if (this.#_snapSignMessageCache[message]) {\n return this.#_snapSignMessageCache[message];\n }\n\n this.#assertIsUnlocked('#snapSignMessage');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapSignMessageRequest(message, entropySourceId),\n )) as string;\n\n this.#_snapSignMessageCache[message] = result;\n\n return result;\n }\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { KeyringControllerGetStateAction, KeyringControllerLockEvent, KeyringControllerUnlockEvent } from "@metamask/keyring-controller";
4
+ import type { SeedlessOnboardingControllerGetStateAction } from "@metamask/seedless-onboarding-controller";
4
5
  import type { HandleSnapRequest } from "@metamask/snaps-controllers";
5
6
  import type { LoginResponse, UserProfile, UserProfileMetaMetrics } from "../../sdk/index.cjs";
6
7
  import { Env } from "../../sdk/index.cjs";
@@ -9,6 +10,8 @@ declare const controllerName = "AuthenticationController";
9
10
  export type AuthenticationControllerState = {
10
11
  isSignedIn: boolean;
11
12
  srpSessionData?: Record<string, LoginResponse>;
13
+ socialPairingDone?: boolean;
14
+ pairingInProgress?: boolean;
12
15
  };
13
16
  export declare const defaultState: AuthenticationControllerState;
14
17
  type ControllerConfig = {
@@ -31,7 +34,7 @@ export type AuthenticationControllerGetUserProfileMetaMetrics = ActionsObj['getU
31
34
  export type AuthenticationControllerIsSignedIn = ActionsObj['isSignedIn'];
32
35
  export type AuthenticationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, AuthenticationControllerState>;
33
36
  export type Events = AuthenticationControllerStateChangeEvent;
34
- export type AllowedActions = HandleSnapRequest | KeyringControllerGetStateAction;
37
+ export type AllowedActions = HandleSnapRequest | KeyringControllerGetStateAction | SeedlessOnboardingControllerGetStateAction;
35
38
  export type AllowedEvents = KeyringControllerLockEvent | KeyringControllerUnlockEvent;
36
39
  export type AuthenticationControllerMessenger = RestrictedMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
37
40
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"AuthenticationController.d.cts","sourceRoot":"","sources":["../../../src/controllers/authentication/AuthenticationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,+BAA+B,EAC/B,0BAA0B,EAC1B,4BAA4B,EAC7B,qCAAqC;AACtC,OAAO,KAAK,EAAE,iBAAiB,EAAE,oCAAoC;AAOrE,OAAO,KAAK,EACV,aAAa,EAEb,WAA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":"AuthenticationController.d.cts","sourceRoot":"","sources":["../../../src/controllers/authentication/AuthenticationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,+BAA+B,EAC/B,0BAA0B,EAC1B,4BAA4B,EAC7B,qCAAqC;AACtC,OAAO,KAAK,EAAE,0CAA0C,EAAE,iDAAiD;AAC3G,OAAO,KAAK,EAAE,iBAAiB,EAAE,oCAAoC;AAOrE,OAAO,KAAK,EACV,aAAa,EAEb,WAAW,EACX,sBAAsB,EACvB,4BAAkB;AACnB,OAAO,EAGL,GAAG,EAEJ,4BAAkB;AACnB,OAAO,KAAK,EAAE,eAAe,EAAE,wCAAoC;AAEnE,QAAA,MAAM,cAAc,6BAA6B,CAAC;AAGlD,MAAM,MAAM,6BAA6B,GAAG;IAC1C,UAAU,EAAE,OAAO,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;IAC/C,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B,CAAC;AACF,eAAO,MAAM,YAAY,EAAE,6BAE1B,CAAC;AAoBF,KAAK,gBAAgB,GAAG;IACtB,GAAG,EAAE,GAAG,CAAC;CACV,CAAC;AAGF,KAAK,gBAAgB,CAAC,UAAU,SAAS,MAAM,wBAAwB,IAAI;KACxE,CAAC,IAAI,UAAU,GAAG;QACjB,IAAI,EAAE,GAAG,OAAO,cAAc,IAAI,CAAC,EAAE,CAAC;QACtC,OAAO,EAAE,wBAAwB,CAAC,CAAC,CAAC,CAAC;KACtC;CACF,CAAC;AACF,KAAK,UAAU,GAAG,gBAAgB,CAC9B,eAAe,GACf,gBAAgB,GAChB,gBAAgB,GAChB,mBAAmB,GACnB,2BAA2B,GAC3B,YAAY,CACf,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AC7C,KAAK,CAAC,EAAE,6BAA6B,CAAC;QACtC,MAAM,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAC;QACnC;;;WAGG;QACH,WAAW,EAAE,eAAe,CAAC;KAC9B;IA+HY,aAAa,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAqBxC,cAAc,IAAI,IAAI;IAQ7B;;;;;OAKG;IAEU,cAAc,CAAC,eAAe,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAKtE;;;;;;;OAOG;IACU,iBAAiB,CAC5B,eAAe,CAAC,EAAE,MAAM,GACvB,OAAO,CAAC,WAAW,CAAC;IAKV,yBAAyB,IAAI,OAAO,CAAC,sBAAsB,CAAC;IAKlE,UAAU,IAAI,OAAO;CAkH7B"}
@@ -1,6 +1,7 @@
1
1
  import type { ControllerGetStateAction, ControllerStateChangeEvent, RestrictedMessenger } from "@metamask/base-controller";
2
2
  import { BaseController } from "@metamask/base-controller";
3
3
  import type { KeyringControllerGetStateAction, KeyringControllerLockEvent, KeyringControllerUnlockEvent } from "@metamask/keyring-controller";
4
+ import type { SeedlessOnboardingControllerGetStateAction } from "@metamask/seedless-onboarding-controller";
4
5
  import type { HandleSnapRequest } from "@metamask/snaps-controllers";
5
6
  import type { LoginResponse, UserProfile, UserProfileMetaMetrics } from "../../sdk/index.mjs";
6
7
  import { Env } from "../../sdk/index.mjs";
@@ -9,6 +10,8 @@ declare const controllerName = "AuthenticationController";
9
10
  export type AuthenticationControllerState = {
10
11
  isSignedIn: boolean;
11
12
  srpSessionData?: Record<string, LoginResponse>;
13
+ socialPairingDone?: boolean;
14
+ pairingInProgress?: boolean;
12
15
  };
13
16
  export declare const defaultState: AuthenticationControllerState;
14
17
  type ControllerConfig = {
@@ -31,7 +34,7 @@ export type AuthenticationControllerGetUserProfileMetaMetrics = ActionsObj['getU
31
34
  export type AuthenticationControllerIsSignedIn = ActionsObj['isSignedIn'];
32
35
  export type AuthenticationControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, AuthenticationControllerState>;
33
36
  export type Events = AuthenticationControllerStateChangeEvent;
34
- export type AllowedActions = HandleSnapRequest | KeyringControllerGetStateAction;
37
+ export type AllowedActions = HandleSnapRequest | KeyringControllerGetStateAction | SeedlessOnboardingControllerGetStateAction;
35
38
  export type AllowedEvents = KeyringControllerLockEvent | KeyringControllerUnlockEvent;
36
39
  export type AuthenticationControllerMessenger = RestrictedMessenger<typeof controllerName, Actions | AllowedActions, Events | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
37
40
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"AuthenticationController.d.mts","sourceRoot":"","sources":["../../../src/controllers/authentication/AuthenticationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,+BAA+B,EAC/B,0BAA0B,EAC1B,4BAA4B,EAC7B,qCAAqC;AACtC,OAAO,KAAK,EAAE,iBAAiB,EAAE,oCAAoC;AAOrE,OAAO,KAAK,EACV,aAAa,EAEb,WAAW,EACX,sBAAsB,EACvB,4BAAkB;AACnB,OAAO,EAGL,GAAG,EAEJ,4BAAkB;AACnB,OAAO,KAAK,EAAE,eAAe,EAAE,wCAAoC;AAEnE,QAAA,MAAM,cAAc,6BAA6B,CAAC;AAGlD,MAAM,MAAM,6BAA6B,GAAG;IAC1C,UAAU,EAAE,OAAO,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAC;CAChD,CAAC;AACF,eAAO,MAAM,YAAY,EAAE,6BAE1B,CAAC;AAYF,KAAK,gBAAgB,GAAG;IACtB,GAAG,EAAE,GAAG,CAAC;CACV,CAAC;AAGF,KAAK,gBAAgB,CAAC,UAAU,SAAS,MAAM,wBAAwB,IAAI;KACxE,CAAC,IAAI,UAAU,G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CAAC,CACtB,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,OAAO,OAAO,wBAAyB,SAAQ,cAAc,CAClE,OAAO,cAAc,EACrB,6BAA6B,EAC7B,iCAAiC,CAClC;;gBA4Ba,EACV,SAAS,EACT,KAAK,EACL,MAAM,EACN,WAAW,GACZ,EAAE;QACD,SAAS,EAAE,iCAAiC,CAAC;QAC7C,KAAK,CAAC,EAAE,6BAA6B,CAAC;QACtC,MAAM,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC,CAAC;QACnC;;;WAGG;QACH,WAAW,EAAE,eAAe,CAAC;KAC9B;IA+HY,aAAa,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;IAgBxC,cAAc,IAAI,IAAI;IAO7B;;;;;OAKG;IAEU,cAAc,CAAC,eAAe,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAKtE;;;;;;;OAOG;IACU,iBAAiB,CAC5B,eAAe,CAAC,EAAE,MAAM,GACvB,OAAO,CAAC,WAAW,CAAC;IAKV,yBAAyB,IAAI,OAAO,CAAC,sBAAsB,CAAC;IAKlE,UAAU,IAAI,OAAO;CAqE7B"}
1
+ {"version":3,"file":"AuthenticationController.d.mts","sourceRoot":"","sources":["../../../src/controllers/authentication/AuthenticationController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC1B,mBAAmB,EAEpB,kCAAkC;AACnC,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,+BAA+B,EAC/B,0BAA0B,EAC1B,4BAA4B,EAC7B,qCAAqC;AACtC,OAAO,KAAK,EAAE,0CAA0C,EAAE,iDAAiD;AAC3G,OAAO,KAAK,EAAE,iBAAiB,EAAE,oCAAoC;AAOrE,OAAO,KAAK,EACV,aAAa,EAEb,WAAW,EACX,sBAAsB,EACvB,4BAAkB;AACnB,OAAO,EAGL,GAAG,EAEJ,4BAAkB;AACnB,OAAO,KAA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@ -9,9 +9,9 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
11
  };
12
- var _AuthenticationController_instances, _AuthenticationController_metametrics, _AuthenticationController_auth, _AuthenticationController_config, _AuthenticationController_isUnlocked, _AuthenticationController_keyringController, _AuthenticationController_registerMessageHandlers, _AuthenticationController_getLoginResponseFromState, _AuthenticationController_setLoginResponseToState, _AuthenticationController_assertIsUnlocked, _AuthenticationController_snapGetPublicKey, _AuthenticationController_snapGetAllPublicKeys, _AuthenticationController__snapSignMessageCache, _AuthenticationController_snapSignMessage;
12
+ var _AuthenticationController_instances, _AuthenticationController_metametrics, _AuthenticationController_auth, _AuthenticationController_config, _AuthenticationController_isUnlocked, _AuthenticationController_keyringController, _AuthenticationController_registerMessageHandlers, _AuthenticationController_getLoginResponseFromState, _AuthenticationController_setLoginResponseToState, _AuthenticationController_assertIsUnlocked, _AuthenticationController_tryPairingWithSocialToken, _AuthenticationController_snapGetPublicKey, _AuthenticationController_snapGetAllPublicKeys, _AuthenticationController__snapSignMessageCache, _AuthenticationController_snapSignMessage;
13
13
  import { BaseController } from "@metamask/base-controller";
14
- import { createSnapPublicKeyRequest, createSnapAllPublicKeysRequest, createSnapSignMessageRequest } from "./auth-snap-requests.mjs";
14
+ import { createSnapAllPublicKeysRequest, createSnapPublicKeyRequest, createSnapSignMessageRequest } from "./auth-snap-requests.mjs";
15
15
  import { assertMessageStartsWithMetamask, AuthType, Env, JwtBearerAuth } from "../../sdk/index.mjs";
16
16
  const controllerName = 'AuthenticationController';
17
17
  export const defaultState = {
@@ -26,6 +26,14 @@ const metadata = {
26
26
  persist: true,
27
27
  anonymous: false,
28
28
  },
29
+ socialPairingDone: {
30
+ persist: true,
31
+ anonymous: true,
32
+ },
33
+ pairingInProgress: {
34
+ persist: false,
35
+ anonymous: true,
36
+ },
29
37
  };
30
38
  /**
31
39
  * Controller that enables authentication for restricted endpoints.
@@ -89,18 +97,23 @@ class AuthenticationController extends BaseController {
89
97
  __classPrivateFieldGet(this, _AuthenticationController_instances, "m", _AuthenticationController_assertIsUnlocked).call(this, 'performSignIn');
90
98
  const allPublicKeys = await __classPrivateFieldGet(this, _AuthenticationController_instances, "m", _AuthenticationController_snapGetAllPublicKeys).call(this);
91
99
  const accessTokens = [];
92
- // We iterate sequentially in order to be sure that the first entry
100
+ // We iterate sequentially to be sure that the first entry
93
101
  // is the primary SRP LoginResponse.
94
102
  for (const [entropySourceId] of allPublicKeys) {
95
103
  const accessToken = await __classPrivateFieldGet(this, _AuthenticationController_auth, "f").getAccessToken(entropySourceId);
96
104
  accessTokens.push(accessToken);
97
105
  }
106
+ // don't await for the pairing to finish
107
+ __classPrivateFieldGet(this, _AuthenticationController_instances, "m", _AuthenticationController_tryPairingWithSocialToken).call(this).catch(() => {
108
+ // no-op. failures must not interfere with the sign-in flow
109
+ });
98
110
  return accessTokens;
99
111
  }
100
112
  performSignOut() {
101
113
  this.update((state) => {
102
114
  state.isSignedIn = false;
103
115
  state.srpSessionData = undefined;
116
+ state.socialPairingDone = false;
104
117
  });
105
118
  }
106
119
  /**
@@ -173,6 +186,44 @@ _AuthenticationController_metametrics = new WeakMap(), _AuthenticationController
173
186
  if (!__classPrivateFieldGet(this, _AuthenticationController_isUnlocked, "f")) {
174
187
  throw new Error(`${methodName} - unable to proceed, wallet is locked`);
175
188
  }
189
+ }, _AuthenticationController_tryPairingWithSocialToken = async function _AuthenticationController_tryPairingWithSocialToken() {
190
+ const { accessToken: socialPairingToken } = this.messagingSystem.call('SeedlessOnboardingController:getState');
191
+ // Early return if no social pairing token
192
+ if (!socialPairingToken) {
193
+ this.update((state) => {
194
+ // set this to false when undefined to signal that an attempt was made.
195
+ state.socialPairingDone = state.socialPairingDone ?? false;
196
+ });
197
+ return;
198
+ }
199
+ // Atomically check and set pairingInProgress to prevent race conditions
200
+ let conditionsMet = false;
201
+ this.update((state) => {
202
+ if (state.socialPairingDone || state.pairingInProgress) {
203
+ return;
204
+ }
205
+ state.pairingInProgress = true;
206
+ conditionsMet = true;
207
+ });
208
+ if (!conditionsMet) {
209
+ return;
210
+ }
211
+ try {
212
+ const paired = await __classPrivateFieldGet(this, _AuthenticationController_auth, "f").pairSocialIdentifier(socialPairingToken);
213
+ if (paired) {
214
+ this.update((state) => {
215
+ // Prevents a race condition when sign-out is performed before pairing completes
216
+ if (state.isSignedIn) {
217
+ state.socialPairingDone = true;
218
+ }
219
+ });
220
+ }
221
+ }
222
+ finally {
223
+ this.update((state) => {
224
+ state.pairingInProgress = false;
225
+ });
226
+ }
176
227
  }, _AuthenticationController_snapGetPublicKey =
177
228
  /**
178
229
  * Returns the auth snap public key.
@@ -1 +1 @@
1
- {"version":3,"file":"AuthenticationController.mjs","sourceRoot":"","sources":["../../../src/controllers/authentication/AuthenticationController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAQ3D,OAAO,EACL,0BAA0B,EAC1B,8BAA8B,EAC9B,4BAA4B,EAC7B,iCAA6B;AAO9B,OAAO,EACL,+BAA+B,EAC/B,QAAQ,EACR,GAAG,EACH,aAAa,EACd,4BAAkB;AAGnB,MAAM,cAAc,GAAG,0BAA0B,CAAC;AAOlD,MAAM,CAAC,MAAM,YAAY,GAAkC;IACzD,UAAU,EAAE,KAAK;CAClB,CAAC;AACF,MAAM,QAAQ,GAAiD;IAC7D,UAAU,EAAE;QACV,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;KAChB;IACD,cAAc,EAAE;QACd,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAiEF;;;GAGG;AACH,MAAqB,wBAAyB,SAAQ,cAIrD;IA4BC,YAAY,EACV,SAAS,EACT,KAAK,EACL,MAAM,EACN,WAAW,GAUZ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AmB,eAAwB;IAC9C,uBAAA,IAAI,uFAAkB,MAAtB,IAAI,EAAmB,mBAAmB,CAAC,CAAC;IAE5C,MAAM,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7C,8BAA8B,EAC9B,0BAA0B,CAAC,eAAe,CAAC,CAC5C,CAAW,CAAC;IAEb,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;GAIG;AACH,KAAK;IACH,uBAAA,IAAI,uFAAkB,MAAtB,IAAI,EAAmB,uBAAuB,CAAC,CAAC;IAEhD,MAAM,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7C,8BAA8B,EAC9B,8BAA8B,EAAE,CACjC,CAAuB,CAAC;IAEzB,OAAO,MAAM,CAAC;AAChB,CAAC;AAID;;;;;;;GAOG;AACH,KAAK,oDACH,OAAe,EACf,eAAwB;IAExB,+BAA+B,CAAC,OAAO,CAAC,CAAC;IAEzC,IAAI,uBAAA,IAAI,uDAAuB,CAAC,OAAO,CAAC,EAAE;QACxC,OAAO,uBAAA,IAAI,uDAAuB,CAAC,OAAO,CAAC,CAAC;KAC7C;IAED,uBAAA,IAAI,uFAAkB,MAAtB,IAAI,EAAmB,kBAAkB,CAAC,CAAC;IAE3C,MAAM,MAAM,GAAG,CAAC,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7C,8BAA8B,EAC9B,4BAA4B,CAAC,OAAO,EAAE,eAAe,CAAC,CACvD,CAAW,CAAC;IAEb,uBAAA,IAAI,uDAAuB,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC;IAE9C,OAAO,MAAM,CAAC;AAChB,CAAC;eAxSkB,wBAAwB","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n KeyringControllerGetStateAction,\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { HandleSnapRequest } from '@metamask/snaps-controllers';\n\nimport {\n createSnapPublicKeyRequest,\n createSnapAllPublicKeysRequest,\n createSnapSignMessageRequest,\n} from './auth-snap-requests';\nimport type {\n LoginResponse,\n SRPInterface,\n UserProfile,\n UserProfileMetaMetrics,\n} from '../../sdk';\nimport {\n assertMessageStartsWithMetamask,\n AuthType,\n Env,\n JwtBearerAuth,\n} from '../../sdk';\nimport type { MetaMetricsAuth } from '../../shared/types/services';\n\nconst controllerName = 'AuthenticationController';\n\n// State\nexport type AuthenticationControllerState = {\n isSignedIn: boolean;\n srpSessionData?: Record<string, LoginResponse>;\n};\nexport const defaultState: AuthenticationControllerState = {\n isSignedIn: false,\n};\nconst metadata: StateMetadata<AuthenticationControllerState> = {\n isSignedIn: {\n persist: true,\n anonymous: true,\n },\n srpSessionData: {\n persist: true,\n anonymous: false,\n },\n};\n\ntype ControllerConfig = {\n env: Env;\n};\n\n// Messenger Actions\ntype CreateActionsObj<Controller extends keyof AuthenticationController> = {\n [K in Controller]: {\n type: `${typeof controllerName}:${K}`;\n handler: AuthenticationController[K];\n };\n};\ntype ActionsObj = CreateActionsObj<\n | 'performSignIn'\n | 'performSignOut'\n | 'getBearerToken'\n | 'getSessionProfile'\n | 'getUserProfileMetaMetrics'\n | 'isSignedIn'\n>;\nexport type Actions =\n | ActionsObj[keyof ActionsObj]\n | AuthenticationControllerGetStateAction;\nexport type AuthenticationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AuthenticationControllerState\n>;\nexport type AuthenticationControllerPerformSignIn = ActionsObj['performSignIn'];\nexport type AuthenticationControllerPerformSignOut =\n ActionsObj['performSignOut'];\nexport type AuthenticationControllerGetBearerToken =\n ActionsObj['getBearerToken'];\nexport type AuthenticationControllerGetSessionProfile =\n ActionsObj['getSessionProfile'];\nexport type AuthenticationControllerGetUserProfileMetaMetrics =\n ActionsObj['getUserProfileMetaMetrics'];\nexport type AuthenticationControllerIsSignedIn = ActionsObj['isSignedIn'];\n\nexport type AuthenticationControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n AuthenticationControllerState\n >;\n\nexport type Events = AuthenticationControllerStateChangeEvent;\n\n// Allowed Actions\nexport type AllowedActions =\n | HandleSnapRequest\n | KeyringControllerGetStateAction;\n\nexport type AllowedEvents =\n | KeyringControllerLockEvent\n | KeyringControllerUnlockEvent;\n\n// Messenger\nexport type AuthenticationControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Controller that enables authentication for restricted endpoints.\n * Used for Backup & Sync, Notifications, and other services.\n */\nexport default class AuthenticationController extends BaseController<\n typeof controllerName,\n AuthenticationControllerState,\n AuthenticationControllerMessenger\n> {\n readonly #metametrics: MetaMetricsAuth;\n\n readonly #auth: SRPInterface;\n\n readonly #config: ControllerConfig = {\n env: Env.PRD,\n };\n\n #isUnlocked = false;\n\n readonly #keyringController = {\n setupLockedStateSubscriptions: () => {\n const { isUnlocked } = this.messagingSystem.call(\n 'KeyringController:getState',\n );\n this.#isUnlocked = isUnlocked;\n\n this.messagingSystem.subscribe('KeyringController:unlock', () => {\n this.#isUnlocked = true;\n });\n\n this.messagingSystem.subscribe('KeyringController:lock', () => {\n this.#isUnlocked = false;\n });\n },\n };\n\n constructor({\n messenger,\n state,\n config,\n metametrics,\n }: {\n messenger: AuthenticationControllerMessenger;\n state?: AuthenticationControllerState;\n config?: Partial<ControllerConfig>;\n /**\n * Not using the Messaging System as we\n * do not want to tie this strictly to extension\n */\n metametrics: MetaMetricsAuth;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n if (!metametrics) {\n throw new Error('`metametrics` field is required');\n }\n\n this.#config = {\n ...this.#config,\n ...config,\n };\n\n this.#metametrics = metametrics;\n\n this.#auth = new JwtBearerAuth(\n {\n env: this.#config.env,\n platform: metametrics.agent,\n type: AuthType.SRP,\n },\n {\n storage: {\n getLoginResponse: this.#getLoginResponseFromState.bind(this),\n setLoginResponse: this.#setLoginResponseToState.bind(this),\n },\n signing: {\n getIdentifier: this.#snapGetPublicKey.bind(this),\n signMessage: this.#snapSignMessage.bind(this),\n },\n metametrics: this.#metametrics,\n },\n );\n\n this.#keyringController.setupLockedStateSubscriptions();\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getBearerToken',\n this.getBearerToken.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getSessionProfile',\n this.getSessionProfile.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:isSignedIn',\n this.isSignedIn.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:performSignIn',\n this.performSignIn.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:performSignOut',\n this.performSignOut.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getUserProfileMetaMetrics',\n this.getUserProfileMetaMetrics.bind(this),\n );\n }\n\n async #getLoginResponseFromState(\n entropySourceId?: string,\n ): Promise<LoginResponse | null> {\n if (entropySourceId) {\n if (!this.state.srpSessionData?.[entropySourceId]) {\n return null;\n }\n return this.state.srpSessionData[entropySourceId];\n }\n\n const primarySrpLoginResponse = Object.values(\n this.state.srpSessionData || {},\n )?.[0];\n\n if (!primarySrpLoginResponse) {\n return null;\n }\n\n return primarySrpLoginResponse;\n }\n\n async #setLoginResponseToState(\n loginResponse: LoginResponse,\n entropySourceId?: string,\n ) {\n const metaMetricsId = await this.#metametrics.getMetaMetricsId();\n this.update((state) => {\n if (entropySourceId) {\n state.isSignedIn = true;\n if (!state.srpSessionData) {\n state.srpSessionData = {};\n }\n state.srpSessionData[entropySourceId] = {\n ...loginResponse,\n profile: {\n ...loginResponse.profile,\n metaMetricsId,\n },\n };\n }\n });\n }\n\n #assertIsUnlocked(methodName: string): void {\n if (!this.#isUnlocked) {\n throw new Error(`${methodName} - unable to proceed, wallet is locked`);\n }\n }\n\n public async performSignIn(): Promise<string[]> {\n this.#assertIsUnlocked('performSignIn');\n\n const allPublicKeys = await this.#snapGetAllPublicKeys();\n const accessTokens = [];\n\n // We iterate sequentially in order to be sure that the first entry\n // is the primary SRP LoginResponse.\n for (const [entropySourceId] of allPublicKeys) {\n const accessToken = await this.#auth.getAccessToken(entropySourceId);\n accessTokens.push(accessToken);\n }\n\n return accessTokens;\n }\n\n public performSignOut(): void {\n this.update((state) => {\n state.isSignedIn = false;\n state.srpSessionData = undefined;\n });\n }\n\n /**\n * Will return a bearer token.\n * Logs a user in if a user is not logged in.\n *\n * @returns profile for the session.\n */\n\n public async getBearerToken(entropySourceId?: string): Promise<string> {\n this.#assertIsUnlocked('getBearerToken');\n return await this.#auth.getAccessToken(entropySourceId);\n }\n\n /**\n * Will return a session profile.\n * Logs a user in if a user is not logged in.\n *\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns profile for the session.\n */\n public async getSessionProfile(\n entropySourceId?: string,\n ): Promise<UserProfile> {\n this.#assertIsUnlocked('getSessionProfile');\n return await this.#auth.getUserProfile(entropySourceId);\n }\n\n public async getUserProfileMetaMetrics(): Promise<UserProfileMetaMetrics> {\n this.#assertIsUnlocked('getUserProfileMetaMetrics');\n return await this.#auth.getUserProfileMetaMetrics();\n }\n\n public isSignedIn(): boolean {\n return this.state.isSignedIn;\n }\n\n /**\n * Returns the auth snap public key.\n *\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns The snap public key.\n */\n async #snapGetPublicKey(entropySourceId?: string): Promise<string> {\n this.#assertIsUnlocked('#snapGetPublicKey');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapPublicKeyRequest(entropySourceId),\n )) as string;\n\n return result;\n }\n\n /**\n * Returns a mapping of entropy source IDs to auth snap public keys.\n *\n * @returns A mapping of entropy source IDs to public keys.\n */\n async #snapGetAllPublicKeys(): Promise<[string, string][]> {\n this.#assertIsUnlocked('#snapGetAllPublicKeys');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapAllPublicKeysRequest(),\n )) as [string, string][];\n\n return result;\n }\n\n #_snapSignMessageCache: Record<`metamask:${string}`, string> = {};\n\n /**\n * Signs a specific message using an underlying auth snap.\n *\n * @param message - A specific tagged message to sign.\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns A Signature created by the snap.\n */\n async #snapSignMessage(\n message: string,\n entropySourceId?: string,\n ): Promise<string> {\n assertMessageStartsWithMetamask(message);\n\n if (this.#_snapSignMessageCache[message]) {\n return this.#_snapSignMessageCache[message];\n }\n\n this.#assertIsUnlocked('#snapSignMessage');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapSignMessageRequest(message, entropySourceId),\n )) as string;\n\n this.#_snapSignMessageCache[message] = result;\n\n return result;\n }\n}\n"]}
1
+ {"version":3,"file":"AuthenticationController.mjs","sourceRoot":"","sources":["../../../src/controllers/authentication/AuthenticationController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAMA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAS3D,OAAO,EACL,8BAA8B,EAC9B,0BAA0B,EAC1B,4BAA4B,EAC7B,iCAA6B;AAO9B,OAAO,EACL,+BAA+B,EAC/B,QAAQ,EACR,GAAG,EACH,aAAa,EACd,4BAAkB;AAGnB,MAAM,cAAc,GAAG,0BAA0B,CAAC;AASlD,MAAM,CAAC,MAAM,YAAY,GAAkC;IACzD,UAAU,EAAE,KAAK;CAClB,CAAC;AACF,MAAM,QAAQ,GAAiD;IAC7D,UAAU,EAAE;QACV,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,IAAI;KAChB;IACD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AG,CAAC,MAAM,IAAI,CAAC,eAAe,CAAC,IAAI,CAC7C,8BAA8B,EAC9B,4BAA4B,CAAC,OAAO,EAAE,eAAe,CAAC,CACvD,CAAW,CAAC;IAEb,uBAAA,IAAI,uDAAuB,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC;IAE9C,OAAO,MAAM,CAAC;AAChB,CAAC;eA3VkB,wBAAwB","sourcesContent":["import type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedMessenger,\n StateMetadata,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport type {\n KeyringControllerGetStateAction,\n KeyringControllerLockEvent,\n KeyringControllerUnlockEvent,\n} from '@metamask/keyring-controller';\nimport type { SeedlessOnboardingControllerGetStateAction } from '@metamask/seedless-onboarding-controller';\nimport type { HandleSnapRequest } from '@metamask/snaps-controllers';\n\nimport {\n createSnapAllPublicKeysRequest,\n createSnapPublicKeyRequest,\n createSnapSignMessageRequest,\n} from './auth-snap-requests';\nimport type {\n LoginResponse,\n SRPInterface,\n UserProfile,\n UserProfileMetaMetrics,\n} from '../../sdk';\nimport {\n assertMessageStartsWithMetamask,\n AuthType,\n Env,\n JwtBearerAuth,\n} from '../../sdk';\nimport type { MetaMetricsAuth } from '../../shared/types/services';\n\nconst controllerName = 'AuthenticationController';\n\n// State\nexport type AuthenticationControllerState = {\n isSignedIn: boolean;\n srpSessionData?: Record<string, LoginResponse>;\n socialPairingDone?: boolean;\n pairingInProgress?: boolean;\n};\nexport const defaultState: AuthenticationControllerState = {\n isSignedIn: false,\n};\nconst metadata: StateMetadata<AuthenticationControllerState> = {\n isSignedIn: {\n persist: true,\n anonymous: true,\n },\n srpSessionData: {\n persist: true,\n anonymous: false,\n },\n socialPairingDone: {\n persist: true,\n anonymous: true,\n },\n pairingInProgress: {\n persist: false,\n anonymous: true,\n },\n};\n\ntype ControllerConfig = {\n env: Env;\n};\n\n// Messenger Actions\ntype CreateActionsObj<Controller extends keyof AuthenticationController> = {\n [K in Controller]: {\n type: `${typeof controllerName}:${K}`;\n handler: AuthenticationController[K];\n };\n};\ntype ActionsObj = CreateActionsObj<\n | 'performSignIn'\n | 'performSignOut'\n | 'getBearerToken'\n | 'getSessionProfile'\n | 'getUserProfileMetaMetrics'\n | 'isSignedIn'\n>;\nexport type Actions =\n | ActionsObj[keyof ActionsObj]\n | AuthenticationControllerGetStateAction;\nexport type AuthenticationControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AuthenticationControllerState\n>;\nexport type AuthenticationControllerPerformSignIn = ActionsObj['performSignIn'];\nexport type AuthenticationControllerPerformSignOut =\n ActionsObj['performSignOut'];\nexport type AuthenticationControllerGetBearerToken =\n ActionsObj['getBearerToken'];\nexport type AuthenticationControllerGetSessionProfile =\n ActionsObj['getSessionProfile'];\nexport type AuthenticationControllerGetUserProfileMetaMetrics =\n ActionsObj['getUserProfileMetaMetrics'];\nexport type AuthenticationControllerIsSignedIn = ActionsObj['isSignedIn'];\n\nexport type AuthenticationControllerStateChangeEvent =\n ControllerStateChangeEvent<\n typeof controllerName,\n AuthenticationControllerState\n >;\n\nexport type Events = AuthenticationControllerStateChangeEvent;\n\n// Allowed Actions\nexport type AllowedActions =\n | HandleSnapRequest\n | KeyringControllerGetStateAction\n | SeedlessOnboardingControllerGetStateAction;\n\nexport type AllowedEvents =\n | KeyringControllerLockEvent\n | KeyringControllerUnlockEvent;\n\n// Messenger\nexport type AuthenticationControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n Actions | AllowedActions,\n Events | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Controller that enables authentication for restricted endpoints.\n * Used for Backup & Sync, Notifications, and other services.\n */\nexport default class AuthenticationController extends BaseController<\n typeof controllerName,\n AuthenticationControllerState,\n AuthenticationControllerMessenger\n> {\n readonly #metametrics: MetaMetricsAuth;\n\n readonly #auth: SRPInterface;\n\n readonly #config: ControllerConfig = {\n env: Env.PRD,\n };\n\n #isUnlocked = false;\n\n readonly #keyringController = {\n setupLockedStateSubscriptions: () => {\n const { isUnlocked } = this.messagingSystem.call(\n 'KeyringController:getState',\n );\n this.#isUnlocked = isUnlocked;\n\n this.messagingSystem.subscribe('KeyringController:unlock', () => {\n this.#isUnlocked = true;\n });\n\n this.messagingSystem.subscribe('KeyringController:lock', () => {\n this.#isUnlocked = false;\n });\n },\n };\n\n constructor({\n messenger,\n state,\n config,\n metametrics,\n }: {\n messenger: AuthenticationControllerMessenger;\n state?: AuthenticationControllerState;\n config?: Partial<ControllerConfig>;\n /**\n * Not using the Messaging System as we\n * do not want to tie this strictly to extension\n */\n metametrics: MetaMetricsAuth;\n }) {\n super({\n messenger,\n metadata,\n name: controllerName,\n state: { ...defaultState, ...state },\n });\n\n if (!metametrics) {\n throw new Error('`metametrics` field is required');\n }\n\n this.#config = {\n ...this.#config,\n ...config,\n };\n\n this.#metametrics = metametrics;\n\n this.#auth = new JwtBearerAuth(\n {\n env: this.#config.env,\n platform: metametrics.agent,\n type: AuthType.SRP,\n },\n {\n storage: {\n getLoginResponse: this.#getLoginResponseFromState.bind(this),\n setLoginResponse: this.#setLoginResponseToState.bind(this),\n },\n signing: {\n getIdentifier: this.#snapGetPublicKey.bind(this),\n signMessage: this.#snapSignMessage.bind(this),\n },\n metametrics: this.#metametrics,\n },\n );\n\n this.#keyringController.setupLockedStateSubscriptions();\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getBearerToken',\n this.getBearerToken.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getSessionProfile',\n this.getSessionProfile.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:isSignedIn',\n this.isSignedIn.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:performSignIn',\n this.performSignIn.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:performSignOut',\n this.performSignOut.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'AuthenticationController:getUserProfileMetaMetrics',\n this.getUserProfileMetaMetrics.bind(this),\n );\n }\n\n async #getLoginResponseFromState(\n entropySourceId?: string,\n ): Promise<LoginResponse | null> {\n if (entropySourceId) {\n if (!this.state.srpSessionData?.[entropySourceId]) {\n return null;\n }\n return this.state.srpSessionData[entropySourceId];\n }\n\n const primarySrpLoginResponse = Object.values(\n this.state.srpSessionData || {},\n )?.[0];\n\n if (!primarySrpLoginResponse) {\n return null;\n }\n\n return primarySrpLoginResponse;\n }\n\n async #setLoginResponseToState(\n loginResponse: LoginResponse,\n entropySourceId?: string,\n ) {\n const metaMetricsId = await this.#metametrics.getMetaMetricsId();\n this.update((state) => {\n if (entropySourceId) {\n state.isSignedIn = true;\n if (!state.srpSessionData) {\n state.srpSessionData = {};\n }\n state.srpSessionData[entropySourceId] = {\n ...loginResponse,\n profile: {\n ...loginResponse.profile,\n metaMetricsId,\n },\n };\n }\n });\n }\n\n #assertIsUnlocked(methodName: string): void {\n if (!this.#isUnlocked) {\n throw new Error(`${methodName} - unable to proceed, wallet is locked`);\n }\n }\n\n public async performSignIn(): Promise<string[]> {\n this.#assertIsUnlocked('performSignIn');\n\n const allPublicKeys = await this.#snapGetAllPublicKeys();\n const accessTokens = [];\n\n // We iterate sequentially to be sure that the first entry\n // is the primary SRP LoginResponse.\n for (const [entropySourceId] of allPublicKeys) {\n const accessToken = await this.#auth.getAccessToken(entropySourceId);\n accessTokens.push(accessToken);\n }\n\n // don't await for the pairing to finish\n this.#tryPairingWithSocialToken().catch(() => {\n // no-op. failures must not interfere with the sign-in flow\n });\n\n return accessTokens;\n }\n\n public performSignOut(): void {\n this.update((state) => {\n state.isSignedIn = false;\n state.srpSessionData = undefined;\n state.socialPairingDone = false;\n });\n }\n\n /**\n * Will return a bearer token.\n * Logs a user in if a user is not logged in.\n *\n * @returns profile for the session.\n */\n\n public async getBearerToken(entropySourceId?: string): Promise<string> {\n this.#assertIsUnlocked('getBearerToken');\n return await this.#auth.getAccessToken(entropySourceId);\n }\n\n /**\n * Will return a session profile.\n * Logs a user in if a user is not logged in.\n *\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns profile for the session.\n */\n public async getSessionProfile(\n entropySourceId?: string,\n ): Promise<UserProfile> {\n this.#assertIsUnlocked('getSessionProfile');\n return await this.#auth.getUserProfile(entropySourceId);\n }\n\n public async getUserProfileMetaMetrics(): Promise<UserProfileMetaMetrics> {\n this.#assertIsUnlocked('getUserProfileMetaMetrics');\n return await this.#auth.getUserProfileMetaMetrics();\n }\n\n public isSignedIn(): boolean {\n return this.state.isSignedIn;\n }\n\n async #tryPairingWithSocialToken(): Promise<void> {\n const { accessToken: socialPairingToken } = this.messagingSystem.call(\n 'SeedlessOnboardingController:getState',\n );\n\n // Early return if no social pairing token\n if (!socialPairingToken) {\n this.update((state) => {\n // set this to false when undefined to signal that an attempt was made.\n state.socialPairingDone = state.socialPairingDone ?? false;\n });\n return;\n }\n\n // Atomically check and set pairingInProgress to prevent race conditions\n let conditionsMet = false;\n this.update((state) => {\n if (state.socialPairingDone || state.pairingInProgress) {\n return;\n }\n state.pairingInProgress = true;\n conditionsMet = true;\n });\n\n if (!conditionsMet) {\n return;\n }\n\n try {\n const paired = await this.#auth.pairSocialIdentifier(socialPairingToken);\n if (paired) {\n this.update((state) => {\n // Prevents a race condition when sign-out is performed before pairing completes\n if (state.isSignedIn) {\n state.socialPairingDone = true;\n }\n });\n }\n } finally {\n this.update((state) => {\n state.pairingInProgress = false;\n });\n }\n }\n\n /**\n * Returns the auth snap public key.\n *\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns The snap public key.\n */\n async #snapGetPublicKey(entropySourceId?: string): Promise<string> {\n this.#assertIsUnlocked('#snapGetPublicKey');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapPublicKeyRequest(entropySourceId),\n )) as string;\n\n return result;\n }\n\n /**\n * Returns a mapping of entropy source IDs to auth snap public keys.\n *\n * @returns A mapping of entropy source IDs to public keys.\n */\n async #snapGetAllPublicKeys(): Promise<[string, string][]> {\n this.#assertIsUnlocked('#snapGetAllPublicKeys');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapAllPublicKeysRequest(),\n )) as [string, string][];\n\n return result;\n }\n\n #_snapSignMessageCache: Record<`metamask:${string}`, string> = {};\n\n /**\n * Signs a specific message using an underlying auth snap.\n *\n * @param message - A specific tagged message to sign.\n * @param entropySourceId - The entropy source ID used to derive the key,\n * when multiple sources are available (Multi-SRP).\n * @returns A Signature created by the snap.\n */\n async #snapSignMessage(\n message: string,\n entropySourceId?: string,\n ): Promise<string> {\n assertMessageStartsWithMetamask(message);\n\n if (this.#_snapSignMessageCache[message]) {\n return this.#_snapSignMessageCache[message];\n }\n\n this.#assertIsUnlocked('#snapSignMessage');\n\n const result = (await this.messagingSystem.call(\n 'SnapController:handleRequest',\n createSnapSignMessageRequest(message, entropySourceId),\n )) as string;\n\n this.#_snapSignMessageCache[message] = result;\n\n return result;\n }\n}\n"]}
@@ -1,13 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMockAuthAccessTokenResponse = exports.MOCK_OATH_TOKEN_RESPONSE = exports.getMockAuthLoginResponse = exports.MOCK_LOGIN_RESPONSE = exports.getMockAuthNonceResponse = exports.MOCK_JWT = exports.MOCK_NONCE = exports.MOCK_NONCE_RESPONSE = void 0;
3
+ exports.getMockPairSocialTokenResponse = exports.getMockAuthAccessTokenResponse = exports.MOCK_OATH_TOKEN_RESPONSE = exports.getMockAuthLoginResponse = exports.MOCK_LOGIN_RESPONSE = exports.getMockAuthNonceResponse = exports.MOCK_JWT = exports.MOCK_NONCE = exports.MOCK_NONCE_RESPONSE = void 0;
4
+ const services_1 = require("../../../sdk/authentication-jwt-bearer/services.cjs");
4
5
  const auth_1 = require("../../../sdk/mocks/auth.cjs");
6
+ const env_1 = require("../../../shared/env.cjs");
5
7
  exports.MOCK_NONCE_RESPONSE = auth_1.MOCK_NONCE_RESPONSE;
6
8
  exports.MOCK_NONCE = exports.MOCK_NONCE_RESPONSE.nonce;
7
9
  exports.MOCK_JWT = auth_1.MOCK_JWT;
8
- const getMockAuthNonceResponse = () => {
10
+ const getMockAuthNonceResponse = (env = env_1.Env.PRD) => {
9
11
  return {
10
- url: auth_1.MOCK_NONCE_URL,
12
+ url: (0, services_1.NONCE_URL)(env),
11
13
  requestMethod: 'GET',
12
14
  response: (_, path, getE2ESrpIdentifierForPublicKey) => {
13
15
  // The goal here is to have this identifier bubble all the way up to being the access token
@@ -16,7 +18,7 @@ const getMockAuthNonceResponse = () => {
16
18
  const e2eIdentifier = getE2ESrpIdentifierForPublicKey?.(identifier ?? '');
17
19
  return {
18
20
  ...exports.MOCK_NONCE_RESPONSE,
19
- nonce: e2eIdentifier ?? exports.MOCK_NONCE_RESPONSE.nonce,
21
+ nonce: e2eIdentifier ?? exports.MOCK_NONCE,
20
22
  identifier: exports.MOCK_NONCE_RESPONSE.identifier,
21
23
  };
22
24
  },
@@ -24,9 +26,9 @@ const getMockAuthNonceResponse = () => {
24
26
  };
25
27
  exports.getMockAuthNonceResponse = getMockAuthNonceResponse;
26
28
  exports.MOCK_LOGIN_RESPONSE = auth_1.MOCK_SRP_LOGIN_RESPONSE;
27
- const getMockAuthLoginResponse = () => {
29
+ const getMockAuthLoginResponse = (env = env_1.Env.PRD) => {
28
30
  return {
29
- url: auth_1.MOCK_SRP_LOGIN_URL,
31
+ url: (0, services_1.SRP_LOGIN_URL)(env),
30
32
  requestMethod: 'POST',
31
33
  // In case this mock is used in an E2E test, we populate token, profile_id and identifier_id with the e2eIdentifier
32
34
  // to make it easier to segregate data in the test environment.
@@ -47,9 +49,9 @@ const getMockAuthLoginResponse = () => {
47
49
  };
48
50
  exports.getMockAuthLoginResponse = getMockAuthLoginResponse;
49
51
  exports.MOCK_OATH_TOKEN_RESPONSE = auth_1.MOCK_OIDC_TOKEN_RESPONSE;
50
- const getMockAuthAccessTokenResponse = () => {
52
+ const getMockAuthAccessTokenResponse = (env = env_1.Env.PRD) => {
51
53
  return {
52
- url: auth_1.MOCK_OIDC_TOKEN_URL,
54
+ url: (0, services_1.OIDC_TOKEN_URL)(env),
53
55
  requestMethod: 'POST',
54
56
  response: (requestJsonBody) => {
55
57
  // We end up setting the access token to the e2eIdentifier in the test environment
@@ -64,4 +66,15 @@ const getMockAuthAccessTokenResponse = () => {
64
66
  };
65
67
  };
66
68
  exports.getMockAuthAccessTokenResponse = getMockAuthAccessTokenResponse;
69
+ const getMockPairSocialTokenResponse = (env = env_1.Env.PRD) => {
70
+ return {
71
+ url: (0, services_1.PAIR_SOCIAL_IDENTIFIER)(env),
72
+ requestMethod: 'POST',
73
+ response: () => {
74
+ return 'OK';
75
+ },
76
+ statusCode: 204,
77
+ };
78
+ };
79
+ exports.getMockPairSocialTokenResponse = getMockPairSocialTokenResponse;
67
80
  //# sourceMappingURL=mockResponses.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"mockResponses.cjs","sourceRoot":"","sources":["../../../../src/controllers/authentication/mocks/mockResponses.ts"],"names":[],"mappings":";;;AAAA,sDAQiC;AAQpB,QAAA,mBAAmB,GAAG,0BAAuB,CAAC;AAC9C,QAAA,UAAU,GAAG,2BAAmB,CAAC,KAAK,CAAC;AACvC,QAAA,QAAQ,GAAG,eAAY,CAAC;AAE9B,MAAM,wBAAwB,GAAG,GAAG,EAAE;IAC3C,OAAO;QACL,GAAG,EAAE,qBAAc;QACnB,aAAa,EAAE,KAAK;QACpB,QAAQ,EAAE,CACR,CAAW,EACX,IAAa,EACb,+BAA+D,EAC/D,EAAE;YACF,2FAA2F;YAC3F,oEAAoE;YACpE,MAAM,UAAU,GAAG,IAAI,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,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AAkF;YAClF,gEAAgE;YAChE,qDAAqD;YACrD,MAAM,aAAa,GAAG,IAAI,eAAe,CAAC,eAAe,CAAC,CAAC,GAAG,CAC5D,WAAW,CACZ,CAAC;YAEF,OAAO;gBACL,GAAG,gCAAwB;gBAC3B,YAAY,EAAE,aAAa,IAAI,gCAAwB,CAAC,YAAY;aACrE,CAAC;QACJ,CAAC;KACqB,CAAC;AAC3B,CAAC,CAAC;AAlBW,QAAA,8BAA8B,kCAkBzC","sourcesContent":["import {\n MOCK_NONCE_RESPONSE as SDK_MOCK_NONCE_RESPONSE,\n MOCK_JWT as SDK_MOCK_JWT,\n MOCK_SRP_LOGIN_RESPONSE as SDK_MOCK_SRP_LOGIN_RESPONSE,\n MOCK_OIDC_TOKEN_RESPONSE as SDK_MOCK_OIDC_TOKEN_RESPONSE,\n MOCK_NONCE_URL,\n MOCK_SRP_LOGIN_URL,\n MOCK_OIDC_TOKEN_URL,\n} from '../../../sdk/mocks/auth';\n\ntype MockResponse = {\n url: string;\n requestMethod: 'GET' | 'POST' | 'PUT';\n response: unknown;\n};\n\nexport const MOCK_NONCE_RESPONSE = SDK_MOCK_NONCE_RESPONSE;\nexport const MOCK_NONCE = MOCK_NONCE_RESPONSE.nonce;\nexport const MOCK_JWT = SDK_MOCK_JWT;\n\nexport const getMockAuthNonceResponse = () => {\n return {\n url: MOCK_NONCE_URL,\n requestMethod: 'GET',\n response: (\n _?: unknown,\n path?: string,\n getE2ESrpIdentifierForPublicKey?: (publicKey: string) => string,\n ) => {\n // The goal here is to have this identifier bubble all the way up to being the access token\n // That way, we can use it to segregate data in the test environment\n const identifier = path?.split('?identifier=')[1];\n const e2eIdentifier = getE2ESrpIdentifierForPublicKey?.(identifier ?? '');\n\n return {\n ...MOCK_NONCE_RESPONSE,\n nonce: e2eIdentifier ?? MOCK_NONCE_RESPONSE.nonce,\n identifier: MOCK_NONCE_RESPONSE.identifier,\n };\n },\n } satisfies MockResponse;\n};\n\nexport const MOCK_LOGIN_RESPONSE = SDK_MOCK_SRP_LOGIN_RESPONSE;\n\nexport const getMockAuthLoginResponse = () => {\n return {\n url: MOCK_SRP_LOGIN_URL,\n requestMethod: 'POST',\n // In case this mock is used in an E2E test, we populate token, profile_id and identifier_id with the e2eIdentifier\n // to make it easier to segregate data in the test environment.\n response: (requestJsonBody?: { raw_message: string }) => {\n const splittedRawMessage = requestJsonBody?.raw_message.split(':');\n const e2eIdentifier = splittedRawMessage?.[splittedRawMessage.length - 2];\n\n return {\n ...MOCK_LOGIN_RESPONSE,\n token: e2eIdentifier ?? MOCK_LOGIN_RESPONSE.token,\n profile: {\n ...MOCK_LOGIN_RESPONSE.profile,\n profile_id: e2eIdentifier ?? MOCK_LOGIN_RESPONSE.profile.profile_id,\n identifier_id:\n e2eIdentifier ?? MOCK_LOGIN_RESPONSE.profile.identifier_id,\n },\n };\n },\n } satisfies MockResponse;\n};\n\nexport const MOCK_OATH_TOKEN_RESPONSE = SDK_MOCK_OIDC_TOKEN_RESPONSE;\n\nexport const getMockAuthAccessTokenResponse = () => {\n return {\n url: MOCK_OIDC_TOKEN_URL,\n requestMethod: 'POST',\n response: (requestJsonBody?: string) => {\n // We end up setting the access token to the e2eIdentifier in the test environment\n // This is then attached to every request's Authorization header\n // and used to segregate data in the test environment\n const e2eIdentifier = new URLSearchParams(requestJsonBody).get(\n 'assertion',\n );\n\n return {\n ...MOCK_OATH_TOKEN_RESPONSE,\n access_token: e2eIdentifier ?? MOCK_OATH_TOKEN_RESPONSE.access_token,\n };\n },\n } satisfies MockResponse;\n};\n"]}
1
+ {"version":3,"file":"mockResponses.cjs","sourceRoot":"","sources":["../../../../src/controllers/authentication/mocks/mockResponses.ts"],"names":[],"mappings":";;;AAAA,kFAIyD;AACzD,sDAKiC;AACjC,iDAA0C;AAS7B,QAAA,mBAAmB,GAAG,0BAAuB,CAAC;AAC9C,QAAA,UAAU,GAAG,2BAAmB,CAAC,KAAK,CAAC;AACvC,QAAA,QAAQ,GAAG,eAAY,CAAC;AAE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O,CAAC,UAAU;oBACnE,aAAa,EACX,aAAa,IAAI,2BAAmB,CAAC,OAAO,CAAC,aAAa;iBAC7D;aACF,CAAC;QACJ,CAAC;KACqB,CAAC;AAC3B,CAAC,CAAC;AAtBW,QAAA,wBAAwB,4BAsBnC;AAEW,QAAA,wBAAwB,GAAG,+BAA4B,CAAC;AAE9D,MAAM,8BAA8B,GAAG,CAAC,MAAW,SAAG,CAAC,GAAG,EAAE,EAAE;IACnE,OAAO;QACL,GAAG,EAAE,IAAA,yBAAc,EAAC,GAAG,CAAC;QACxB,aAAa,EAAE,MAAM;QACrB,QAAQ,EAAE,CAAC,eAAwB,EAAE,EAAE;YACrC,kFAAkF;YAClF,gEAAgE;YAChE,qDAAqD;YACrD,MAAM,aAAa,GAAG,IAAI,eAAe,CAAC,eAAe,CAAC,CAAC,GAAG,CAC5D,WAAW,CACZ,CAAC;YAEF,OAAO;gBACL,GAAG,gCAAwB;gBAC3B,YAAY,EAAE,aAAa,IAAI,gCAAwB,CAAC,YAAY;aACrE,CAAC;QACJ,CAAC;KACqB,CAAC;AAC3B,CAAC,CAAC;AAlBW,QAAA,8BAA8B,kCAkBzC;AAEK,MAAM,8BAA8B,GAAG,CAAC,MAAW,SAAG,CAAC,GAAG,EAAE,EAAE;IACnE,OAAO;QACL,GAAG,EAAE,IAAA,iCAAsB,EAAC,GAAG,CAAC;QAChC,aAAa,EAAE,MAAM;QACrB,QAAQ,EAAE,GAAG,EAAE;YACb,OAAO,IAAI,CAAC;QACd,CAAC;QACD,UAAU,EAAE,GAAG;KACO,CAAC;AAC3B,CAAC,CAAC;AATW,QAAA,8BAA8B,kCASzC","sourcesContent":["import {\n NONCE_URL,\n OIDC_TOKEN_URL, PAIR_SOCIAL_IDENTIFIER,\n SRP_LOGIN_URL,\n} from '../../../sdk/authentication-jwt-bearer/services';\nimport {\n MOCK_JWT as SDK_MOCK_JWT,\n MOCK_NONCE_RESPONSE as SDK_MOCK_NONCE_RESPONSE,\n MOCK_OIDC_TOKEN_RESPONSE as SDK_MOCK_OIDC_TOKEN_RESPONSE,\n MOCK_SRP_LOGIN_RESPONSE as SDK_MOCK_SRP_LOGIN_RESPONSE,\n} from '../../../sdk/mocks/auth';\nimport { Env } from '../../../shared/env';\n\ntype MockResponse = {\n url: string;\n requestMethod: 'GET' | 'POST' | 'PUT';\n response: unknown;\n statusCode?: number;\n};\n\nexport const MOCK_NONCE_RESPONSE = SDK_MOCK_NONCE_RESPONSE;\nexport const MOCK_NONCE = MOCK_NONCE_RESPONSE.nonce;\nexport const MOCK_JWT = SDK_MOCK_JWT;\n\nexport const getMockAuthNonceResponse = (env: Env = Env.PRD) => {\n return {\n url: NONCE_URL(env),\n requestMethod: 'GET',\n response: (\n _?: unknown,\n path?: string,\n getE2ESrpIdentifierForPublicKey?: (publicKey: string) => string,\n ) => {\n // The goal here is to have this identifier bubble all the way up to being the access token\n // That way, we can use it to segregate data in the test environment\n const identifier = path?.split('?identifier=')[1];\n const e2eIdentifier = getE2ESrpIdentifierForPublicKey?.(identifier ?? '');\n\n return {\n ...MOCK_NONCE_RESPONSE,\n nonce: e2eIdentifier ?? MOCK_NONCE,\n identifier: MOCK_NONCE_RESPONSE.identifier,\n };\n },\n } satisfies MockResponse;\n};\n\nexport const MOCK_LOGIN_RESPONSE = SDK_MOCK_SRP_LOGIN_RESPONSE;\n\nexport const getMockAuthLoginResponse = (env: Env = Env.PRD) => {\n return {\n url: SRP_LOGIN_URL(env),\n requestMethod: 'POST',\n // In case this mock is used in an E2E test, we populate token, profile_id and identifier_id with the e2eIdentifier\n // to make it easier to segregate data in the test environment.\n response: (requestJsonBody?: { raw_message: string }) => {\n const splittedRawMessage = requestJsonBody?.raw_message.split(':');\n const e2eIdentifier = splittedRawMessage?.[splittedRawMessage.length - 2];\n\n return {\n ...MOCK_LOGIN_RESPONSE,\n token: e2eIdentifier ?? MOCK_LOGIN_RESPONSE.token,\n profile: {\n ...MOCK_LOGIN_RESPONSE.profile,\n profile_id: e2eIdentifier ?? MOCK_LOGIN_RESPONSE.profile.profile_id,\n identifier_id:\n e2eIdentifier ?? MOCK_LOGIN_RESPONSE.profile.identifier_id,\n },\n };\n },\n } satisfies MockResponse;\n};\n\nexport const MOCK_OATH_TOKEN_RESPONSE = SDK_MOCK_OIDC_TOKEN_RESPONSE;\n\nexport const getMockAuthAccessTokenResponse = (env: Env = Env.PRD) => {\n return {\n url: OIDC_TOKEN_URL(env),\n requestMethod: 'POST',\n response: (requestJsonBody?: string) => {\n // We end up setting the access token to the e2eIdentifier in the test environment\n // This is then attached to every request's Authorization header\n // and used to segregate data in the test environment\n const e2eIdentifier = new URLSearchParams(requestJsonBody).get(\n 'assertion',\n );\n\n return {\n ...MOCK_OATH_TOKEN_RESPONSE,\n access_token: e2eIdentifier ?? MOCK_OATH_TOKEN_RESPONSE.access_token,\n };\n },\n } satisfies MockResponse;\n};\n\nexport const getMockPairSocialTokenResponse = (env: Env = Env.PRD) => {\n return {\n url: PAIR_SOCIAL_IDENTIFIER(env),\n requestMethod: 'POST',\n response: () => {\n return 'OK';\n },\n statusCode: 204,\n } satisfies MockResponse;\n};\n"]}
@@ -1,3 +1,4 @@
1
+ import { Env } from "../../../shared/env.cjs";
1
2
  export declare const MOCK_NONCE_RESPONSE: {
2
3
  nonce: string;
3
4
  identifier: string;
@@ -5,7 +6,7 @@ export declare const MOCK_NONCE_RESPONSE: {
5
6
  };
6
7
  export declare const MOCK_NONCE: string;
7
8
  export declare const MOCK_JWT = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImIwNzE2N2U2LWJjNWUtNDgyZC1hNjRhLWU1MjQ0MjY2MGU3NyJ9.eyJzdWIiOiI1MzE0ODc5YWM2NDU1OGI3OTQ5ZmI4NWIzMjg2ZjZjNjUwODAzYmFiMTY0Y2QyOWNmMmM3YzdmMjMzMWMwZTRlIiwiaWF0IjoxNzA2MTEzMDYyLCJleHAiOjE3NjkxODUwNjMsImlzcyI6ImF1dGgubWV0YW1hc2suaW8iLCJhdWQiOiJwb3J0Zm9saW8ubWV0YW1hc2suaW8ifQ.E5UL6oABNweS8t5a6IBTqTf7NLOJbrhJSmEcsr7kwLp4bGvcENJzACwnsHDkA6PlzfDV09ZhAGU_F3hlS0j-erbY0k0AFR-GAtyS7E9N02D8RgUDz5oDR65CKmzM8JilgFA8UvruJ6OJGogroaOSOqzRES_s8MjHpP47RJ9lXrUesajsbOudXbuksXWg5QmWip6LLvjwr8UUzcJzNQilyIhiEpo4WdzWM4R3VtTwr4rHnWEvtYnYCov1jmI2w3YQ48y0M-3Y9IOO0ov_vlITRrOnR7Y7fRUGLUFmU5msD8mNWRywjQFLHfJJ1yNP5aJ8TkuCK3sC6kcUH335IVvukQ";
8
- export declare const getMockAuthNonceResponse: () => {
9
+ export declare const getMockAuthNonceResponse: (env?: Env) => {
9
10
  url: string;
10
11
  requestMethod: "GET";
11
12
  response: (_?: unknown, path?: string, getE2ESrpIdentifierForPublicKey?: ((publicKey: string) => string) | undefined) => {
@@ -25,7 +26,7 @@ export declare const MOCK_LOGIN_RESPONSE: {
25
26
  encrypted_storage_key: string;
26
27
  };
27
28
  };
28
- export declare const getMockAuthLoginResponse: () => {
29
+ export declare const getMockAuthLoginResponse: (env?: Env) => {
29
30
  url: string;
30
31
  requestMethod: "POST";
31
32
  response: (requestJsonBody?: {
@@ -46,7 +47,7 @@ export declare const MOCK_OATH_TOKEN_RESPONSE: {
46
47
  access_token: string;
47
48
  expires_in: number;
48
49
  };
49
- export declare const getMockAuthAccessTokenResponse: () => {
50
+ export declare const getMockAuthAccessTokenResponse: (env?: Env) => {
50
51
  url: string;
51
52
  requestMethod: "POST";
52
53
  response: (requestJsonBody?: string) => {
@@ -54,4 +55,10 @@ export declare const getMockAuthAccessTokenResponse: () => {
54
55
  expires_in: number;
55
56
  };
56
57
  };
58
+ export declare const getMockPairSocialTokenResponse: (env?: Env) => {
59
+ url: string;
60
+ requestMethod: "POST";
61
+ response: () => string;
62
+ statusCode: number;
63
+ };
57
64
  //# sourceMappingURL=mockResponses.d.cts.map