x509.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.X509 = void 0;
  4. const error_1 = require("../../error");
  5. const utils_1 = require("../../utils");
  6. const auth_provider_1 = require("./auth_provider");
  7. class X509 extends auth_provider_1.AuthProvider {
  8. async prepare(handshakeDoc, authContext) {
  9. const { credentials } = authContext;
  10. if (!credentials) {
  11. throw new error_1.MongoMissingCredentialsError('AuthContext must provide credentials.');
  12. }
  13. return { ...handshakeDoc, speculativeAuthenticate: x509AuthenticateCommand(credentials) };
  14. }
  15. async auth(authContext) {
  16. const connection = authContext.connection;
  17. const credentials = authContext.credentials;
  18. if (!credentials) {
  19. throw new error_1.MongoMissingCredentialsError('AuthContext must provide credentials.');
  20. }
  21. const response = authContext.response;
  22. if (response?.speculativeAuthenticate) {
  23. return;
  24. }
  25. await connection.commandAsync((0, utils_1.ns)('$external.$cmd'), x509AuthenticateCommand(credentials), undefined);
  26. }
  27. }
  28. exports.X509 = X509;
  29. function x509AuthenticateCommand(credentials) {
  30. const command = { authenticate: 1, mechanism: 'MONGODB-X509' };
  31. if (credentials.username) {
  32. command.user = credentials.username;
  33. }
  34. return command;
  35. }
  36. //# sourceMappingURL=x509.js.map