@kevisual/auth 1.0.5 → 2.0.1

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/is-me.d.ts DELETED
@@ -1,14 +0,0 @@
1
- type MeAuthOptions = {
2
- id?: string;
3
- username?: string;
4
- orgs?: string[];
5
- };
6
- /**
7
- * check is me
8
- * @param tokenUser
9
- * @param opts
10
- * @returns
11
- */
12
- declare const isMe: (tokenUser: any, opts: MeAuthOptions) => Promise<boolean>;
13
-
14
- export { isMe };
package/dist/is-me.mjs DELETED
@@ -1,28 +0,0 @@
1
- /**
2
- * check is me
3
- * @param tokenUser
4
- * @param opts
5
- * @returns
6
- */
7
- const isMe = async (tokenUser, opts) => {
8
- if (!opts.id && !opts.username && !opts.orgs) {
9
- return false;
10
- }
11
- if (opts.id && tokenUser?.id === opts.id) {
12
- return true;
13
- }
14
- if (opts.username && tokenUser?.username === opts.username) {
15
- return true;
16
- }
17
- if (opts.orgs && tokenUser?.orgs) {
18
- const orgs = tokenUser.orgs;
19
- for (const org of opts.orgs) {
20
- if (orgs.includes(org)) {
21
- return true;
22
- }
23
- }
24
- }
25
- return false;
26
- };
27
-
28
- export { isMe };
package/dist/proxy.d.ts DELETED
@@ -1,42 +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用户, 自己去处理ctx.state['tokenUser']
23
- * 可能tokenUser的内容会有一些特殊的处理
24
- * @param ctx
25
- * @param data
26
- * @returns
27
- */
28
- setTokenUser?: (ctx: any, data: any) => Promise<any>;
29
- cacheMe: (token: string) => Promise<any>;
30
- setCahceMe: (token: string, data: any) => Promise<any>;
31
- };
32
- type CreateAuthRouteOptions = {
33
- app?: any;
34
- addToApp?: boolean;
35
- proxy: AuthProxy;
36
- };
37
- declare const createAuthRoute: ({ app, addToApp, proxy }: CreateAuthRouteOptions) => {
38
- route: any;
39
- authRouteFn: (ctx: any) => Promise<void>;
40
- };
41
-
42
- export { AuthQuery, createAuthRoute, proxyFetch };
package/dist/proxy.mjs 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/router';
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
- ctx.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
- ctx.throw(result.code, result.message);
72
- }
73
- if (proxy?.setTokenUser) {
74
- await proxy?.setTokenUser?.(ctx, result.data);
75
- }
76
- else {
77
- ctx.state['tokenUser'] = result.data;
78
- }
79
- if (proxy?.setCahceMe) {
80
- await proxy?.setCahceMe?.(token, result.data);
81
- }
82
- }
83
- catch (e) {
84
- ctx.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 };