-
Notifications
You must be signed in to change notification settings - Fork 1
/
component.js
320 lines (288 loc) · 11 KB
/
component.js
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
/* global HTMLElement: false, forge: false, FileReader: false, customElements: false, fetch: false */
import { Modal } from './modal.js'
const registerTmpl = document.createElement('template')
registerTmpl.innerHTML = `
<pp-modal>
<h1 slot="header">Client Cert Registration</h1>
<form id="register-cert" enctype="multipart/form-data" method="post">
<label for="email">Import Certificate</label>
<input type="file" name="client-cert" id="client-cert"></input>
<p>Please fill out these details to generate a client certificate, or import one using the button above.</p>
<label for="email">Email address</label>
<input type="text" name="email" id="email"></input><br><br>
<label for="password">Password</label>
<input type="password" name="password" id="password"></input><br><br>
<label for="password2">Repeat password</label>
<input type="password" name="password2" id="password2"></input><br><br>
<span style="color: red; display: none;" id="form-error"></span><br><br>
<input type="submit" name="generate" value="Generate Client Certificate" />
</form>
<pp-modal>
`
const browserInstTmpl = document.createElement('template')
browserInstTmpl.innerHTML = `
<p>
After installing the certificate in your browser, <a onClick="window.location='/'">click here</a>, or reload this page. You should now see a welcome message, instead of a registration form.
</p>
<h5>Browser Installation Instructions</h5>
<ul>
<li><a href="https://support.securly.com/hc/en-us/articles/206081828-How-to-manually-install-the-Securly-SSL-certificate-in-Chrome" target="_blank">Chrome</a></li>
<li><a href="https://support.globalsign.com/customer/portal/articles/1211486-install-client-digital-certificate---firefox-for-windows" target="_blank">Firefox</a></li>
<li><a href="https://support.globalsign.com/customer/en/portal/articles/2374828-in-browser-installation-of-client-certificates" target="_blank">Edge</a></li>
</ul>
`
const welcomeTmpl = document.createElement('template')
welcomeTmpl.innerHTML = `
<p>
You have successfully registered with the MTLS example app. Your session is now end to end encrypted, and you will not need to refresh the session until the next browser restart or cache clear.
</p>
`
// TODO replace these getters with slotted templates
function getDownloadKeyTmp (cert, privkey) {
const downloadKeyTmpl = document.createElement('template')
downloadKeyTmpl.innerHTML = `
<p>
Please <a id="privkey-p12" download="privkey.p12" href="data:application/x-pkcs12;base64,${encodeURIComponent(privkey)}">download</a> (if download wasn't automatically initiated) your new private key file, and install it in your browser.
</p>
<p>
You may also <a id="certificate-link" download="client-cert.pem" href="data:application/x-x509-user-cert;charset=utf-8,${encodeURIComponent(cert)}">download your certificate</a>, though that was already sent to the server, and it is bundled with your private key in the .p12 file.
</p>
`
return downloadKeyTmpl
}
class ClientCertRegistration extends HTMLElement {
constructor () {
super()
const shadow = this.attachShadow({ mode: 'open' })
shadow.appendChild(registerTmpl.content.cloneNode(true))
}
static get observedAttributes () {
return ['cert', 'cn']
}
set cert (value) {
if (value) {
this.setAttribute('cert', value)
} else {
this.removeAttribute('cert');
}
}
get cert () {
if (this.hasAttribute('cert')) return this.getAttribute('cert')
}
set cn (value) {
if (value) {
this.setAttribute('cn', value)
this.shadowRoot.appendChild(welcomeTmpl.content.cloneNode(true))
this.shadowRoot.querySelector("pp-modal").visible = false
} else {
this.removeAttribute('cn');
this.shadowRoot.querySelector("pp-modal").visible = true
}
}
get cn () {
if (this.hasAttribute('cn')) return this.getAttribute('cn')
}
set formError (value) {
if (value) {
this.shadowRoot.querySelector('#form-error').style.display = 'inline'
this.shadowRoot.querySelector('#form-error').innerHTML = value
} else {
this.shadowRoot.querySelector('#form-error').style.display = 'none'
this.shadowRoot.querySelector('#form-error').innerHTML = ''
}
}
get formError () {
if (this.shadowRoot.querySelector('#form-error').style.display !== 'none') return this.shadowRoot.querySelector('#form-error').innerHTML
}
connectedCallback () {
if (this.hasOwnProperty('cert')) {
let value = this.cert
delete this.cert
this.cert = value
}
if (this.hasOwnProperty('cn')) {
let value = this.cn
delete this.cn
this.cn = value
}
if (this.cn && this.cn.length > 0) {
// user is logged in!
this.shadowRoot.appendChild(welcomeTmpl.content.cloneNode(true))
this.shadowRoot.querySelector("pp-modal").visible = false
} else {
this.shadowRoot.querySelector("pp-modal").visible = true
this.shadowRoot.querySelector('#client-cert').addEventListener('change', this.importSubmit.bind(this))
this.shadowRoot.querySelector('#register-cert').addEventListener('submit', this.generateSubmit.bind(this))
}
}
disconnectedCallback () {
this.shadowRoot.querySelector('#client-cert').removeEventListener('change')
this.shadowRoot.querySelector('#register-cert').removeEventListener('submit')
}
attributeChangedCallback (name, oldVal, newVal) {
const hasValue = newVal !== null
switch (name) {
case 'cert':
break
case 'cn':
break
}
}
showAuthenticated (cert, privkey) {
/*
* Default view when user is authenticated.
* In this case it is a personal welcome message.
* It could be a profile picture, or another complex element.
* Ideally, it would be nice to integrate this into a menu.
*/
this.shadowRoot.querySelector("pp-modal").innerHTML = `
<h1 slot="header">Successfully Registered</h1>
`
if (privkey) {
this.shadowRoot.querySelector("pp-modal").innerHTML += getDownloadKeyTmp(cert, privkey).innerHTML
}
this.shadowRoot.querySelector("pp-modal").innerHTML += browserInstTmpl.innerHTML
// hack to automatically start file download
var tmpel = this.shadowRoot.querySelector('#privkey-p12')
if (tmpel) tmpel.click()
}
generateSubmit (e) {
var self = this
if (e && e.preventDefault) e.preventDefault()
var email = this.shadowRoot.querySelector('#email').value
if (email.indexOf('@') === -1) {
this.lastError = 'Please enter a valid email.'
return
}
var password = this.shadowRoot.querySelector('#password').value
var password2 = this.shadowRoot.querySelector('#password2').value
if (password !== password2) {
this.lastError = 'Passwords do not match.'
return
} else {
this.shadowRoot.querySelector('pp-modal').innerHTML = `<h3 slot="header">Generating your key</h3>
<span>Please wait... This may take a minute.</span>
`
this.generateClientCert(email, password).then((bundle) => {
var privkey = bundle.privateKeyP12
this.cert = bundle.certificate
self.register(email, bundle.certificate, privkey)
})
}
}
importSubmit (e) {
var self = this
if (e && e.preventDefault) e.preventDefault()
var reader = new FileReader()
reader.readAsText(this.shadowRoot.querySelector('#client-cert').files[0])
reader.onload = checkCert
function checkCert (evt) {
try {
var cert = forge.pki.certificateFromPem(evt.target.result)
} catch (e) {
self.lastError = e.toString()
}
self.cert = evt.target.result
self.register(cert.subject.getField('CN').value, evt.target.result)
}
}
async register (cn, pem, privkey) {
/*
* Send client certificate to the server for registration.
* Override this method with your own to customize the route/format.
* Default is to send the cert in the POST body to /register.
*/
this.innerHTML = `<span>Registering your key with server...</span>
`
var registration = await fetch('/register', {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-509-user-cert'
},
method: 'POST',
body: pem
})
if (registration && registration.ok) {
this.cn = cn
this.showAuthenticated(pem, privkey)
} else {
this.shadowRoot.appendChild(registerTmpl.content.cloneNode(true))
if (registration.status === 401) {
this.lastError = await registration.text()
return false
} else {
this.lastError = 'Registration failed. Try again?'
return false
}
}
}
async generateClientCert (email, password) {
/*
* Generates an RSA 2048 bit key, then an X.509 cert, a CSR and finally a P12 file.
* Response is the base64 encoded P12, and pem encoded cert.
*/
// generate a keypair
return new Promise(async (resolve, reject) => {
forge.pki.rsa.generateKeyPair({bits: 2048, workers: 2}, function(err, keys) {
if (err) {
this.lastError = err.toString()
reject(err)
} else {
// create a certificate
var cert = forge.pki.createCertificate()
cert.publicKey = keys.publicKey
cert.serialNumber = '01'
cert.validity.notBefore = new Date()
cert.validity.notAfter = new Date()
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1)
var attrs = [{
name: 'commonName',
value: email
}]
cert.setSubject(attrs)
cert.setIssuer(attrs)
cert.setExtensions([{
name: 'basicConstraints',
cA: true
}, {
name: 'keyUsage',
keyCertSign: true,
digitalSignature: true,
nonRepudiation: true,
keyEncipherment: true,
dataEncipherment: true
}, {
name: 'subjectAltName',
altNames: [{
type: 6, // URI
value: 'http://localhost'
}]
}])
// self-sign certificate
cert.sign(keys.privateKey, forge.md.sha256.create())
// create PKCS12
var newPkcs12Asn1 = forge.pkcs12.toPkcs12Asn1(
keys.privateKey, [cert], password,
{
generateLocalKeyId: true,
friendlyName: 'Client Cert',
algorithm: '3des'
}
)
var newPkcs12Der = forge.asn1.toDer(newPkcs12Asn1).getBytes()
var pkcs12Asn1 = forge.asn1.fromDer(newPkcs12Der)
var pkcs12 = forge.pkcs12.pkcs12FromAsn1(pkcs12Asn1, false, password)
// base64-encode p12
var p12Der = forge.asn1.toDer(pkcs12Asn1).getBytes()
var p12b64 = forge.util.encode64(p12Der)
resolve({
'privateKeyP12': p12b64,
'certificate': forge.pki.certificateToPem(cert)
})
}
})
})
}
}
customElements.define('client-cert-login', ClientCertRegistration)
export default ClientCertRegistration