firebase-tools-with-isolate 13.16.0 → 13.26.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 (220) hide show
  1. package/lib/api.js +3 -1
  2. package/lib/apiv2.js +10 -3
  3. package/lib/appdistribution/client.js +62 -8
  4. package/lib/appdistribution/distribution.js +1 -1
  5. package/lib/apphosting/backend.js +299 -0
  6. package/lib/apphosting/config.js +31 -8
  7. package/lib/apphosting/githubConnections.js +70 -22
  8. package/lib/apphosting/index.js +3 -312
  9. package/lib/apphosting/rollout.js +138 -0
  10. package/lib/apphosting/secrets/index.js +70 -5
  11. package/lib/apphosting/utils.js +41 -0
  12. package/lib/apphosting/yaml.js +82 -0
  13. package/lib/archiveDirectory.js +1 -1
  14. package/lib/auth.js +31 -5
  15. package/lib/bin/firebase.js +2 -2
  16. package/lib/command.js +18 -4
  17. package/lib/commands/appdistribution-distribute.js +4 -4
  18. package/lib/commands/{appdistribution-group-create.js → appdistribution-groups-create.js} +2 -1
  19. package/lib/commands/{appdistribution-group-delete.js → appdistribution-groups-delete.js} +3 -2
  20. package/lib/commands/appdistribution-groups-list.js +55 -0
  21. package/lib/commands/appdistribution-testers-list.js +52 -0
  22. package/lib/commands/appdistribution-testers-remove.js +1 -1
  23. package/lib/commands/apphosting-backends-create.js +4 -4
  24. package/lib/commands/apphosting-backends-delete.js +7 -15
  25. package/lib/commands/apphosting-backends-get.js +1 -1
  26. package/lib/commands/apphosting-config-export.js +51 -0
  27. package/lib/commands/apphosting-repos-create.js +23 -0
  28. package/lib/commands/apphosting-rollouts-create.js +14 -15
  29. package/lib/commands/apphosting-secrets-grantaccess.js +2 -2
  30. package/lib/commands/database-import.js +4 -2
  31. package/lib/commands/database-push.js +4 -2
  32. package/lib/commands/database-set.js +4 -2
  33. package/lib/commands/database-settings-get.js +1 -1
  34. package/lib/commands/database-settings-set.js +1 -1
  35. package/lib/commands/dataconnect-sdk-generate.js +4 -1
  36. package/lib/commands/dataconnect-services-list.js +4 -3
  37. package/lib/commands/dataconnect-sql-diff.js +1 -1
  38. package/lib/commands/dataconnect-sql-grant.js +37 -0
  39. package/lib/commands/dataconnect-sql-migrate.js +1 -1
  40. package/lib/commands/dataconnect-sql-shell.js +115 -0
  41. package/lib/commands/deploy.js +2 -0
  42. package/lib/commands/emulators-start.js +4 -1
  43. package/lib/commands/ext-dev-init.js +2 -2
  44. package/lib/commands/ext-dev-list.js +1 -1
  45. package/lib/commands/ext-dev-register.js +2 -2
  46. package/lib/commands/ext-dev-upload.js +2 -2
  47. package/lib/commands/ext-dev-usage.js +2 -2
  48. package/lib/commands/ext-info.js +3 -1
  49. package/lib/commands/ext-install.js +2 -2
  50. package/lib/commands/ext-sdk-install.js +88 -0
  51. package/lib/commands/ext.js +1 -0
  52. package/lib/commands/firestore-databases-restore.js +39 -1
  53. package/lib/commands/functions-secrets-access.js +2 -4
  54. package/lib/commands/index.js +15 -2
  55. package/lib/commands/init.js +25 -15
  56. package/lib/commands/setup-emulators-dataconnect.js +0 -13
  57. package/lib/commands/use.js +1 -1
  58. package/lib/config.js +14 -4
  59. package/lib/crashlytics/buildToolsJarHelper.js +2 -2
  60. package/lib/dataconnect/client.js +1 -1
  61. package/lib/dataconnect/dataplaneClient.js +12 -9
  62. package/lib/dataconnect/ensureApis.js +6 -1
  63. package/lib/dataconnect/fileUtils.js +15 -39
  64. package/lib/dataconnect/freeTrial.js +45 -9
  65. package/lib/dataconnect/load.js +3 -1
  66. package/lib/dataconnect/provisionCloudSql.js +39 -26
  67. package/lib/dataconnect/schemaMigration.js +215 -74
  68. package/lib/dataconnect/types.js +4 -1
  69. package/lib/dataconnect/webhook.js +32 -0
  70. package/lib/deploy/dataconnect/deploy.js +18 -15
  71. package/lib/deploy/dataconnect/prepare.js +41 -0
  72. package/lib/deploy/dataconnect/release.js +16 -7
  73. package/lib/deploy/extensions/deploy.js +3 -1
  74. package/lib/deploy/extensions/deploymentSummary.js +7 -3
  75. package/lib/deploy/extensions/planner.js +44 -5
  76. package/lib/deploy/extensions/prepare.js +45 -73
  77. package/lib/deploy/extensions/release.js +11 -10
  78. package/lib/deploy/extensions/tasks.js +32 -21
  79. package/lib/deploy/firestore/prepare.js +9 -0
  80. package/lib/deploy/firestore/release.js +3 -6
  81. package/lib/deploy/functions/checkIam.js +7 -2
  82. package/lib/deploy/functions/ensure.js +11 -3
  83. package/lib/deploy/functions/prepare.js +10 -2
  84. package/lib/deploy/functions/release/planner.js +2 -1
  85. package/lib/deploy/functions/runtimes/discovery/index.js +6 -2
  86. package/lib/deploy/functions/runtimes/node/index.js +5 -0
  87. package/lib/deploy/functions/services/auth.js +34 -3
  88. package/lib/deploy/functions/services/index.js +2 -0
  89. package/lib/deploy/index.js +8 -5
  90. package/lib/deploy/lifecycleHooks.js +2 -1
  91. package/lib/emulator/apphosting/config.js +27 -0
  92. package/lib/emulator/apphosting/index.js +38 -0
  93. package/lib/emulator/apphosting/serve.js +50 -0
  94. package/lib/emulator/apphosting/utils.js +18 -0
  95. package/lib/emulator/auth/handlers.js +27 -0
  96. package/lib/emulator/auth/operations.js +41 -2
  97. package/lib/emulator/auth/state.js +2 -1
  98. package/lib/emulator/commandUtils.js +8 -3
  99. package/lib/emulator/constants.js +4 -1
  100. package/lib/emulator/controller.js +48 -5
  101. package/lib/emulator/dataconnect/pg-gateway/auth/base-auth-flow.js +11 -0
  102. package/lib/emulator/dataconnect/pg-gateway/auth/cert.js +58 -0
  103. package/lib/emulator/dataconnect/pg-gateway/auth/index.js +22 -0
  104. package/lib/emulator/dataconnect/pg-gateway/auth/md5.js +124 -0
  105. package/lib/emulator/dataconnect/pg-gateway/auth/password.js +54 -0
  106. package/lib/emulator/dataconnect/pg-gateway/auth/sasl/sasl-mechanism.js +34 -0
  107. package/lib/emulator/dataconnect/pg-gateway/auth/sasl/scram-sha-256.js +270 -0
  108. package/lib/emulator/dataconnect/pg-gateway/auth/trust.js +2 -0
  109. package/lib/emulator/dataconnect/pg-gateway/backend-error.js +75 -0
  110. package/lib/emulator/dataconnect/pg-gateway/buffer-reader.js +54 -0
  111. package/lib/emulator/dataconnect/pg-gateway/buffer-writer.js +79 -0
  112. package/lib/emulator/dataconnect/pg-gateway/connection.js +315 -0
  113. package/lib/emulator/dataconnect/pg-gateway/connection.types.js +8 -0
  114. package/lib/emulator/dataconnect/pg-gateway/crypto.js +40 -0
  115. package/lib/emulator/dataconnect/pg-gateway/duplex.js +53 -0
  116. package/lib/emulator/dataconnect/pg-gateway/index.js +27 -0
  117. package/lib/emulator/dataconnect/pg-gateway/message-buffer.js +82 -0
  118. package/lib/emulator/dataconnect/pg-gateway/message-codes.js +52 -0
  119. package/lib/emulator/dataconnect/pg-gateway/platforms/node/index.js +16 -0
  120. package/lib/emulator/dataconnect/pg-gateway/polyfills/readable-stream-async-iterator.js +35 -0
  121. package/lib/emulator/dataconnect/pg-gateway/utils.js +40 -0
  122. package/lib/emulator/dataconnect/pgliteServer.js +95 -0
  123. package/lib/emulator/dataconnectEmulator.js +58 -75
  124. package/lib/emulator/dataconnectToolkitController.js +44 -0
  125. package/lib/emulator/downloadableEmulators.js +27 -16
  126. package/lib/emulator/emulatorLogger.js +3 -0
  127. package/lib/emulator/eventarcEmulator.js +1 -1
  128. package/lib/emulator/extensionsEmulator.js +38 -6
  129. package/lib/emulator/functionsEmulator.js +85 -41
  130. package/lib/emulator/functionsEmulatorShared.js +2 -1
  131. package/lib/emulator/functionsRuntimeWorker.js +2 -1
  132. package/lib/emulator/hub.js +13 -9
  133. package/lib/emulator/hubExport.js +10 -3
  134. package/lib/emulator/loggingEmulator.js +2 -2
  135. package/lib/emulator/portUtils.js +10 -11
  136. package/lib/emulator/registry.js +1 -0
  137. package/lib/emulator/storage/persistence.js +2 -2
  138. package/lib/emulator/storage/rules/runtime.js +1 -1
  139. package/lib/emulator/tasksEmulator.js +5 -2
  140. package/lib/emulator/types.js +3 -0
  141. package/lib/emulator/ui.js +51 -25
  142. package/lib/error.js +31 -1
  143. package/lib/experiments.js +5 -0
  144. package/lib/extensions/emulator/triggerHelper.js +1 -1
  145. package/lib/extensions/extensionsApi.js +14 -10
  146. package/lib/extensions/extensionsHelper.js +15 -6
  147. package/lib/extensions/localHelper.js +31 -0
  148. package/lib/extensions/runtimes/common.js +179 -39
  149. package/lib/extensions/runtimes/node.js +401 -0
  150. package/lib/extensions/types.js +25 -14
  151. package/lib/extensions/warnings.js +5 -2
  152. package/lib/firestore/api.js +2 -1
  153. package/lib/firestore/checkDatabaseType.js +10 -3
  154. package/lib/firestore/delete.js +11 -0
  155. package/lib/firestore/options.js +7 -0
  156. package/lib/frameworks/angular/index.js +15 -3
  157. package/lib/frameworks/constants.js +1 -1
  158. package/lib/frameworks/flutter/index.js +6 -6
  159. package/lib/frameworks/flutter/utils.js +34 -1
  160. package/lib/frameworks/next/index.js +10 -2
  161. package/lib/frameworks/next/utils.js +8 -5
  162. package/lib/frameworks/vite/index.js +2 -2
  163. package/lib/fsutils.js +13 -1
  164. package/lib/functions/constants.js +4 -0
  165. package/lib/functions/events/v1.js +9 -2
  166. package/lib/gcp/apphosting.js +1 -1
  167. package/lib/gcp/cloudmonitoring.js +3 -3
  168. package/lib/gcp/cloudsql/cloudsqladmin.js +32 -20
  169. package/lib/gcp/cloudsql/connect.js +49 -37
  170. package/lib/gcp/cloudsql/interactive.js +49 -0
  171. package/lib/gcp/cloudsql/permissions.js +165 -0
  172. package/lib/gcp/devConnect.js +38 -1
  173. package/lib/gcp/firestore.js +5 -1
  174. package/lib/gcp/secretManager.js +12 -5
  175. package/lib/getProjectNumber.js +1 -1
  176. package/lib/init/features/apphosting.js +15 -0
  177. package/lib/init/features/dataconnect/index.js +159 -120
  178. package/lib/init/features/dataconnect/sdk.js +77 -84
  179. package/lib/init/features/emulators.js +2 -15
  180. package/lib/init/features/firestore/index.js +20 -1
  181. package/lib/init/features/firestore/indexes.js +4 -4
  182. package/lib/init/features/genkit/index.js +444 -0
  183. package/lib/init/features/hosting/index.js +2 -2
  184. package/lib/init/features/index.js +1 -1
  185. package/lib/init/features/project.js +6 -6
  186. package/lib/init/features/storage.js +0 -2
  187. package/lib/init/index.js +1 -0
  188. package/lib/init/spawn.js +23 -2
  189. package/lib/logger.js +2 -2
  190. package/lib/management/projects.js +24 -4
  191. package/lib/projectUtils.js +1 -1
  192. package/lib/prompt.js +1 -1
  193. package/lib/rc.js +1 -8
  194. package/lib/requireAuth.js +19 -5
  195. package/lib/requireDatabaseInstance.js +1 -1
  196. package/lib/requirePermissions.js +1 -1
  197. package/lib/responseToError.js +5 -2
  198. package/lib/rulesDeploy.js +1 -1
  199. package/lib/templates.js +2 -2
  200. package/lib/utils.js +16 -11
  201. package/lib/vsCodeUtils.js +8 -0
  202. package/package.json +7 -6
  203. package/schema/connector-yaml.json +57 -17
  204. package/schema/firebase-config.json +24 -0
  205. package/templates/extensions/javascript/package.lint.json +2 -2
  206. package/templates/extensions/javascript/package.nolint.json +2 -2
  207. package/templates/extensions/typescript/package.lint.json +2 -2
  208. package/templates/extensions/typescript/package.nolint.json +2 -2
  209. package/templates/genkit/firebase.0.9.0.template +57 -0
  210. package/templates/init/apphosting/apphosting.yaml +23 -0
  211. package/templates/init/dataconnect/connector.yaml +0 -1
  212. package/templates/init/dataconnect/dataconnect-fdccompatiblemode.yaml +12 -0
  213. package/templates/init/dataconnect/dataconnect.yaml +1 -1
  214. package/templates/init/dataconnect/queries.gql +1 -2
  215. package/templates/init/functions/javascript/package.lint.json +2 -2
  216. package/templates/init/functions/javascript/package.nolint.json +2 -2
  217. package/templates/init/functions/typescript/package.lint.json +2 -2
  218. package/templates/init/functions/typescript/package.nolint.json +2 -2
  219. package/lib/ensureCloudResourceLocation.js +0 -12
  220. package/lib/init/features/genkit.js +0 -53
