From 1fa861b84542608da938b876d35fbf78f3aafd47 Mon Sep 17 00:00:00 2001 From: Kristoffer Walker Date: Tue, 25 Oct 2016 12:25:31 -0400 Subject: [PATCH] Introduce new async function for jsonwebtoken lib modified: lib/services/identity/queries/index.js --- lib/services/identity/queries/index.js | 60 +++++++++++++++++--------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/lib/services/identity/queries/index.js b/lib/services/identity/queries/index.js index 783126c..8a00145 100644 --- a/lib/services/identity/queries/index.js +++ b/lib/services/identity/queries/index.js @@ -13,13 +13,42 @@ module.exports = function (service) { const JWT_SECRET = options.jwtSecret; const JWT_ISSUER = options.jwtIssuer; + const jwtVerify = (token, secret, options) => { + return new Promise((resolve, reject) => { + try { + jwt.verify(token, secret, options, (err, object) => { + if (err) { + return reject(err); + } + return resolve(object); + }); + } catch (err) { + return reject(err); + } + }); + }; + + const jwtSign = (payload, secret, options) => { + return new Promise((resolve, reject) => { + try { + jwt.sign(payload, secret, options, (err, token) => { + if (err) { + return reject(err); + } + return resolve(token); + }); + } catch (err) { + return reject(err); + } + }); + }; + // args.token - String *required* queries.verify = args => { const token = args.token; - return new Promise((resolve, reject) => { - try { - const object = jwt.verify(token, JWT_SECRET, {issuer: JWT_ISSUER}); + return jwtVerify(token, JWT_SECRET, {issuer: JWT_ISSUER}) + .then(object => { const role = 'store'; const cmd = 'get'; const channelId = object.channel; @@ -41,10 +70,11 @@ module.exports = function (service) { if (!channel) { throw new Error(`Channel ${channelId} not found.`); } - return resolve({audience, subject, channel}); + + return {audience, subject, channel}; }); } else if (subject) { - return resolve({audience, subject}); + return {audience, subject}; } else if (viewerId && platformId) { return Promise .all([ @@ -73,7 +103,7 @@ module.exports = function (service) { throw new Error(`User ${viewerId} not found.`); } - return resolve({audience, channel, platform, viewer}); + return {audience, channel, platform, viewer}; }); } else if (platformId) { return Promise @@ -95,15 +125,12 @@ module.exports = function (service) { throw new Error(`Platform ${platformId} not found.`); } - return resolve({audience, channel, platform}); + return {audience, channel, platform}; }); } - throw new Error('JSON Web Token has no subject or platform.'); - } catch (err) { - reject(err); - } - }); + return Promise.reject(new Error('JSON Web Token has no subject or platform.')); + }); }; // args.audience - Array of Strings *required* @@ -146,14 +173,7 @@ module.exports = function (service) { throw new Error('args.subject or args.platform is required'); } - return new Promise((resolve, reject) => { - try { - const token = jwt.sign(payload, secret, options); - return resolve(token); - } catch (err) { - return reject(err); - } - }); + return jwtSign(payload, secret, options); }; queries.composeConfig = args => {