-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathauthenticator.mjs
More file actions
161 lines (144 loc) · 4.61 KB
/
authenticator.mjs
File metadata and controls
161 lines (144 loc) · 4.61 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
import debugModule from './../debug.mjs'
import validUrl from 'valid-url'
import * as webid from '../webid/tls/index.mjs'
import provider from '@solid/oidc-auth-manager/src/preferred-provider.js'
import oidcManager from '@solid/oidc-auth-manager/src/oidc-manager.js'
const { domainMatches } = oidcManager
const debug = debugModule.authentication
export class Authenticator {
constructor (options) {
this.accountManager = options.accountManager
}
static fromParams (req, options) {
throw new Error('Must override method')
}
findValidUser () {
throw new Error('Must override method')
}
}
export class PasswordAuthenticator extends Authenticator {
constructor (options) {
super(options)
this.userStore = options.userStore
this.username = options.username
this.password = options.password
}
static fromParams (req, options) {
const body = req.body || {}
options.username = body.username
options.password = body.password
return new PasswordAuthenticator(options)
}
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
}
}
findValidUser () {
let error
let userOptions
return Promise.resolve()
.then(() => this.validate())
.then(() => {
if (validUrl.isUri(this.username)) {
userOptions = { webId: this.username }
} else {
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) {
error = new Error('Invalid username/password combination.')
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) {
error = new Error('Invalid username/password combination.')
error.statusCode = 400
throw error
}
debug('User found, password matches')
return this.accountManager.userAccountFrom(validUser)
})
}
}
export class TlsAuthenticator extends Authenticator {
constructor (options) {
super(options)
this.connection = options.connection
}
static fromParams (req, options) {
options.connection = req.connection
return new TlsAuthenticator(options)
}
findValidUser () {
return this.renegotiateTls()
.then(() => this.getCertificate())
.then(cert => this.extractWebId(cert))
.then(webId => this.loadUser(webId))
}
renegotiateTls () {
const connection = this.connection
return new Promise((resolve, reject) => {
connection.renegotiate({ requestCert: true, rejectUnauthorized: false }, (error) => {
if (error) {
debug('Error renegotiating TLS:', error)
return reject(error)
}
resolve()
})
})
}
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
}
extractWebId (certificate) {
return new Promise((resolve, reject) => {
this.verifyWebId(certificate, (error, webId) => {
if (error) {
debug('Error processing certificate:', error)
return reject(error)
}
resolve(webId)
})
})
}
verifyWebId (certificate, callback) {
debug('Verifying WebID URI')
webid.verify(certificate, callback)
}
discoverProviderFor (webId) {
return provider.discoverProviderFor(webId)
}
loadUser (webId) {
const serverUri = this.accountManager.host.serverUri
if (domainMatches(serverUri, webId)) {
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 })
}
}
}