-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathregister.js
More file actions
96 lines (86 loc) · 2.35 KB
/
register.js
File metadata and controls
96 lines (86 loc) · 2.35 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
import { authRegisterPost } from '@rctf/api-types/routes'
import { makeFastifyRoute } from '../helpers'
import { v4 as uuidv4 } from 'uuid'
import emailValidator from 'email-validator'
import * as cache from '../../cache'
import * as util from '../../util'
import * as auth from '../../auth'
import config from '../../config/server'
import { getUserByNameOrEmail } from '../../database/users'
import { sendVerification } from '../../email'
export default makeFastifyRoute(authRegisterPost, async ({ req, res }) => {
if (!config.registrationsEnabled) {
return res.badRegistrationsDisabled()
}
let email
let ctftimeId
if (req.body.ctftimeToken !== undefined) {
const ctftimeData = await auth.token.getData(
auth.token.tokenKinds.ctftimeAuth,
req.body.ctftimeToken
)
if (ctftimeData === null) {
return res.badCtftimeToken()
}
ctftimeId = ctftimeData.ctftimeId
} else {
email = util.normalize.normalizeEmail(req.body.email)
if (!emailValidator.validate(email)) {
return res.badEmail()
}
}
const name = util.normalize.normalizeName(req.body.name)
if (!util.validate.validateName(name)) {
return res.badName()
}
if (!config.email) {
const division = config.defaultDivision || Object.keys(config.divisions)[0]
return auth.register.register(
{
division,
email,
name,
ctftimeId,
},
res
)
}
const division = config.divisionACLs
? util.restrict.allowedDivisions(email)[0]
: config.defaultDivision || Object.keys(config.divisions)[0]
if (division === undefined) {
return res.badCompetitionNotAllowed()
}
if (req.body.ctftimeToken !== undefined) {
return auth.register.register(
{
division,
name,
ctftimeId,
},
res
)
}
const conflictRes = await getUserByNameOrEmail({ name, email })
if (conflictRes) {
if (conflictRes.email === email) {
return res.badKnownEmail()
}
return res.badKnownName()
}
const verifyUuid = uuidv4()
await cache.login.makeLogin({ id: verifyUuid })
const verifyToken = await auth.token.getToken(auth.token.tokenKinds.verify, {
verifyId: verifyUuid,
kind: 'register',
email,
name,
division,
})
await sendVerification({
email,
kind: 'register',
token: verifyToken,
})
return res.goodVerifySent()
})