How to use the passport-jwt.Strategy.prototype function in passport-jwt

To help you get started, we’ve selected a few passport-jwt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github promethe42 / cocorico / api / src / routes / api / auth.js View on Github external
exports.googleCallback = getLoginCallbackFunction('google');
}

var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
// secretOrKey is not really important since we will set it dynamically according
// to the "Cocorico-App-Id" HTTP header. But it still has to be != false.
opts.secretOrKey = 'secret';

// JwtStrategy reads the JWT secret from the option object above. But
// we need the secret to be the one set for the corresponding App.
// Thus, we override the JwtStrategy.prototype.authenticate method in order to set
// the secret according to the App found using the "Cocorico-App-Id" header.
var authenticate = JwtStrategy.prototype.authenticate;
JwtStrategy.prototype.authenticate = function(req, options) {
  var token = this._jwtFromRequest(req);
  if (!token) {
    return this.fail();
  }

  var appId = req.headers['cocorico-app-id'];

  if (!appId) {
    return this.fail(new Error('Missing Cocorico-App-Id header'));
  }

  return App.model.findById(appId).exec((err, app) => {
    if (err) {
      return this.fail(err);
    }
github promethe42 / cocorico / api / src / routes / api / auth.js View on Github external
exports.googleLogin = getLoginFunction('google', { scope: [ 'profile' ] });

  exports.googleCallback = getLoginCallbackFunction('google');
}

var opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeader();
// secretOrKey is not really important since we will set it dynamically according
// to the "Cocorico-App-Id" HTTP header. But it still has to be != false.
opts.secretOrKey = 'secret';

// JwtStrategy reads the JWT secret from the option object above. But
// we need the secret to be the one set for the corresponding App.
// Thus, we override the JwtStrategy.prototype.authenticate method in order to set
// the secret according to the App found using the "Cocorico-App-Id" header.
var authenticate = JwtStrategy.prototype.authenticate;
JwtStrategy.prototype.authenticate = function(req, options) {
  var token = this._jwtFromRequest(req);
  if (!token) {
    return this.fail();
  }

  var appId = req.headers['cocorico-app-id'];

  if (!appId) {
    return this.fail(new Error('Missing Cocorico-App-Id header'));
  }

  return App.model.findById(appId).exec((err, app) => {
    if (err) {
      return this.fail(err);
    }