@@ -45,6 +45,9 @@ const pubsubEmulator_1 = require("./pubsubEmulator");
45
45
  const storage_1 = require("./storage");
46
46
  const fileUtils_1 = require("../dataconnect/fileUtils");
47
47
  const tasksEmulator_1 = require("./tasksEmulator");
48
+ const apphosting_1 = require("./apphosting");
49
+ const webhook_1 = require("../dataconnect/webhook");
50
+ const api_1 = require("../api");
48
51
  const START_LOGGING_EMULATOR = utils.envOverride("START_LOGGING_EMULATOR", "false", (val) => val === "true");
49
52
  async function exportOnExit(options) {
50
53
  const exportOnExitDir = options.exportOnExit;
@@ -68,6 +71,7 @@ exports.onExit = onExit;
68
71
  async function cleanShutdown() {
69
72
  emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.HUB).logLabeled("BULLET", "emulators", "Shutting down emulators.");
70
73
  await registry_1.EmulatorRegistry.stopAll();
74
+ await (0, webhook_1.sendVSCodeMessage)({ message: webhook_1.VSCODE_MESSAGE.EMULATORS_SHUTDOWN });
71
75
  }
72
76
  exports.cleanShutdown = cleanShutdown;
73
77
  function filterEmulatorTargets(options) {
@@ -76,6 +80,9 @@ function filterEmulatorTargets(options) {
76
80
  targets = targets.filter((e) => {
77
81
  return options.config.has(e) || options.config.has(`emulators.${e}`);
78
82
  });
83
+ if (targets.includes(types_1.Emulators.FUNCTIONS) && !targets.includes(types_1.Emulators.EXTENSIONS)) {
84
+ targets.push(types_1.Emulators.EXTENSIONS);
85
+ }
79
86
  const onlyOptions = options.only;
80
87
  if (onlyOptions) {
81
88
  const only = onlyOptions.split(",").map((o) => {
@@ -200,11 +207,21 @@ async function startAll(options, showUI = true, runningTestScript = false) {
200
207
  const emulatableBackends = [];
201
208
  let extensionEmulator = undefined;
202
209
  if (shouldStart(options, types_1.Emulators.EXTENSIONS)) {
203
- const projectNumber = isDemoProject
204
- ? constants_1.Constants.FAKE_PROJECT_NUMBER
205
- : await (0, projectUtils_1.needProjectNumber)(options);
210
+ let projectNumber = constants_1.Constants.FAKE_PROJECT_NUMBER;
211
+ if (!isDemoProject) {
212
+ try {
213
+ projectNumber = await (0, projectUtils_1.needProjectNumber)(options);
214
+ }
215
+ catch (err) {
216
+ emulatorLogger_1.EmulatorLogger.forEmulator(types_1.Emulators.EXTENSIONS).logLabeled("ERROR", types_1.Emulators.EXTENSIONS, `Unable to look up project number for ${options.project}.\n` +
217
+ " If this is a real project, ensure that you are logged in and have access to it.\n" +
218
+ " If this is a fake project, please use a project ID starting with 'demo-' to skip production calls.\n" +
219
+ " Continuing with a fake project number - secrets and other features that require production access may behave unexpectedly.");
220
+ }
221
+ }
206
222
  const aliases = (0, projectUtils_1.getAliases)(options, projectId);
207
223
  extensionEmulator = new extensionsEmulator_1.ExtensionsEmulator({
224
+ options,
208
225
  projectId,
209
226
  projectDir: options.config.projectDir,
210
227
  projectNumber,
@@ -212,7 +229,7 @@ async function startAll(options, showUI = true, runningTestScript = false) {
212
229
  extensions: options.config.get("extensions"),
213
230
  });
214
231
  const extensionsBackends = await extensionEmulator.getExtensionBackends();
215
- const filteredExtensionsBackends = extensionEmulator.filterUnemulatedTriggers(options, extensionsBackends);
232
+ const filteredExtensionsBackends = extensionEmulator.filterUnemulatedTriggers(extensionsBackends);
216
233
  emulatableBackends.push(...filteredExtensionsBackends);
217
234
  (0, track_1.trackGA4)("extensions_emulated", {
218
235
  number_of_extensions_emulated: filteredExtensionsBackends.length,
@@ -246,6 +263,14 @@ async function startAll(options, showUI = true, runningTestScript = false) {
246
263
  portFixed: !!wsPortConfig,
247
264
  };
248
265
  }
266
+ if (emulator === types_1.Emulators.DATACONNECT && !(0, api_1.dataConnectLocalConnString)()) {
267
+ const pglitePortConfig = options.config.src.emulators?.dataconnect?.postgresPort;
268
+ listenConfig["dataconnect.postgres"] = {
269
+ host: config.host,
270
+ port: pglitePortConfig || 5432,
271
+ portFixed: !!pglitePortConfig,
272
+ };
273
+ }
249
274
  }
250
275
  }
251
276
  let listenForEmulator = await (0, portUtils_1.resolveHostAndAssignPorts)(listenConfig);
@@ -368,6 +393,7 @@ async function startAll(options, showUI = true, runningTestScript = false) {
368
393
  debugPort: inspectFunctions,
369
394
  verbosity: options.logVerbosity,
370
395
  projectAlias: options.projectAlias,
396
+ extensionsEmulator: extensionEmulator,
371
397
  });
372
398
  await startEmulator(functionsEmulator);
373
399
  const eventarcAddr = legacyGetFirstAddr(types_1.Emulators.EVENTARC);
@@ -548,6 +574,10 @@ async function startAll(options, showUI = true, runningTestScript = false) {
548
574
  configDir,
549
575
  rc: options.rc,
550
576
  config: options.config,
577
+ autoconnectToPostgres: true,
578
+ postgresListen: listenForEmulator["dataconnect.postgres"],
579
+ enable_output_generated_sdk: true,
580
+ enable_output_schema_extensions: true,
551
581
  });
552
582
  await startEmulator(dataConnectEmulator);
553
583
  }
@@ -576,6 +606,20 @@ async function startAll(options, showUI = true, runningTestScript = false) {
576
606
  });
577
607
  await startEmulator(hostingEmulator);
578
608
  }
609
+ if (experiments.isEnabled("emulatorapphosting")) {
610
+ const apphostingConfig = options.config.src.emulators?.[types_1.Emulators.APPHOSTING];
611
+ if (listenForEmulator.apphosting) {
612
+ const apphostingAddr = legacyGetFirstAddr(types_1.Emulators.APPHOSTING);
613
+ const apphostingEmulator = new apphosting_1.AppHostingEmulator({
614
+ host: apphostingAddr.host,
615
+ port: apphostingAddr.port,
616
+ startCommandOverride: apphostingConfig?.startCommandOverride,
617
+ rootDirectory: apphostingConfig?.rootDirectory,
618
+ options,
619
+ });
620
+ await startEmulator(apphostingEmulator);
621
+ }
622
+ }
579
623
  if (listenForEmulator.logging) {
580
624
  const loggingAddr = legacyGetFirstAddr(types_1.Emulators.LOGGING);
581
625
  const loggingEmulator = new loggingEmulator_1.LoggingEmulator({
@@ -592,7 +636,6 @@ async function startAll(options, showUI = true, runningTestScript = false) {
592
636
  if (listenForEmulator.ui) {
593
637
  const ui = new ui_1.EmulatorUI({
594
638
  projectId: projectId,
595
- auto_download: true,
596
639
  listen: listenForEmulator[types_1.Emulators.UI],
597
640
  });
598
641
  await startEmulator(ui);
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BaseAuthFlow = void 0;
4
+ class BaseAuthFlow {
5
+ constructor(params) {
6
+ this.reader = params.reader;
7
+ this.writer = params.writer;
8
+ this.connectionState = params.connectionState;
9
+ }
10
+ }
11
+ exports.BaseAuthFlow = BaseAuthFlow;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CertAuthFlow = void 0;
4
+ const backend_error_1 = require("../backend-error");
5
+ const base_auth_flow_1 = require("./base-auth-flow");
6
+ const connection_1 = require("../connection");
7
+ class CertAuthFlow extends base_auth_flow_1.BaseAuthFlow {
8
+ constructor(params) {
9
+ super(params);
10
+ this.completed = false;
11
+ this.auth = {
12
+ ...params.auth,
13
+ validateCredentials: params.auth.validateCredentials ??
14
+ (async ({ username, certificate }) => {
15
+ return certificate.subject.CN === username;
16
+ }),
17
+ };
18
+ this.username = params.username;
19
+ }
20
+ async *handleClientMessage(message) {
21
+ if (false) {
22
+ yield (0, backend_error_1.createBackendErrorMessage)({
23
+ severity: 'FATAL',
24
+ code: '08000',
25
+ message: `ssl connection required when auth mode is 'certificate'`,
26
+ });
27
+ yield connection_1.closeSignal;
28
+ return;
29
+ }
30
+ if (false) {
31
+ yield (0, backend_error_1.createBackendErrorMessage)({
32
+ severity: 'FATAL',
33
+ code: '08000',
34
+ message: 'client certificate is invalid',
35
+ });
36
+ yield connection_1.closeSignal;
37
+ return;
38
+ }
39
+ const isValid = false;
40
+ if (!isValid) {
41
+ yield (0, backend_error_1.createBackendErrorMessage)({
42
+ severity: 'FATAL',
43
+ code: '08000',
44
+ message: 'client certificate is invalid',
45
+ });
46
+ yield connection_1.closeSignal;
47
+ return;
48
+ }
49
+ this.completed = true;
50
+ }
51
+ createInitialAuthMessage() {
52
+ return undefined;
53
+ }
54
+ get isCompleted() {
55
+ return this.completed;
56
+ }
57
+ }
58
+ exports.CertAuthFlow = CertAuthFlow;
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createAuthFlow = void 0;
4
+ const cert_1 = require("./cert");
5
+ const md5_1 = require("./md5");
6
+ const password_1 = require("./password");
7
+ const scram_sha_256_1 = require("./sasl/scram-sha-256");
8
+ function createAuthFlow(options) {
9
+ switch (options.auth.method) {
10
+ case 'password':
11
+ return new password_1.PasswordAuthFlow({ ...options, auth: options.auth });
12
+ case 'md5':
13
+ return new md5_1.Md5AuthFlow({ ...options, auth: options.auth });
14
+ case 'scram-sha-256':
15
+ return new scram_sha_256_1.ScramSha256AuthFlow({ ...options, auth: options.auth });
16
+ case 'cert':
17
+ return new cert_1.CertAuthFlow({ ...options, auth: options.auth });
18
+ default:
19
+ throw new Error(`Unsupported auth method: ${options.auth.method}`);
20
+ }
21
+ }
22
+ exports.createAuthFlow = createAuthFlow;
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateBinaryLike = exports.encodeHex = exports.concat = exports.createPreHashedPassword = exports.generateMd5Salt = exports.md5 = exports.hashPreHashedPassword = exports.Md5AuthFlow = void 0;
4
+ const backend_error_1 = require("../backend-error");
5
+ const connection_1 = require("../connection");
6
+ const message_codes_1 = require("../message-codes");
7
+ const base_auth_flow_1 = require("./base-auth-flow");
8
+ class Md5AuthFlow extends base_auth_flow_1.BaseAuthFlow {
9
+ constructor(params) {
10
+ super(params);
11
+ this.completed = false;
12
+ this.auth = {
13
+ ...params.auth,
14
+ validateCredentials: params.auth.validateCredentials ??
15
+ (async ({ preHashedPassword, hashedPassword, salt }) => {
16
+ const expectedHashedPassword = await hashPreHashedPassword(preHashedPassword, salt);
17
+ return hashedPassword === expectedHashedPassword;
18
+ }),
19
+ };
20
+ this.username = params.username;
21
+ this.salt = generateMd5Salt();
22
+ }
23
+ async *handleClientMessage(message) {
24
+ const length = this.reader.int32();
25
+ const hashedPassword = this.reader.cstring();
26
+ const preHashedPassword = await this.auth.getPreHashedPassword({
27
+ username: this.username,
28
+ }, this.connectionState);
29
+ const isValid = await this.auth.validateCredentials({
30
+ username: this.username,
31
+ hashedPassword,
32
+ preHashedPassword,
33
+ salt: this.salt,
34
+ }, this.connectionState);
35
+ if (!isValid) {
36
+ yield (0, backend_error_1.createBackendErrorMessage)({
37
+ severity: 'FATAL',
38
+ code: '28P01',
39
+ message: `password authentication failed for user "${this.username}"`,
40
+ });
41
+ yield connection_1.closeSignal;
42
+ return;
43
+ }
44
+ this.completed = true;
45
+ }
46
+ createInitialAuthMessage() {
47
+ return this.createAuthenticationMD5Password();
48
+ }
49
+ get isCompleted() {
50
+ return this.completed;
51
+ }
52
+ createAuthenticationMD5Password() {
53
+ this.writer.addInt32(5);
54
+ this.writer.add(Buffer.from(this.salt));
55
+ return this.writer.flush(message_codes_1.BackendMessageCode.AuthenticationResponse);
56
+ }
57
+ }
58
+ exports.Md5AuthFlow = Md5AuthFlow;
59
+ async function hashPreHashedPassword(preHashedPassword, salt) {
60
+ const hash = await md5(concat([
61
+ new TextEncoder().encode(preHashedPassword),
62
+ salt instanceof ArrayBuffer
63
+ ? new Uint8Array(salt)
64
+ : new Uint8Array(salt.buffer, salt.byteOffset, salt.byteLength),
65
+ ]));
66
+ return `md5${hash}`;
67
+ }
68
+ exports.hashPreHashedPassword = hashPreHashedPassword;
69
+ async function md5(value) {
70
+ const hash = await crypto.subtle.digest('MD5', typeof value === 'string' ? new TextEncoder().encode(value) : value);
71
+ return encodeHex(hash);
72
+ }
73
+ exports.md5 = md5;
74
+ function generateMd5Salt() {
75
+ const salt = new Uint8Array(4);
76
+ crypto.getRandomValues(salt);
77
+ return salt;
78
+ }
79
+ exports.generateMd5Salt = generateMd5Salt;
80
+ async function createPreHashedPassword(username, password) {
81
+ return await md5(`${password}${username}`);
82
+ }
83
+ exports.createPreHashedPassword = createPreHashedPassword;
84
+ function concat(buffers) {
85
+ let length = 0;
86
+ for (const buffer of buffers) {
87
+ length += buffer.length;
88
+ }
89
+ const output = new Uint8Array(length);
90
+ let index = 0;
91
+ for (const buffer of buffers) {
92
+ output.set(buffer, index);
93
+ index += buffer.length;
94
+ }
95
+ return output;
96
+ }
97
+ exports.concat = concat;
98
+ const hexTable = new TextEncoder().encode("0123456789abcdef");
99
+ const textEncoder = new TextEncoder();
100
+ const textDecoder = new TextDecoder();
101
+ function encodeHex(src) {
102
+ const u8 = validateBinaryLike(src);
103
+ const dst = new Uint8Array(u8.length * 2);
104
+ for (let i = 0; i < u8.length; i++) {
105
+ const v = u8[i];
106
+ dst[i * 2] = hexTable[v >> 4];
107
+ dst[i * 2 + 1] = hexTable[v & 0x0f];
108
+ }
109
+ return textDecoder.decode(dst);
110
+ }
111
+ exports.encodeHex = encodeHex;
112
+ function validateBinaryLike(source) {
113
+ if (typeof source === "string") {
114
+ return textEncoder.encode(source);
115
+ }
116
+ else if (source instanceof Uint8Array) {
117
+ return source;
118
+ }
119
+ else if (source instanceof ArrayBuffer) {
120
+ return new Uint8Array(source);
121
+ }
122
+ throw new TypeError(`Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer`);
123
+ }
124
+ exports.validateBinaryLike = validateBinaryLike;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PasswordAuthFlow = void 0;
4
+ const backend_error_1 = require("../backend-error");
5
+ const connection_1 = require("../connection");
6
+ const message_codes_1 = require("../message-codes");
7
+ const base_auth_flow_1 = require("./base-auth-flow");
8
+ class PasswordAuthFlow extends base_auth_flow_1.BaseAuthFlow {
9
+ constructor(params) {
10
+ super(params);
11
+ this.completed = false;
12
+ this.auth = {
13
+ ...params.auth,
14
+ validateCredentials: params.auth.validateCredentials ??
15
+ (async ({ password, clearTextPassword }) => {
16
+ return password === clearTextPassword;
17
+ }),
18
+ };
19
+ this.username = params.username;
20
+ }
21
+ async *handleClientMessage(message) {
22
+ const length = this.reader.int32();
23
+ const password = this.reader.cstring();
24
+ const clearTextPassword = await this.auth.getClearTextPassword({
25
+ username: this.username,
26
+ }, this.connectionState);
27
+ const isValid = await this.auth.validateCredentials({
28
+ username: this.username,
29
+ password,
30
+ clearTextPassword,
31
+ }, this.connectionState);
32
+ if (!isValid) {
33
+ yield (0, backend_error_1.createBackendErrorMessage)({
34
+ severity: 'FATAL',
35
+ code: '28P01',
36
+ message: `password authentication failed for user "${this.username}"`,
37
+ });
38
+ yield connection_1.closeSignal;
39
+ return;
40
+ }
41
+ this.completed = true;
42
+ }
43
+ createInitialAuthMessage() {
44
+ return this.createAuthenticationCleartextPassword();
45
+ }
46
+ get isCompleted() {
47
+ return this.completed;
48
+ }
49
+ createAuthenticationCleartextPassword() {
50
+ this.writer.addInt32(3);
51
+ return this.writer.flush(message_codes_1.BackendMessageCode.AuthenticationResponse);
52
+ }
53
+ }
54
+ exports.PasswordAuthFlow = PasswordAuthFlow;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SaslMechanism = void 0;
4
+ const message_codes_1 = require("../../message-codes");
5
+ const SaslMessageCode = {
6
+ AuthenticationSASL: 10,
7
+ AuthenticationSASLContinue: 11,
8
+ AuthenticationSASLFinal: 12,
9
+ };
10
+ class SaslMechanism {
11
+ constructor(params) {
12
+ this.writer = params.writer;
13
+ }
14
+ createAuthenticationSASL() {
15
+ const mechanisms = ['SCRAM-SHA-256'];
16
+ this.writer.addInt32(SaslMessageCode.AuthenticationSASL);
17
+ for (const mechanism of mechanisms) {
18
+ this.writer.addCString(mechanism);
19
+ }
20
+ this.writer.addCString('');
21
+ return this.writer.flush(message_codes_1.BackendMessageCode.AuthenticationResponse);
22
+ }
23
+ createAuthenticationSASLContinue(message) {
24
+ this.writer.addInt32(SaslMessageCode.AuthenticationSASLContinue);
25
+ this.writer.addString(message);
26
+ return this.writer.flush(message_codes_1.BackendMessageCode.AuthenticationResponse);
27
+ }
28
+ createAuthenticationSASLFinal(message) {
29
+ this.writer.addInt32(SaslMessageCode.AuthenticationSASLFinal);
30
+ this.writer.addString(message);
31
+ return this.writer.flush(message_codes_1.BackendMessageCode.AuthenticationResponse);
32
+ }
33
+ }
34
+ exports.SaslMechanism = SaslMechanism;