Skip to content

Commit

Permalink
refactor: multiple client secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
Adnan Rahic committed Jun 25, 2021
1 parent 082f3ba commit 3c11a81
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
4 changes: 3 additions & 1 deletion config/examples/vercel-input-es-output.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ input:
module: input-vercel
port: 8400
useIndexFromUrlPath: true
clientSecret: <CLIENT_SECRET>
# workers: 4
clientSecrets:
- <CLIENT_SECRET>
- <CLIENT_SECRET>


outputFilter:
Expand Down
27 changes: 19 additions & 8 deletions lib/plugins/input/vercel.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,27 @@ class Vercel {
}

verifySignature (req, body) {
const signature = crypto
.createHmac('sha1', this.config.clientSecret)
.update(body)
.digest('hex')

if (this.config.debug) {
consoleLogger.log("Vercel signature didn't match")
if (!Array.isArray(this.config.clientSecrets)) {
if (this.config.debug) {
consoleLogger.log('clientSecrets config value is not an array. Please set it to an array.')
}
return
}

return signature === req.headers['x-zeit-signature']
const verified = this.config.clientSecrets.some(clientSecret => {
const signature = crypto
.createHmac('sha1', clientSecret)
.update(body)
.digest('hex')

if (this.config.debug) {
consoleLogger.log(`Vercel signature didn't match for Vercel Client Secret: ${clientSecret}`)
}

return signature === req.headers['x-zeit-signature']
})

return verified
}

HttpHandler (req, res) {
Expand Down
47 changes: 47 additions & 0 deletions test/vercel/vercelSignature.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* global describe, it */
const assert = require('assert')
const crypto = require('crypto')
const sampleClientSecret = 'idmnMEd7Yx4QmgzZpZ4axXoe'
const sampleBody = {
id: 1,
message: '1'
}
const sampleBodyBuf = Buffer.from(JSON.stringify(sampleBody))
const sampleSignature = crypto
.createHmac('sha1', sampleClientSecret)
.update(sampleBodyBuf)
.digest('hex')

const sampleReq = {
headers: {
'x-zeit-signature': sampleSignature
}
}
const configWithArrayWithTwoClientSecrets = {
clientSecrets: [sampleClientSecret, sampleClientSecret]
}
const configWithArrayWithOneClientSecret = {
clientSecrets: [sampleClientSecret]
}
const EventEmitter = require('events')
const evem = new EventEmitter()

/**
* Init Vercel Class
*/
const Vercel = require('../../lib/plugins/input/vercel')
const vercelWithArrayWithTwoSecrets = new Vercel(configWithArrayWithTwoClientSecrets, evem)
const vercelWithArrayWithOneSecret = new Vercel(configWithArrayWithOneClientSecret, evem)

describe('verifySignature should', function () {
it('return true for an array with 2 secrets', function (done) {
const signature = vercelWithArrayWithTwoSecrets.verifySignature(sampleReq, sampleBodyBuf)
assert.strictEqual(signature, true)
done()
})
it('return true for an array with 1 secret', function (done) {
const signature = vercelWithArrayWithOneSecret.verifySignature(sampleReq, sampleBodyBuf)
assert.strictEqual(signature, true)
done()
})
})

0 comments on commit 3c11a81

Please sign in to comment.