mongodb_oidc.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MongoDBOIDC = exports.OIDC_WORKFLOWS = void 0;
  4. const error_1 = require("../../error");
  5. const auth_provider_1 = require("./auth_provider");
  6. const aws_service_workflow_1 = require("./mongodb_oidc/aws_service_workflow");
  7. const azure_service_workflow_1 = require("./mongodb_oidc/azure_service_workflow");
  8. const callback_workflow_1 = require("./mongodb_oidc/callback_workflow");
  9. /** Error when credentials are missing. */
  10. const MISSING_CREDENTIALS_ERROR = 'AuthContext must provide credentials.';
  11. /** @internal */
  12. exports.OIDC_WORKFLOWS = new Map();
  13. exports.OIDC_WORKFLOWS.set('callback', new callback_workflow_1.CallbackWorkflow());
  14. exports.OIDC_WORKFLOWS.set('aws', new aws_service_workflow_1.AwsServiceWorkflow());
  15. exports.OIDC_WORKFLOWS.set('azure', new azure_service_workflow_1.AzureServiceWorkflow());
  16. /**
  17. * OIDC auth provider.
  18. * @experimental
  19. */
  20. class MongoDBOIDC extends auth_provider_1.AuthProvider {
  21. /**
  22. * Instantiate the auth provider.
  23. */
  24. constructor() {
  25. super();
  26. }
  27. /**
  28. * Authenticate using OIDC
  29. */
  30. async auth(authContext) {
  31. const { connection, reauthenticating, response } = authContext;
  32. const credentials = getCredentials(authContext);
  33. const workflow = getWorkflow(credentials);
  34. await workflow.execute(connection, credentials, reauthenticating, response);
  35. }
  36. /**
  37. * Add the speculative auth for the initial handshake.
  38. */
  39. async prepare(handshakeDoc, authContext) {
  40. const credentials = getCredentials(authContext);
  41. const workflow = getWorkflow(credentials);
  42. const result = await workflow.speculativeAuth(credentials);
  43. return { ...handshakeDoc, ...result };
  44. }
  45. }
  46. exports.MongoDBOIDC = MongoDBOIDC;
  47. /**
  48. * Get credentials from the auth context, throwing if they do not exist.
  49. */
  50. function getCredentials(authContext) {
  51. const { credentials } = authContext;
  52. if (!credentials) {
  53. throw new error_1.MongoMissingCredentialsError(MISSING_CREDENTIALS_ERROR);
  54. }
  55. return credentials;
  56. }
  57. /**
  58. * Gets either a device workflow or callback workflow.
  59. */
  60. function getWorkflow(credentials) {
  61. const providerName = credentials.mechanismProperties.PROVIDER_NAME;
  62. const workflow = exports.OIDC_WORKFLOWS.get(providerName || 'callback');
  63. if (!workflow) {
  64. throw new error_1.MongoInvalidArgumentError(`Could not load workflow for provider ${credentials.mechanismProperties.PROVIDER_NAME}`);
  65. }
  66. return workflow;
  67. }
  68. //# sourceMappingURL=mongodb_oidc.js.map