UNPKG

@graphql-hive/external-composition

Version:

Compose schemas outside GraphQL Hive

27 lines (26 loc) 1.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.signatureHeaderName = void 0; exports.verifyRequest = verifyRequest; exports.compose = compose; const tslib_1 = require("tslib"); const node_crypto_1 = tslib_1.__importDefault(require("node:crypto")); exports.signatureHeaderName = 'x-hive-signature-256'; const sigHashAlg = 'sha256'; function hash(secret, algo, data) { return node_crypto_1.default.createHmac(algo, secret).update(data, 'utf-8').digest('hex'); } 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 || !node_crypto_1.default.timingSafeEqual(digest, sig)) { return 'ERR_INVALID_SIGNATURE'; } } function compose(composition) { return (input) => composition(input); }