Skip to content

Commit

Permalink
Merge pull request #192 from pusher/fix-extratokens-issue
Browse files Browse the repository at this point in the history
Fix extra tokens issue
  • Loading branch information
deeborania authored May 3, 2023
2 parents ba64416 + a2a0d42 commit 1812cb4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Changelog

## 5.1.3

[FIXED] Parsing of the extraTokens in webhook's isValid method

## 5.1.2

- [CHANGED] Add types/node-fetch to dependencies.
- [CHANGED] Add types/node-fetch to dependencies.

## 5.1.1-beta (2022-06-01)

[FIXED] Updated typescript types with new user features.
Expand Down Expand Up @@ -90,7 +95,7 @@ const pusher = new Pusher.forURL(process.env.PUSHER_URL, {

## 2.2.1 (2019-07-03)

no-op release to fix the description on https://www.npmjs.com/package/pusher
no-op release to fix the description on <https://www.npmjs.com/package/pusher>

## 2.2.0 (2018-11-26)

Expand Down
50 changes: 25 additions & 25 deletions lib/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ const util = require("./util")
* @param {String} key app key
* @param {String} secret app secret
*/
function Token(key, secret) {
this.key = key
this.secret = secret
}

/** Signs the string using the secret.
*
* @param {String} string
* @returns {String}
*/
Token.prototype.sign = function (string) {
return crypto
.createHmac("sha256", this.secret)
.update(Buffer.from(string))
.digest("hex")
}

/** Checks if the string has correct signature.
*
* @param {String} string
* @param {String} signature
* @returns {Boolean}
*/
Token.prototype.verify = function (string, signature) {
return util.secureCompare(this.sign(string), signature)
class Token {
constructor(key, secret) {
this.key = key
this.secret = secret
}
/** Signs the string using the secret.
*
* @param {String} string
* @returns {String}
*/
sign(string) {
return crypto
.createHmac("sha256", this.secret)
.update(Buffer.from(string))
.digest("hex")
}
/** Checks if the string has correct signature.
*
* @param {String} string
* @param {String} signature
* @returns {Boolean}
*/
verify(string, signature) {
return util.secureCompare(this.sign(string), signature)
}
}

module.exports = Token
6 changes: 5 additions & 1 deletion lib/webhook.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const errors = require("./errors")
const Token = require("./token")

/** Provides validation and access methods for a WebHook.
*
Expand Down Expand Up @@ -46,7 +47,10 @@ WebHook.prototype.isValid = function (extraTokens) {

const tokens = [this.token].concat(extraTokens)
for (const i in tokens) {
const token = tokens[i]
let token = tokens[i]
if (token instanceof Token === false) {
token = new Token(token.key, token.secret)
}
if (this.key == token.key && token.verify(this.body, this.signature)) {
return true
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pusher",
"description": "Node.js client to interact with the Pusher Channels REST API",
"version": "5.1.2",
"version": "5.1.3",
"author": "Pusher <[email protected]>",
"contributors": [
{
Expand Down

0 comments on commit 1812cb4

Please sign in to comment.