forked from zce/fearless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
65 lines (53 loc) · 1.88 KB
/
auth.ts
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
import { Router } from 'express'
import { users, tokens, uuid } from './data'
const router = Router()
const genToken = (): string => uuid() + uuid()
const accessTokenLifetime = 10 * 60 * 1000 // 10 minutes
router.post('/token', (req, res) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { grant_type, username, password, refresh_token } = req.body
if (grant_type === 'password') {
const user = users.find(u => u.username === username && u.password === password)
if (user == null) {
return res.status(401).send({ message: 'Bad credentials: invalid username or password' })
}
const token = {
access: genToken(),
refresh: genToken(),
expires: Date.now() + accessTokenLifetime,
userId: user.id
}
tokens.push(token)
res.send({
token_type: 'Bearer',
expires_in: (token.expires - Date.now()) / 1000,
access_token: token.access,
refresh_token: token.refresh
})
} else if (grant_type === 'refresh_token') {
const token = tokens.find(t => t.refresh === refresh_token)
if (token == null) {
return res.status(403).send({ message: 'Bad credentials: invalid refresh_token' })
}
token.access = genToken()
token.expires = Date.now() + accessTokenLifetime
res.send({
token_type: 'Bearer',
expires_in: (token.expires - Date.now()) / 1000,
access_token: token.access,
refresh_token: token.refresh
})
} else {
res.status(400).send({ message: 'Bad Request: invalid grant_type' })
}
})
router.delete('/token', (req, res) => {
const { token } = req.body
const tokenIndex = tokens.findIndex(t => t.refresh === token)
if (tokenIndex !== -1) {
tokens.splice(tokenIndex, 1)
}
res.send({ message: 'refresh_token revoked' })
// TODO: res.status(401).send({ message: 'Bad credentials: invalid refresh_token' })
})
export default router