@kevisual/auth 1.0.5-alpha.1 → 1.0.5

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.
@@ -13,6 +13,19 @@ declare const createToken: (user: {
13
13
  username: string;
14
14
  [key: string]: any;
15
15
  }, secret: string, expiresIn?: string) => Promise<string>;
16
+ /**
17
+ * check token
18
+ * @param token
19
+ * @param secret
20
+ * @returns
21
+ */
16
22
  declare const checkToken: (token: string, secret: string) => Promise<jwt.Jwt>;
23
+ /**
24
+ * check auth and return token user
25
+ * @param token
26
+ * @param secret
27
+ * @returns
28
+ */
29
+ declare const checkTokenUser: (token: string, secret: string) => Promise<string | jwt.JwtPayload>;
17
30
 
18
- export { checkToken, createToken };
31
+ export { checkToken, checkTokenUser, createToken };
@@ -6332,8 +6332,24 @@ const createToken = async (user, secret, expiresIn = '7d') => {
6332
6332
  expiresIn
6333
6333
  });
6334
6334
  };
6335
+ /**
6336
+ * check token
6337
+ * @param token
6338
+ * @param secret
6339
+ * @returns
6340
+ */
6335
6341
  const checkToken = async (token, secret) => {
6336
6342
  return jwt.verify(token, secret, { complete: true });
6337
6343
  };
6344
+ /**
6345
+ * check auth and return token user
6346
+ * @param token
6347
+ * @param secret
6348
+ * @returns
6349
+ */
6350
+ const checkTokenUser = async (token, secret) => {
6351
+ const result = await checkToken(token, secret);
6352
+ return result.payload;
6353
+ };
6338
6354
 
6339
- export { checkToken, createToken };
6355
+ export { checkToken, checkTokenUser, createToken };
package/dist/index.d.ts CHANGED
@@ -34,6 +34,12 @@ declare const createToken: (user: {
34
34
  username: string;
35
35
  [key: string]: any;
36
36
  }, secret: string, expiresIn?: string) => Promise<string>;
37
+ /**
38
+ * check token
39
+ * @param token
40
+ * @param secret
41
+ * @returns
42
+ */
37
43
  declare const checkToken: (token: string, secret: string) => Promise<jwt.Jwt>;
38
44
 
39
45
  type CreateAuthRouteOptions = {
@@ -7457,6 +7457,12 @@ const createToken = async (user, secret, expiresIn = '7d') => {
7457
7457
  expiresIn
7458
7458
  });
7459
7459
  };
7460
+ /**
7461
+ * check token
7462
+ * @param token
7463
+ * @param secret
7464
+ * @returns
7465
+ */
7460
7466
  const checkToken = async (token, secret) => {
7461
7467
  return jwt.verify(token, secret, { complete: true });
7462
7468
  };
