-
Notifications
You must be signed in to change notification settings - Fork 187
/
validation.ts
132 lines (112 loc) · 3.44 KB
/
validation.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
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
import { ALLOWED_EMAIL_DOMAINS, REGISTRATION_CUSTOM_FIELDS, MIN_PASSWORD_LENGTH } from './config'
import Joi from '@hapi/joi'
interface ExtendedStringSchema extends Joi.StringSchema {
allowedDomains(): this
}
interface ExtendedJoi extends Joi.Root {
string(): ExtendedStringSchema
}
const extendedJoi: ExtendedJoi = Joi.extend((joi) => ({
type: 'string',
base: joi.string(),
messages: {
'string.allowedDomains': '{{#label}} is not in an authorised domain'
},
rules: {
allowedDomains: {
method(): unknown {
return this.$_addRule({ name: 'allowedDomains' })
},
validate(value: string, helpers): unknown {
if (ALLOWED_EMAIL_DOMAINS) {
const lowerValue = value.toLowerCase()
const allowedEmailDomains = ALLOWED_EMAIL_DOMAINS.split(',')
if (allowedEmailDomains.every((domain) => !lowerValue.endsWith(domain.toLowerCase()))) {
return helpers.error('string.allowedDomains')
}
}
return value
}
}
}
}))
const passwordRule = Joi.string().min(MIN_PASSWORD_LENGTH).max(128).required()
const emailRule = extendedJoi.string().email().required().allowedDomains()
const accountFields = {
email: emailRule,
password: passwordRule
}
export const userDataFields = {
user_data: Joi.object(
REGISTRATION_CUSTOM_FIELDS.reduce<{ [k: string]: Joi.Schema[] }>(
(aggr, key) => ({
...aggr,
[key]: [
Joi.string(),
Joi.number(),
Joi.boolean(),
Joi.object(),
Joi.array().items(Joi.string(), Joi.number(), Joi.boolean(), Joi.object())
]
}),
{}
)
),
register_options: Joi.object({
allowed_roles: Joi.array().items(Joi.string()),
default_role: Joi.string()
})
}
export const registerSchema = Joi.object({
...accountFields,
...userDataFields,
cookie: Joi.boolean()
})
export const registerUserDataSchema = Joi.object(userDataFields)
const ticketFields = {
ticket: Joi.string().uuid({ version: 'uuidv4' }).required()
}
const codeFields = {
code: Joi.string().length(6).required()
}
export const resetPasswordWithTicketSchema = Joi.object({
...ticketFields,
new_password: passwordRule
})
export const changePasswordFromOldSchema = Joi.object({
old_password: passwordRule,
new_password: passwordRule
})
export const emailResetSchema = Joi.object({
new_email: emailRule
})
export const logoutSchema = Joi.object({
all: Joi.boolean()
})
export const mfaSchema = Joi.object(codeFields)
export const loginAnonymouslySchema = Joi.object({
anonymous: Joi.boolean(),
email: Joi.string(), // these will be checked more regeriously in `loginSchema`
password: Joi.string() // these will be checked more regeriously in `loginSchema`
})
export const loginSchema = extendedJoi.object({
...accountFields,
cookie: Joi.boolean()
})
export const forgotSchema = Joi.object({ email: emailRule })
export const verifySchema = Joi.object({ ...ticketFields })
export const totpSchema = Joi.object({
...codeFields,
...ticketFields,
cookie: Joi.boolean()
})
export const imgTransformParams = Joi.object({
w: Joi.number().integer().min(0).max(8192),
h: Joi.number().integer().min(0).max(8192),
q: Joi.number().integer().min(0).max(100).default(100),
token: Joi.string().uuid()
})
export const fileMetadataUpdate = Joi.object({
// action: Joi.string().valid('revoke-token','some-other-action').required(),
action: Joi.string().valid('revoke-token').required()
})