befly 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/libs/jwt.js DELETED
@@ -1,97 +0,0 @@
1
- import { createHmac } from 'crypto';
2
-
3
- /**
4
- * JWT基础工具类
5
- * 提供JWT相关的基础工具方法
6
- */
7
- export class Jwt {
8
- // 支持的算法映射
9
- static ALGORITHMS = {
10
- HS256: 'sha256',
11
- HS384: 'sha384',
12
- HS512: 'sha512'
13
- };
14
-
15
- /**
16
- * Base64 URL 编码
17
- * @param {string|Buffer} input - 需要编码的输入
18
- * @returns {string} Base64 URL编码结果
19
- */
20
- static base64UrlEncode(input) {
21
- const base64 = Buffer.isBuffer(input) ? input.toString('base64') : Buffer.from(input, 'utf8').toString('base64');
22
- return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
23
- }
24
-
25
- /**
26
- * Base64 URL 解码
27
- * @param {string} str - Base64 URL编码的字符串
28
- * @returns {string} 解码后的字符串
29
- */
30
- static base64UrlDecode(str) {
31
- const padding = 4 - (str.length % 4);
32
- if (padding !== 4) str += '='.repeat(padding);
33
- str = str.replace(/-/g, '+').replace(/_/g, '/');
34
- return Buffer.from(str, 'base64').toString('utf8');
35
- }
36
-
37
- /**
38
- * 解析过期时间为秒数
39
- * @param {string|number} expiresIn - 过期时间表达式
40
- * @returns {number} 过期时间(秒数)
41
- */
42
- static parseExpiration(expiresIn) {
43
- if (typeof expiresIn === 'number') return expiresIn;
44
- if (typeof expiresIn !== 'string') throw new Error('过期时间格式无效');
45
-
46
- // 如果是纯数字字符串,直接转换为数字
47
- const numericValue = parseInt(expiresIn);
48
- if (!isNaN(numericValue) && numericValue.toString() === expiresIn) {
49
- return numericValue;
50
- }
51
-
52
- // 支持毫秒(ms)和其他时间单位
53
- const match = expiresIn.match(/^(\d+)(ms|[smhdwy])$/);
54
- if (!match) throw new Error('过期时间格式无效');
55
-
56
- const value = parseInt(match[1]);
57
- const unit = match[2];
58
-
59
- if (unit === 'ms') {
60
- return Math.floor(value / 1000); // 毫秒转秒,向下取整
61
- }
62
-
63
- const multipliers = { s: 1, m: 60, h: 3600, d: 86400, w: 604800, y: 31536000 };
64
- return value * multipliers[unit];
65
- }
66
-
67
- /**
68
- * 创建HMAC签名
69
- * @param {string} algorithm - 算法名称
70
- * @param {string} secret - 密钥
71
- * @param {string} data - 待签名数据
72
- * @returns {string} Base64 URL编码的签名
73
- */
74
- static createSignature(algorithm, secret, data) {
75
- const hashAlgorithm = this.ALGORITHMS[algorithm];
76
- if (!hashAlgorithm) throw new Error(`不支持的算法: ${algorithm}`);
77
-
78
- const hmac = createHmac(hashAlgorithm, secret);
79
- hmac.update(data);
80
- return this.base64UrlEncode(hmac.digest());
81
- }
82
-
83
- /**
84
- * 常量时间字符串比较(防止时序攻击)
85
- * @param {string} a - 字符串A
86
- * @param {string} b - 字符串B
87
- * @returns {boolean} 是否相等
88
- */
89
- static constantTimeCompare(a, b) {
90
- if (a.length !== b.length) return false;
91
- let result = 0;
92
- for (let i = 0; i < a.length; i++) {
93
- result |= a.charCodeAt(i) ^ b.charCodeAt(i);
94
- }
95
- return result === 0;
96
- }
97
- }
File without changes
File without changes