mongo_credentials.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';
  2. // Resolves the default auth mechanism according to
  3. // https://github.com/mongodb/specifications/blob/master/source/auth/auth.rst
  4. function getDefaultAuthMechanism(ismaster) {
  5. if (ismaster) {
  6. // If ismaster contains saslSupportedMechs, use scram-sha-256
  7. // if it is available, else scram-sha-1
  8. if (Array.isArray(ismaster.saslSupportedMechs)) {
  9. return ismaster.saslSupportedMechs.indexOf('SCRAM-SHA-256') >= 0
  10. ? 'scram-sha-256'
  11. : 'scram-sha-1';
  12. }
  13. // Fallback to legacy selection method. If wire version >= 3, use scram-sha-1
  14. if (ismaster.maxWireVersion >= 3) {
  15. return 'scram-sha-1';
  16. }
  17. }
  18. // Default for wireprotocol < 3
  19. return 'mongocr';
  20. }
  21. /**
  22. * A representation of the credentials used by MongoDB
  23. * @class
  24. * @property {string} mechanism The method used to authenticate
  25. * @property {string} [username] The username used for authentication
  26. * @property {string} [password] The password used for authentication
  27. * @property {string} [source] The database that the user should authenticate against
  28. * @property {object} [mechanismProperties] Special properties used by some types of auth mechanisms
  29. */
  30. class MongoCredentials {
  31. /**
  32. * Creates a new MongoCredentials object
  33. * @param {object} [options]
  34. * @param {string} [options.username] The username used for authentication
  35. * @param {string} [options.password] The password used for authentication
  36. * @param {string} [options.source] The database that the user should authenticate against
  37. * @param {string} [options.mechanism] The method used to authenticate
  38. * @param {object} [options.mechanismProperties] Special properties used by some types of auth mechanisms
  39. */
  40. constructor(options) {
  41. options = options || {};
  42. this.username = options.username;
  43. this.password = options.password;
  44. this.source = options.source || options.db;
  45. this.mechanism = options.mechanism || 'default';
  46. this.mechanismProperties = options.mechanismProperties;
  47. }
  48. /**
  49. * Determines if two MongoCredentials objects are equivalent
  50. * @param {MongoCredentials} other another MongoCredentials object
  51. * @returns {boolean} true if the two objects are equal.
  52. */
  53. equals(other) {
  54. return (
  55. this.mechanism === other.mechanism &&
  56. this.username === other.username &&
  57. this.password === other.password &&
  58. this.source === other.source
  59. );
  60. }
  61. /**
  62. * If the authentication mechanism is set to "default", resolves the authMechanism
  63. * based on the server version and server supported sasl mechanisms.
  64. *
  65. * @param {Object} [ismaster] An ismaster response from the server
  66. */
  67. resolveAuthMechanism(ismaster) {
  68. // If the mechanism is not "default", then it does not need to be resolved
  69. if (this.mechanism.toLowerCase() === 'default') {
  70. this.mechanism = getDefaultAuthMechanism(ismaster);
  71. }
  72. }
  73. }
  74. module.exports = { MongoCredentials };