@@ -0,0 +1,14 @@
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 ADDED
@@ -0,0 +1,28 @@
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 CHANGED
@@ -19,7 +19,8 @@ declare class AuthQuery {
19
19
  type AuthProxy = {
20
20
  queryMe: (token: string) => Promise<any>;
21
21
  /**
22
- * 代理设置token用户
22
+ * 代理设置token用户, 自己去处理ctx.state['tokenUser']
23
+ * 可能tokenUser的内容会有一些特殊的处理
23
24
  * @param ctx
24
25
  * @param data
25
26
  * @returns
@@ -18,7 +18,7 @@ class AuthQuery {
18
18
  path;
19
19
  constructor(opts) {
20
20
  this.host = opts?.host || 'localhost:114000';
21
- this.path = opts?.path || '/api/auth';
21
+ this.path = opts?.path || '/api/router';
22
22
  const protol = opts?.protol || 'http';
23
23
  this.url = `${protol}://${this.host}${this.path}`;
24
24
  }
@@ -51,7 +51,7 @@ const createAuthRoute = ({ app, addToApp = true, proxy }) => {
51
51
  const authRouteFn = async (ctx) => {
52
52
  const token = ctx.query.token;
53
53
  if (!token) {
54
- app.throw(401, 'Token is Unauthorized');
54
+ ctx.throw(401, 'Token is Unauthorized');
55
55
  }
56
56
  if (proxy?.cacheMe) {
57
57
  const cache = await proxy?.cacheMe?.(token);
@@ -67,21 +67,21 @@ const createAuthRoute = ({ app, addToApp = true, proxy }) => {
67
67
  }
68
68
  try {
69
69
  const result = await proxy?.queryMe?.(token);
70
- if (result.code === 200) {
71
- result.payload = result.data;
70
+ if (result.code !== 200) {
71
+ ctx.throw(result.code, result.message);
72
72
  }
73
73
  if (proxy?.setTokenUser) {
74
74
  await proxy?.setTokenUser?.(ctx, result.data);
75
75
  }
76
76
  else {
77
- ctx.state['tokenUser'] = result.payload;
77
+ ctx.state['tokenUser'] = result.data;
78
78
  }
79
79
  if (proxy?.setCahceMe) {
80
80
  await proxy?.setCahceMe?.(token, result.data);
81
81
  }
82
82
  }
83
83
  catch (e) {
84
- app.throw(401, 'Token is invalid');
84
+ ctx.throw(401, 'Token is invalid');
85
85
  }
86
86
  };
87
87
  let authRoute;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/auth",
3
- "version": "1.0.5-alpha.1",
3
+ "version": "1.0.5",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -20,9 +20,6 @@
20
20
  ],
21
21
  "author": "",
22
22
  "license": "ISC",
23
- "peerDependencies": {
24
- "@kevisual/router": ">=0.0.4"
25
- },
26
23
  "devDependencies": {
27
24
  "@rollup/plugin-commonjs": "^28.0.1",
28
25
  "@rollup/plugin-node-resolve": "^15.3.0",
@@ -31,7 +28,7 @@
31
28
  "@types/jsonwebtoken": "^9.0.7",
32
29
  "crypto-js": "^4.2.0",
33
30
  "jsonwebtoken": "^9.0.2",
34
- "rollup": "^4.27.2",
31
+ "rollup": "^4.28.1",
35
32
  "rollup-plugin-dts": "^6.1.1",
36
33
  "tslib": "^2.8.1"
37
34
  },
@@ -41,20 +38,24 @@
41
38
  },
42
39
  "exports": {
43
40
  ".": {
44
- "import": "./dist/index.js",
45
- "require": "./dist/index.js"
41
+ "import": "./dist/index.mjs",
42
+ "require": "./dist/index.mjs"
46
43
  },
47
44
  "./token": {
48
- "import": "./dist/create-token.js",
49
- "require": "./dist/create-token.js"
45
+ "import": "./dist/create-token.mjs",
46
+ "require": "./dist/create-token.mjs"
50
47
  },
51
48
  "./salt": {
52
- "import": "./dist/create-token.js",
53
- "require": "./dist/create-token.js"
49
+ "import": "./dist/create-token.mjs",
50
+ "require": "./dist/create-token.mjs"
54
51
  },
55
52
  "./proxy": {
56
- "import": "./dist/proxy.js",
57
- "require": "./dist/proxy.js"
53
+ "import": "./dist/proxy.mjs",
54
+ "require": "./dist/proxy.mjs"
55
+ },
56
+ "./is-me": {
57
+ "import": "./dist/is-me.mjs",
58
+ "require": "./dist/is-me.mjs"
58
59
  }
59
60
  },
60
61
  "publishConfig": {
@@ -24,6 +24,23 @@ export const createToken = async (
24
24
  });
25
25
  };
26
26
 
27
+ /**
28
+ * check token
29
+ * @param token
30
+ * @param secret
31
+ * @returns
32
+ */
27
33
  export const checkToken = async (token: string, secret: string) => {
28
34
  return jwt.verify(token, secret, { complete: true });
29
35
  };
36
+
37
+ /**
38
+ * check auth and return token user
39
+ * @param token
40
+ * @param secret
41
+ * @returns
42
+ */
43
+ export const checkTokenUser = async (token: string, secret: string) => {
44
+ const result = await checkToken(token, secret);
45
+ return result.payload;
46
+ };
package/src/is-me.ts ADDED
@@ -0,0 +1,32 @@
1
+ type MeAuthOptions = {
2
+ id?: string;
3
+ username?: string;
4
+ orgs?: string[];
5
+ };
6
+
7
+ /**
8
+ * check is me
9
+ * @param tokenUser
10
+ * @param opts
11
+ * @returns
12
+ */
13
+ export const isMe = async (tokenUser: any, opts: MeAuthOptions) => {
14
+ if (!opts.id && !opts.username && !opts.orgs) {
15
+ return false;
16
+ }
17
+ if (opts.id && tokenUser?.id === opts.id) {
18
+ return true;
19
+ }
20
+ if (opts.username && tokenUser?.username === opts.username) {
21
+ return true;
22
+ }
23
+ if (opts.orgs && tokenUser?.orgs) {
24
+ const orgs = tokenUser.orgs;
25
+ for (const org of opts.orgs) {
26
+ if (orgs.includes(org)) {
27
+ return true;
28
+ }
29
+ }
30
+ }
31
+ return false;
32
+ };
package/src/proxy.ts CHANGED
@@ -23,7 +23,7 @@ export class AuthQuery {
23
23
  path: string;
24
24
  constructor(opts?: AuthProxyOptions) {
25
25
  this.host = opts?.host || 'localhost:114000';
26
- this.path = opts?.path || '/api/auth';
26
+ this.path = opts?.path || '/api/router';
27
27
  const protol = opts?.protol || 'http';
28
28
  this.url = `${protol}://${this.host}${this.path}`;
29
29
  }
@@ -51,7 +51,8 @@ export class AuthQuery {
51
51
  type AuthProxy = {
52
52
  queryMe: (token: string) => Promise<any>;
53
53
  /**
54
- * 代理设置token用户
54
+ * 代理设置token用户, 自己去处理ctx.state['tokenUser']
55
+ * 可能tokenUser的内容会有一些特殊的处理
55
56
  * @param ctx
56
57
  * @param data
57
58
  * @returns
@@ -73,7 +74,7 @@ export const createAuthRoute = ({ app, addToApp = true, proxy }: CreateAuthRoute
73
74
  const authRouteFn = async (ctx: any) => {
74
75
  const token = ctx.query.token;
75
76
  if (!token) {
76
- app.throw(401, 'Token is Unauthorized');
77
+ ctx.throw(401, 'Token is Unauthorized');
77
78
  }
78
79
  if (proxy?.cacheMe) {
79
80
  const cache = await proxy?.cacheMe?.(token);
@@ -88,19 +89,19 @@ export const createAuthRoute = ({ app, addToApp = true, proxy }: CreateAuthRoute
88
89
  }
89
90
  try {
90
91
  const result = await proxy?.queryMe?.(token);
91
- if (result.code === 200) {
92
- result.payload = result.data;
92
+ if (result.code !== 200) {
93
+ ctx.throw(result.code, result.message);
93
94
  }
94
95
  if (proxy?.setTokenUser) {
95
96
  await proxy?.setTokenUser?.(ctx, result.data);
96
97
  } else {
97
- ctx.state['tokenUser'] = result.payload;
98
+ ctx.state['tokenUser'] = result.data;
98
99
  }
99
100
  if (proxy?.setCahceMe) {
100
101
  await proxy?.setCahceMe?.(token, result.data);
101
102
  }
102
103
  } catch (e) {
103
- app.throw(401, 'Token is invalid');
104
+ ctx.throw(401, 'Token is invalid');
104
105
  }
105
106
  };
106
107
  let authRoute;
File without changes