@kevisual/auth 1.0.5-alpha.1 → 2.0.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/dist/proxy.d.ts DELETED
@@ -1,41 +0,0 @@
1
- declare const proxyFetch: (url: string, data: any) => Promise<any>;
2
- type AuthProxyOptions = {
3
- host: string;
4
- path?: string;
5
- protol?: string;
6
- };
7
- /**
8
- * Auth Query 轻量级代理
9
- */
10
- declare class AuthQuery {
11
- url: string;
12
- host: string;
13
- path: string;
14
- constructor(opts?: AuthProxyOptions);
15
- queryMe(token: string): Promise<any>;
16
- login(username: string, password: string): Promise<any>;
17
- query(data: any): Promise<any>;
18
- }
19
- type AuthProxy = {
20
- queryMe: (token: string) => Promise<any>;
21
- /**
22
- * 代理设置token用户
23
- * @param ctx
24
- * @param data
25
- * @returns
26
- */
27
- setTokenUser?: (ctx: any, data: any) => Promise<any>;
28
- cacheMe: (token: string) => Promise<any>;
29
- setCahceMe: (token: string, data: any) => Promise<any>;
30
- };
31
- type CreateAuthRouteOptions = {
32
- app?: any;
33
- addToApp?: boolean;
34
- proxy: AuthProxy;
35
- };
36
- declare const createAuthRoute: ({ app, addToApp, proxy }: CreateAuthRouteOptions) => {
37
- route: any;
38
- authRouteFn: (ctx: any) => Promise<void>;
39
- };
40
-
41
- export { AuthQuery, createAuthRoute, proxyFetch };
package/dist/proxy.js DELETED
@@ -1,98 +0,0 @@
1
- const proxyFetch = async (url, data) => {
2
- const res = await fetch(url, {
3
- method: 'POST',
4
- headers: {
5
- 'Content-Type': 'application/json'
6
- },
7
- body: JSON.stringify(data)
8
- });
9
- const _res = await res.json();
10
- return _res;
11
- };
12
- /**
13
- * Auth Query 轻量级代理
14
- */
15
- class AuthQuery {
16
- url;
17
- host;
18
- path;
19
- constructor(opts) {
20
- this.host = opts?.host || 'localhost:114000';
21
- this.path = opts?.path || '/api/auth';
22
- const protol = opts?.protol || 'http';
23
- this.url = `${protol}://${this.host}${this.path}`;
24
- }
25
- async queryMe(token) {
26
- return proxyFetch(this.url, {
27
- path: 'user',
28
- key: 'me',
29
- token
30
- });
31
- }
32
- async login(username, password) {
33
- return proxyFetch(this.url, {
34
- path: 'user',
35
- key: 'login',
36
- data: {
37
- username,
38
- password
39
- }
40
- });
41
- }
42
- async query(data) {
43
- return proxyFetch(this.url, data);
44
- }
45
- }
46
- const createAuthRoute = ({ app, addToApp = true, proxy }) => {
47
- /**
48
- * 中间件执行函数
49
- * @param ctx
50
- */
51
- const authRouteFn = async (ctx) => {
52
- const token = ctx.query.token;
53
- if (!token) {
54
- app.throw(401, 'Token is Unauthorized');
55
- }
56
- if (proxy?.cacheMe) {
57
- const cache = await proxy?.cacheMe?.(token);
58
- if (cache) {
59
- if (proxy?.setTokenUser) {
60
- await proxy?.setTokenUser?.(ctx, cache);
61
- }
62
- else {
63
- ctx.state['tokenUser'] = cache;
64
- }
65
- return;
66
- }
67
- }
68
- try {
69
- const result = await proxy?.queryMe?.(token);
70
- if (result.code === 200) {
71
- result.payload = result.data;
72
- }
73
- if (proxy?.setTokenUser) {
74
- await proxy?.setTokenUser?.(ctx, result.data);
75
- }
76
- else {
77
- ctx.state['tokenUser'] = result.payload;
78
- }
79
- if (proxy?.setCahceMe) {
80
- await proxy?.setCahceMe?.(token, result.data);
81
- }
82
- }
83
- catch (e) {
84
- app.throw(401, 'Token is invalid');
85
- }
86
- };
87
- let authRoute;
88
- if (app) {
89
- authRoute = app.route('auth', '', { id: 'auth' });
90
- authRoute.run = authRouteFn;
91
- if (addToApp) {
92
- authRoute.addTo(app);
93
- }
94
- }
95
- return { route: authRoute, authRouteFn };
96
- };
97
-
98
- export { AuthQuery, createAuthRoute, proxyFetch };
package/dist/salt.d.ts DELETED
@@ -1,22 +0,0 @@
1
- /**
2
- * 生成随机盐
3
- * @returns
4
- */
5
- declare const getRandomSalt: () => string;
6
- /**
7
- * 加密密码
8
- * @param password
9
- * @param salt
10
- * @returns
11
- */
12
- declare const cryptPwd: (password: string, salt?: string) => string;
13
- /**
14
- * Check password
15
- * @param password
16
- * @param salt
17
- * @param md5
18
- * @returns
19
- */
20
- declare const checkPwd: (password: string, salt: string, md5: string) => boolean;
21
-
22
- export { checkPwd, cryptPwd, getRandomSalt };