From 78c34fa8aeadbdaf06a5bf566a6b2c35a40ccf89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Jos=C3=A9=20Marques=20Vieira?= Date: Mon, 15 Jan 2018 12:22:13 +0000 Subject: [PATCH] Use reject to prevent errors from being swallowed --- auth/auth0/src/auth0Authentication.js | 44 +++++++++++++++------------ 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/auth/auth0/src/auth0Authentication.js b/auth/auth0/src/auth0Authentication.js index f35cfd4..2690827 100644 --- a/auth/auth0/src/auth0Authentication.js +++ b/auth/auth0/src/auth0Authentication.js @@ -5,7 +5,7 @@ const fromEvent = require('graphcool-lib').fromEvent //Validates the request JWT token const verifyToken = token => - new Promise(resolve => { + new Promise((resolve, reject) => { //Decode the JWT Token const decoded = jwt.decode(token, { complete: true }) if (!decoded || !decoded.header || !decoded.header.kid) { @@ -22,23 +22,29 @@ const verifyToken = token => }) //Retrieve the JKWS's signing key using the decode token's key identifier (kid) jkwsClient.getSigningKey(decoded.header.kid, (err, key) => { - if (err) throw new Error(err) - const signingKey = key.publicKey || key.rsaPublicKey - //If the JWT Token was valid, verify its validity against the JKWS's signing key - jwt.verify( - token, - signingKey, - { - algorithms: ['RS256'], - audience: process.env.AUTH0_API_IDENTIFIER, - ignoreExpiration: false, - issuer: `https://${process.env.AUTH0_DOMAIN}/` - }, - (err, decoded) => { - if (err) throw new Error(err) - return resolve(decoded) - } - ) + if (err) { + reject(new Error(err)) + } else { + const signingKey = key.publicKey || key.rsaPublicKey + //If the JWT Token was valid, verify its validity against the JKWS's signing key + jwt.verify( + token, + signingKey, + { + algorithms: ['RS256'], + audience: process.env.AUTH0_API_IDENTIFIER, + ignoreExpiration: false, + issuer: `https://${process.env.AUTH0_DOMAIN}/` + }, + (err, decoded) => { + if (err) { + reject(new Error(err)) + } else { + return resolve(decoded) + } + } + ) + } }) }) @@ -118,4 +124,4 @@ export default async event => { console.log(err) return { error: 'An unexpected error occured' } } -} \ No newline at end of file +}