-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathaccount-manager.mjs
More file actions
297 lines (269 loc) · 9.96 KB
/
account-manager.mjs
File metadata and controls
297 lines (269 loc) · 9.96 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
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
import { URL } from 'url'
import rdf from 'rdflib'
import vocab from 'solid-namespace'
import defaults from '../../config/defaults.mjs'
import UserAccount from './user-account.mjs'
import AccountTemplate, { TEMPLATE_EXTENSIONS, TEMPLATE_FILES } from './account-template.mjs'
import debugModule from './../debug.mjs'
const ns = vocab(rdf)
const debug = debugModule.accounts
const DEFAULT_PROFILE_CONTENT_TYPE = 'text/turtle'
const DEFAULT_ADMIN_USERNAME = 'admin'
class AccountManager {
constructor (options = {}) {
if (!options.host) {
throw Error('AccountManager requires a host instance')
}
this.host = options.host
this.emailService = options.emailService
this.tokenService = options.tokenService
this.authMethod = options.authMethod || defaults.auth
this.multiuser = options.multiuser || false
this.store = options.store
this.pathCard = options.pathCard || 'profile/card'
this.suffixURI = options.suffixURI || '#me'
this.accountTemplatePath = options.accountTemplatePath || './default-templates/new-account/'
}
static from (options) {
return new AccountManager(options)
}
accountExists (accountName) {
let accountUri
let cardPath
try {
accountUri = this.accountUriFor(accountName)
accountUri = new URL(accountUri).hostname
// `pathCard` is a path fragment like 'profile/card' -> ensure it starts with '/'
cardPath = this.pathCard && this.pathCard.startsWith('/') ? this.pathCard : '/' + this.pathCard
} catch (err) {
return Promise.reject(err)
}
return this.accountUriExists(accountUri, cardPath)
}
async accountUriExists (accountUri, accountResource = '/') {
try {
return await this.store.exists(accountUri, accountResource)
} catch (err) {
return false
}
}
accountDirFor (accountName) {
const { hostname } = new URL(this.accountUriFor(accountName))
return this.store.resourceMapper.resolveFilePath(hostname)
}
accountUriFor (accountName) {
const accountUri = this.multiuser
? this.host.accountUriFor(accountName)
: this.host.serverUri
return accountUri
}
accountWebIdFor (accountName) {
const accountUri = this.accountUriFor(accountName)
const webIdUri = new URL(this.pathCard, accountUri)
webIdUri.hash = this.suffixURI
return webIdUri.toString()
}
rootAclFor (userAccount) {
const accountUri = this.accountUriFor(userAccount.username)
return new URL(this.store.suffixAcl, accountUri).toString()
}
addCertKeyToProfile (certificate, userAccount) {
if (!certificate) {
throw new TypeError('Cannot add empty certificate to user profile')
}
return this.getProfileGraphFor(userAccount)
.then(profileGraph => this.addCertKeyToGraph(certificate, profileGraph))
.then(profileGraph => this.saveProfileGraph(profileGraph, userAccount))
}
getProfileGraphFor (userAccount, contentType = DEFAULT_PROFILE_CONTENT_TYPE) {
const webId = userAccount.webId
if (!webId) {
const error = new Error('Cannot fetch profile graph, missing WebId URI')
error.status = 400
return Promise.reject(error)
}
const uri = userAccount.profileUri
return this.store.getGraph(uri, contentType)
.catch(error => {
error.message = `Error retrieving profile graph ${uri}: ` + error.message
throw error
})
}
saveProfileGraph (profileGraph, userAccount, contentType = DEFAULT_PROFILE_CONTENT_TYPE) {
const webId = userAccount.webId
if (!webId) {
const error = new Error('Cannot save profile graph, missing WebId URI')
error.status = 400
return Promise.reject(error)
}
const uri = userAccount.profileUri
return this.store.putGraph(profileGraph, uri, contentType)
}
addCertKeyToGraph (certificate, graph) {
const webId = rdf.namedNode(certificate.webId)
const key = rdf.namedNode(certificate.keyUri)
const timeCreated = rdf.literal(certificate.date.toISOString(), ns.xsd('dateTime'))
const modulus = rdf.literal(certificate.modulus, ns.xsd('hexBinary'))
const exponent = rdf.literal(certificate.exponent, ns.xsd('int'))
const title = rdf.literal('Created by solid-server')
const label = rdf.literal(certificate.commonName)
graph.add(webId, ns.cert('key'), key)
graph.add(key, ns.rdf('type'), ns.cert('RSAPublicKey'))
graph.add(key, ns.dct('title'), title)
graph.add(key, ns.rdfs('label'), label)
graph.add(key, ns.dct('created'), timeCreated)
graph.add(key, ns.cert('modulus'), modulus)
graph.add(key, ns.cert('exponent'), exponent)
return graph
}
userAccountFrom (userData) {
const userConfig = {
username: userData.username,
email: userData.email,
name: userData.name,
externalWebId: userData.externalWebId,
localAccountId: userData.localAccountId,
webId: userData.webid || userData.webId || userData.externalWebId,
idp: this.host.serverUri
}
if (userConfig.username) {
userConfig.username = userConfig.username.toLowerCase()
}
try {
userConfig.webId = userConfig.webId || this.accountWebIdFor(userConfig.username)
} catch (err) {
if (err.message === 'Cannot construct uri for blank account name') {
throw new Error('Username or web id is required', { cause: err })
} else {
throw err
}
}
if (userConfig.username) {
if (userConfig.externalWebId && !userConfig.localAccountId) {
userConfig.localAccountId = this.accountWebIdFor(userConfig.username)
.split('//')[1]
}
} else {
if (userConfig.externalWebId) {
userConfig.username = userConfig.externalWebId
} else {
userConfig.username = this.usernameFromWebId(userConfig.webId)
}
}
return UserAccount.from(userConfig)
}
usernameFromWebId (webId) {
if (!this.multiuser) {
return DEFAULT_ADMIN_USERNAME
}
const profileUrl = new URL(webId)
const hostname = profileUrl.hostname
return hostname.split('.')[0]
}
createAccountFor (userAccount) {
const template = AccountTemplate.for(userAccount)
const templatePath = this.accountTemplatePath
const accountDir = this.accountDirFor(userAccount.username)
debug(`Creating account folder for ${userAccount.webId} at ${accountDir}`)
return AccountTemplate.copyTemplateDir(templatePath, accountDir)
.then(() => template.processAccount(accountDir))
}
generateResetToken (userAccount) {
return this.tokenService.generate('reset-password', { webId: userAccount.webId })
}
generateDeleteToken (userAccount) {
return this.tokenService.generate('delete-account.mjs', {
webId: userAccount.webId,
email: userAccount.email
})
}
validateDeleteToken (token) {
const tokenValue = this.tokenService.verify('delete-account.mjs', token)
if (!tokenValue) {
throw new Error('Invalid or expired delete account token')
}
return tokenValue
}
validateResetToken (token) {
const tokenValue = this.tokenService.verify('reset-password', token)
if (!tokenValue) {
throw new Error('Invalid or expired reset token')
}
return tokenValue
}
passwordResetUrl (token, returnToUrl) {
let resetUrl = new URL(`/account/password/change?token=${token}`, this.host.serverUri).toString()
if (returnToUrl) {
resetUrl += `&returnToUrl=${returnToUrl}`
}
return resetUrl
}
getAccountDeleteUrl (token) {
return new URL(`/account/delete/confirm?token=${token}`, this.host.serverUri).toString()
}
loadAccountRecoveryEmail (userAccount) {
return Promise.resolve()
.then(() => {
const rootAclUri = this.rootAclFor(userAccount)
return this.store.getGraph(rootAclUri)
})
.then(rootAclGraph => {
const matches = rootAclGraph.match(null, ns.acl('agent'))
let recoveryMailto = matches.find(agent => agent.object.value.startsWith('mailto:'))
if (recoveryMailto) {
recoveryMailto = recoveryMailto.object.value.replace('mailto:', '')
}
return recoveryMailto
})
}
verifyEmailDependencies (userAccount) {
if (!this.emailService) {
throw new Error('Email service is not set up')
}
if (userAccount && !userAccount.email) {
throw new Error('Account recovery email has not been provided')
}
}
sendDeleteAccountEmail (userAccount) {
return Promise.resolve()
.then(() => this.verifyEmailDependencies(userAccount))
.then(() => this.generateDeleteToken(userAccount))
.then(resetToken => {
const deleteUrl = this.getAccountDeleteUrl(resetToken)
const emailData = {
to: userAccount.email,
webId: userAccount.webId,
deleteUrl: deleteUrl
}
return this.emailService.sendWithTemplate('delete-account.mjs', emailData)
})
}
sendPasswordResetEmail (userAccount, returnToUrl) {
return Promise.resolve()
.then(() => this.verifyEmailDependencies(userAccount))
.then(() => this.generateResetToken(userAccount))
.then(resetToken => {
const resetUrl = this.passwordResetUrl(resetToken, returnToUrl)
const emailData = {
to: userAccount.email,
webId: userAccount.webId,
resetUrl
}
return this.emailService.sendWithTemplate('reset-password.mjs', emailData)
})
}
sendWelcomeEmail (newUser) {
const emailService = this.emailService
if (!emailService || !newUser.email) {
return Promise.resolve(null)
}
const emailData = {
to: newUser.email,
webid: newUser.webId,
name: newUser.displayName
}
return emailService.sendWithTemplate('welcome.mjs', emailData)
}
}
export default AccountManager
export { TEMPLATE_EXTENSIONS, TEMPLATE_FILES }