strategy.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Module dependencies.
  3. */
  4. var passport = require('passport-strategy')
  5. , util = require('util')
  6. , lookup = require('./utils').lookup;
  7. /**
  8. * `Strategy` constructor.
  9. *
  10. * The local authentication strategy authenticates requests based on the
  11. * credentials submitted through an HTML-based login form.
  12. *
  13. * Applications must supply a `verify` callback which accepts `username` and
  14. * `password` credentials, and then calls the `done` callback supplying a
  15. * `user`, which should be set to `false` if the credentials are not valid.
  16. * If an exception occured, `err` should be set.
  17. *
  18. * Optionally, `options` can be used to change the fields in which the
  19. * credentials are found.
  20. *
  21. * Options:
  22. * - `usernameField` field name where the username is found, defaults to _username_
  23. * - `passwordField` field name where the password is found, defaults to _password_
  24. * - `passReqToCallback` when `true`, `req` is the first argument to the verify callback (default: `false`)
  25. *
  26. * Examples:
  27. *
  28. * passport.use(new LocalStrategy(
  29. * function(username, password, done) {
  30. * User.findOne({ username: username, password: password }, function (err, user) {
  31. * done(err, user);
  32. * });
  33. * }
  34. * ));
  35. *
  36. * @param {Object} options
  37. * @param {Function} verify
  38. * @api public
  39. */
  40. function Strategy(options, verify) {
  41. if (typeof options == 'function') {
  42. verify = options;
  43. options = {};
  44. }
  45. if (!verify) { throw new TypeError('LocalStrategy requires a verify callback'); }
  46. this._usernameField = options.usernameField || 'username';
  47. this._passwordField = options.passwordField || 'password';
  48. passport.Strategy.call(this);
  49. this.name = 'local';
  50. this._verify = verify;
  51. this._passReqToCallback = options.passReqToCallback;
  52. }
  53. /**
  54. * Inherit from `passport.Strategy`.
  55. */
  56. util.inherits(Strategy, passport.Strategy);
  57. /**
  58. * Authenticate request based on the contents of a form submission.
  59. *
  60. * @param {Object} req
  61. * @api protected
  62. */
  63. Strategy.prototype.authenticate = function(req, options) {
  64. options = options || {};
  65. var username = lookup(req.body, this._usernameField) || lookup(req.query, this._usernameField);
  66. var password = lookup(req.body, this._passwordField) || lookup(req.query, this._passwordField);
  67. if (!username || !password) {
  68. return this.fail({ message: options.badRequestMessage || 'Missing credentials' }, 400);
  69. }
  70. var self = this;
  71. function verified(err, user, info) {
  72. if (err) { return self.error(err); }
  73. if (!user) { return self.fail(info); }
  74. self.success(user, info);
  75. }
  76. try {
  77. if (self._passReqToCallback) {
  78. this._verify(req, username, password, verified);
  79. } else {
  80. this._verify(username, password, verified);
  81. }
  82. } catch (ex) {
  83. return self.error(ex);
  84. }
  85. };
  86. /**
  87. * Expose `Strategy`.
  88. */
  89. module.exports = Strategy;