@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/package.json
CHANGED
|
@@ -1,25 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kevisual/auth",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "bun run bun.config.ts"
|
|
7
7
|
},
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"src"
|
|
11
|
+
],
|
|
8
12
|
"keywords": [],
|
|
9
13
|
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
|
10
14
|
"license": "MIT",
|
|
11
|
-
"packageManager": "pnpm@10.
|
|
15
|
+
"packageManager": "pnpm@10.28.1",
|
|
12
16
|
"type": "module",
|
|
13
|
-
"dependencies": {},
|
|
14
17
|
"devDependencies": {
|
|
15
|
-
"@kevisual/types": "^0.0.12",
|
|
16
|
-
"@kevisual/use-config": "^1.0.28",
|
|
17
18
|
"@kevisual/query": "^0.0.38",
|
|
18
19
|
"@kevisual/router": "^0.0.60",
|
|
20
|
+
"@kevisual/types": "^0.0.12",
|
|
21
|
+
"@kevisual/use-config": "^1.0.28",
|
|
19
22
|
"@types/bun": "^1.3.6",
|
|
20
23
|
"@types/node": "^25.0.10",
|
|
21
24
|
"es-toolkit": "^1.44.0",
|
|
22
|
-
"jose": "^6.1.3"
|
|
25
|
+
"jose": "^6.1.3",
|
|
26
|
+
"lru-cache": "^11.2.4"
|
|
23
27
|
},
|
|
24
28
|
"exports": {
|
|
25
29
|
".": "./dist/app.js",
|
package/src/index.ts
CHANGED
package/src/query.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Query, Result } from "@kevisual/query/query";
|
|
2
|
+
import { LRUCache } from 'lru-cache'
|
|
3
|
+
|
|
4
|
+
type TokenUser = {
|
|
5
|
+
id: string;
|
|
6
|
+
username: string;
|
|
7
|
+
nickname: string;
|
|
8
|
+
description: string;
|
|
9
|
+
/** user or org */
|
|
10
|
+
type: string;
|
|
11
|
+
avatar?: string;
|
|
12
|
+
orgs?: string[];
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type AuthQueryOpts = {
|
|
17
|
+
url?: string;
|
|
18
|
+
}
|
|
19
|
+
export class AuthQuery extends Query {
|
|
20
|
+
cache: LRUCache<string, any>;
|
|
21
|
+
constructor(opts: AuthQueryOpts = {}) {
|
|
22
|
+
if (!opts.url) {
|
|
23
|
+
opts.url = 'https://kevisual.cn/api/router/';
|
|
24
|
+
}
|
|
25
|
+
super(opts);
|
|
26
|
+
this.cache = new LRUCache<string, any>({
|
|
27
|
+
max: 10000, // 最大缓存数量
|
|
28
|
+
ttl: 1000 * 60 * 60 * 2, // 缓存过期时间,单位为毫秒,这里设置为2小时
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
getTokenUser = async (token: string): Promise<Result<TokenUser>> => {
|
|
32
|
+
const res = await this.post({
|
|
33
|
+
path: 'user',
|
|
34
|
+
key: 'me',
|
|
35
|
+
token: token,
|
|
36
|
+
});
|
|
37
|
+
return res;
|
|
38
|
+
}
|
|
39
|
+
getTokenUserCache = async (token: string): Promise<Result<TokenUser>> => {
|
|
40
|
+
const tokenUser = await this.cache.get(token);
|
|
41
|
+
if (tokenUser) {
|
|
42
|
+
return {
|
|
43
|
+
code: 200,
|
|
44
|
+
data: tokenUser,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const res = await this.getTokenUser(token);
|
|
48
|
+
if (res.code === 200) {
|
|
49
|
+
this.cache.set(token, res.data);
|
|
50
|
+
}
|
|
51
|
+
return res;
|
|
52
|
+
}
|
|
53
|
+
}
|
package/bun.config.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { resolvePath } from '@kevisual/use-config';
|
|
2
|
-
import { execSync } from 'node:child_process';
|
|
3
|
-
|
|
4
|
-
const entry = 'src/index.ts';
|
|
5
|
-
const naming = 'app';
|
|
6
|
-
const external = ['pm2'];
|
|
7
|
-
await Bun.build({
|
|
8
|
-
target: 'browser',
|
|
9
|
-
format: 'esm',
|
|
10
|
-
entrypoints: [resolvePath(entry, { meta: import.meta })],
|
|
11
|
-
outdir: resolvePath('./dist', { meta: import.meta }),
|
|
12
|
-
naming: {
|
|
13
|
-
entry: `${naming}.js`,
|
|
14
|
-
},
|
|
15
|
-
external,
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const cmd ='dts -i src/index.ts -o app.d.ts'
|
|
19
|
-
execSync(cmd, { stdio: 'inherit' });
|
|
20
|
-
console.log('Build completed.');
|
package/test/create.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import * as jose from 'jose';
|
|
2
|
-
import { signJWT, decodeJWT, verifyJWT } from '../src/auth.ts';
|
|
3
|
-
import { getValues } from '../src/jwks/get.ts';
|
|
4
|
-
|
|
5
|
-
const payload = {
|
|
6
|
-
iss: "https://convex.kevisual.cn",
|
|
7
|
-
sub: "user:123456",
|
|
8
|
-
aud: "convex-app",
|
|
9
|
-
name: "John Doe",
|
|
10
|
-
email: "john.doe@example.com",
|
|
11
|
-
// exp: Math.floor(Date.now() / 1000) - (2 * 60 * 60) // 2 hours from now
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const createToken = async () => {
|
|
15
|
-
const { privateJWK, privateKeyPEM } = getValues();
|
|
16
|
-
const token = await signJWT(payload, privateKeyPEM);
|
|
17
|
-
console.log('Generated JWT:', token);
|
|
18
|
-
|
|
19
|
-
// console.log('expited at:', new Date(payload.exp * 1000).toLocaleString());
|
|
20
|
-
return token;
|
|
21
|
-
}
|
|
22
|
-
const token = await createToken()
|
|
23
|
-
|
|
24
|
-
const decode = decodeJWT(token)
|
|
25
|
-
|
|
26
|
-
console.log('Decoded JWT:', decode);
|
|
27
|
-
|
|
28
|
-
const verify = async () => {
|
|
29
|
-
const { publicKeyPEM, privateJWK, jwks } = getValues();
|
|
30
|
-
try {
|
|
31
|
-
const verifiedPayload = await verifyJWT(token, publicKeyPEM);
|
|
32
|
-
console.log('Verified JWT payload:', verifiedPayload);
|
|
33
|
-
} catch (error) {
|
|
34
|
-
console.error('Verification failed:', error);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
await verify();
|