@metamask/profile-sync-controller 9.0.0 → 10.1.0

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 (154) hide show
  1. package/CHANGELOG.md +20 -1
  2. package/dist/controllers/authentication/AuthenticationController.cjs +53 -90
  3. package/dist/controllers/authentication/AuthenticationController.cjs.map +1 -1
  4. package/dist/controllers/authentication/AuthenticationController.d.cts +11 -23
  5. package/dist/controllers/authentication/AuthenticationController.d.cts.map +1 -1
  6. package/dist/controllers/authentication/AuthenticationController.d.mts +11 -23
  7. package/dist/controllers/authentication/AuthenticationController.d.mts.map +1 -1
  8. package/dist/controllers/authentication/AuthenticationController.mjs +53 -90
  9. package/dist/controllers/authentication/AuthenticationController.mjs.map +1 -1
  10. package/dist/controllers/authentication/mocks/mockResponses.cjs +10 -23
  11. package/dist/controllers/authentication/mocks/mockResponses.cjs.map +1 -1
  12. package/dist/controllers/authentication/mocks/mockResponses.d.cts +42 -10
  13. package/dist/controllers/authentication/mocks/mockResponses.d.cts.map +1 -1
  14. package/dist/controllers/authentication/mocks/mockResponses.d.mts +42 -10
  15. package/dist/controllers/authentication/mocks/mockResponses.d.mts.map +1 -1
  16. package/dist/controllers/authentication/mocks/mockResponses.mjs +9 -22
  17. package/dist/controllers/authentication/mocks/mockResponses.mjs.map +1 -1
  18. package/dist/controllers/user-storage/UserStorageController.cjs +76 -127
  19. package/dist/controllers/user-storage/UserStorageController.cjs.map +1 -1
  20. package/dist/controllers/user-storage/UserStorageController.d.cts +10 -4
  21. package/dist/controllers/user-storage/UserStorageController.d.cts.map +1 -1
  22. package/dist/controllers/user-storage/UserStorageController.d.mts +10 -4
  23. package/dist/controllers/user-storage/UserStorageController.d.mts.map +1 -1
  24. package/dist/controllers/user-storage/UserStorageController.mjs +76 -127
  25. package/dist/controllers/user-storage/UserStorageController.mjs.map +1 -1
  26. package/dist/controllers/user-storage/account-syncing/controller-integration.cjs +1 -1
  27. package/dist/controllers/user-storage/account-syncing/controller-integration.cjs.map +1 -1
  28. package/dist/controllers/user-storage/account-syncing/controller-integration.mjs +1 -1
  29. package/dist/controllers/user-storage/account-syncing/controller-integration.mjs.map +1 -1
  30. package/dist/controllers/user-storage/account-syncing/sync-utils.cjs +1 -0
  31. package/dist/controllers/user-storage/account-syncing/sync-utils.cjs.map +1 -1
  32. package/dist/controllers/user-storage/account-syncing/sync-utils.d.cts.map +1 -1
  33. package/dist/controllers/user-storage/account-syncing/sync-utils.d.mts.map +1 -1
  34. package/dist/controllers/user-storage/account-syncing/sync-utils.mjs +1 -0
  35. package/dist/controllers/user-storage/account-syncing/sync-utils.mjs.map +1 -1
  36. package/dist/controllers/user-storage/mocks/mockResponses.cjs +3 -3
  37. package/dist/controllers/user-storage/mocks/mockResponses.cjs.map +1 -1
  38. package/dist/controllers/user-storage/mocks/mockResponses.d.cts +1 -1
  39. package/dist/controllers/user-storage/mocks/mockResponses.d.cts.map +1 -1
  40. package/dist/controllers/user-storage/mocks/mockResponses.d.mts +1 -1
  41. package/dist/controllers/user-storage/mocks/mockResponses.d.mts.map +1 -1
  42. package/dist/controllers/user-storage/mocks/mockResponses.mjs +3 -3
  43. package/dist/controllers/user-storage/mocks/mockResponses.mjs.map +1 -1
  44. package/dist/controllers/user-storage/network-syncing/controller-integration.cjs +14 -14
  45. package/dist/controllers/user-storage/network-syncing/controller-integration.cjs.map +1 -1
  46. package/dist/controllers/user-storage/network-syncing/controller-integration.d.cts +3 -3
  47. package/dist/controllers/user-storage/network-syncing/controller-integration.d.cts.map +1 -1
  48. package/dist/controllers/user-storage/network-syncing/controller-integration.d.mts +3 -3
  49. package/dist/controllers/user-storage/network-syncing/controller-integration.d.mts.map +1 -1
  50. package/dist/controllers/user-storage/network-syncing/controller-integration.mjs +14 -14
  51. package/dist/controllers/user-storage/network-syncing/controller-integration.mjs.map +1 -1
  52. package/dist/controllers/user-storage/network-syncing/services.cjs +27 -23
  53. package/dist/controllers/user-storage/network-syncing/services.cjs.map +1 -1
  54. package/dist/controllers/user-storage/network-syncing/services.d.cts +16 -7
  55. package/dist/controllers/user-storage/network-syncing/services.d.cts.map +1 -1
  56. package/dist/controllers/user-storage/network-syncing/services.d.mts +16 -7
  57. package/dist/controllers/user-storage/network-syncing/services.d.mts.map +1 -1
  58. package/dist/controllers/user-storage/network-syncing/services.mjs +27 -23
  59. package/dist/controllers/user-storage/network-syncing/services.mjs.map +1 -1
  60. package/dist/controllers/user-storage/network-syncing/sync-mutations.cjs.map +1 -1
  61. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.cts +13 -5
  62. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.cts.map +1 -1
  63. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.mts +13 -5
  64. package/dist/controllers/user-storage/network-syncing/sync-mutations.d.mts.map +1 -1
  65. package/dist/controllers/user-storage/network-syncing/sync-mutations.mjs.map +1 -1
  66. package/dist/controllers/user-storage/types.cjs +3 -0
  67. package/dist/controllers/user-storage/types.cjs.map +1 -0
  68. package/dist/controllers/user-storage/types.d.cts +23 -0
  69. package/dist/controllers/user-storage/types.d.cts.map +1 -0
  70. package/dist/controllers/user-storage/types.d.mts +23 -0
  71. package/dist/controllers/user-storage/types.d.mts.map +1 -0
  72. package/dist/controllers/user-storage/types.mjs +2 -0
  73. package/dist/controllers/user-storage/types.mjs.map +1 -0
  74. package/dist/sdk/authentication-jwt-bearer/flow-srp.cjs +7 -8
  75. package/dist/sdk/authentication-jwt-bearer/flow-srp.cjs.map +1 -1
  76. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.cts +2 -0
  77. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.cts.map +1 -1
  78. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.mts +2 -0
  79. package/dist/sdk/authentication-jwt-bearer/flow-srp.d.mts.map +1 -1
  80. package/dist/sdk/authentication-jwt-bearer/flow-srp.mjs +8 -9
  81. package/dist/sdk/authentication-jwt-bearer/flow-srp.mjs.map +1 -1
  82. package/dist/sdk/authentication-jwt-bearer/services.cjs +10 -2
  83. package/dist/sdk/authentication-jwt-bearer/services.cjs.map +1 -1
  84. package/dist/sdk/authentication-jwt-bearer/services.d.cts +3 -1
  85. package/dist/sdk/authentication-jwt-bearer/services.d.cts.map +1 -1
  86. package/dist/sdk/authentication-jwt-bearer/services.d.mts +3 -1
  87. package/dist/sdk/authentication-jwt-bearer/services.d.mts.map +1 -1
  88. package/dist/sdk/authentication-jwt-bearer/services.mjs +10 -2
  89. package/dist/sdk/authentication-jwt-bearer/services.mjs.map +1 -1
  90. package/dist/sdk/authentication-jwt-bearer/types.cjs.map +1 -1
  91. package/dist/sdk/authentication-jwt-bearer/types.mjs.map +1 -1
  92. package/dist/sdk/authentication.cjs.map +1 -1
  93. package/dist/sdk/authentication.d.cts +1 -1
  94. package/dist/sdk/authentication.d.cts.map +1 -1
  95. package/dist/sdk/authentication.d.mts +1 -1
  96. package/dist/sdk/authentication.d.mts.map +1 -1
  97. package/dist/sdk/authentication.mjs.map +1 -1
  98. package/dist/sdk/mocks/auth.cjs +44 -0
  99. package/dist/sdk/mocks/auth.cjs.map +1 -0
  100. package/dist/sdk/mocks/auth.d.cts +39 -0
  101. package/dist/sdk/mocks/auth.d.cts.map +1 -0
  102. package/dist/sdk/mocks/auth.d.mts +39 -0
  103. package/dist/sdk/mocks/auth.d.mts.map +1 -0
  104. package/dist/sdk/mocks/auth.mjs +41 -0
  105. package/dist/sdk/mocks/auth.mjs.map +1 -0
  106. package/dist/sdk/mocks/userstorage.cjs +43 -0
  107. package/dist/sdk/mocks/userstorage.cjs.map +1 -0
  108. package/dist/sdk/mocks/userstorage.d.cts +10 -0
  109. package/dist/sdk/mocks/userstorage.d.cts.map +1 -0
  110. package/dist/sdk/mocks/userstorage.d.mts +10 -0
  111. package/dist/sdk/mocks/userstorage.d.mts.map +1 -0
  112. package/dist/sdk/mocks/userstorage.mjs +15 -0
  113. package/dist/sdk/mocks/userstorage.mjs.map +1 -0
  114. package/dist/sdk/user-storage.cjs +39 -43
  115. package/dist/sdk/user-storage.cjs.map +1 -1
  116. package/dist/sdk/user-storage.d.cts +13 -8
  117. package/dist/sdk/user-storage.d.cts.map +1 -1
  118. package/dist/sdk/user-storage.d.mts +13 -8
  119. package/dist/sdk/user-storage.d.mts.map +1 -1
  120. package/dist/sdk/user-storage.mjs +39 -43
  121. package/dist/sdk/user-storage.mjs.map +1 -1
  122. package/dist/sdk/utils/messaging-signing-snap-requests.cjs +12 -1
  123. package/dist/sdk/utils/messaging-signing-snap-requests.cjs.map +1 -1
  124. package/dist/sdk/utils/messaging-signing-snap-requests.d.cts +6 -0
  125. package/dist/sdk/utils/messaging-signing-snap-requests.d.cts.map +1 -1
  126. package/dist/sdk/utils/messaging-signing-snap-requests.d.mts +6 -0
  127. package/dist/sdk/utils/messaging-signing-snap-requests.d.mts.map +1 -1
  128. package/dist/sdk/utils/messaging-signing-snap-requests.mjs +10 -0
  129. package/dist/sdk/utils/messaging-signing-snap-requests.mjs.map +1 -1
  130. package/dist/shared/types/services.cjs +3 -0
  131. package/dist/shared/types/services.cjs.map +1 -0
  132. package/dist/shared/types/services.d.cts +10 -0
  133. package/dist/shared/types/services.d.cts.map +1 -0
  134. package/dist/shared/types/services.d.mts +10 -0
  135. package/dist/shared/types/services.d.mts.map +1 -0
  136. package/dist/shared/types/services.mjs +2 -0
  137. package/dist/shared/types/services.mjs.map +1 -0
  138. package/package.json +6 -6
  139. package/dist/controllers/authentication/services.cjs +0 -132
  140. package/dist/controllers/authentication/services.cjs.map +0 -1
  141. package/dist/controllers/authentication/services.d.cts +0 -72
  142. package/dist/controllers/authentication/services.d.cts.map +0 -1
  143. package/dist/controllers/authentication/services.d.mts +0 -72
  144. package/dist/controllers/authentication/services.d.mts.map +0 -1
  145. package/dist/controllers/authentication/services.mjs +0 -129
  146. package/dist/controllers/authentication/services.mjs.map +0 -1
  147. package/dist/controllers/user-storage/services.cjs +0 -306
  148. package/dist/controllers/user-storage/services.cjs.map +0 -1
  149. package/dist/controllers/user-storage/services.d.cts +0 -85
  150. package/dist/controllers/user-storage/services.d.cts.map +0 -1
  151. package/dist/controllers/user-storage/services.d.mts +0 -85
  152. package/dist/controllers/user-storage/services.d.mts.map +0 -1
  153. package/dist/controllers/user-storage/services.mjs +0 -276
  154. package/dist/controllers/user-storage/services.mjs.map +0 -1
