"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.bindWorkspaceRouteContext = bindWorkspaceRouteContext;
exports.requireWorkspaceRouteContext = requireWorkspaceRouteContext;
const auth_1 = require("./auth");
const resourceOwnership_1 = require("../services/resourceOwnership");
function sanitizeWorkspaceId(value) {
    if (typeof value !== 'string')
        return '';
    return value.trim().replace(/[^a-zA-Z0-9._:-]/g, '').slice(0, 128);
}
function bindWorkspaceRouteContext(req, res, next) {
    const workspaceId = sanitizeWorkspaceId(req.params.workspaceId);
    if (!workspaceId) {
        res.status(400).json({
            success: false,
            error: 'workspaceId is required',
        });
        return;
    }
    req.workspaceRouteContext = { workspaceId };
    req.headers['x-workspace-id'] = workspaceId;
    next();
}
function requireWorkspaceRouteContext(req, res, next) {
    const expectedWorkspaceId = req.workspaceRouteContext?.workspaceId;
    if (!expectedWorkspaceId) {
        res.status(400).json({
            success: false,
            error: 'workspace route context is missing',
        });
        return;
    }
    const context = (0, auth_1.getRequestContext)(req);
    if (!context || context.workspaceId !== expectedWorkspaceId) {
        (0, resourceOwnership_1.sendResourceNotFound)(res);
        return;
    }
    next();
}
//# sourceMappingURL=workspaceRouteContext.js.map