Skip to content

Commit

Permalink
return a Promise when no callback function is specified. #35
Browse files Browse the repository at this point in the history
  • Loading branch information
mreinstein committed May 4, 2018
1 parent f77b68c commit 535e6fc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
})
})
}
43 changes: 43 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit 535e6fc

Please sign in to comment.