import get from 'lodash-es/get.js';
import connectionService from '../../../services/connection.service.js';
import configService from '../../../services/config.service.js';
import { getLogger } from '@nangohq/utils';
import crypto from 'crypto';
const logger = getLogger('Webhook.GithubAppOauth');
function validate(integration, headerSignature, body) {
    const custom = integration.custom;
    const private_key = custom['private_key'];
    const hash = `${custom['app_id']}${private_key}${integration.app_link}`;
    const secret = crypto.createHash('sha256').update(hash).digest('hex');
    const signature = crypto.createHmac('sha256', secret).update(JSON.stringify(body)).digest('hex');
    const trusted = Buffer.from(`sha256=${signature}`, 'ascii');
    const untrusted = Buffer.from(headerSignature, 'ascii');
    return crypto.timingSafeEqual(trusted, untrusted);
}
export default async function route(nango, integration, headers, body, logContextGetter) {
    const signature = headers['x-hub-signature-256'];
    if (signature) {
        const valid = validate(integration, signature, body);
        if (!valid) {
            logger.error('Github App webhook signature invalid. Exiting');
            return;
        }
    }
    if (get(body, 'action') === 'created') {
        await handleCreateWebhook(integration, body, logContextGetter);
    }
    return nango.executeScriptForWebhooks(integration, body, 'installation.id', 'installation_id', logContextGetter);
}
async function handleCreateWebhook(integration, body, logContextGetter) {
    if (!get(body, 'requester.login')) {
        return;
    }
    const connections = await connectionService.findConnectionsByMultipleConnectionConfigValues({ app_id: get(body, 'installation.app_id'), pending: true, handle: get(body, 'requester.login') }, integration.environment_id);
    if (connections?.length === 0) {
        logger.info('No connections found for app_id', get(body, 'installation.app_id'));
        return;
    }
    else {
        const installationId = get(body, 'installation.id');
        const [connection] = connections;
        // if there is no matching connection or if the connection config already has an installation_id, exit
        if (!connection || connection.connection_config['installation_id']) {
            logger.info('no connection or existing installation_id');
            return;
        }
        const template = configService.getTemplate(integration?.provider);
        const activityLogId = connection.connection_config['pendingLog'];
        // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
        delete connection.connection_config['pendingLog'];
        // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
        delete connection.connection_config['pendingLog'];
        const connectionConfig = {
            ...connection.connection_config,
            installation_id: installationId
        };
        const logCtx = logContextGetter.get({ id: activityLogId });
        await connectionService.getAppCredentialsAndFinishConnection(connection.connection_id, integration, template, connectionConfig, activityLogId, logCtx, logContextGetter);
    }
}
//# sourceMappingURL=github-app-oauth-webhook-routing.js.map