Search code examples
javascriptnode.jspromisebluebird

How to turn callback function with Promise


I'm using Bluebird promises and trying to get below function with Promisify working:

var jwt = require('jsonwebtoken');

function _test_encode() {
    var cert = fs.readFileSync('public.pub'); 
    return jwt.verify(token, cert, function(err, decoded) {
        console.log(decoded);
    });
}

I tried below:

var jwt = require('jsonwebtoken');
var Promise = require('bluebird');
var jwtVerifyAsync = Promise.promisify(jwt.verify);

function _test_encode() {
    var cert = fs.readFileSync('public.pub');  // get public key
    return jwtVerifyAsync(token, cert).then(function(decoded) {
        console.log(decoded);
    });
}

But I'm getting:

Possibly unhandled TypeError: Object #<Object> has no method 'decode'

Is there something I'm missing here that I need to get it working? I just want to get _test_encode function to return result of the promise and use it in my other functions.

Verify function is below:

module.exports.verify = function(jwtString, secretOrPublicKey, options, callback) {
  if ((typeof options === 'function') && !callback) {
    callback = options;
    options = {};
  }

  if (!options) options = {};

  var done;

  if (callback) {
    done = function() {
      var args = Array.prototype.slice.call(arguments, 0);
      return process.nextTick(function() {
        callback.apply(null, args);
      });
    };
  } else {
    done = function(err, data) {
      if (err) throw err;
      return data;
    };
  }

  if (!jwtString){
    return done(new JsonWebTokenError('jwt must be provided'));
  }

  var parts = jwtString.split('.');

  if (parts.length !== 3){
    return done(new JsonWebTokenError('jwt malformed'));
  }

  if (parts[2].trim() === '' && secretOrPublicKey){
    return done(new JsonWebTokenError('jwt signature is required'));
  }

  var valid;

  try {
    valid = jws.verify(jwtString, secretOrPublicKey);
  } catch (e) {
    return done(e);
  }

  if (!valid)
    return done(new JsonWebTokenError('invalid signature'));

  var payload;

  try {
   payload = this.decode(jwtString);
  } catch(err) {
    return done(err);
  }

  if (typeof payload.exp !== 'undefined') {
    if (typeof payload.exp !== 'number') {
      return done(new JsonWebTokenError('invalid exp value'));
    }
    if (Math.floor(Date.now() / 1000) >= payload.exp)
      return done(new TokenExpiredError('jwt expired', new Date(payload.exp * 1000)));
  }

  if (options.audience) {
    var audiences = Array.isArray(options.audience)? options.audience : [options.audience];
    var target = Array.isArray(payload.aud) ? payload.aud : [payload.aud];

    var match = target.some(function(aud) { return audiences.indexOf(aud) != -1; });

    if (!match)
      return done(new JsonWebTokenError('jwt audience invalid. expected: ' + payload.aud));
  }

  if (options.issuer) {
    if (payload.iss !== options.issuer)
      return done(new JsonWebTokenError('jwt issuer invalid. expected: ' + payload.iss));
  }

  return done(null, payload);
};

Solution

  • It's trying to call this.decode, but you've stripped away the context by promisifying it, so this isn't jwt like it expects. You can keep context through promisification by passing it as a second argument:

    var jwtVerifyAsync = Promise.promisify(jwt.verify, jwt);