mongocr.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MongoCR = void 0;
  4. const crypto = require("crypto");
  5. const error_1 = require("../../error");
  6. const utils_1 = require("../../utils");
  7. const auth_provider_1 = require("./auth_provider");
  8. class MongoCR extends auth_provider_1.AuthProvider {
  9. async auth(authContext) {
  10. const { connection, credentials } = authContext;
  11. if (!credentials) {
  12. throw new error_1.MongoMissingCredentialsError('AuthContext must provide credentials.');
  13. }
  14. const { username, password, source } = credentials;
  15. const { nonce } = await connection.commandAsync((0, utils_1.ns)(`${source}.$cmd`), { getnonce: 1 }, undefined);
  16. const hashPassword = crypto
  17. .createHash('md5')
  18. .update(`${username}:mongo:${password}`, 'utf8')
  19. .digest('hex');
  20. // Final key
  21. const key = crypto
  22. .createHash('md5')
  23. .update(`${nonce}${username}${hashPassword}`, 'utf8')
  24. .digest('hex');
  25. const authenticateCommand = {
  26. authenticate: 1,
  27. user: username,
  28. nonce,
  29. key
  30. };
  31. await connection.commandAsync((0, utils_1.ns)(`${source}.$cmd`), authenticateCommand, undefined);
  32. }
  33. }
  34. exports.MongoCR = MongoCR;
  35. //# sourceMappingURL=mongocr.js.map