From 9acc863e338805186857691e7fad713bf2613b21 Mon Sep 17 00:00:00 2001 From: Robert Bullen Date: Wed, 5 Jun 2019 22:13:23 -0500 Subject: [PATCH 1/2] rewrote a unit test to surface the callback bug --- test/strategy.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/test/strategy.js b/test/strategy.js index 3ab7a2f..80b7a93 100755 --- a/test/strategy.js +++ b/test/strategy.js @@ -119,14 +119,12 @@ describe("Strategy Positive Scenarios", () => { }); it("should check if Validate function can fail successfully when invalid token is passed (Promise)", async () => { - await strategy.init(async callback => { - try { - await strategy.validate("token"); - expect(true).to.eql(false); - } catch (err) { - expect(err).to.eql("Not a valid JWT token"); - }; - }); + await strategy.init(result => expect(result).to.eql(true)); + try { + await strategy.validate("token"); + } catch (err) { + expect(err).to.eql("Not a valid JWT token"); + }; }); From 1eb89fc2b860351c12bb852c8a043531821998b1 Mon Sep 17 00:00:00 2001 From: Robert Bullen Date: Wed, 5 Jun 2019 22:25:38 -0500 Subject: [PATCH 2/2] fixed the callback bug --- lib/strategy.js | 50 +++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/strategy.js b/lib/strategy.js index 78d8d85..65484e1 100755 --- a/lib/strategy.js +++ b/lib/strategy.js @@ -46,46 +46,48 @@ class CognitoExpress { } validate(token, callback) { - const p = this.promise.then(() => { - let decodedJwt = jwt.decode(token, { complete: true }); + // Perform all the validation work using promises and exceptions. + const validatePromise = this.promise.then(() => { + const decodedJwt = jwt.decode(token, { complete: true }); - if (!decodedJwt) return callback(`Not a valid JWT token`, null); + if (!decodedJwt) throw `Not a valid JWT token`; if (decodedJwt.payload.iss !== this.iss) - return callback(`token is not from your User Pool`, null); + throw `token is not from your User Pool`; if (decodedJwt.payload.token_use !== this.tokenUse) - return callback(`Not an ${this.tokenUse} token`, null); + throw `Not an ${this.tokenUse} token`; - let kid = decodedJwt.header.kid; - let pem = this.pems[kid]; + const kid = decodedJwt.header.kid; + const pem = this.pems[kid]; - if (!pem) return callback(`Invalid ${this.tokenUse} token`, null); + if (!pem) throw `Invalid ${this.tokenUse} token`; - let params = { + const params = { token: token, pem: pem, iss: this.iss, maxAge: this.tokenExpiration }; - if (callback) { - jwtVerify(params, callback); - } else { - return new Promise((resolve, reject) => { - jwtVerify(params, (err, result) => { - if (err) { - reject(err); - } else { - resolve(result); - } - }); + return new Promise((resolve, reject) => { + jwtVerify(params, (err, result) => { + if (err) { + reject(err); + } else { + resolve(result); + } }); - } + }); }); - - if (!callback) { - return p; + + // Adapt to the callback metaphor if necessary. + if (callback) { + validatePromise + .then(value => callback(undefined, value)) + .catch(error => callback(error, undefined)); + } else { + return validatePromise; } } }