@@ -1,132 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createLoginRawMessage = exports.getAccessToken = exports.login = exports.getNonce = exports.OIDC_TOKENS_ENDPOINT = exports.AUTH_LOGIN_ENDPOINT = exports.AUTH_NONCE_ENDPOINT = void 0;
7
- const loglevel_1 = __importDefault(require("loglevel"));
8
- const env_1 = require("../../shared/env.cjs");
9
- const ENV_URLS = (0, env_1.getEnvUrls)(env_1.Env.PRD);
10
- const AUTH_ENDPOINT = ENV_URLS.authApiUrl;
11
- exports.AUTH_NONCE_ENDPOINT = `${AUTH_ENDPOINT}/api/v2/nonce`;
12
- exports.AUTH_LOGIN_ENDPOINT = `${AUTH_ENDPOINT}/api/v2/srp/login`;
13
- const OIDC_ENDPOINT = ENV_URLS.oidcApiUrl || '';
14
- exports.OIDC_TOKENS_ENDPOINT = `${OIDC_ENDPOINT}/oauth2/token`;
15
- const OIDC_CLIENT_ID = (platform) => {
16
- if (platform === 'extension') {
17
- return (0, env_1.getOidcClientId)(env_1.Env.PRD, env_1.Platform.EXTENSION);
18
- }
19
- if (platform === 'mobile') {
20
- return (0, env_1.getOidcClientId)(env_1.Env.PRD, env_1.Platform.MOBILE);
21
- }
22
- throw new Error(`Unsupported platform - ${platform}`);
23
- };
24
- const OIDC_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
25
- /**
26
- * Auth Service - Get Nonce. Used for the initial JWTBearer flow
27
- *
28
- * @param publicKey - public key to associate a nonce with
29
- * @returns the nonce or null if failed
30
- */
31
- async function getNonce(publicKey) {
32
- const nonceUrl = new URL(exports.AUTH_NONCE_ENDPOINT);
33
- nonceUrl.searchParams.set('identifier', publicKey);
34
- try {
35
- const nonceResponse = await fetch(nonceUrl.toString());
36
- if (!nonceResponse.ok) {
37
- loglevel_1.default.error(`authentication-controller/services: unable to get nonce - HTTP ${nonceResponse.status}`);
38
- return null;
39
- }
40
- const nonceJson = await nonceResponse.json();
41
- return nonceJson?.nonce ?? null;
42
- }
43
- catch (e) {
44
- loglevel_1.default.error('authentication-controller/services: unable to get nonce', e);
45
- return null;
46
- }
47
- }
48
- exports.getNonce = getNonce;
49
- /**
50
- * Auth Service - Login. Will perform login with a given signature and will return a single use JWT Token.
51
- *
52
- * @param rawMessage - the original message before signing
53
- * @param signature - the signed message
54
- * @param clientMetaMetrics - optional client metametrics id (to associate on backend)
55
- * @returns The Login Response
56
- */
57
- async function login(rawMessage, signature, clientMetaMetrics) {
58
- try {
59
- const response = await fetch(exports.AUTH_LOGIN_ENDPOINT, {
60
- method: 'POST',
61
- headers: {
62
- 'Content-Type': 'application/json',
63
- },
64
- body: JSON.stringify({
65
- signature,
66
- raw_message: rawMessage,
67
- metametrics: {
68
- metametrics_id: clientMetaMetrics.metametricsId,
69
- agent: clientMetaMetrics.agent,
70
- },
71
- }),
72
- });
73
- if (!response.ok) {
74
- loglevel_1.default.error(`authentication-controller/services: unable to login - HTTP ${response.status}`);
75
- return null;
76
- }
77
- const loginResponse = await response.json();
78
- return loginResponse ?? null;
79
- }
80
- catch (e) {
81
- loglevel_1.default.error('authentication-controller/services: unable to login', e);
82
- return null;
83
- }
84
- }
85
- exports.login = login;
86
- /**
87
- * OIDC Service - Access Token. Trades the Auth Token for an access token (to be used for other authenticated endpoints)
88
- * NOTE - the access token is short lived, which means it is best practice to validate session before calling authenticated endpoints
89
- *
90
- * @param jwtToken - the JWT Auth Token, received from `/login`
91
- * @param platform - the OIDC platform to retrieve access token
92
- * @returns JWT Access token to store and use on authorized endpoints.
93
- */
94
- async function getAccessToken(jwtToken, platform) {
95
- const headers = new Headers({
96
- 'Content-Type': 'application/x-www-form-urlencoded',
97
- });
98
- const urlEncodedBody = new URLSearchParams();
99
- urlEncodedBody.append('grant_type', OIDC_GRANT_TYPE);
100
- urlEncodedBody.append('client_id', OIDC_CLIENT_ID(platform));
101
- urlEncodedBody.append('assertion', jwtToken);
102
- try {
103
- const response = await fetch(exports.OIDC_TOKENS_ENDPOINT, {
104
- method: 'POST',
105
- headers,
106
- body: urlEncodedBody.toString(),
107
- });
108
- if (!response.ok) {
109
- loglevel_1.default.error(`authentication-controller/services: unable to get access token - HTTP ${response.status}`);
110
- return null;
111
- }
112
- const accessTokenResponse = await response.json();
113
- return accessTokenResponse?.access_token ?? null;
114
- }
115
- catch (e) {
116
- loglevel_1.default.error('authentication-controller/services: unable to get access token', e);
117
- return null;
118
- }
119
- }
120
- exports.getAccessToken = getAccessToken;
121
- /**
122
- * Utility to create the raw login message for the JWT bearer flow (via SRP)
123
- *
124
- * @param nonce - nonce received from `/nonce` endpoint
125
- * @param publicKey - public key used to retrieve nonce and for message signing
126
- * @returns Raw Message which will be used for signing & logging in.
127
- */
128
- function createLoginRawMessage(nonce, publicKey) {
129
- return `metamask:${nonce}:${publicKey}`;
130
- }
131
- exports.createLoginRawMessage = createLoginRawMessage;
132
- //# sourceMappingURL=services.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"services.cjs","sourceRoot":"","sources":["../../../src/controllers/authentication/services.ts"],"names":[],"mappings":";;;;;;AAAA,wDAA2B;AAE3B,8CAA8E;AAE9E,MAAM,QAAQ,GAAG,IAAA,gBAAU,EAAC,SAAG,CAAC,GAAG,CAAC,CAAC;AAErC,MAAM,aAAa,GAAW,QAAQ,CAAC,UAAU,CAAC;AACrC,QAAA,mBAAmB,GAAG,GAAG,aAAa,eAAe,CAAC;AACtD,QAAA,mBAAmB,GAAG,GAAG,aAAa,mBAAmB,CAAC;AAEvE,MAAM,aAAa,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,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,4BAAoB,EAAE;YACjD,MAAM,EAAE,MAAM;YACd,OAAO;YACP,IAAI,EAAE,cAAc,CAAC,QAAQ,EAAE;SAChC,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,kBAAG,CAAC,KAAK,CACP,yEAAyE,QAAQ,CAAC,MAAM,EAAE,CAC3F,CAAC;YACF,OAAO,IAAI,CAAC;SACb;QAED,MAAM,mBAAmB,GAAuB,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACtE,OAAO,mBAAmB,EAAE,YAAY,IAAI,IAAI,CAAC;KAClD;IAAC,OAAO,CAAC,EAAE;QACV,kBAAG,CAAC,KAAK,CACP,gEAAgE,EAChE,CAAC,CACF,CAAC;QACF,OAAO,IAAI,CAAC;KACb;AACH,CAAC;AApCD,wCAoCC;AAED;;;;;;GAMG;AACH,SAAgB,qBAAqB,CACnC,KAAa,EACb,SAAiB;IAEjB,OAAO,YAAY,KAAK,IAAI,SAAS,EAAW,CAAC;AACnD,CAAC;AALD,sDAKC","sourcesContent":["import log from 'loglevel';\n\nimport { Env, Platform, getEnvUrls, getOidcClientId } from '../../shared/env';\n\nconst ENV_URLS = getEnvUrls(Env.PRD);\n\nconst AUTH_ENDPOINT: string = ENV_URLS.authApiUrl;\nexport const AUTH_NONCE_ENDPOINT = `${AUTH_ENDPOINT}/api/v2/nonce`;\nexport const AUTH_LOGIN_ENDPOINT = `${AUTH_ENDPOINT}/api/v2/srp/login`;\n\nconst OIDC_ENDPOINT: string = ENV_URLS.oidcApiUrl || '';\nexport const OIDC_TOKENS_ENDPOINT = `${OIDC_ENDPOINT}/oauth2/token`;\nconst OIDC_CLIENT_ID = (platform: 'mobile' | 'extension') => {\n if (platform === 'extension') {\n return getOidcClientId(Env.PRD, Platform.EXTENSION);\n }\n if (platform === 'mobile') {\n return getOidcClientId(Env.PRD, Platform.MOBILE);\n }\n\n throw new Error(`Unsupported platform - ${platform as string}`);\n};\nconst OIDC_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:jwt-bearer';\n\nexport type NonceResponse = {\n nonce: string;\n};\n\n/**\n * Auth Service - Get Nonce. Used for the initial JWTBearer flow\n *\n * @param publicKey - public key to associate a nonce with\n * @returns the nonce or null if failed\n */\nexport async function getNonce(publicKey: string): Promise<string | null> {\n const nonceUrl = new URL(AUTH_NONCE_ENDPOINT);\n nonceUrl.searchParams.set('identifier', publicKey);\n\n try {\n const nonceResponse = await fetch(nonceUrl.toString());\n if (!nonceResponse.ok) {\n log.error(\n `authentication-controller/services: unable to get nonce - HTTP ${nonceResponse.status}`,\n );\n return null;\n }\n\n const nonceJson: NonceResponse = await nonceResponse.json();\n return nonceJson?.nonce ?? null;\n } catch (e) {\n log.error('authentication-controller/services: unable to get nonce', e);\n return null;\n }\n}\n\n/**\n * The Login API Server Response Shape\n */\nexport type LoginResponse = {\n token: string;\n\n expires_in: string;\n /**\n * Contains anonymous information about the logged in profile.\n *\n * identifier_id - a deterministic unique identifier on the method used to sign in\n *\n * profile_id - a unique id for a given profile\n *\n * metametrics_id - an anonymous server id\n */\n profile: {\n identifier_id: string;\n\n profile_id: string;\n };\n};\n\ntype ClientMetaMetrics = {\n metametricsId: string;\n agent: 'extension' | 'mobile';\n};\n\n/**\n * Auth Service - Login. Will perform login with a given signature and will return a single use JWT Token.\n *\n * @param rawMessage - the original message before signing\n * @param signature - the signed message\n * @param clientMetaMetrics - optional client metametrics id (to associate on backend)\n * @returns The Login Response\n */\nexport async function login(\n rawMessage: string,\n signature: string,\n clientMetaMetrics: ClientMetaMetrics,\n): Promise<LoginResponse | null> {\n try {\n const response = await fetch(AUTH_LOGIN_ENDPOINT, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({\n signature,\n\n raw_message: rawMessage,\n metametrics: {\n metametrics_id: clientMetaMetrics.metametricsId,\n agent: clientMetaMetrics.agent,\n },\n }),\n });\n\n if (!response.ok) {\n log.error(\n `authentication-controller/services: unable to login - HTTP ${response.status}`,\n );\n return null;\n }\n\n const loginResponse: LoginResponse = await response.json();\n return loginResponse ?? null;\n } catch (e) {\n log.error('authentication-controller/services: unable to login', e);\n return null;\n }\n}\n\n/**\n * The Auth API Token Response Shape\n */\nexport type OAuthTokenResponse = {\n access_token: string;\n\n expires_in: number;\n};\n\n/**\n * OIDC Service - Access Token. Trades the Auth Token for an access token (to be used for other authenticated endpoints)\n * NOTE - the access token is short lived, which means it is best practice to validate session before calling authenticated endpoints\n *\n * @param jwtToken - the JWT Auth Token, received from `/login`\n * @param platform - the OIDC platform to retrieve access token\n * @returns JWT Access token to store and use on authorized endpoints.\n */\nexport async function getAccessToken(\n jwtToken: string,\n platform: ClientMetaMetrics['agent'],\n): Promise<string | null> {\n const headers = new Headers({\n 'Content-Type': 'application/x-www-form-urlencoded',\n });\n\n const urlEncodedBody = new URLSearchParams();\n urlEncodedBody.append('grant_type', OIDC_GRANT_TYPE);\n urlEncodedBody.append('client_id', OIDC_CLIENT_ID(platform));\n urlEncodedBody.append('assertion', jwtToken);\n\n try {\n const response = await fetch(OIDC_TOKENS_ENDPOINT, {\n method: 'POST',\n headers,\n body: urlEncodedBody.toString(),\n });\n\n if (!response.ok) {\n log.error(\n `authentication-controller/services: unable to get access token - HTTP ${response.status}`,\n );\n return null;\n }\n\n const accessTokenResponse: OAuthTokenResponse = await response.json();\n return accessTokenResponse?.access_token ?? null;\n } catch (e) {\n log.error(\n 'authentication-controller/services: unable to get access token',\n e,\n );\n return null;\n }\n}\n\n/**\n * Utility to create the raw login message for the JWT bearer flow (via SRP)\n *\n * @param nonce - nonce received from `/nonce` endpoint\n * @param publicKey - public key used to retrieve nonce and for message signing\n * @returns Raw Message which will be used for signing & logging in.\n */\nexport function createLoginRawMessage(\n nonce: string,\n publicKey: string,\n): `metamask:${string}:${string}` {\n return `metamask:${nonce}:${publicKey}` as const;\n}\n"]}
@@ -1,72 +0,0 @@
1
- export declare const AUTH_NONCE_ENDPOINT: string;
2
- export declare const AUTH_LOGIN_ENDPOINT: string;
3
- export declare const OIDC_TOKENS_ENDPOINT: string;
4
- export type NonceResponse = {
5
- nonce: string;
6
- };
7
- /**
8
- * Auth Service - Get Nonce. Used for the initial JWTBearer flow
9
- *
10
- * @param publicKey - public key to associate a nonce with
11
- * @returns the nonce or null if failed
12
- */
13
- export declare function getNonce(publicKey: string): Promise<string | null>;
14
- /**
15
- * The Login API Server Response Shape
16
- */
17
- export type LoginResponse = {
18
- token: string;
19
- expires_in: string;
20
- /**
21
- * Contains anonymous information about the logged in profile.
22
- *
23
- * identifier_id - a deterministic unique identifier on the method used to sign in
24
- *
25
- * profile_id - a unique id for a given profile
26
- *
27
- * metametrics_id - an anonymous server id
28
- */
29
- profile: {
30
- identifier_id: string;
31
- profile_id: string;
32
- };
33
- };
34
- type ClientMetaMetrics = {
35
- metametricsId: string;
36
- agent: 'extension' | 'mobile';
37
- };
38
- /**
39
- * Auth Service - Login. Will perform login with a given signature and will return a single use JWT Token.
40
- *
41
- * @param rawMessage - the original message before signing
42
- * @param signature - the signed message
43
- * @param clientMetaMetrics - optional client metametrics id (to associate on backend)
44
- * @returns The Login Response
45
- */
46
- export declare function login(rawMessage: string, signature: string, clientMetaMetrics: ClientMetaMetrics): Promise<LoginResponse | null>;
47
- /**
48
- * The Auth API Token Response Shape
49
- */
50
- export type OAuthTokenResponse = {
51
- access_token: string;
52
- expires_in: number;
53
- };
54
- /**
55
- * OIDC Service - Access Token. Trades the Auth Token for an access token (to be used for other authenticated endpoints)
56
- * NOTE - the access token is short lived, which means it is best practice to validate session before calling authenticated endpoints
57
- *
58
- * @param jwtToken - the JWT Auth Token, received from `/login`
59
- * @param platform - the OIDC platform to retrieve access token
60
- * @returns JWT Access token to store and use on authorized endpoints.
61
- */
62
- export declare function getAccessToken(jwtToken: string, platform: ClientMetaMetrics['agent']): Promise<string | null>;
63
- /**
64
- * Utility to create the raw login message for the JWT bearer flow (via SRP)
65
- *
66
- * @param nonce - nonce received from `/nonce` endpoint
67
- * @param publicKey - public key used to retrieve nonce and for message signing
68
- * @returns Raw Message which will be used for signing & logging in.
69
- */
70
- export declare function createLoginRawMessage(nonce: string, publicKey: string): `metamask:${string}:${string}`;
71
- export {};
72
- //# sourceMappingURL=services.d.cts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"services.d.cts","sourceRoot":"","sources":["../../../src/controllers/authentication/services.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,mBAAmB,QAAkC,CAAC;AACnE,eAAO,MAAM,mBAAmB,QAAsC,CAAC;AAGvE,eAAO,MAAM,oBAAoB,QAAkC,CAAC;AAapE,MAAM,MAAM,aAAa,GAAG;IAC1B,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAmBxE;AAED;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,KAAK,EAAE,MAAM,CAAC;IAEd,UAAU,EAAE,MAAM,CAAC;IACnB;;;;;;;;OAQG;IACH,OAAO,EAAE;QACP,aAAa,EAAE,MAAM,CAAC;QAEtB,UAAU,EAAE,MAAM,CAAC;KACpB,CAAC;CACH,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,WAAW,GAAG,QAAQ,CAAC;CAC/B,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAsB,KAAK,CACzB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,iBAAiB,EAAE,iBAAiB,GACnC,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CA+B/B;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,YAAY,EAAE,MAAM,CAAC;IAErB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,iBAAiB,CAAC,OAAO,CAAC,GACnC,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAiCxB;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,GAChB,YAAY,MAAM,IAAI,MAAM,EAAE,CAEhC"}
@@ -1,72 +0,0 @@
1
- export declare const AUTH_NONCE_ENDPOINT: string;
2
- export declare const AUTH_LOGIN_ENDPOINT: string;
3
- export declare const OIDC_TOKENS_ENDPOINT: string;
4
- export type NonceResponse = {
5
- nonce: string;
6
- };
7
- /**
8
- * Auth Service - Get Nonce. Used for the initial JWTBearer flow
9
- *
10
- * @param publicKey - public key to associate a nonce with
11
- * @returns the nonce or null if failed
12
- */
13
- export declare function getNonce(publicKey: string): Promise<string | null>;
14
- /**
15
- * The Login API Server Response Shape
16
- */
17
- export type LoginResponse = {
18
- token: string;
19
- expires_in: string;
20
- /**
21
- * Contains anonymous information about the logged in profile.
22
- *
23
- * identifier_id - a deterministic unique identifier on the method used to sign in
24
- *
25
- * profile_id - a unique id for a given profile
26
- *
27
- * metametrics_id - an anonymous server id
28
- */
29
- profile: {
30
- identifier_id: string;
31
- profile_id: string;
32
- };
33
- };
34
- type ClientMetaMetrics = {
35
- metametricsId: string;
36
- agent: 'extension' | 'mobile';
37
- };
38
- /**
39
- * Auth Service - Login. Will perform login with a given signature and will return a single use JWT Token.
40
- *
41
- * @param rawMessage - the original message before signing
42
- * @param signature - the signed message
43
- * @param clientMetaMetrics - optional client metametrics id (to associate on backend)
44
- * @returns The Login Response
45
- */
46
- export declare function login(rawMessage: string, signature: string, clientMetaMetrics: ClientMetaMetrics): Promise<LoginResponse | null>;
47
- /**
48
- * The Auth API Token Response Shape
49
- */
50
- export type OAuthTokenResponse = {
51
- access_token: string;
52
- expires_in: number;
53
- };
54
- /**
55
- * OIDC Service - Access Token. Trades the Auth Token for an access token (to be used for other authenticated endpoints)
56
- * NOTE - the access token is short lived, which means it is best practice to validate session before calling authenticated endpoints
57
- *
58
- * @param jwtToken - the JWT Auth Token, received from `/login`
59
- * @param platform - the OIDC platform to retrieve access token
60
- * @returns JWT Access token to store and use on authorized endpoints.
61
- */
62
- export declare function getAccessToken(jwtToken: string, platform: ClientMetaMetrics['agent']): Promise<string | null>;
63
- /**
64
- * Utility to create the raw login message for the JWT bearer flow (via SRP)
65
- *
66
- * @param nonce - nonce received from `/nonce` endpoint
67
- * @param publicKey - public key used to retrieve nonce and for message signing
68
- * @returns Raw Message which will be used for signing & logging in.
69
- */
70
- export declare function createLoginRawMessage(nonce: string, publicKey: string): `metamask:${string}:${string}`;
71
- export {};
72
- //# sourceMappingURL=services.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"services.d.mts","sourceRoot":"","sources":["../../../src/controllers/authentication/services.ts"],"names":[],"mappings":"AAOA,eAAO,MAAM,mBAAmB,QAAkC,CAAC;AACnE,eAAO,MAAM,mBAAmB,QAAsC,CAAC;AAGvE,eAAO,MAAM,oBAAoB,QAAkC,CAAC;AAapE,MAAM,MAAM,aAAa,GAAG;IAC1B,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAmBxE;AAED;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,KAAK,EAAE,MAAM,CAAC;IAEd,UAAU,EAAE,MAAM,CAAC;IACnB;;;;;;;;OAQG;IACH,OAAO,EAAE;QACP,aAAa,EAAE,MAAM,CAAC;QAEtB,UAAU,EAAE,MAAM,CAAC;KACpB,CAAC;CACH,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,WAAW,GAAG,QAAQ,CAAC;CAC/B,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAsB,KAAK,CACzB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,iBAAiB,EAAE,iBAAiB,GACnC,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CA+B/B;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,YAAY,EAAE,MAAM,CAAC;IAErB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,iBAAiB,CAAC,OAAO,CAAC,GACnC,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAiCxB;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,GAChB,YAAY,MAAM,IAAI,MAAM,EAAE,CAEhC"}
@@ -1,129 +0,0 @@
1
- function $importDefault(module) {
2
- if (module?.__esModule) {
3
- return module.default;
4
- }
5
- return module;
6
- }
7
- import $log from "loglevel";
8
- const log = $importDefault($log);
9
- import { Env, Platform, getEnvUrls, getOidcClientId } from "../../shared/env.mjs";
10
- const ENV_URLS = getEnvUrls(Env.PRD);
11
- const AUTH_ENDPOINT = ENV_URLS.authApiUrl;
12
- export const AUTH_NONCE_ENDPOINT = `${AUTH_ENDPOINT}/api/v2/nonce`;
13
- export const AUTH_LOGIN_ENDPOINT = `${AUTH_ENDPOINT}/api/v2/srp/login`;
14
- const OIDC_ENDPOINT = ENV_URLS.oidcApiUrl || '';
15
- export const OIDC_TOKENS_ENDPOINT = `${OIDC_ENDPOINT}/oauth2/token`;
16
- const OIDC_CLIENT_ID = (platform) => {
17
- if (platform === 'extension') {
18
- return getOidcClientId(Env.PRD, Platform.EXTENSION);
19
- }
20
- if (platform === 'mobile') {
21
- return getOidcClientId(Env.PRD, Platform.MOBILE);
22
- }
23
- throw new Error(`Unsupported platform - ${platform}`);
24
- };
25
- const OIDC_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
26
- /**
27
- * Auth Service - Get Nonce. Used for the initial JWTBearer flow
28
- *
29
- * @param publicKey - public key to associate a nonce with
30
- * @returns the nonce or null if failed
31
- */
32
- export async function getNonce(publicKey) {
33
- const nonceUrl = new URL(AUTH_NONCE_ENDPOINT);
34
- nonceUrl.searchParams.set('identifier', publicKey);
35
- try {
36
- const nonceResponse = await fetch(nonceUrl.toString());
37
- if (!nonceResponse.ok) {
38
- log.error(`authentication-controller/services: unable to get nonce - HTTP ${nonceResponse.status}`);
39
- return null;
40
- }
41
- const nonceJson = await nonceResponse.json();
42
- return nonceJson?.nonce ?? null;
43
- }
44
- catch (e) {
45
- log.error('authentication-controller/services: unable to get nonce', e);
46
- return null;
47
- }
48
- }
49
- /**
50
- * Auth Service - Login. Will perform login with a given signature and will return a single use JWT Token.
51
- *
52
- * @param rawMessage - the original message before signing
53
- * @param signature - the signed message
54
- * @param clientMetaMetrics - optional client metametrics id (to associate on backend)
55
- * @returns The Login Response
56
- */
57
- export async function login(rawMessage, signature, clientMetaMetrics) {
58
- try {
59
- const response = await fetch(AUTH_LOGIN_ENDPOINT, {
60
- method: 'POST',
61
- headers: {
62
- 'Content-Type': 'application/json',
63
- },
64
- body: JSON.stringify({
65
- signature,
66
- raw_message: rawMessage,
67
- metametrics: {
68
- metametrics_id: clientMetaMetrics.metametricsId,
69
- agent: clientMetaMetrics.agent,
70
- },
71
- }),
72
- });
73
- if (!response.ok) {
74
- log.error(`authentication-controller/services: unable to login - HTTP ${response.status}`);
75
- return null;
76
- }
77
- const loginResponse = await response.json();
78
- return loginResponse ?? null;
79
- }
80
- catch (e) {
81
- log.error('authentication-controller/services: unable to login', e);
82
- return null;
83
- }
84
- }
85
- /**
86
- * OIDC Service - Access Token. Trades the Auth Token for an access token (to be used for other authenticated endpoints)
87
- * NOTE - the access token is short lived, which means it is best practice to validate session before calling authenticated endpoints
88
- *
89
- * @param jwtToken - the JWT Auth Token, received from `/login`
90
- * @param platform - the OIDC platform to retrieve access token
91
- * @returns JWT Access token to store and use on authorized endpoints.
92
- */
93
- export async function getAccessToken(jwtToken, platform) {
94
- const headers = new Headers({
95
- 'Content-Type': 'application/x-www-form-urlencoded',
96
- });
97
- const urlEncodedBody = new URLSearchParams();
98
- urlEncodedBody.append('grant_type', OIDC_GRANT_TYPE);
99
- urlEncodedBody.append('client_id', OIDC_CLIENT_ID(platform));
100
- urlEncodedBody.append('assertion', jwtToken);
101
- try {
102
- const response = await fetch(OIDC_TOKENS_ENDPOINT, {
103
- method: 'POST',
104
- headers,
105
- body: urlEncodedBody.toString(),
106
- });
107
- if (!response.ok) {
108
- log.error(`authentication-controller/services: unable to get access token - HTTP ${response.status}`);
109
- return null;
110
- }
111
- const accessTokenResponse = await response.json();
112
- return accessTokenResponse?.access_token ?? null;
113
- }
114
- catch (e) {
115
- log.error('authentication-controller/services: unable to get access token', e);
116
- return null;
117
- }
118
- }
119
- /**
120
- * Utility to create the raw login message for the JWT bearer flow (via SRP)
121
- *
122
- * @param nonce - nonce received from `/nonce` endpoint
123
- * @param publicKey - public key used to retrieve nonce and for message signing
124
- * @returns Raw Message which will be used for signing & logging in.
125
- */
126
- export function createLoginRawMessage(nonce, publicKey) {
127
- return `metamask:${nonce}:${publicKey}`;
128
- }
129
- //# sourceMappingURL=services.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"services.mjs","sourceRoot":"","sources":["../../../src/controllers/authentication/services.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,IAAG,iBAAiB;;AAE3B,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe,EAAE,6BAAyB;AAE9E,MAAM,QAAQ,GAAG,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AAErC,MAAM,aAAa,GAAW,QAAQ,CAAC,UAAU,CAAC;AAClD,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,aAAa,eAAe,CAAC;A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AAC,CAAC;IAC7D,cAAc,CAAC,MAAM,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;IAE7C,IAAI;QACF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,oBAAoB,EAAE;YACjD,MAAM,EAAE,MAAM;YACd,OAAO;YACP,IAAI,EAAE,cAAc,CAAC,QAAQ,EAAE;SAChC,CAAC,CAAC;QAEH,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,GAAG,CAAC,KAAK,CACP,yEAAyE,QAAQ,CAAC,MAAM,EAAE,CAC3F,CAAC;YACF,OAAO,IAAI,CAAC;SACb;QAED,MAAM,mBAAmB,GAAuB,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QACtE,OAAO,mBAAmB,EAAE,YAAY,IAAI,IAAI,CAAC;KAClD;IAAC,OAAO,CAAC,EAAE;QACV,GAAG,CAAC,KAAK,CACP,gEAAgE,EAChE,CAAC,CACF,CAAC;QACF,OAAO,IAAI,CAAC;KACb;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CACnC,KAAa,EACb,SAAiB;IAEjB,OAAO,YAAY,KAAK,IAAI,SAAS,EAAW,CAAC;AACnD,CAAC","sourcesContent":["import log from 'loglevel';\n\nimport { Env, Platform, getEnvUrls, getOidcClientId } from '../../shared/env';\n\nconst ENV_URLS = getEnvUrls(Env.PRD);\n\nconst AUTH_ENDPOINT: string = ENV_URLS.authApiUrl;\nexport const AUTH_NONCE_ENDPOINT = `${AUTH_ENDPOINT}/api/v2/nonce`;\nexport const AUTH_LOGIN_ENDPOINT = `${AUTH_ENDPOINT}/api/v2/srp/login`;\n\nconst OIDC_ENDPOINT: string = ENV_URLS.oidcApiUrl || '';\nexport const OIDC_TOKENS_ENDPOINT = `${OIDC_ENDPOINT}/oauth2/token`;\nconst OIDC_CLIENT_ID = (platform: 'mobile' | 'extension') => {\n if (platform === 'extension') {\n return getOidcClientId(Env.PRD, Platform.EXTENSION);\n }\n if (platform === 'mobile') {\n return getOidcClientId(Env.PRD, Platform.MOBILE);\n }\n\n throw new Error(`Unsupported platform - ${platform as string}`);\n};\nconst OIDC_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:jwt-bearer';\n\nexport type NonceResponse = {\n nonce: string;\n};\n\n/**\n * Auth Service - Get Nonce. Used for the initial JWTBearer flow\n *\n * @param publicKey - public key to associate a nonce with\n * @returns the nonce or null if failed\n */\nexport async function getNonce(publicKey: string): Promise<string | null> {\n const nonceUrl = new URL(AUTH_NONCE_ENDPOINT);\n nonceUrl.searchParams.set('identifier', publicKey);\n\n try {\n const nonceResponse = await fetch(nonceUrl.toString());\n if (!nonceResponse.ok) {\n log.error(\n `authentication-controller/services: unable to get nonce - HTTP ${nonceResponse.status}`,\n );\n return null;\n }\n\n const nonceJson: NonceResponse = await nonceResponse.json();\n return nonceJson?.nonce ?? null;\n } catch (e) {\n log.error('authentication-controller/services: unable to get nonce', e);\n return null;\n }\n}\n\n/**\n * The Login API Server Response Shape\n */\nexport type LoginResponse = {\n token: string;\n\n expires_in: string;\n /**\n * Contains anonymous information about the logged in profile.\n *\n * identifier_id - a deterministic unique identifier on the method used to sign in\n *\n * profile_id - a unique id for a given profile\n *\n * metametrics_id - an anonymous server id\n */\n profile: {\n identifier_id: string;\n\n profile_id: string;\n };\n};\n\ntype ClientMetaMetrics = {\n metametricsId: string;\n agent: 'extension' | 'mobile';\n};\n\n/**\n * Auth Service - Login. Will perform login with a given signature and will return a single use JWT Token.\n *\n * @param rawMessage - the original message before signing\n * @param signature - the signed message\n * @param clientMetaMetrics - optional client metametrics id (to associate on backend)\n * @returns The Login Response\n */\nexport async function login(\n rawMessage: string,\n signature: string,\n clientMetaMetrics: ClientMetaMetrics,\n): Promise<LoginResponse | null> {\n try {\n const response = await fetch(AUTH_LOGIN_ENDPOINT, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({\n signature,\n\n raw_message: rawMessage,\n metametrics: {\n metametrics_id: clientMetaMetrics.metametricsId,\n agent: clientMetaMetrics.agent,\n },\n }),\n });\n\n if (!response.ok) {\n log.error(\n `authentication-controller/services: unable to login - HTTP ${response.status}`,\n );\n return null;\n }\n\n const loginResponse: LoginResponse = await response.json();\n return loginResponse ?? null;\n } catch (e) {\n log.error('authentication-controller/services: unable to login', e);\n return null;\n }\n}\n\n/**\n * The Auth API Token Response Shape\n */\nexport type OAuthTokenResponse = {\n access_token: string;\n\n expires_in: number;\n};\n\n/**\n * OIDC Service - Access Token. Trades the Auth Token for an access token (to be used for other authenticated endpoints)\n * NOTE - the access token is short lived, which means it is best practice to validate session before calling authenticated endpoints\n *\n * @param jwtToken - the JWT Auth Token, received from `/login`\n * @param platform - the OIDC platform to retrieve access token\n * @returns JWT Access token to store and use on authorized endpoints.\n */\nexport async function getAccessToken(\n jwtToken: string,\n platform: ClientMetaMetrics['agent'],\n): Promise<string | null> {\n const headers = new Headers({\n 'Content-Type': 'application/x-www-form-urlencoded',\n });\n\n const urlEncodedBody = new URLSearchParams();\n urlEncodedBody.append('grant_type', OIDC_GRANT_TYPE);\n urlEncodedBody.append('client_id', OIDC_CLIENT_ID(platform));\n urlEncodedBody.append('assertion', jwtToken);\n\n try {\n const response = await fetch(OIDC_TOKENS_ENDPOINT, {\n method: 'POST',\n headers,\n body: urlEncodedBody.toString(),\n });\n\n if (!response.ok) {\n log.error(\n `authentication-controller/services: unable to get access token - HTTP ${response.status}`,\n );\n return null;\n }\n\n const accessTokenResponse: OAuthTokenResponse = await response.json();\n return accessTokenResponse?.access_token ?? null;\n } catch (e) {\n log.error(\n 'authentication-controller/services: unable to get access token',\n e,\n );\n return null;\n }\n}\n\n/**\n * Utility to create the raw login message for the JWT bearer flow (via SRP)\n *\n * @param nonce - nonce received from `/nonce` endpoint\n * @param publicKey - public key used to retrieve nonce and for message signing\n * @returns Raw Message which will be used for signing & logging in.\n */\nexport function createLoginRawMessage(\n nonce: string,\n publicKey: string,\n): `metamask:${string}:${string}` {\n return `metamask:${nonce}:${publicKey}` as const;\n}\n"]}