From 4345725c838da581f4bf29e50d8644d74fd74819 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Mon, 24 Jul 2017 16:19:51 -0600 Subject: [PATCH] made verifyAsync available to check other tokens (like refresh tokens) --- lib/oauth3.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/oauth3.js b/lib/oauth3.js index 3dd1b16..27d7220 100644 --- a/lib/oauth3.js +++ b/lib/oauth3.js @@ -50,6 +50,15 @@ function extractAccessToken(req) { function verifyToken(token) { var jwt = require('jsonwebtoken'); var decoded; + + if (!token) { + return PromiseA.reject({ + message: 'no token provided' + , code: 'E_NO_TOKEN' + , url: 'https://oauth3.org/docs/errors#E_NO_TOKEN' + }); + } + try { decoded = jwt.decode(token, {complete: true}); } catch (e) {} @@ -160,6 +169,10 @@ function attachOauth3(req, res, next) { req.oauth3 = {}; extractAccessToken(req).then(function (token) { + req.oauth3.verifyAsync = function (jwt) { + return verifyToken(jwt || token); + }; + if (!token) { return null; } @@ -181,14 +194,10 @@ function attachOauth3(req, res, next) { req.oauth3.token = decoded; req.oauth3.ppid = ppid; - req.oauth3.verifyAsync = function () { - return verifyToken(token); - }; - - req.oauth3.rescope = function () { + req.oauth3.rescope = function (sub) { // TODO: this function is supposed to convert PPIDs of different parties to some account // ID that allows application to keep track of permisions and what-not. - return PromiseA.resolve(ppid); + return PromiseA.resolve(sub || ppid); }; }).then(function () { next();