UNPKG

@graphql-hive/external-composition

Version:

Compose schemas outside GraphQL Hive

21 lines (20 loc) 763 B
import crypto from 'node:crypto'; export const signatureHeaderName = 'x-hive-signature-256'; const sigHashAlg = 'sha256'; function hash(secret, algo, data) { return crypto.createHmac(algo, secret).update(data, 'utf-8').digest('hex'); } export function verifyRequest(input) { const { body, signature, secret } = input; if (!body) { return 'ERR_EMPTY_BODY'; } const sig = Buffer.from(signature !== null && signature !== void 0 ? signature : '', 'utf8'); const digest = Buffer.from(hash(secret, sigHashAlg, body), 'utf8'); if (sig.length !== digest.length || !crypto.timingSafeEqual(digest, sig)) { return 'ERR_INVALID_SIGNATURE'; } } export function compose(composition) { return (input) => composition(input); }