Repository navigation
Expand file tree
/
Copy pathmiddleware.ts
More file actions
213 lines (181 loc) · 5.72 KB
/
Copy pathmiddleware.ts
File metadata and controls
213 lines (181 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { randomUUID } from 'crypto';
import type { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
import { logger, Logger } from 'kv-logger';
import _ from 'lodash';
import moment from 'moment';
import { Op } from 'sequelize';
import { UserTokens } from '../models/user_tokens';
import { Users, UsersInterface } from '../models/users';
import { AppError, Unauthorized } from './app-error';
import { config } from './config';
import { t } from './i18n';
import { shouldHideWebUI } from './utils/common';
import { parseToken, md5 } from './utils/security';
export type LocaleI18n = 'en' | 'ko' | 'zh';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface Req<P = Record<string, string>, B = any, Q = Record<string, string | string[]>>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
extends Request<P, any, B, Partial<Q>> {
users: UsersInterface;
logger: Logger;
lang?: LocaleI18n;
t?: (key: string, vars?: Record<string, any>) => string;
}
// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/no-explicit-any
export interface Res<B = any> extends Response<B> {}
/**
* bind logger to request
*/
export function withLogger(req: Req, res: Res, next: NextFunction) {
const { method, path, headers } = req;
const requestId = headers['x-request-id'] || randomUUID();
req.logger = logger.bindContext({
path,
method,
requestId,
});
res.header('X-Request-Id', requestId);
next();
}
async function checkAuthToken(authToken: string) {
const objToken = parseToken(authToken);
const users = await Users.findOne({
where: { identical: objToken.identical },
});
if (_.isEmpty(users)) {
throw new Unauthorized();
}
const tokenInfo = await UserTokens.findOne({
where: {
tokens: authToken,
uid: users.id,
expires_at: {
[Op.gt]: moment().format('YYYY-MM-DD HH:mm:ss'),
},
},
});
if (_.isEmpty(tokenInfo)) {
throw new Unauthorized();
}
return users;
}
async function checkAccessToken(accessToken: string) {
if (_.isEmpty(accessToken)) {
throw new Unauthorized();
}
let authData: { uid: number; hash: string };
try {
authData = jwt.verify(accessToken, config.jwt.tokenSecret) as {
uid: number;
hash: string;
};
} catch (e) {
throw new Unauthorized();
}
const { uid, hash } = authData;
if (uid <= 0) {
throw new Unauthorized();
}
const users = await Users.findOne({
where: { id: uid },
});
if (_.isEmpty(users)) {
throw new Unauthorized();
}
if (hash !== md5(users.get('ack_code'))) {
throw new Unauthorized();
}
return users;
}
/**
* check user token and bind user to request
*/
export function checkToken(req: Req, res: Res, next: NextFunction) {
// get token and type
let authType: 1 | 2 = 1;
let authToken = '';
const authArr = _.split(req.get('Authorization'), ' ');
if (authArr[0] === 'Bearer') {
[, authToken] = authArr; // Bearer
if (authToken && authToken.length > 64) {
authType = 2;
} else {
authType = 1;
}
} else if (authArr[0] === 'Basic') {
authType = 2;
const b = Buffer.from(authArr[1], 'base64');
const user = _.split(b.toString(), ':');
[, authToken] = user;
}
// do check token
let checkTokenResult: Promise<UsersInterface>;
if (authToken && authType === 1) {
checkTokenResult = checkAuthToken(authToken);
} else if (authToken && authType === 2) {
checkTokenResult = checkAccessToken(authToken);
} else {
res.send(new Unauthorized(`Auth type not supported.`));
return;
}
checkTokenResult
.then((users) => {
req.users = users;
next();
})
.catch((e) => {
if (e instanceof AppError) {
res.status(e.status || 404).send(e.message);
} else {
next(e);
}
});
}
function getLocaleFromReq(req: Req): LocaleI18n {
const langHeader = (req.headers['x-lang'] || req.headers['accept-language'] || '').toString();
if (langHeader.startsWith('ko')) {
return 'ko';
}
return 'en';
}
export function i18nMiddleware(req: Req, res: Res, next: NextFunction) {
const locale = getLocaleFromReq(req);
req.lang = locale;
req.t = (key: string, vars?: Record<string, any>) => t(locale, key, vars);
next();
}
export function webUiGuard(req: Req, res: Response, next: NextFunction) {
if (!shouldHideWebUI()) {
return next();
}
req.logger?.info?.('blocked web UI access', {
path: req.path,
method: req.method,
});
return res.status(405).send('Method Not Allowed');
}
export function ipWhitelistOnly(req: Req, res: Response, next: NextFunction) {
const whitelist = config.common.webUIWhitelist || [];
if (!Array.isArray(whitelist) || whitelist.length === 0) {
return next();
}
const defaultIp = req.ip;
const forwardedFor = req.headers['x-forwarded-for'];
const realIp = Array.isArray(forwardedFor)
? forwardedFor[0]
: forwardedFor?.split(',')[0]?.trim() || defaultIp;
const isAllowed = whitelist.includes(realIp);
if (!isAllowed) {
req.logger?.info?.('IP whitelist blocked', {
requestIp: realIp,
path: req.path,
});
return res.status(403).send('Forbidden');
}
req.logger?.info?.('IP whitelist allowed', {
requestIp: realIp,
path: req.path,
});
return next();
}