11'use strict'
22
33const AuthRequest = require ( './auth-request' )
4- const WebIdTlsCertificate = require ( '../models/webid-tls-certificate' )
54const debug = require ( '../debug' ) . accounts
65
76/**
@@ -12,7 +11,7 @@ const debug = require('../debug').accounts
1211 * a command line, use the `AccountManager` class directly.
1312 *
1413 * This is an abstract class, subclasses are created (for example
15- * `CreateTlsAccountRequest `) depending on which Authentication mode the server
14+ * `CreateOidcAccountRequest `) depending on which Authentication mode the server
1615 * is running in.
1716 *
1817 * @class CreateAccountRequest
@@ -73,9 +72,6 @@ class CreateAccountRequest extends AuthRequest {
7372 options . password = req . body . password
7473 options . userStore = locals . oidc . users
7574 return new CreateOidcAccountRequest ( options )
76- case 'tls' :
77- options . spkac = req . body . spkac
78- return new CreateTlsAccountRequest ( options )
7975 default :
8076 throw new TypeError ( 'Unsupported authentication scheme' )
8177 }
@@ -259,112 +255,5 @@ class CreateOidcAccountRequest extends CreateAccountRequest {
259255 }
260256}
261257
262- /**
263- * Models a Create Account request for a server using WebID-TLS as primary
264- * authentication mode. Handles generating and saving a TLS certificate, etc.
265- *
266- * @class CreateTlsAccountRequest
267- * @extends CreateAccountRequest
268- */
269- class CreateTlsAccountRequest extends CreateAccountRequest {
270- /**
271- * @constructor
272- *
273- * @param [options={ }] {Object} See `CreateAccountRequest` constructor docstring
274- * @param [options.spkac] {string}
275- */
276- constructor ( options = { } ) {
277- super ( options )
278- this . spkac = options . spkac
279- this . certificate = null
280- }
281-
282- /**
283- * Generates a new X.509v3 RSA certificate (if `spkac` was passed in) and
284- * adds it to the user account. Used for storage in an agent's WebID
285- * Profile, for WebID-TLS authentication.
286- *
287- * @param userAccount {UserAccount}
288- * @param userAccount.webId {string} An agent's WebID URI
289- *
290- * @throws {Error } HTTP 400 error if errors were encountering during
291- * certificate generation.
292- *
293- * @return {Promise<UserAccount> } Chainable
294- */
295- generateTlsCertificate ( userAccount ) {
296- if ( ! this . spkac ) {
297- debug ( 'Missing spkac param, not generating cert during account creation' )
298- return Promise . resolve ( userAccount )
299- }
300-
301- return Promise . resolve ( )
302- . then ( ( ) => {
303- let host = this . accountManager . host
304- return WebIdTlsCertificate . fromSpkacPost ( this . spkac , userAccount , host )
305- . generateCertificate ( )
306- } )
307- . catch ( err => {
308- err . status = 400
309- err . message = 'Error generating a certificate: ' + err . message
310- throw err
311- } )
312- . then ( certificate => {
313- debug ( 'Generated a WebID-TLS certificate as part of account creation' )
314- this . certificate = certificate
315- return userAccount
316- } )
317- }
318-
319- /**
320- * Generates a WebID-TLS certificate and saves it to the user's profile
321- * graph.
322- *
323- * @param userAccount {UserAccount}
324- *
325- * @return {Promise<UserAccount> } Chainable
326- */
327- saveCredentialsFor ( userAccount ) {
328- return this . generateTlsCertificate ( userAccount )
329- . then ( userAccount => {
330- if ( this . certificate ) {
331- return this . accountManager
332- . addCertKeyToProfile ( this . certificate , userAccount )
333- . then ( ( ) => {
334- debug ( 'Saved generated WebID-TLS certificate to profile' )
335- } )
336- } else {
337- debug ( 'No certificate generated, no need to save to profile' )
338- }
339- } )
340- . then ( ( ) => {
341- return userAccount
342- } )
343- }
344-
345- /**
346- * Writes the generated TLS certificate to the http Response object.
347- *
348- * @param userAccount {UserAccount}
349- *
350- * @return {UserAccount } Chainable
351- */
352- sendResponse ( userAccount ) {
353- let res = this . response
354- res . set ( 'User' , userAccount . webId )
355- res . status ( 200 )
356-
357- if ( this . certificate ) {
358- res . set ( 'Content-Type' , 'application/x-x509-user-cert' )
359- res . send ( this . certificate . toDER ( ) )
360- } else {
361- res . end ( )
362- }
363-
364- return userAccount
365- }
366- }
367-
368258module . exports = CreateAccountRequest
369259module . exports . CreateAccountRequest = CreateAccountRequest
370- module . exports . CreateTlsAccountRequest = CreateTlsAccountRequest
0 commit comments