forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-accounts.js
More file actions
88 lines (74 loc) · 2.64 KB
/
user-accounts.js
File metadata and controls
88 lines (74 loc) · 2.64 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
'use strict'
const express = require('express')
const bodyParser = require('body-parser').urlencoded({ extended: false })
const debug = require('../../debug').accounts
const restrictToTopDomain = require('../../handlers/restrict-to-top-domain')
const CreateAccountRequest = require('../../requests/create-account-request')
const AddCertificateRequest = require('../../requests/add-cert-request')
const DeleteAccountRequest = require('../../requests/delete-account-request')
const DeleteAccountConfirmRequest = require('../../requests/delete-account-confirm-request')
/**
* Returns an Express middleware handler for checking if a particular account
* exists (used by Signup apps).
*
* @param accountManager {AccountManager}
*
* @return {Function}
*/
function checkAccountExists (accountManager) {
return (req, res, next) => {
const accountUri = req.hostname
accountManager.accountUriExists(accountUri)
.then(found => {
if (!found) {
debug(`Account ${accountUri} is available (for ${req.originalUrl})`)
return res.sendStatus(404)
}
debug(`Account ${accountUri} is not available (for ${req.originalUrl})`)
next()
})
.catch(next)
}
}
/**
* Returns an Express middleware handler for adding a new certificate to an
* existing account (POST to /api/accounts/cert).
*
* @param accountManager
*
* @return {Function}
*/
function newCertificate (accountManager) {
return (req, res, next) => {
return AddCertificateRequest.handle(req, res, accountManager)
.catch(err => {
err.status = err.status || 400
next(err)
})
}
}
/**
* Returns an Express router for providing user account related middleware
* handlers.
*
* @param accountManager {AccountManager}
*
* @return {Router}
*/
function middleware (accountManager) {
const router = express.Router('/')
router.get('/', checkAccountExists(accountManager))
router.post('/api/accounts/new', restrictToTopDomain, bodyParser, CreateAccountRequest.post)
router.get(['/register', '/api/accounts/new'], restrictToTopDomain, CreateAccountRequest.get)
router.post('/api/accounts/cert', restrictToTopDomain, bodyParser, newCertificate(accountManager))
router.get('/account/delete', restrictToTopDomain, DeleteAccountRequest.get)
router.post('/account/delete', restrictToTopDomain, bodyParser, DeleteAccountRequest.post)
router.get('/account/delete/confirm', restrictToTopDomain, DeleteAccountConfirmRequest.get)
router.post('/account/delete/confirm', restrictToTopDomain, bodyParser, DeleteAccountConfirmRequest.post)
return router
}
module.exports = {
middleware,
checkAccountExists,
newCertificate
}