@kevisual/auth 1.0.5 → 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/bun.config.ts +20 -0
- package/package.json +18 -51
- package/readme.md +45 -18
- package/src/auth.ts +123 -0
- package/src/generate.ts +39 -0
- package/src/index.ts +1 -6
- package/src/jwks/common.ts +8 -0
- package/src/jwks/create.ts +23 -0
- package/src/jwks/get.ts +22 -0
- package/src/router.ts +2 -0
- package/test/create.ts +38 -0
- package/dist/create-token.d.ts +0 -31
- package/dist/create-token.mjs +0 -6355
- package/dist/index.d.ts +0 -55
- package/dist/index.mjs +0 -7512
- package/dist/is-me.d.ts +0 -14
- package/dist/is-me.mjs +0 -28
- package/dist/proxy.d.ts +0 -42
- package/dist/proxy.mjs +0 -98
- package/dist/salt.d.ts +0 -22
- package/dist/salt.mjs +0 -1130
- package/src/create-token.ts +0 -46
- package/src/is-me.ts +0 -32
- package/src/proxy.ts +0 -116
- package/src/route.ts +0 -44
- package/src/salt.ts +0 -32
package/src/create-token.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import jwt from 'jsonwebtoken';
|
|
2
|
-
|
|
3
|
-
// https://www.robinwieruch.de/graphql-apollo-server-tutorial#apollo-server-authentication
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
6
|
-
* @param user
|
|
7
|
-
* @param secret
|
|
8
|
-
*
|
|
9
|
-
* @param expiresIn default 7d expressed in seconds or a string describing a time span [zeit/ms](https://github.com/zeit/ms.js). Eg: 60, "2 days", "10h", "7d"
|
|
10
|
-
* @returns
|
|
11
|
-
*/
|
|
12
|
-
export const createToken = async (
|
|
13
|
-
user: {
|
|
14
|
-
id: string;
|
|
15
|
-
username: string;
|
|
16
|
-
[key: string]: any;
|
|
17
|
-
},
|
|
18
|
-
secret: string,
|
|
19
|
-
expiresIn = '7d'
|
|
20
|
-
) => {
|
|
21
|
-
const { id, username, ...rest } = user;
|
|
22
|
-
return jwt.sign({ id, username, ...rest }, secret, {
|
|
23
|
-
expiresIn
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* check token
|
|
29
|
-
* @param token
|
|
30
|
-
* @param secret
|
|
31
|
-
* @returns
|
|
32
|
-
*/
|
|
33
|
-
export const checkToken = async (token: string, secret: string) => {
|
|
34
|
-
return jwt.verify(token, secret, { complete: true });
|
|
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
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
export const proxyFetch = async (url: string, data: any) => {
|
|
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
|
-
type AuthProxyOptions = {
|
|
13
|
-
host: string;
|
|
14
|
-
path?: string;
|
|
15
|
-
protol?: string;
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Auth Query 轻量级代理
|
|
19
|
-
*/
|
|
20
|
-
export class AuthQuery {
|
|
21
|
-
url: string;
|
|
22
|
-
host: string;
|
|
23
|
-
path: string;
|
|
24
|
-
constructor(opts?: AuthProxyOptions) {
|
|
25
|
-
this.host = opts?.host || 'localhost:114000';
|
|
26
|
-
this.path = opts?.path || '/api/router';
|
|
27
|
-
const protol = opts?.protol || 'http';
|
|
28
|
-
this.url = `${protol}://${this.host}${this.path}`;
|
|
29
|
-
}
|
|
30
|
-
async queryMe(token: string) {
|
|
31
|
-
return proxyFetch(this.url, {
|
|
32
|
-
path: 'user',
|
|
33
|
-
key: 'me',
|
|
34
|
-
token
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
async login(username: string, password: string) {
|
|
38
|
-
return proxyFetch(this.url, {
|
|
39
|
-
path: 'user',
|
|
40
|
-
key: 'login',
|
|
41
|
-
data: {
|
|
42
|
-
username,
|
|
43
|
-
password
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
async query(data: any) {
|
|
48
|
-
return proxyFetch(this.url, data);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
type AuthProxy = {
|
|
52
|
-
queryMe: (token: string) => Promise<any>;
|
|
53
|
-
/**
|
|
54
|
-
* 代理设置token用户, 自己去处理ctx.state['tokenUser']
|
|
55
|
-
* 可能tokenUser的内容会有一些特殊的处理
|
|
56
|
-
* @param ctx
|
|
57
|
-
* @param data
|
|
58
|
-
* @returns
|
|
59
|
-
*/
|
|
60
|
-
setTokenUser?: (ctx: any, data: any) => Promise<any>;
|
|
61
|
-
cacheMe: (token: string) => Promise<any>;
|
|
62
|
-
setCahceMe: (token: string, data: any) => Promise<any>;
|
|
63
|
-
};
|
|
64
|
-
type CreateAuthRouteOptions = {
|
|
65
|
-
app?: any;
|
|
66
|
-
addToApp?: boolean;
|
|
67
|
-
proxy: AuthProxy;
|
|
68
|
-
};
|
|
69
|
-
export const createAuthRoute = ({ app, addToApp = true, proxy }: CreateAuthRouteOptions) => {
|
|
70
|
-
/**
|
|
71
|
-
* 中间件执行函数
|
|
72
|
-
* @param ctx
|
|
73
|
-
*/
|
|
74
|
-
const authRouteFn = async (ctx: any) => {
|
|
75
|
-
const token = ctx.query.token;
|
|
76
|
-
if (!token) {
|
|
77
|
-
ctx.throw(401, 'Token is Unauthorized');
|
|
78
|
-
}
|
|
79
|
-
if (proxy?.cacheMe) {
|
|
80
|
-
const cache = await proxy?.cacheMe?.(token);
|
|
81
|
-
if (cache) {
|
|
82
|
-
if (proxy?.setTokenUser) {
|
|
83
|
-
await proxy?.setTokenUser?.(ctx, cache);
|
|
84
|
-
} else {
|
|
85
|
-
ctx.state['tokenUser'] = cache;
|
|
86
|
-
}
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
try {
|
|
91
|
-
const result = await proxy?.queryMe?.(token);
|
|
92
|
-
if (result.code !== 200) {
|
|
93
|
-
ctx.throw(result.code, result.message);
|
|
94
|
-
}
|
|
95
|
-
if (proxy?.setTokenUser) {
|
|
96
|
-
await proxy?.setTokenUser?.(ctx, result.data);
|
|
97
|
-
} else {
|
|
98
|
-
ctx.state['tokenUser'] = result.data;
|
|
99
|
-
}
|
|
100
|
-
if (proxy?.setCahceMe) {
|
|
101
|
-
await proxy?.setCahceMe?.(token, result.data);
|
|
102
|
-
}
|
|
103
|
-
} catch (e) {
|
|
104
|
-
ctx.throw(401, 'Token is invalid');
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
let authRoute;
|
|
108
|
-
if (app) {
|
|
109
|
-
authRoute = app.route('auth', '', { id: 'auth' });
|
|
110
|
-
authRoute.run = authRouteFn;
|
|
111
|
-
if (addToApp) {
|
|
112
|
-
authRoute.addTo(app);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return { route: authRoute, authRouteFn };
|
|
116
|
-
};
|
package/src/route.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { checkToken } from './create-token.ts';
|
|
2
|
-
|
|
3
|
-
type CreateAuthRouteOptions = {
|
|
4
|
-
secret: string;
|
|
5
|
-
app?: any;
|
|
6
|
-
addToApp?: boolean;
|
|
7
|
-
};
|
|
8
|
-
export const createAuthRoute = ({ secret, app, addToApp = true }: CreateAuthRouteOptions) => {
|
|
9
|
-
/**
|
|
10
|
-
* 中间件执行函数
|
|
11
|
-
* @param ctx
|
|
12
|
-
*/
|
|
13
|
-
const authRouteFn = async (ctx: any) => {
|
|
14
|
-
const token = ctx.query.token;
|
|
15
|
-
if (!token) {
|
|
16
|
-
// throw new CustomError(401, 'Token is Unauthorized');
|
|
17
|
-
app.throw(401, 'Token is Unauthorized');
|
|
18
|
-
}
|
|
19
|
-
try {
|
|
20
|
-
const result = await checkToken(token, secret);
|
|
21
|
-
ctx.state['tokenUser'] = result.payload;
|
|
22
|
-
} catch (e) {
|
|
23
|
-
if (e.name === 'TokenExpiredError') {
|
|
24
|
-
app.throw(401, 'Token is expired');
|
|
25
|
-
} else if (e.name === 'JsonWebTokenError') {
|
|
26
|
-
app.throw(401, 'Token is invalid');
|
|
27
|
-
} else if (e.name === 'NotBeforeError') {
|
|
28
|
-
app.throw(401, 'Token is not active');
|
|
29
|
-
} else {
|
|
30
|
-
console.error('checkToken error', e);
|
|
31
|
-
app.throw(401, 'Token is invalid');
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
let authRoute;
|
|
36
|
-
if (app) {
|
|
37
|
-
authRoute = app.route('auth', '', { id: 'auth' });
|
|
38
|
-
authRoute.run = authRouteFn;
|
|
39
|
-
if (addToApp) {
|
|
40
|
-
authRoute.addTo(app);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return { route: authRoute, authRouteFn };
|
|
44
|
-
};
|
package/src/salt.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import MD5 from 'crypto-js/md5.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 生成随机盐
|
|
5
|
-
* @returns
|
|
6
|
-
*/
|
|
7
|
-
export const getRandomSalt = () => {
|
|
8
|
-
return Math.random().toString().slice(2, 7);
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 加密密码
|
|
13
|
-
* @param password
|
|
14
|
-
* @param salt
|
|
15
|
-
* @returns
|
|
16
|
-
*/
|
|
17
|
-
export const cryptPwd = (password: string, salt = '') => {
|
|
18
|
-
const saltPassword = password + ':' + salt;
|
|
19
|
-
const md5 = MD5(saltPassword);
|
|
20
|
-
return md5.toString();
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Check password
|
|
25
|
-
* @param password
|
|
26
|
-
* @param salt
|
|
27
|
-
* @param md5
|
|
28
|
-
* @returns
|
|
29
|
-
*/
|
|
30
|
-
export const checkPwd = (password: string, salt: string, md5: string) => {
|
|
31
|
-
return cryptPwd(password, salt) === md5;
|
|
32
|
-
};
|