x509.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. prepare(handshakeDoc, authContext, callback) {
  9. const { credentials } = authContext;
  10. if (!credentials) {
  11. return callback(new error_1.MongoMissingCredentialsError('AuthContext must provide credentials.'));
  12. }
  13. Object.assign(handshakeDoc, {
  14. speculativeAuthenticate: x509AuthenticateCommand(credentials)
  15. });
  16. callback(undefined, handshakeDoc);
  17. }
  18. auth(authContext, callback) {
  19. const connection = authContext.connection;
  20. const credentials = authContext.credentials;
  21. if (!credentials) {
  22. return callback(new error_1.MongoMissingCredentialsError('AuthContext must provide credentials.'));
  23. }
  24. const response = authContext.response;
  25. if (response && response.speculativeAuthenticate) {
  26. return callback();
  27. }
  28. connection.command((0, utils_1.ns)('$external.$cmd'), x509AuthenticateCommand(credentials), undefined, callback);
  29. }
  30. }
  31. exports.X509 = X509;
  32. function x509AuthenticateCommand(credentials) {
  33. const command = { authenticate: 1, mechanism: 'MONGODB-X509' };
  34. if (credentials.username) {
  35. command.user = credentials.username;
  36. }
  37. return command;
  38. }
  39. //# sourceMappingURL=x509.js.map