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 { ClusterKey as ONEUPTIME_SECRET } from "../EnvironmentConfig";
import Response from "../Utils/Response";
import BadDataException from "../../Types/Exception/BadDataException";
import CaptureSpan from "../Utils/Telemetry/CaptureSpan";
export default class ClusterKeyAuthorization {
    static getClusterKeyHeaders() {
        return {
            clusterkey: ClusterKeyAuthorization.getClusterKey(),
        };
    }
    static getClusterKey() {
        // we encode uri component because a lot of people use special characters in their cluster key secret
        return encodeURIComponent(ONEUPTIME_SECRET.toString());
    }
    static async isAuthorizedServiceMiddleware(req, res, next) {
        let clusterKey;
        if (req.params && req.params["clusterKey"]) {
            clusterKey = req.params["clusterKey"];
        }
        else if (req.query && req.query["clusterKey"]) {
            clusterKey = req.query["clusterKey"];
        }
        else if (req.headers && req.headers["clusterkey"]) {
            // Header keys are automatically transformed to lowercase
            clusterKey = req.headers["clusterkey"];
        }
        else if (req.headers && req.headers["x-clusterkey"]) {
            // KEDA TriggerAuthentication sends headers with X- prefix
            clusterKey = req.headers["x-clusterkey"];
        }
        else if (req.body && req.body.clusterKey) {
            clusterKey = req.body.clusterKey;
        }
        else {
            return Response.sendErrorResponse(req, res, new BadDataException("Cluster key not found."));
        }
        const isAuthorized = clusterKey.toString() === ClusterKeyAuthorization.getClusterKey();
        if (!isAuthorized) {
            return Response.sendErrorResponse(req, res, new BadDataException("Invalid cluster key provided"));
        }
        return next();
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Object)
], ClusterKeyAuthorization, "getClusterKeyHeaders", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", String)
], ClusterKeyAuthorization, "getClusterKey", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, Object, Function]),
    __metadata("design:returntype", Promise)
], ClusterKeyAuthorization, "isAuthorizedServiceMiddleware", null);
//# sourceMappingURL=ClusterKeyAuthorization.js.map