// 本地存储加密工具类 import { LicenseService } from '@/services/license-service'; import type { LicenseJWSResult, LicenseSetting, SavedLicense } from '@/types/license'; import { KJUR } from 'jsrsasign'; export class LicenseStorage { private static readonly STORAGE_KEY_PREFIX = 'alicorns_plugin_'; private static readonly STORAGE_SETTING_KEY_PREFIX = this.STORAGE_KEY_PREFIX + 'setting_'; /** * 生成存储key */ private static generateStorageKey(pluginId: string): string { return `${this.STORAGE_KEY_PREFIX}${pluginId}`; } /** * 保存 JWT 到本地存储 */ static saveLicense(jwt: string): SavedLicense | null { try { // 从 JWT 中提取 pluginId 以生成存储key if (!jwt) return null; // 解码JWT const savedLicense = this.convertToSavedLicense(jwt); if (!savedLicense) { return null; } const key = this.generateStorageKey(savedLicense.licenseInfo.pluginId); localStorage.setItem(key, jwt); return savedLicense; } catch (error) { console.error('Failed to save license:', error); throw error; } } /** * 从本地存储获取许可证信息 */ static getLicense(pluginId: string): SavedLicense | null { try { const key = this.generateStorageKey(pluginId); const storedJWT = localStorage.getItem(key); if (!storedJWT) return null; // 解码JWT const savedLicense = this.convertToSavedLicense(storedJWT); if (!savedLicense) { // JWT解析失败,清理存储 this.clearLicense(pluginId); return null; } const isValid = LicenseService.checkLicenseAvailable(savedLicense); if (!isValid) { // 获取许可证信息失败,清理存储 this.clearLicense(pluginId); return null; } return savedLicense; } catch (error) { console.error('Failed to get license:', error); // 如果获取失败,清空本地存储 this.clearLicense(pluginId); return null; } } static convertToSavedLicense(storedJWT: string): SavedLicense | null { if (!storedJWT) return null; // decode let decodedJWT: LicenseJWSResult; try { decodedJWT = KJUR.jws.JWS.parse(storedJWT) as LicenseJWSResult; if (!decodedJWT) { return null; } } catch (error) { console.error('Failed to get license:', error); return null; } return { jwt: storedJWT, parsed: decodedJWT, licenseInfo: decodedJWT.payloadObj!, } } /** * 清除许可证信息 */ static clearLicense(pluginId: string): void { const key = this.generateStorageKey(pluginId); localStorage.removeItem(key); } /** * 生成存储key */ private static generateSettingStorageKey(pluginId: string): string { return `${this.STORAGE_SETTING_KEY_PREFIX}${pluginId}`; } /** * 保存设置信息到本地存储 */ static saveSetting(setting: LicenseSetting, pluginId: string): void { try { const key = this.generateSettingStorageKey(pluginId); localStorage.setItem(key, JSON.stringify(setting)); } catch (error) { console.error('Failed to save setting:', error); throw error; } } /** * 从本地存储获取设置信息 */ static getSettings(pluginId: string): LicenseSetting { try { const key = this.generateSettingStorageKey(pluginId); const storedData = localStorage.getItem(key); if (!storedData) return {}; return JSON.parse(storedData); } catch (error) { console.error('Failed to get setting:', error); return {}; } } /** * 清除设置信息 */ static clearSetting(pluginId: string): void { const key = this.generateSettingStorageKey(pluginId); localStorage.removeItem(key); } }