|
| 1 | +import crypto from 'crypto'; |
| 2 | +import KeyStore from './keyStore'; |
| 3 | +import NonceCache from '../cache/nonceCache'; |
| 4 | + |
| 5 | +export interface SignatureOptions { |
| 6 | + timestampTolerance?: number; // seconds |
| 7 | + clockSkewTolerance?: number; // seconds |
| 8 | + nonceTtl?: number; // seconds |
| 9 | +} |
| 10 | + |
| 11 | +export default class SignatureService { |
| 12 | + private keys: KeyStore; |
| 13 | + private nonceCache: NonceCache; |
| 14 | + private opts: Required<SignatureOptions>; |
| 15 | + |
| 16 | + constructor(keyStore: KeyStore, opts?: SignatureOptions) { |
| 17 | + this.keys = keyStore; |
| 18 | + this.nonceCache = new NonceCache(); |
| 19 | + this.opts = { |
| 20 | + timestampTolerance: opts?.timestampTolerance ?? 300, |
| 21 | + clockSkewTolerance: opts?.clockSkewTolerance ?? 30, |
| 22 | + nonceTtl: opts?.nonceTtl ?? 600, |
| 23 | + }; |
| 24 | + } |
| 25 | + |
| 26 | + generate(body: string, secret?: string, timestamp?: number, nonce?: string) { |
| 27 | + const ts = timestamp ?? Math.floor(Date.now() / 1000); |
| 28 | + const n = nonce ?? crypto.randomBytes(12).toString('hex'); |
| 29 | + const key = secret ?? this.keys.getCurrent(); |
| 30 | + const hmac = crypto.createHmac('sha256', key).update(`${ts}.${body}`).digest(); |
| 31 | + const sig = hmac.toString('base64'); |
| 32 | + const header = `t=${ts},s=${sig},v=1,n=${n}`; |
| 33 | + return { header, sig, ts, nonce: n }; |
| 34 | + } |
| 35 | + |
| 36 | + parseHeader(header: string) { |
| 37 | + const parts = header.split(',').map(p => p.trim()); |
| 38 | + const map: Record<string, string> = {}; |
| 39 | + for (const part of parts) { |
| 40 | + const [k, v] = part.split('='); |
| 41 | + if (k && v) map[k] = v; |
| 42 | + } |
| 43 | + return map; |
| 44 | + } |
| 45 | + |
| 46 | + async verify(rawBody: string, header: string) { |
| 47 | + if (!header) throw new Error('missing signature header'); |
| 48 | + const parsed = this.parseHeader(header); |
| 49 | + const ts = parseInt(parsed.t, 10); |
| 50 | + const sig = parsed.s; |
| 51 | + const ver = parsed.v; |
| 52 | + const nonce = parsed.n; |
| 53 | + if (!ts || !sig || !ver || !nonce) throw new Error('invalid signature header'); |
| 54 | + if (ver !== '1') throw new Error('unsupported signature version'); |
| 55 | + |
| 56 | + const now = Math.floor(Date.now() / 1000); |
| 57 | + const allowed = this.opts.timestampTolerance + this.opts.clockSkewTolerance; |
| 58 | + if (Math.abs(now - ts) > allowed) throw new Error('timestamp outside tolerance'); |
| 59 | + |
| 60 | + // nonce replay check |
| 61 | + if (await this.nonceCache.has(nonce)) { |
| 62 | + throw new Error('replay detected'); |
| 63 | + } |
| 64 | + |
| 65 | + // compute hmac against active keys |
| 66 | + const keys = this.keys.getActiveKeys(); |
| 67 | + let match = false; |
| 68 | + for (const k of keys) { |
| 69 | + const h = crypto.createHmac('sha256', k).update(`${ts}.${rawBody}`).digest().toString('base64'); |
| 70 | + if (h === sig) { |
| 71 | + match = true; |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + if (!match) throw new Error('signature mismatch'); |
| 76 | + |
| 77 | + // store nonce |
| 78 | + await this.nonceCache.set(nonce, this.opts.nonceTtl); |
| 79 | + return true; |
| 80 | + } |
| 81 | +} |
0 commit comments