1 | 'use strict'
|
2 |
|
3 | const AuthRequest = require('./auth-request')
|
4 | const WebIdTlsCertificate = require('../models/webid-tls-certificate')
|
5 | const debug = require('../debug').accounts
|
6 | const blacklistService = require('../services/blacklist-service')
|
7 | const { isValidUsername } = require('../common/user-utils')
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | class CreateAccountRequest extends AuthRequest {
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | constructor (options) {
|
36 | super(options)
|
37 |
|
38 | this.username = options.username
|
39 | this.userAccount = options.userAccount
|
40 | this.acceptToc = options.acceptToc
|
41 | this.disablePasswordChecks = options.disablePasswordChecks
|
42 | }
|
43 |
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | static fromParams (req, res) {
|
58 | let options = AuthRequest.requestOptions(req, res)
|
59 |
|
60 | let locals = req.app.locals
|
61 | let authMethod = locals.authMethod
|
62 | let accountManager = locals.accountManager
|
63 |
|
64 | let body = req.body || {}
|
65 |
|
66 | if (body.username) {
|
67 | options.username = body.username.toLowerCase()
|
68 | options.userAccount = accountManager.userAccountFrom(body)
|
69 | }
|
70 |
|
71 | options.enforceToc = locals.enforceToc
|
72 | options.tocUri = locals.tocUri
|
73 | options.disablePasswordChecks = locals.disablePasswordChecks
|
74 |
|
75 | switch (authMethod) {
|
76 | case 'oidc':
|
77 | options.password = body.password
|
78 | return new CreateOidcAccountRequest(options)
|
79 | case 'tls':
|
80 | options.spkac = body.spkac
|
81 | return new CreateTlsAccountRequest(options)
|
82 | default:
|
83 | throw new TypeError('Unsupported authentication scheme')
|
84 | }
|
85 | }
|
86 |
|
87 | static async post (req, res) {
|
88 | let request = CreateAccountRequest.fromParams(req, res)
|
89 |
|
90 | try {
|
91 | request.validate()
|
92 | await request.createAccount()
|
93 | } catch (error) {
|
94 | request.error(error, req.body)
|
95 | }
|
96 | }
|
97 |
|
98 | static get (req, res) {
|
99 | let request = CreateAccountRequest.fromParams(req, res)
|
100 |
|
101 | return Promise.resolve()
|
102 | .then(() => request.renderForm())
|
103 | .catch(error => request.error(error))
|
104 | }
|
105 |
|
106 | |
107 |
|
108 |
|
109 | renderForm (error, data = {}) {
|
110 | let authMethod = this.accountManager.authMethod
|
111 |
|
112 | let params = Object.assign({}, this.authQueryParams, {
|
113 | enforceToc: this.enforceToc,
|
114 | loginUrl: this.loginUrl(),
|
115 | multiuser: this.accountManager.multiuser,
|
116 | registerDisabled: authMethod === 'tls',
|
117 | returnToUrl: this.returnToUrl,
|
118 | tocUri: this.tocUri,
|
119 | disablePasswordChecks: this.disablePasswordChecks,
|
120 | username: data.username,
|
121 | name: data.name,
|
122 | email: data.email,
|
123 | externalWebId: data.externalWebId,
|
124 | acceptToc: data.acceptToc,
|
125 | connectExternalWebId: data.connectExternalWebId
|
126 | })
|
127 |
|
128 | if (error) {
|
129 | params.error = error.message
|
130 | this.response.status(error.statusCode)
|
131 | }
|
132 |
|
133 | this.response.render('account/register', params)
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | async createAccount () {
|
144 | let userAccount = this.userAccount
|
145 | let accountManager = this.accountManager
|
146 |
|
147 | this.cancelIfUsernameInvalid(userAccount)
|
148 | this.cancelIfBlacklistedUsername(userAccount)
|
149 | await this.cancelIfAccountExists(userAccount)
|
150 | await this.createAccountStorage(userAccount)
|
151 | await this.saveCredentialsFor(userAccount)
|
152 | await this.sendResponse(userAccount)
|
153 |
|
154 |
|
155 | if (userAccount && userAccount.email) {
|
156 | debug('Sending Welcome email')
|
157 | accountManager.sendWelcomeEmail(userAccount)
|
158 | }
|
159 |
|
160 | return userAccount
|
161 | }
|
162 |
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | cancelIfAccountExists (userAccount) {
|
172 | let accountManager = this.accountManager
|
173 |
|
174 | return accountManager.accountExists(userAccount.username)
|
175 | .then(exists => {
|
176 | if (exists) {
|
177 | debug(`Canceling account creation, ${userAccount.webId} already exists`)
|
178 | let error = new Error('Account already exists')
|
179 | error.status = 400
|
180 | throw error
|
181 | }
|
182 |
|
183 | return userAccount
|
184 | })
|
185 | }
|
186 |
|
187 | |
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | createAccountStorage (userAccount) {
|
199 | return this.accountManager.createAccountFor(userAccount)
|
200 | .catch(error => {
|
201 | error.message = 'Error creating account storage: ' + error.message
|
202 | throw error
|
203 | })
|
204 | .then(() => {
|
205 | debug('Account storage resources created')
|
206 | return userAccount
|
207 | })
|
208 | }
|
209 |
|
210 | |
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | cancelIfUsernameInvalid (userAccount) {
|
221 | if (!userAccount.username || !isValidUsername(userAccount.username)) {
|
222 | debug('Invalid username ' + userAccount.username)
|
223 | const error = new Error('Invalid username (contains invalid characters)')
|
224 | error.status = 400
|
225 | throw error
|
226 | }
|
227 |
|
228 | return userAccount
|
229 | }
|
230 |
|
231 | |
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | cancelIfBlacklistedUsername (userAccount) {
|
241 | const validUsername = blacklistService.validate(userAccount.username)
|
242 | if (!validUsername) {
|
243 | debug('Invalid username ' + userAccount.username)
|
244 | const error = new Error('Invalid username (username is blacklisted)')
|
245 | error.status = 400
|
246 | throw error
|
247 | }
|
248 |
|
249 | return userAccount
|
250 | }
|
251 | }
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | class CreateOidcAccountRequest extends CreateAccountRequest {
|
262 | |
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 | constructor (options) {
|
270 | super(options)
|
271 |
|
272 | this.password = options.password
|
273 | }
|
274 |
|
275 | |
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 | validate () {
|
282 | let error
|
283 |
|
284 | if (!this.username) {
|
285 | error = new Error('Username required')
|
286 | error.statusCode = 400
|
287 | throw error
|
288 | }
|
289 |
|
290 | if (!this.password) {
|
291 | error = new Error('Password required')
|
292 | error.statusCode = 400
|
293 | throw error
|
294 | }
|
295 |
|
296 | if (this.enforceToc && !this.acceptToc) {
|
297 | error = new Error('Accepting Terms & Conditions is required for this service')
|
298 | error.statusCode = 400
|
299 | throw error
|
300 | }
|
301 | }
|
302 |
|
303 | |
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | saveCredentialsFor (userAccount) {
|
311 | return this.userStore.createUser(userAccount, this.password)
|
312 | .then(() => {
|
313 | debug('User credentials stored')
|
314 | return userAccount
|
315 | })
|
316 | }
|
317 |
|
318 | |
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | sendResponse (userAccount) {
|
326 | let redirectUrl = this.returnToUrl || userAccount.podUri
|
327 | this.response.redirect(redirectUrl)
|
328 |
|
329 | return userAccount
|
330 | }
|
331 | }
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 | class CreateTlsAccountRequest extends CreateAccountRequest {
|
341 | |
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 | constructor (options) {
|
349 | super(options)
|
350 |
|
351 | this.spkac = options.spkac
|
352 | this.certificate = null
|
353 | }
|
354 |
|
355 | |
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | validate () {
|
362 | let error
|
363 |
|
364 | if (!this.username) {
|
365 | error = new Error('Username required')
|
366 | error.statusCode = 400
|
367 | throw error
|
368 | }
|
369 |
|
370 | if (this.enforceToc && !this.acceptToc) {
|
371 | error = new Error('Accepting Terms & Conditions is required for this service')
|
372 | error.statusCode = 400
|
373 | throw error
|
374 | }
|
375 | }
|
376 |
|
377 | |
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 | generateTlsCertificate (userAccount) {
|
391 | if (!this.spkac) {
|
392 | debug('Missing spkac param, not generating cert during account creation')
|
393 | return Promise.resolve(userAccount)
|
394 | }
|
395 |
|
396 | return Promise.resolve()
|
397 | .then(() => {
|
398 | let host = this.accountManager.host
|
399 | return WebIdTlsCertificate.fromSpkacPost(this.spkac, userAccount, host)
|
400 | .generateCertificate()
|
401 | })
|
402 | .catch(err => {
|
403 | err.status = 400
|
404 | err.message = 'Error generating a certificate: ' + err.message
|
405 | throw err
|
406 | })
|
407 | .then(certificate => {
|
408 | debug('Generated a WebID-TLS certificate as part of account creation')
|
409 | this.certificate = certificate
|
410 | return userAccount
|
411 | })
|
412 | }
|
413 |
|
414 | |
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | saveCredentialsFor (userAccount) {
|
423 | return this.generateTlsCertificate(userAccount)
|
424 | .then(userAccount => {
|
425 | if (this.certificate) {
|
426 | return this.accountManager
|
427 | .addCertKeyToProfile(this.certificate, userAccount)
|
428 | .then(() => {
|
429 | debug('Saved generated WebID-TLS certificate to profile')
|
430 | })
|
431 | } else {
|
432 | debug('No certificate generated, no need to save to profile')
|
433 | }
|
434 | })
|
435 | .then(() => {
|
436 | return userAccount
|
437 | })
|
438 | }
|
439 |
|
440 | |
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 | sendResponse (userAccount) {
|
448 | let res = this.response
|
449 | res.set('User', userAccount.webId)
|
450 | res.status(200)
|
451 |
|
452 | if (this.certificate) {
|
453 | res.set('Content-Type', 'application/x-x509-user-cert')
|
454 | res.send(this.certificate.toDER())
|
455 | } else {
|
456 | res.end()
|
457 | }
|
458 |
|
459 | return userAccount
|
460 | }
|
461 | }
|
462 |
|
463 | module.exports = CreateAccountRequest
|
464 | module.exports.CreateAccountRequest = CreateAccountRequest
|
465 | module.exports.CreateTlsAccountRequest = CreateTlsAccountRequest
|