mongocr.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. auth(authContext, callback) {
  10. const { connection, credentials } = authContext;
  11. if (!credentials) {
  12. return callback(new error_1.MongoMissingCredentialsError('AuthContext must provide credentials.'));
  13. }
  14. const username = credentials.username;
  15. const password = credentials.password;
  16. const source = credentials.source;
  17. connection.command((0, utils_1.ns)(`${source}.$cmd`), { getnonce: 1 }, undefined, (err, r) => {
  18. let nonce = null;
  19. let key = null;
  20. // Get nonce
  21. if (err == null) {
  22. nonce = r.nonce;
  23. // Use node md5 generator
  24. let md5 = crypto.createHash('md5');
  25. // Generate keys used for authentication
  26. md5.update(`${username}:mongo:${password}`, 'utf8');
  27. const hash_password = md5.digest('hex');
  28. // Final key
  29. md5 = crypto.createHash('md5');
  30. md5.update(nonce + username + hash_password, 'utf8');
  31. key = md5.digest('hex');
  32. }
  33. const authenticateCommand = {
  34. authenticate: 1,
  35. user: username,
  36. nonce,
  37. key
  38. };
  39. connection.command((0, utils_1.ns)(`${source}.$cmd`), authenticateCommand, undefined, callback);
  40. });
  41. }
  42. }
  43. exports.MongoCR = MongoCR;
  44. //# sourceMappingURL=mongocr.js.map