import crypto from 'node:crypto';
import { getLogger } from '@nangohq/utils';
const logger = getLogger('Webhook.Linear');
function validate(integration, headerSignature, rawBody) {
    if (!integration.custom?.['webhookSecret']) {
        return false;
    }
    const signature = crypto.createHmac('sha256', integration.custom['webhookSecret']).update(rawBody).digest('hex');
    return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(headerSignature));
}
const route = async (nango, integration, headers, body, rawBody, logContextGetter) => {
    const signature = headers['linear-signature'];
    logger.info('received', { configId: integration.id });
    if (!validate(integration, signature, rawBody)) {
        logger.error('invalid signature', { configId: integration.id });
        return;
    }
    const parsedBody = body;
    logger.info(`valid ${parsedBody.type}`, { configId: integration.id });
    const response = await nango.executeScriptForWebhooks(integration, parsedBody, 'type', 'organizationId', logContextGetter, 'organizationId');
    return { parsedBody, connectionIds: response?.connectionIds || [] };
};
export default route;
//# sourceMappingURL=linear-webhook-routing.js.map