From 535e6fcca62d76c32e345e71a3060f2a458800f3 Mon Sep 17 00:00:00 2001 From: Mike Reinstein Date: Fri, 4 May 2018 09:27:16 -0700 Subject: [PATCH] return a Promise when no callback function is specified. #35 --- README.md | 15 ++++++++++++++- index.js | 18 ++++++++++++++++-- test/index.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 61e31e1..552a848 100644 --- a/README.md +++ b/README.md @@ -22,13 +22,26 @@ This module provides a function to handle this validation. ### usage +You may include a callback function, in the standard node error argument-first format: + +```javascript +const verifier = require('alexa-verifier') + + +verifier(cert_url, signature, requestRawBody, function callbackFn(er) { + // if er, something went wrong +}) +``` + +Ommiting a callback function returns a promise: ```javascript const verifier = require('alexa-verifier') -verifier(cert_url, signature, requestRawBody, callback) +const verifyPromise = verifier(cert_url, signature, requestRawBody, callback) ``` + * `cert_url` full url of the certificate to verify (from HTTP request header named `signaturecertchainurl`) * `signature` signature of the request (from HTTP request header named `signature`) * `requestRawBody` full body string from POST request diff --git a/index.js b/index.js index 7f8c6c1..8eac4d2 100644 --- a/index.js +++ b/index.js @@ -61,8 +61,7 @@ function validateTimestamp (requestBody) { } -// certificate validator express middleware for amazon echo -module.exports = function verifier (cert_url, signature, requestBody, callback) { +function verifier (cert_url, signature, requestBody, callback) { var er if(!cert_url) @@ -92,3 +91,18 @@ module.exports = function verifier (cert_url, signature, requestBody, callback) callback() }) } + + +// certificate validator for amazon echo +module.exports = function (cert_url, signature, requestBody, cb) { + if(cb) + return verifier(cert_url, signature, requestBody, cb) + + return new Promise(function( resolve, reject) { + verifier(cert_url, signature, requestBody, function(er) { + if(er) + return reject(er) + resolve() + }) + }) +} diff --git a/test/index.js b/test/index.js index aadcc5b..7fe50c5 100644 --- a/test/index.js +++ b/test/index.js @@ -197,3 +197,46 @@ test('handle valid signature with double byte utf8 encodings', function (t) { t.end() }) }) + + +test('invocation', function (t) { + var ts = '2017-04-05T12:02:36Z' + var now = new Date(ts) + var clock = sinon.useFakeTimers(now.getTime()) + var cert_url = 'https://s3.amazonaws.com/echo.api/echo-api-cert-4.pem' + var signature = 'Qc8OuaGEHWeL/39XTEDYFbOCufYWpwi45rqmM2R4WaSEYcSXq+hUko/88wv48+6SPUiEddWSEEINJFAFV5auYZsnBzqCK+SO8mGNOGHmLYpcFuSEHI3eA3nDIEARrXTivqqbH/LCPJHc0tqNYr3yPZRIR2mYFndJOxgDNSOooZX+tp2GafHHsjjShCjmePaLxJiGG1DmrL6fyOJoLrzc0olUxLmnJviS6Q5wBir899TMEZ/zX+aiBTt/khVvwIh+hI/PZsRq/pQw4WAvQz1bcnGNamvMA/TKSJtR0elJP+TgCqbVoYisDgQXkhi8/wonkLhs68pN+TurbR7GyC1vxw==' + var body = { + "version": "1.0", + "session": { + "new": true, + "sessionId": "SessionId.7745e45d-3042-45eb-8e86-cab2cf285daf", + "application": { + "applicationId": "amzn1.ask.skill.75c997b8-610f-4eb4-bf2e-95810e15fba2" + }, + "attributes": {}, + "user": { + "userId": "amzn1.ask.account.AF6Z7574YHBQCNNTJK45QROUSCUJEHIYAHZRP35FVU673VDGDKV4PH2M52PX4XWGCSYDM66B6SKEEFJN6RYWN7EME3FKASDIG7DPNGFFFNTN4ZT6B64IIZKSNTXQXEMVBXMA7J3FN3ERT2A4EDYFUYMGM4NSQU4RTAQOZWDD2J7JH6P2ROP2A6QEGLNLZDXNZU2DL7BKGCVLMNA" + } + }, + "request": { + "type": "IntentRequest", + "requestId": "EdwRequestId.fa7428b7-75d0-44c8-aebb-4c222ed48ebe", + "timestamp": ts, + "locale": "en-US", + "intent": { + "name": "HelloWorld" + }, + "inDialog": false + } + } + + var result = verifier(cert_url, signature, JSON.stringify(body)) + result.catch(function(er) { }) + t.assert(result instanceof Promise, 'omitting callback returns a promise') + + var callbackResult = verifier(cert_url, signature, JSON.stringify(body), function(er) { }) + + t.equal(callbackResult, undefined, 'including callback does not return a promise') + + t.end() +})