-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuser.ts
More file actions
401 lines (367 loc) · 9.66 KB
/
Copy pathuser.ts
File metadata and controls
401 lines (367 loc) · 9.66 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import { DbAuthorityKeys, DbUserDoc } from '../types/Database'
import { COLNAME } from './config'
import { attachUsers, router, unique } from './utils'
import * as crypto from 'crypto'
import { v4 as UUID } from 'uuid'
import { nanoid } from 'nanoid'
import {
passwordStrength,
defaultOptions as passwordStrengthOptions,
} from 'check-password-strength'
import { VercelRequest, VercelResponse } from '@vercel/node'
import { getPostModel } from './post'
/**
* @desc authority ```
* 0: Everyone
* 1: Member
* 2: Editor
* 3: Moderator
* 4: System Operator
* ```
*/
export const AUTHORITY_DEFAULTS: Record<DbAuthorityKeys, number> = {
post_create: 2,
post_delete_any: 3,
post_edit_any: 4,
comment_create: 0,
comment_delete_any: 3,
comment_edit_any: 4,
comment_protect: 3,
user_admin: 4,
user_block: 3,
user_group_edit: 4,
user_register: 0,
user_unblock_self: 4,
site_admin: 4,
}
export const TOKEN_COOKIE_NAME = 'BLOG_NOW_TOKEN'
export const USERDATA_DEFAULTS: DbUserDoc = {
allow_comment: true,
authority: 0,
avatar: '',
created_at: new Date(0),
gender: 'other',
nickname: '',
slogan: '',
title: '',
uid: -1,
username: '',
uuid: '',
// sensitive
email: '',
password_hash: '',
salt: '',
token: '',
token_expires: 0,
}
export const PASSWORD_STRENGTH: 0 | 1 | 2 | 3 = 1
export function getUserModel(
payload: Partial<DbUserDoc>,
removeSensitive?: boolean
) {
const data: DbUserDoc & { avatar: string; not_exist?: boolean } = {
...USERDATA_DEFAULTS,
...payload,
}
data.created_at = new Date(data.created_at)
// Handle avatar
const email_hash = crypto
.createHash('md5')
.update(data.email || '')
.digest('hex')
data.avatar = `https://gravatar.loli.net/avatar/${email_hash}`
if (data.uid < 0) data.not_exist = true
if (removeSensitive) {
delete data.email
delete data.password_hash
delete data.token
delete data.token_expires
delete data.salt
}
return data
}
// Utils
function getPasswordHash(salt: string, password: string) {
return crypto
.createHash('sha256')
.update(`salt=${salt},password=${password}`)
.digest('hex')
}
function trimUsername(str: string) {
return str
.replace(/^[\s_\-\.~]+/, '')
.replace(/[\s_\-\.~]+$/, '')
.replace(/\s+/g, ' ')
}
export default (req: VercelRequest, res: VercelResponse) => {
router.endpoint('/api/user')
router.setCollection(COLNAME.USER)
// Get single user
router
.addRoute()
.method('GET')
.path(['uuid', 'uid', 'username'], 'selector')
.path(/.+/, 'target')
.action(async (ctx) => {
const filter = {
[ctx.params.selector]:
ctx.params.selector === 'uid'
? parseInt(ctx.params.target)
: ctx.params.target,
}
const user = await ctx.col.findOne(filter)
if (!user) {
ctx.status = 404
ctx.message = 'User not found'
ctx.body = {
filter,
user: null,
}
return
}
ctx.status = 200
ctx.message = 'Get user by filter'
ctx.body = {
user: getUserModel(user, true),
filter,
}
})
// Get users list
router
.addRoute()
.method('GET')
.endpoint('/api/users')
.path(['uuid', 'uid', 'username'], 'selector')
.path(/.+/, 'rawList')
.action(async (ctx) => {
const list = unique(ctx.params.rawList.split(/[|,]/).map((i) => i.trim()))
if (list.length > 25) {
ctx.customBody = {
info: 'Too many requests, only the first 25 users are returned',
has_next: true,
next_uuids: list.slice(25 - list.length),
}
}
const find = {
$or: list.slice(0, 25).map((i) => ({
[ctx.params.selector]:
ctx.params.selector === 'uid' ? parseInt(i) : i,
})),
}
const users = await ctx.col.find(find).toArray()
ctx.message = 'Get users'
ctx.body = {
users: users.map((i) => getUserModel(i, true)),
uuids: list.slice(0, 25),
}
})
// Verify current user
router
.addRoute()
.method('GET')
.path('auth')
.path('profile')
.check((ctx) => {
if (!ctx.user.uuid || ctx.user.uid < 0) {
ctx.status = 401
ctx.message = 'Please login'
ctx.body = {
profile: getUserModel(ctx.user, true),
}
return false
}
})
.action(async (ctx) => {
ctx.body = {
profile: getUserModel(ctx.user, true),
}
})
router
.addRoute()
.method('POST')
.path('auth')
.path('register')
.check((ctx) => {
const { username, password } = ctx.req.body || {}
if (!username || !password) {
ctx.status = 400
ctx.message = 'Missing params'
return false
}
})
.check<{
username: string
password: string
}>((ctx) => {
let { username, password } = ctx.req.body || {}
// Trim username
username = trimUsername(username)
const usernameTest = /^[_\-\.~\s0-9A-Za-z\u0080-\uFFFF]+$/
if (!usernameTest.test(username) || username.length <= 5) {
ctx.status = 400
ctx.message = 'Invalid username.'
ctx.body = {
invalid_item: 'username',
item_standard: username,
item_test: false,
item_required: {
allowed_symbols: ['_', '-', '.', '~', ' '],
regexp: usernameTest.toString(),
min_length: 5,
},
}
return false
}
const passwordTest = passwordStrength(password)
if (passwordTest.id < PASSWORD_STRENGTH) {
ctx.status = 400
ctx.message = 'Password is too weak.'
ctx.body = {
invalid_item: 'password',
item_test: passwordTest,
item_required: passwordStrengthOptions[PASSWORD_STRENGTH],
}
return false
}
ctx.username = username
ctx.password = password
})
.check(async (ctx) => {
const { username } = ctx
const already = await ctx.col.findOne({
username: new RegExp(username, 'i'),
})
if (already) {
ctx.status = 409
ctx.message = 'Username has been taken'
return false
}
})
.action(async (ctx) => {
const { username, password } = ctx
const [lastUser] = (await ctx.col
.find()
.sort({ uid: -1 })
.project({ uid: 1 })
.limit(1)
.toArray()) as DbUserDoc[]
const uid = isNaN(lastUser?.uid)
? (await ctx.col.countDocuments()) + 10000
: lastUser.uid + 1
const salt = nanoid(32)
const insert: DbUserDoc = getUserModel({
authority: 1,
username,
uid,
uuid: UUID(),
password_hash: getPasswordHash(salt, password),
salt,
created_at: new Date(),
})
const dbRes = await ctx.col.insertOne(insert)
ctx.message = 'User created'
ctx.body = {
...dbRes,
username,
}
})
router
.addRoute()
.method('POST')
.path('auth')
.path(/(log-?in|sign-?in)/)
.check((ctx) => {
const { username, password } = ctx.req.body || {}
if (!username || !password) {
ctx.status = 400
ctx.message = 'Missing params'
return false
}
})
.action(async (ctx) => {
const { username, password } = ctx.req.body || {}
const profile = await ctx.col.findOne({
username,
})
if (!profile) {
ctx.status = 403
ctx.message = 'Invalid username'
return false
}
const password_hash = getPasswordHash(profile.salt, password)
if (password_hash !== profile.password_hash) {
ctx.status = 403
ctx.message = 'Invalid password'
return false
}
const token =
profile.token_expires - Date.now() < 0 ? nanoid(32) : profile.token
const token_expires = Date.now() + 7 * 24 * 60 * 60 * 1000
await ctx.col.updateOne(
{ uuid: profile.uuid },
{ $set: { token, token_expires } }
)
ctx.res.setHeader(
'set-cookie',
`${TOKEN_COOKIE_NAME}=${token}; expires=${new Date(
token_expires
).toUTCString()}; path=/`
)
ctx.body = { token, profile: getUserModel(profile, true) }
})
// Get user posts
router
.addRoute()
.method('GET')
.path(['uuid', 'uid', 'username'], 'selector')
.path(/.+/, 'target')
.path(/posts?/)
.check<{
offset: number
limit: number
}>((ctx) => {
ctx.offset = parseInt((ctx.req.query.offset as string) || '0')
ctx.limit = Math.min(
25,
parseInt((ctx.req.query.limit as string) || '10')
)
})
.check<{ author_uuid: string }>(async (ctx) => {
const user = (await ctx.db.collection(COLNAME.USER).findOne({
[ctx.params.selector]:
ctx.params.selector === 'uid'
? parseInt(ctx.params.target)
: ctx.params.target,
})) as DbUserDoc
if (!user) {
ctx.status = 404
ctx.message = 'Reqested user not found'
ctx.body = {
posts: [],
}
return false
}
ctx.author_uuid = user.uuid
})
.action(async (ctx) => {
const posts = await ctx.db
.collection(COLNAME.POST)
.find({ author_uuid: ctx.author_uuid })
.sort({ pid: -1 })
.skip(ctx.offset)
.limit(ctx.limit)
.toArray()
let has_next = false
if (posts.length > ctx.limit) {
has_next = true
posts.pop()
}
ctx.body = {
posts: await attachUsers(ctx, posts.map(getPostModel)),
has_next,
limit: ctx.limit,
offset: ctx.offset,
}
})
return router.init(req, res)
}