mongodb_oidc.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MongoDBOIDC = exports.OIDC_WORKFLOWS = exports.OIDC_VERSION = void 0;
  4. const error_1 = require("../../error");
  5. const auth_provider_1 = require("./auth_provider");
  6. const automated_callback_workflow_1 = require("./mongodb_oidc/automated_callback_workflow");
  7. const azure_machine_workflow_1 = require("./mongodb_oidc/azure_machine_workflow");
  8. const gcp_machine_workflow_1 = require("./mongodb_oidc/gcp_machine_workflow");
  9. const k8s_machine_workflow_1 = require("./mongodb_oidc/k8s_machine_workflow");
  10. const token_cache_1 = require("./mongodb_oidc/token_cache");
  11. const token_machine_workflow_1 = require("./mongodb_oidc/token_machine_workflow");
  12. /** Error when credentials are missing. */
  13. const MISSING_CREDENTIALS_ERROR = 'AuthContext must provide credentials.';
  14. /** The current version of OIDC implementation. */
  15. exports.OIDC_VERSION = 1;
  16. /** @internal */
  17. exports.OIDC_WORKFLOWS = new Map();
  18. exports.OIDC_WORKFLOWS.set('test', () => new automated_callback_workflow_1.AutomatedCallbackWorkflow(new token_cache_1.TokenCache(), token_machine_workflow_1.callback));
  19. exports.OIDC_WORKFLOWS.set('azure', () => new automated_callback_workflow_1.AutomatedCallbackWorkflow(new token_cache_1.TokenCache(), azure_machine_workflow_1.callback));
  20. exports.OIDC_WORKFLOWS.set('gcp', () => new automated_callback_workflow_1.AutomatedCallbackWorkflow(new token_cache_1.TokenCache(), gcp_machine_workflow_1.callback));
  21. exports.OIDC_WORKFLOWS.set('k8s', () => new automated_callback_workflow_1.AutomatedCallbackWorkflow(new token_cache_1.TokenCache(), k8s_machine_workflow_1.callback));
  22. /**
  23. * OIDC auth provider.
  24. */
  25. class MongoDBOIDC extends auth_provider_1.AuthProvider {
  26. /**
  27. * Instantiate the auth provider.
  28. */
  29. constructor(workflow) {
  30. super();
  31. if (!workflow) {
  32. throw new error_1.MongoInvalidArgumentError('No workflow provided to the OIDC auth provider.');
  33. }
  34. this.workflow = workflow;
  35. }
  36. /**
  37. * Authenticate using OIDC
  38. */
  39. async auth(authContext) {
  40. const { connection, reauthenticating, response } = authContext;
  41. if (response?.speculativeAuthenticate?.done && !reauthenticating) {
  42. return;
  43. }
  44. const credentials = getCredentials(authContext);
  45. if (reauthenticating) {
  46. await this.workflow.reauthenticate(connection, credentials);
  47. }
  48. else {
  49. await this.workflow.execute(connection, credentials, response);
  50. }
  51. }
  52. /**
  53. * Add the speculative auth for the initial handshake.
  54. */
  55. async prepare(handshakeDoc, authContext) {
  56. const { connection } = authContext;
  57. const credentials = getCredentials(authContext);
  58. const result = await this.workflow.speculativeAuth(connection, credentials);
  59. return { ...handshakeDoc, ...result };
  60. }
  61. }
  62. exports.MongoDBOIDC = MongoDBOIDC;
  63. /**
  64. * Get credentials from the auth context, throwing if they do not exist.
  65. */
  66. function getCredentials(authContext) {
  67. const { credentials } = authContext;
  68. if (!credentials) {
  69. throw new error_1.MongoMissingCredentialsError(MISSING_CREDENTIALS_ERROR);
  70. }
  71. return credentials;
  72. }
  73. //# sourceMappingURL=mongodb_oidc.js.map