@@ -17,6 +17,7 @@ var uriBase = require('./utils').uriBase
1717var forge = require ( 'node-forge' )
1818var asn1 = forge . asn1
1919var pki = forge . pki
20+ var parse = require ( './utils' ) . parse
2021
2122function defaultBuildURI ( account , host , port ) {
2223 var hostAndPort = ( host || 'localhost' ) + ( port || '' )
@@ -297,19 +298,99 @@ IdentityProvider.prototype.get = function (req, res, next) {
297298 } )
298299}
299300
301+ IdentityProvider . prototype . newCert = function ( req , res , next ) {
302+ var self = this
303+ var options = req . body
304+
305+ if ( ! options . spkac || ! options . webid ) {
306+ var err = new Error ( 'Request for certificate not valid' )
307+ err . status = 500
308+ return next ( err )
309+ }
310+
311+ if ( res . session . agent !== options . webid ) {
312+ var error = new Error ( 'You are not logged in, so you can\'t create a certificate' )
313+ error . status = 401
314+ return next ( error )
315+ }
316+
317+ options . host = req . get ( 'host' )
318+
319+ // Get a new cert
320+ webid ( 'tls' ) . generate ( {
321+ spkac : options . spkac ,
322+ agent : options . webid
323+ } , function ( err , cert ) {
324+ if ( err ) {
325+ err . status = 500
326+ return next ( err )
327+ }
328+
329+ // Get the current graph
330+ self . getGraph ( options . webid , function ( err , card ) {
331+ if ( err ) {
332+ return next ( err )
333+ }
334+
335+ addKey ( card , options . webid , cert )
336+ self . putGraph ( options . webid , card ) ( function ( err ) {
337+ if ( err ) {
338+ return next ( err )
339+ }
340+
341+ res . set ( 'Content-Type' , 'application/x-x509-user-cert' )
342+ // Convert to DER
343+ var der = asn1 . toDer ( pki . certificateToAsn1 ( cert ) ) . getBytes ( )
344+ res . send ( der )
345+ } )
346+ } )
347+ } )
348+ }
349+
350+ IdentityProvider . prototype . getGraph = function ( uri , callback ) {
351+ var self = this
352+
353+ var parsed = url . parse ( uri )
354+ var hostname = parsed . hostname
355+ var reqPath = parsed . path
356+
357+ self . store . get ( hostname , reqPath , null , true , 'text/turtle' , function ( err , stream ) {
358+ if ( err ) {
359+ var notexists = new Error ( 'Cannot find WebID card' )
360+ notexists . status = 500
361+ return callback ( notexists )
362+ }
363+ var data = ''
364+ stream
365+ . on ( 'data' , function ( chunk ) {
366+ data += chunk
367+ } )
368+ . on ( 'end' , function ( ) {
369+ parse ( data , function ( err , card ) {
370+ if ( err ) {
371+ var invalid = new Error ( 'You have an invalid WebID card' )
372+ invalid . status = 500
373+ return callback ( invalid )
374+ }
375+ } )
376+ } )
377+ } )
378+ }
379+
300380// Handle POST requests on account creation
301381IdentityProvider . prototype . post = function ( req , res , next ) {
302382 var self = this
303383 var options = req . body
304- options . host = req . get ( 'host' )
305- debug ( 'Create account with settings ' , options )
306384
307385 if ( ! options || ! options . username ) {
308386 var err = new Error ( 'You must enter an account name!' )
309387 err . status = 406
310388 return next ( err )
311389 }
312390
391+ options . host = req . get ( 'host' )
392+ debug ( 'Create account with settings ' , options )
393+
313394 var agent = self . agent ( options )
314395
315396 var cert = null
@@ -331,6 +412,7 @@ IdentityProvider.prototype.post = function (req, res, next) {
331412 }
332413 ] , function ( err ) {
333414 if ( err ) {
415+ err . status = err . status || 500
334416 return next ( err )
335417 }
336418
@@ -340,7 +422,6 @@ IdentityProvider.prototype.post = function (req, res, next) {
340422 res . status ( 201 )
341423
342424 if ( cert ) {
343- res . set ( 'Accept-Ranges' , 'bytes' ) ;
344425 res . set ( 'Content-Type' , 'application/x-x509-user-cert' )
345426 // Convert to DER
346427 var der = asn1 . toDer ( pki . certificateToAsn1 ( cert ) ) . getBytes ( )
@@ -354,10 +435,16 @@ IdentityProvider.prototype.post = function (req, res, next) {
354435
355436// Middleware (or Router) to serve the IdentityProvider
356437IdentityProvider . prototype . middleware = function ( corsSettings ) {
357- var self = this
358438 var router = express . Router ( '/' )
359- if ( corsSettings ) router . use ( corsSettings )
360- router . post ( '/' , bodyParser . urlencoded ( { extended : false } ) , this . post . bind ( this ) )
439+ var parser = bodyParser . urlencoded ( { extended : false } )
440+
441+ if ( corsSettings ) {
442+ router . use ( corsSettings )
443+ }
444+
445+ router . post ( '/new' , parser , this . post . bind ( this ) )
446+ router . post ( '/cert' , parser , this . newCert . bind ( this ) )
447+
361448 router . all ( '/*' , function ( req , res ) {
362449 var host = uriBase ( req )
363450 res . redirect ( 'https://solid.github.io/solid-signup/?domain=' + host + '/accounts' )
0 commit comments