liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * Module dependencies.
 */
var passport = require('passport-strategy')
  , util = require('util');
 
 
/**
 * `ClientPasswordStrategy` constructor.
 *
 * @api protected
 */
function Strategy(options, verify) {
  if (typeof options == 'function') {
    verify = options;
    options = {};
  }
  if (!verify) throw new Error('OAuth 2.0 client password strategy requires a verify function');
 
  passport.Strategy.call(this);
  this.name = 'oauth2-client-password';
  this._verify = verify;
  this._passReqToCallback = options.passReqToCallback;
}
 
/**
 * Inherit from `passport.Strategy`.
 */
util.inherits(Strategy, passport.Strategy);
 
/**
 * Authenticate request based on client credentials in the request body.
 *
 * @param {Object} req
 * @api protected
 */
Strategy.prototype.authenticate = function(req) {
  if (!req.body || (!req.body['client_id'] || !req.body['client_secret'])) {
    return this.fail();
  }
 
  var clientId = req.body['client_id'];
  var clientSecret = req.body['client_secret'];
 
  var self = this;
 
  function verified(err, client, info) {
    if (err) { return self.error(err); }
    if (!client) { return self.fail(); }
    self.success(client, info);
  }
 
  if (self._passReqToCallback) {
    this._verify(req, clientId, clientSecret, verified);
  } else {
    this._verify(clientId, clientSecret, verified);
  }
}
 
 
/**
 * Expose `Strategy`.
 */
module.exports = Strategy;