@kevisual/auth 2.0.0 → 2.0.2
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/dist/app.d.ts +92 -0
- package/dist/app.js +3495 -0
- package/package.json +10 -6
- package/src/index.ts +5 -1
- package/src/query.ts +53 -0
- package/bun.config.ts +0 -20
- package/test/create.ts +0 -38
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import * as jose from 'jose';
|
|
2
|
+
import { Query, Result } from '@kevisual/query/query';
|
|
3
|
+
import { LRUCache } from 'lru-cache';
|
|
4
|
+
|
|
5
|
+
/***
|
|
6
|
+
* 验证 JWT
|
|
7
|
+
* @param token JWT 字符串
|
|
8
|
+
* @param publicKey 公钥,可以是 CryptoKey、PEM 字符串或 JWK/JWKS 对象
|
|
9
|
+
* @returns 解码后的有效载荷
|
|
10
|
+
* @throws 如果验证失败或令牌无效,则抛出错误
|
|
11
|
+
*/
|
|
12
|
+
declare function verifyJWT(token: string, publicKey: jose.CryptoKey | string | jose.JWK): Promise<any>;
|
|
13
|
+
type JWTPayload<T = {}> = {
|
|
14
|
+
/**
|
|
15
|
+
* 会设置默认值: "https://convex.kevisual.cn"
|
|
16
|
+
*/
|
|
17
|
+
iss?: string;
|
|
18
|
+
/**
|
|
19
|
+
* "user:8fa2be73c2229e85"
|
|
20
|
+
* "ip:192.168.1.1"
|
|
21
|
+
*/
|
|
22
|
+
sub: string;
|
|
23
|
+
/**
|
|
24
|
+
* 会设置默认值:"convex-app"
|
|
25
|
+
*/
|
|
26
|
+
aud?: string;
|
|
27
|
+
/**
|
|
28
|
+
* 会设置默认值:当前时间 + 2 小时
|
|
29
|
+
*/
|
|
30
|
+
exp?: number;
|
|
31
|
+
/**
|
|
32
|
+
* 会设置默认值:当前时间
|
|
33
|
+
*/
|
|
34
|
+
iat?: number;
|
|
35
|
+
/**
|
|
36
|
+
* 其他自定义字段
|
|
37
|
+
*/
|
|
38
|
+
name?: string;
|
|
39
|
+
/**
|
|
40
|
+
* email
|
|
41
|
+
*/
|
|
42
|
+
email?: string;
|
|
43
|
+
} & T;
|
|
44
|
+
/***
|
|
45
|
+
* 签发 JWT
|
|
46
|
+
* @param payload 有效载荷
|
|
47
|
+
* @param privateKey 私钥,可以是 CryptoKey、PEM 字符串或 JWK 对象
|
|
48
|
+
* @returns 签发的 JWT 字符串
|
|
49
|
+
*/
|
|
50
|
+
declare function signJWT(payload: JWTPayload, privateKey: jose.CryptoKey | string | jose.JWK): Promise<string>;
|
|
51
|
+
/**
|
|
52
|
+
* 单独解码 JWT,不验证签名
|
|
53
|
+
* @param token
|
|
54
|
+
* @returns
|
|
55
|
+
*/
|
|
56
|
+
declare const decodeJWT: (token: string) => JWTPayload;
|
|
57
|
+
|
|
58
|
+
type GenerateOpts = {
|
|
59
|
+
kid?: string;
|
|
60
|
+
};
|
|
61
|
+
declare const generate: (opts?: GenerateOpts) => Promise<{
|
|
62
|
+
jwks: {
|
|
63
|
+
keys: any[];
|
|
64
|
+
};
|
|
65
|
+
privateJWK: any;
|
|
66
|
+
privatePEM: any;
|
|
67
|
+
publicPEM: any;
|
|
68
|
+
}>;
|
|
69
|
+
|
|
70
|
+
type TokenUser = {
|
|
71
|
+
id: string;
|
|
72
|
+
username: string;
|
|
73
|
+
nickname: string;
|
|
74
|
+
description: string;
|
|
75
|
+
/** user or org */
|
|
76
|
+
type: string;
|
|
77
|
+
avatar?: string;
|
|
78
|
+
orgs?: string[];
|
|
79
|
+
[key: string]: any;
|
|
80
|
+
};
|
|
81
|
+
type AuthQueryOpts = {
|
|
82
|
+
url?: string;
|
|
83
|
+
};
|
|
84
|
+
declare class AuthQuery extends Query {
|
|
85
|
+
cache: LRUCache<string, any>;
|
|
86
|
+
constructor(opts?: AuthQueryOpts);
|
|
87
|
+
getTokenUser: (token: string) => Promise<Result<TokenUser>>;
|
|
88
|
+
getTokenUserCache: (token: string) => Promise<Result<TokenUser>>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { AuthQuery, decodeJWT, generate, signJWT, verifyJWT };
|
|
92
|
+
export type { JWTPayload };
|