/**
 * CSRF 防护工具
 * 提供 CSRF Token 的生成、存储和验证功能
 */ /**
 * CSRF Token 存储接口
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get CSRFTokenManager () {
        return CSRFTokenManager;
    },
    get getCSRFManager () {
        return getCSRFManager;
    },
    get initCSRFManager () {
        return initCSRFManager;
    }
});
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
/**
 * 默认 Token 存储（使用 Cookie）
 */ let CookieTokenStorage = class CookieTokenStorage {
    getToken() {
        if (typeof document === 'undefined') {
            return null;
        }
        const cookies = document.cookie.split(';');
        for (const cookie of cookies){
            const [name, value] = cookie.trim().split('=');
            if (name === this.cookieName) {
                return decodeURIComponent(value);
            }
        }
        return null;
    }
    setToken(token) {
        if (typeof document === 'undefined') {
            return;
        }
        // 设置 Cookie（HttpOnly 由服务端设置，这里只设置客户端可访问的）
        document.cookie = `${this.cookieName}=${encodeURIComponent(token)}; path=/; SameSite=Strict`;
    }
    clearToken() {
        if (typeof document === 'undefined') {
            return;
        }
        document.cookie = `${this.cookieName}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
    }
    constructor(cookieName = 'csrf-token'){
        _define_property(this, "cookieName", void 0);
        this.cookieName = cookieName;
    }
};
/**
 * 内存 Token 存储（用于 SSR 或测试）
 */ let MemoryTokenStorage = class MemoryTokenStorage {
    getToken() {
        return this.token;
    }
    setToken(token) {
        this.token = token;
    }
    clearToken() {
        this.token = null;
    }
    constructor(){
        _define_property(this, "token", null);
    }
};
let CSRFTokenManager = class CSRFTokenManager {
    /**
   * 获取 CSRF Token
   */ async getToken() {
        if (this.getTokenFn) {
            try {
                return await Promise.resolve(this.getTokenFn());
            } catch (error) {
                console.warn('CSRF Token 获取失败:', error);
                return null;
            }
        }
        // 从存储中获取
        return this.storage.getToken();
    }
    /**
   * 设置 CSRF Token
   */ setToken(token) {
        this.storage.setToken(token);
    }
    /**
   * 验证 CSRF Token
   */ async validateToken(token) {
        if (this.validateTokenFn) {
            try {
                return await Promise.resolve(this.validateTokenFn(token));
            } catch (error) {
                console.warn('CSRF Token 验证失败:', error);
                return false;
            }
        }
        // 默认验证：检查存储中的 Token 是否匹配
        const storedToken = this.storage.getToken();
        return storedToken !== null && storedToken === token;
    }
    /**
   * 清除 CSRF Token
   */ clearToken() {
        this.storage.clearToken();
    }
    /**
   * 获取请求头名称
   */ getHeaderName() {
        return this.headerName;
    }
    constructor(config){
        _define_property(this, "storage", void 0);
        _define_property(this, "headerName", void 0);
        _define_property(this, "cookieName", void 0);
        _define_property(this, "getTokenFn", void 0);
        _define_property(this, "validateTokenFn", void 0);
        this.headerName = config?.headerName ?? 'X-CSRF-Token';
        this.cookieName = config?.cookieName ?? 'csrf-token';
        this.storage = config?.storage ?? (typeof document !== 'undefined' ? new CookieTokenStorage(this.cookieName) : new MemoryTokenStorage());
        this.getTokenFn = config?.getToken;
        this.validateTokenFn = config?.validateToken;
    }
};
/**
 * 全局 CSRF Token 管理器实例
 */ let globalCSRFManager = null;
function initCSRFManager(config) {
    globalCSRFManager = new CSRFTokenManager(config);
    return globalCSRFManager;
}
function getCSRFManager() {
    return globalCSRFManager;
}

//# sourceMappingURL=csrf.js.map