Files
testing-tool/utils/jwt.ts
T
雨霖铃 aa23a263fe refactor: 迁移 i18n 系统从 react-i18next 到 chrome.i18n
- 移除 react-i18next、i18next 及相关依赖
- 删除旧的 i18n/ 目录和 useLazyTranslation 工具
- 新增 utils/chromeI18n.ts 类型安全 wrapper(useI18n Hook + getMessage)
- 生成 public/_locales/{zh,en}/messages.json(298 个翻译 key)
- 批量更新 39+ 组件文件的导入和翻译调用
- 转换翻译键格式:namespace:key → namespace_key
- 修复 ErrorBoundary/PageErrorBoundary 从 withTranslation HOC 改为直接调用 getMessage
- 更新 vitest.setup.ts mock 加载实际翻译文本
- 修复 11 个测试文件的断言以匹配中文翻译
- TypeScript、ESLint、547 项测试全部通过

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-28 19:21:54 +08:00

130 lines
2.8 KiB
TypeScript

/**
* JWT 解析工具
*/
import { getMessage } from '@/utils/chromeI18n';
export interface JwtHeader {
alg: string;
typ?: string;
[key: string]: unknown;
}
export interface JwtPayload {
iss?: string;
sub?: string;
aud?: string | string[];
exp?: number;
nbf?: number;
iat?: number;
jti?: string;
[key: string]: unknown;
}
export interface JwtResult {
header: JwtHeader | null;
payload: JwtPayload | null;
signature: string;
raw: {
header: string;
payload: string;
signature: string;
};
error?: string;
}
/**
* Base64URL 解码
* @param str Base64URL 编码字符串
*/
export function decodeBase64Url(str: string): string {
let base64 = str.replace(/-/g, '+').replace(/_/g, '/');
const pad = base64.length % 4;
if (pad) {
if (pad === 1) {
throw new Error(getMessage('jwt_errors_invalidBase64String'));
}
base64 += new Array(5 - pad).join('=');
}
try {
const binStr = atob(base64);
const binLen = binStr.length;
const bytes = new Uint8Array(binLen);
for (let i = 0; i < binLen; i++) {
bytes[i] = binStr.charCodeAt(i);
}
const decoder = new TextDecoder('utf-8');
return decoder.decode(bytes);
} catch (e) {
throw new Error(
getMessage('jwt_errors_failedToDecode') + (e instanceof Error ? e.message : String(e)),
{ cause: e },
);
}
}
/**
* 解析 JWT 字符串
* @param token JWT 字符串
*/
export function parseJwt(token: string): JwtResult {
const parts = token.trim().split('.');
if (parts.length !== 3) {
return {
header: null,
payload: null,
signature: '',
raw: { header: '', payload: '', signature: '' },
error: getMessage('jwt_errors_invalidFormat'),
};
}
const [headerB64, payloadB64, signatureB64] = parts;
const result: JwtResult = {
header: null,
payload: null,
signature: signatureB64,
raw: {
header: headerB64,
payload: payloadB64,
signature: signatureB64,
},
};
try {
const headerJson = decodeBase64Url(headerB64);
result.header = JSON.parse(headerJson);
} catch (e) {
result.error =
getMessage('jwt_errors_parseHeaderFailed') + (e instanceof Error ? e.message : String(e));
return result;
}
try {
const payloadJson = decodeBase64Url(payloadB64);
result.payload = JSON.parse(payloadJson);
} catch (e) {
result.error =
getMessage('jwt_errors_parsePayloadFailed') + (e instanceof Error ? e.message : String(e));
return result;
}
return result;
}
/**
* 将对象格式化为 JSON 字符串
* @param obj 对象
*/
export function stringifyJson(obj: unknown): string {
try {
return JSON.stringify(obj, null, 2);
} catch (e) {
console.error('格式化 JSON 失败:', e);
return String(obj);
}
}