forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthenticator.js
More file actions
337 lines (292 loc) · 9.03 KB
/
authenticator.js
File metadata and controls
337 lines (292 loc) · 9.03 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
'use strict'
const debug = require('./../debug').authentication
const validUrl = require('valid-url')
const webid = require('../webid/tls')
const provider = require('@solid/oidc-auth-manager/src/preferred-provider')
const { domainMatches } = require('@solid/oidc-auth-manager/src/oidc-manager')
/**
* Abstract Authenticator class, representing a local login strategy.
* To subclass, implement `fromParams()` and `findValidUser()`.
* Used by the `LoginRequest` handler class.
*
* @abstract
* @class Authenticator
*/
class Authenticator {
constructor (options) {
this.accountManager = options.accountManager
}
/**
* @param req {IncomingRequest}
* @param options {Object}
*/
static fromParams (req, options) {
throw new Error('Must override method')
}
/**
* @returns {Promise<UserAccount>}
*/
findValidUser () {
throw new Error('Must override method')
}
}
/**
* Authenticates user via Username+Password.
*/
class PasswordAuthenticator extends Authenticator {
/**
* @constructor
* @param options {Object}
*
* @param [options.username] {string} Unique identifier submitted by user
* from the Login form. Can be one of:
* - An account name (e.g. 'alice'), if server is in Multi-User mode
* - A WebID URI (e.g. 'https://alice.example.com/#me')
*
* @param [options.password] {string} Plaintext password as submitted by user
*
* @param [options.userStore] {UserStore}
*
* @param [options.accountManager] {AccountManager}
*/
constructor (options) {
super(options)
this.userStore = options.userStore
this.username = options.username
this.password = options.password
}
/**
* Factory method, returns an initialized instance of PasswordAuthenticator
* from an incoming http request.
*
* @param req {IncomingRequest}
* @param [req.body={}] {Object}
* @param [req.body.username] {string}
* @param [req.body.password] {string}
*
* @param options {Object}
*
* @param [options.accountManager] {AccountManager}
* @param [options.userStore] {UserStore}
*
* @return {PasswordAuthenticator}
*/
static fromParams (req, options) {
const body = req.body || {}
options.username = body.username
options.password = body.password
return new PasswordAuthenticator(options)
}
/**
* Ensures required parameters are present,
* and throws an error if not.
*
* @throws {Error} If missing required params
*/
validate () {
let error
if (!this.username) {
error = new Error('Username required')
error.statusCode = 400
throw error
}
if (!this.password) {
error = new Error('Password required')
error.statusCode = 400
throw error
}
}
/**
* Loads a user from the user store, and if one is found and the
* password matches, returns a `UserAccount` instance for that user.
*
* @throws {Error} If failures to load user are encountered
*
* @return {Promise<UserAccount>}
*/
findValidUser () {
let error
let userOptions
return Promise.resolve()
.then(() => this.validate())
.then(() => {
if (validUrl.isUri(this.username)) {
// A WebID URI was entered into the username field
userOptions = { webId: this.username }
} else {
// A regular username
userOptions = { username: this.username }
}
const user = this.accountManager.userAccountFrom(userOptions)
debug(`Attempting to login user: ${user.id}`)
return this.userStore.findUser(user.id)
})
.then(foundUser => {
if (!foundUser) {
// CWE - CWE-200: Exposure of Sensitive Information to an Unauthorized Actor (4.13)
// https://cwe.mitre.org/data/definitions/200.html
error = new Error('Invalid username/password combination.') // no detail for security 'No user found for that username')
error.statusCode = 400
throw error
}
if (foundUser.link) {
throw new Error('Linked users not currently supported, sorry (external WebID without TLS?)')
}
return this.userStore.matchPassword(foundUser, this.password)
})
.then(validUser => {
if (!validUser) {
// CWE - CWE-200: Exposure of Sensitive Information to an Unauthorized Actor (4.13)
// https://cwe.mitre.org/data/definitions/200.html
error = new Error('Invalid username/password combination.') // no detail for security 'User found but no password match')
error.statusCode = 400
throw error
}
debug('User found, password matches')
return this.accountManager.userAccountFrom(validUser)
})
}
}
/**
* Authenticates a user via a WebID-TLS client side certificate.
*/
class TlsAuthenticator extends Authenticator {
/**
* @constructor
* @param options {Object}
*
* @param [options.accountManager] {AccountManager}
*
* @param [options.connection] {Socket} req.connection
*/
constructor (options) {
super(options)
this.connection = options.connection
}
/**
* Factory method, returns an initialized instance of TlsAuthenticator
* from an incoming http request.
*
* @param req {IncomingRequest}
* @param req.connection {Socket}
*
* @param options {Object}
* @param [options.accountManager] {AccountManager}
*
* @return {TlsAuthenticator}
*/
static fromParams (req, options) {
options.connection = req.connection
return new TlsAuthenticator(options)
}
/**
* Requests a client certificate from the current TLS connection via
* renegotiation, extracts and verifies the user's WebID URI,
* and makes sure that WebID is hosted on this server.
*
* @throws {Error} If error is encountered extracting the WebID URI from
* certificate, or if the user's account is hosted by a remote system.
*
* @return {Promise<UserAccount>}
*/
findValidUser () {
return this.renegotiateTls()
.then(() => this.getCertificate())
.then(cert => this.extractWebId(cert))
.then(webId => this.loadUser(webId))
}
/**
* Renegotiates the current TLS connection to ask for a client certificate.
*
* @throws {Error}
*
* @returns {Promise}
*/
renegotiateTls () {
const connection = this.connection
return new Promise((resolve, reject) => {
// Typically, certificates for WebID-TLS are not signed or self-signed,
// and would hence be rejected by Node.js for security reasons.
// However, since WebID-TLS instead dereferences the profile URL to validate ownership,
// we can safely skip the security check.
connection.renegotiate({ requestCert: true, rejectUnauthorized: false }, (error) => {
if (error) {
debug('Error renegotiating TLS:', error)
return reject(error)
}
resolve()
})
})
}
/**
* Requests and returns a client TLS certificate from the current connection.
*
* @throws {Error} If no certificate is presented, or if it is empty.
*
* @return {Promise<X509Certificate|null>}
*/
getCertificate () {
const certificate = this.connection.getPeerCertificate()
if (!certificate || !Object.keys(certificate).length) {
debug('No client certificate detected')
throw new Error('No client certificate detected. ' +
'(You may need to restart your browser to retry.)')
}
return certificate
}
/**
* Extracts (and verifies) the WebID URI from a client certificate.
*
* @param certificate {X509Certificate}
*
* @return {Promise<string>} WebID URI
*/
extractWebId (certificate) {
return new Promise((resolve, reject) => {
this.verifyWebId(certificate, (error, webId) => {
if (error) {
debug('Error processing certificate:', error)
return reject(error)
}
resolve(webId)
})
})
}
/**
* Performs WebID-TLS verification (requests the WebID Profile from the
* WebID URI extracted from certificate, and makes sure the public key
* from the profile matches the key from certificate).
*
* @param certificate {X509Certificate}
* @param callback {Function} Gets invoked with signature `callback(error, webId)`
*/
verifyWebId (certificate, callback) {
debug('Verifying WebID URI')
webid.verify(certificate, callback)
}
discoverProviderFor (webId) {
return provider.discoverProviderFor(webId)
}
/**
* Returns a user account instance for a given Web ID.
*
* @param webId {string}
*
* @return {UserAccount}
*/
loadUser (webId) {
const serverUri = this.accountManager.host.serverUri
if (domainMatches(serverUri, webId)) {
// This is a locally hosted Web ID
return this.accountManager.userAccountFrom({ webId })
} else {
debug(`WebID URI ${JSON.stringify(webId)} is not a local account, using it as an external WebID`)
return this.accountManager.userAccountFrom({ webId, username: webId, externalWebId: true })
}
}
}
module.exports = {
Authenticator,
PasswordAuthenticator,
TlsAuthenticator
}