-
Notifications
You must be signed in to change notification settings - Fork 188
/
validation.ts
170 lines (145 loc) · 4.45 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
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
import { REGISTRATION } 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 (REGISTRATION.ALLOWED_EMAIL_DOMAINS) {
const lowerValue = value.toLowerCase()
const allowedEmailDomains = REGISTRATION.ALLOWED_EMAIL_DOMAINS.split(',')
if (allowedEmailDomains.every((domain) => !lowerValue.endsWith(domain.toLowerCase()))) {
return helpers.error('string.allowedDomains')
}
}
return value
}
}
}
}))
const passwordRule = Joi.string().min(REGISTRATION.MIN_PASSWORD_LENGTH).max(128)
const passwordRuleRequired = passwordRule.required()
const emailRule = extendedJoi.string().email().required().allowedDomains()
const accountFields = {
email: emailRule,
password: passwordRuleRequired
}
const accountFieldsMagicLink = {
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 getRegisterSchema = () => {
return registerSchema
}
export const registerSchemaMagicLink = Joi.object({
...accountFieldsMagicLink,
...userDataFields,
cookie: Joi.boolean()
})
export const getRegisterSchemaMagicLink = () => {
return registerSchemaMagicLink
}
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 rigorously in `loginSchema`
password: Joi.string() // these will be checked more rigorously in `loginSchema`
})
export const magicLinkLoginAnonymouslySchema = Joi.object({
anonymous: Joi.boolean(),
email: Joi.string() // these will be checked more rigorously in `loginSchema`
})
export const loginSchema = extendedJoi.object({
email: emailRule,
password: Joi.string().required(),
cookie: Joi.boolean()
})
export const loginSchemaMagicLink = extendedJoi.object({
email: emailRule,
password: Joi.string(),
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),
b: Joi.number().integer().min(0.3).max(1000),
r: Joi.alternatives().try(Joi.number(), Joi.string().valid('full')),
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()
})
export const magicLinkQuery = Joi.object({
token: Joi.string().required(),
action: Joi.string().valid('log-in', 'register').required(),
cookie: Joi.boolean().optional()
})