var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import Response from "../Utils/Response";
import BadDataException from "../../Types/Exception/BadDataException";
import { SlackAppSigningSecret } from "../EnvironmentConfig";
import crypto from "crypto";
import logger, { getLogAttributesFromRequest } from "../Utils/Logger";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export default class SlackAuthorization {
    static async isAuthorizedSlackRequest(req, res, next) {
        logger.debug("Starting Slack request authorization", getLogAttributesFromRequest(req));
        if (!SlackAppSigningSecret) {
            logger.error("SLACK_APP_SIGNING_SECRET env variable not found.", getLogAttributesFromRequest(req));
            return Response.sendErrorResponse(req, res, new BadDataException("SLACK_APP_SIGNING_SECRET env variable not found."));
        }
        // validate slack signing secret
        const slackSigningSecret = SlackAppSigningSecret.toString();
        const slackSignature = req.headers["x-slack-signature"];
        const timestamp = req.headers["x-slack-request-timestamp"];
        // Use rawBody for both JSON and URL-encoded requests, fallback to rawFormUrlEncodedBody for backward compatibility
        const requestBody = req.rawBody ||
            req.rawFormUrlEncodedBody ||
            "";
        logger.debug(`slackSignature: ${slackSignature}`, getLogAttributesFromRequest(req));
        logger.debug(`timestamp: ${timestamp}`, getLogAttributesFromRequest(req));
        logger.debug(`requestBody: `, getLogAttributesFromRequest(req));
        logger.debug(requestBody, getLogAttributesFromRequest(req));
        const baseString = `v0:${timestamp}:${requestBody}`;
        const signature = `v0=${crypto.createHmac("sha256", slackSigningSecret).update(baseString).digest("hex")}`;
        logger.debug(`Generated signature: ${signature}`, getLogAttributesFromRequest(req));
        // check if the signature is valid
        if (!crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(slackSignature))) {
            logger.error("Slack Signature Verification Failed.", getLogAttributesFromRequest(req));
            return Response.sendErrorResponse(req, res, new BadDataException("Slack Signature Verification Failed."));
        }
        logger.debug("Slack request authorized successfully", getLogAttributesFromRequest(req));
        next();
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, Function]),
    __metadata("design:returntype", Promise)
], SlackAuthorization, "isAuthorizedSlackRequest", null);
//# sourceMappingURL=SlackAuthorization.js.map