"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PLUGIN_SESSION_TTL_SECONDS = void 0;
exports.issuePluginSessionToken = issuePluginSessionToken;
exports.verifyPluginSessionToken = verifyPluginSessionToken;
/**
 * Plugin session tokens — HMAC-signed authorization for the /api/plugin-bridge surface.
 *
 * Why: the previous bridge identified the calling plugin by an `x-plugin-id` header,
 * which any authenticated user could forge via curl to gain that plugin's manifest
 * permissions (terminal:send, session:read, ...). This module replaces the header
 * with a short-lived JWT signed by the server's jwtSecret, so the caller must have
 * first obtained the token from `POST /api/plugins/:id/session` (which is gated on
 * a valid user JWT + installed-and-enabled plugin).
 *
 * Token typ is namespaced to prevent cross-use with the main user-auth JWTs.
 */
const jwt = __importStar(require("jsonwebtoken"));
const config_1 = require("./config");
const plugin_manager_1 = require("./plugin-manager");
const TOKEN_TYP = 'ccweb-plugin-session';
const TOKEN_TTL_SECONDS = 2 * 60 * 60; // 2 hours
function issuePluginSessionToken(pluginId, username) {
    const plugin = plugin_manager_1.pluginManager.get(pluginId);
    if (!plugin)
        throw new Error('Plugin not installed');
    if (!plugin.registry.enabled)
        throw new Error('Plugin disabled');
    const secret = (0, config_1.getConfig)().jwtSecret;
    const payload = {
        typ: TOKEN_TYP,
        pid: pluginId,
        usr: username,
        scp: plugin.manifest.permissions,
    };
    return jwt.sign(payload, secret, { expiresIn: TOKEN_TTL_SECONDS });
}
function verifyPluginSessionToken(token) {
    try {
        const secret = (0, config_1.getConfig)().jwtSecret;
        const decoded = jwt.verify(token, secret);
        if (decoded.typ !== TOKEN_TYP)
            return null;
        if (typeof decoded.pid !== 'string' || typeof decoded.usr !== 'string' || !Array.isArray(decoded.scp))
            return null;
        return {
            typ: TOKEN_TYP,
            pid: decoded.pid,
            usr: decoded.usr,
            scp: decoded.scp.filter((s) => typeof s === 'string'),
        };
    }
    catch {
        return null;
    }
}
exports.PLUGIN_SESSION_TTL_SECONDS = TOKEN_TTL_SECONDS;
//# sourceMappingURL=plugin-session.js.map