UNPKG

pagaris

Version:

Pagaris API client for Node

33 lines (26 loc) 970 B
const Pagaris = require('./pagaris') const crypto = require('crypto') class Signature { constructor(path, method, body, timestamp = Math.floor(Date.now() / 1000)) { this.path = path this.method = method this.body = body if (!this.body) this.body = '' this.timestamp = timestamp this.value = crypto.createHmac('sha256', (Pagaris.privateKey || '')) .update(`${this.timestamp}-${this.method}-${this.path}-${this.body}`) .digest('hex') } headerValue() { return `Pagaris ${Pagaris.applicationId}:${this.timestamp}:${this.value}` } static validate(headerValue, path, body, method = 'POST') { const headerRegexp = /^(Pagaris\s)?(.+):(.+):(.+)$/g const matches = headerRegexp.exec(headerValue) let timestamp = matches && matches[3] let signature = matches && matches[4] let expected = new Signature(path, method, body, timestamp) return expected.value == signature } } module.exports = Signature