/
IUser.js
312 lines (284 loc) · 9.52 KB
/
IUser.js
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
const { logger } = require('../log')
const config = require('../config')
const { prisma, formatInputData } = require('../lib/prisma')
const crypto = require('../lib/crypto.js')
const uuidgen = require('uuid')
const httpError = require('http-errors')
const { onFail, renameKeys } = require('../lib/utils')
const R = require('ramda')
const Joi = require('@hapi/joi')
const { redisClient } = require('../lib/redis')
const CacheFirstStrategy = require('../lib/prisma-cache/src/cache-first-strategy')
//* *****************************************************************************
// Fragments for how the data should be returned from Prisma.
//* *****************************************************************************
const fragments = {
internal: `fragment InternalUser on User {
id
username
email
emailVerified
lastVerifiedEmail
passwordHash
role
company {
id
}
}`,
basic: `fragment BasicUser on User {
id
username
email
role
company {
id
}
}`,
profile: `fragment UserProfile on User {
id
username
email
emailVerified
role
company {
id
name
type
}
}`,
userEmail: `fragment UserEmail on User {
email
}`,
emailVerification: `fragment BasicEmailVerification on EmailVerification {
id
uuid
email
changeRequested
user {
id
}
}`
}
// ******************************************************************************
// Database Client
// ******************************************************************************
const DB = new CacheFirstStrategy({
name: 'user',
pluralName: 'users',
fragments,
defaultFragmentKey: 'basic',
prisma,
redis: redisClient,
log: logger.info.bind(logger)
})
// *********************************************************************
// Helpers
// *********************************************************************
const userCreateSchema = Joi.object().keys({
username: Joi.string().required(),
password: Joi.string().required(),
companyId: Joi.string().required(),
role: Joi.string(),
email: Joi.string().email({ minDomainSegments: 2 }).when('role', { is: 'ADMIN', then: Joi.required(), otherwise: Joi.optional() })
})
const renameQueryKeys = renameKeys({ search: 'username_contains' })
async function retrieveEmailVerification (uuid) {
const ev = await onFail(400, () => prisma.emailVerification({ uuid }).$fragment(fragments.emailVerification))
if (!ev) throw httpError(404, 'Email verification token not found')
return ev
}
function deleteEmailVerification (uuid) {
return onFail(400, () => prisma.deleteEmailVerification({ uuid }))
}
// Starts the process of verifying email. We assume the email address has
// already been updated by the caller, with the old email saved and the new
// email set in the database, and emailVerified is set to false (otherwise we
// get into this whole big chicken-and-the-egg problem, trying to update the
// same record). Sends email to old and new email addresses. New can accept or
// reject the change, old can reject. Emails include links to accept/reject.
//
// userId - the id of the user to sent the email for
// username - the user's username (for emails)
// newemail - the new email address for this user
// oldemail - the previous email address for this user, if any
// urlRoot - the root URL for verification/rejection links
//
// Returns a promise that handles the email verification.
async function emailVerify (userId, username, newemail, oldemail, urlRoot) {
var data = formatInputData({
// Need a UUID for the verification record.
uuid: uuidgen.v4(),
userId,
email: newemail,
// timestamp
changeRequested: new Date().toISOString()
})
try {
const record = await prisma.createEmailVerification(data)
// Sending verification emails.
this.modelAPI.emails.verifyEmail(oldemail,
newemail,
username,
urlRoot,
data.uuid)
return record
}
catch (err) {
logger.error('Failed to write emailVerification record: ', err)
throw err
}
}
const dropInternalProps = R.omit(['passwordHash', 'emailVerified', 'lastVerifiedEmail'])
// *********************************************************************
// Model
// *********************************************************************
module.exports = class User {
constructor (modelAPI) {
this.modelAPI = modelAPI
}
async init () {
// Expire records every 24 hours.
// (24 hours * 60 minutes * 60 seconds * 1000 milliseconds)
setInterval(this.expireEmailVerify.bind(this), 24 * 60 * 60 * 1000)
// Run expiration code at startup as well.
this.expireEmailVerify()
}
load (id, fragment = 'basic') {
return DB.load({ id }, { fragment })
}
loadByUsername (username, fragment = 'basic') {
return DB.load({ username }, { fragment })
}
async list (query = {}, opts) {
return DB.list(renameQueryKeys(query), opts)
}
async create (data) {
// Check payload against Joi schema
let { error } = Joi.validate(data, userCreateSchema)
if (error) throw httpError(400, error.message)
// Verify password passes company password policy
await this.modelAPI.passwordPolicies.validatePassword(data.companyId, data.password)
let user
try {
data.passwordHash = await crypto.hashPassword(data.password)
delete data.password
user = await DB.create(data, { fragment: 'profile' })
}
catch (err) {
throw httpError(400, err.message)
}
if (user.email) {
try {
await emailVerify.call(this, user.id, user.username, user.email, null, config.base_url)
}
catch (err) {
// Log error, but resolve anyway.
logger.error('Error starting email verification:', err)
}
}
return dropInternalProps(user)
}
async update ({ id, ...data }) {
// MUST have at least an ID field in the passed update record.
if (!id) throw httpError(400, 'No existing user ID')
let originalUser = await this.load(id)
if (data.password) {
const companyId = data.companyId || data.company ? data.company.id : originalUser.company.id
// Validate the password.
await this.modelAPI.passwordPolicies.validatePassword(companyId, data.password)
data.passwordHash = await crypto.hashPassword(data.password)
delete data.password
}
if (data.role === 'ADMIN' && !(data.email || originalUser.email)) {
throw httpError(400, 'Invalid change to ADMIN without an email')
}
function setEmailProps ({ email, ...data }) {
if (email === originalUser.email) return data
const result = { ...data, emailVerified: false }
if (originalUser.emailVerified) {
result.lastVerifiedEmail = originalUser.email
}
return result
}
if (data.email) data = setEmailProps(data)
let user = await DB.update({ id }, data, { fragment: 'internal' })
if (data.email) {
try {
await emailVerify(
user.id,
user.username,
user.email,
user.lastVerifiedEmail,
config.base_url
)
}
catch (err) {
logger.error('Error starting email verification:', err)
}
}
return dropInternalProps(user)
}
remove (id) {
return DB.remove({ id })
}
// We run this "periodically" to delete old emailVerify records. We user
// the handler code above with a "reject" type and an "expired" source so
// the handling and reporting code is in one place.
async expireEmailVerify () {
logger.info('Running expiration of email verify records')
// 14 day limit on email changes.
var timedOut = new Date()
// Move now back 14 days.
timedOut.setDate(timedOut.getDate() - 14)
const where = { changeRequested_lt: timedOut.toISOString() }
const records = await prisma.emailVerifications({ where })
logger.info(records.length + ' email verify records to be expired')
return Promise.all(
records.map(x => this.handleEmailVerifyResponse(x.uuid, 'reject', 'expired verification request'))
)
}
async authorizeUser (username, password) {
try {
const user = await this.loadByUsername(username, 'internal')
const matches = await crypto.verifyPassword(password, user.passwordHash)
if (!matches) throw new Error(`authorizeUser: passwords don't match username "${username}`)
return dropInternalProps(user)
}
catch (err) {
// don't specify whether or not username is valid
throw new httpError.Unauthorized()
}
}
async handleEmailVerifyResponse (uuid, type, source) {
const ev = await retrieveEmailVerification(uuid)
const user = await this.load(ev.user.id)
let userUpdate = { id: user.id }
if (type === 'accept') {
userUpdate.emailVerified = true
}
else {
this.modelAPI.emails.notifyAdminsAboutReject(user, source)
Object.assign(userUpdate, {
email: user.lastVerifiedEmail,
lastVerifiedEmail: '',
emailVerified: true
})
}
try {
await this.update(userUpdate)
await deleteEmailVerification(uuid)
}
catch (err) {
logger.error('Update for EmailVerify failed. UUID = ' + uuid +
', type = ' + type +
', source = ' + source +
': ', err)
throw httpError.InternalServerError()
}
}
async getCompanyAdmins (companyId) {
const where = { company: { id: companyId } }
const users = await prisma.users({ where }).$fragment(fragments.userEmail)
return users.map(x => x.email).filter(R.identity)
}
}