@@ -21,7 +21,7 @@ var parse = require('./utils').parse
2121
2222function defaultBuildURI ( account , host , port ) {
2323 var hostAndPort = ( host || 'localhost' ) + ( port || '' )
24- return 'https://' + account . toLowerCase ( ) + '.' + hostAndPort + '/'
24+ return 'https://' + hostAndPort + '/'
2525}
2626
2727// IdentityProvider singleton
@@ -40,9 +40,10 @@ function IdentityProvider (options) {
4040// Generate the future webid from the options and the IdentityProvider Settings
4141IdentityProvider . prototype . agent = function ( options ) {
4242 options = options || { }
43- var url = options . url || this . buildURI ( options . username , this . host )
43+ var url = options . url || this . buildURI ( options . username , options . host )
4444 var card = url + this . pathCard
4545 var agent = card + this . suffixURI
46+ console . log ( url , agent )
4647 return agent
4748}
4849
@@ -65,6 +66,7 @@ IdentityProvider.prototype.create = function (options, cert, callback) {
6566 options . url = options . url || self . buildURI ( options . username , options . host )
6667 options . card = options . url + self . pathCard
6768 options . agent = options . card + self . suffixURI
69+ debug ( 'Creating space ' + options . url )
6870 options . suffixAcl = self . suffixAcl
6971
7072 var card = createCard ( options , cert )
@@ -74,25 +76,29 @@ IdentityProvider.prototype.create = function (options, cert, callback) {
7476 // TODO create workspaces
7577 // remove port from host
7678
77- var hostname = options . host . split ( ':' ) [ 0 ]
78- var subdomain = options . username + '.' + hostname
79+ var subdomain = options . host . split ( ':' ) [ 0 ]
7980 self . store . exists ( subdomain , '/' , function ( err ) {
8081 // if page exists
8182 if ( ! err || err . status !== 404 ) {
83+ debug ( 'Cannot create ' + subdomain + ', it already exists' )
8284 var error = new Error ( 'Account already exists' )
8385 error . status = 406
8486 return callback ( error )
8587 }
8688
89+ debug ( subdomain + ' is free' )
90+
8791 parallel ( [
8892 self . putGraph ( options . card , card ) ,
8993 self . putGraph ( options . card + self . suffixAcl , cardAcl ) ,
9094 self . putGraph ( options . url + self . suffixAcl , rootAcl )
9195 ] , function ( err ) {
9296 if ( err ) {
9397 err . status = 500
98+ debug ( 'Error creating account ' + options . username + ': ' + err . message )
9499 }
95100
101+ debug ( 'Created files for ' + options . username )
96102 callback ( err )
97103 } )
98104 } )
@@ -300,15 +306,18 @@ IdentityProvider.prototype.get = function (req, res, next) {
300306
301307IdentityProvider . prototype . newCert = function ( req , res , next ) {
302308 var self = this
303- var options = req . body
309+ debug ( 'Requesting new cert for ' + options . webid )
304310
311+ var options = req . body
305312 if ( ! options . spkac || ! options . webid ) {
313+ debug ( 'Request for certificate not valid' )
306314 var err = new Error ( 'Request for certificate not valid' )
307315 err . status = 500
308316 return next ( err )
309317 }
310318
311- if ( res . session . agent !== options . webid ) {
319+ if ( req . session . userId !== options . webid ) {
320+ debug ( 'user is not logged in: ' + req . session . userId + ' is not ' + options . webid )
312321 var error = new Error ( 'You are not logged in, so you can\'t create a certificate' )
313322 error . status = 401
314323 return next ( error )
@@ -322,22 +331,26 @@ IdentityProvider.prototype.newCert = function (req, res, next) {
322331 agent : options . webid
323332 } , function ( err , cert ) {
324333 if ( err ) {
334+ debug ( 'Error generating a certificate: ' + err . message )
325335 err . status = 500
326336 return next ( err )
327337 }
328338
329339 // Get the current graph
330340 self . getGraph ( options . webid , function ( err , card ) {
331341 if ( err ) {
342+ debug ( 'Error getting the webID: ' + err . message )
332343 return next ( err )
333344 }
334345
335346 addKey ( card , options . webid , cert )
336347 self . putGraph ( options . webid , card ) ( function ( err ) {
337348 if ( err ) {
349+ debug ( 'Error saving the WebID: ' + err . message )
338350 return next ( err )
339351 }
340352
353+ debug ( 'Sending new cert as response' )
341354 res . set ( 'Content-Type' , 'application/x-x509-user-cert' )
342355 // Convert to DER
343356 var der = asn1 . toDer ( pki . certificateToAsn1 ( cert ) ) . getBytes ( )
@@ -356,6 +369,7 @@ IdentityProvider.prototype.getGraph = function (uri, callback) {
356369
357370 self . store . get ( hostname , reqPath , null , true , 'text/turtle' , function ( err , stream ) {
358371 if ( err ) {
372+ debug ( 'Cannot find WebID card' )
359373 var notexists = new Error ( 'Cannot find WebID card' )
360374 notexists . status = 500
361375 return callback ( notexists )
@@ -368,10 +382,13 @@ IdentityProvider.prototype.getGraph = function (uri, callback) {
368382 . on ( 'end' , function ( ) {
369383 parse ( data , function ( err , card ) {
370384 if ( err ) {
385+ debug ( 'WebId can\'t be parsed' )
371386 var invalid = new Error ( 'You have an invalid WebID card' )
372387 invalid . status = 500
373388 return callback ( invalid )
374389 }
390+
391+ callback ( null , card )
375392 } )
376393 } )
377394 } )
@@ -383,6 +400,7 @@ IdentityProvider.prototype.post = function (req, res, next) {
383400 var options = req . body
384401
385402 if ( ! options || ! options . username ) {
403+ debug ( 'Account creation is missing username field' )
386404 var err = new Error ( 'You must enter an account name!' )
387405 err . status = 406
388406 return next ( err )
@@ -396,9 +414,9 @@ IdentityProvider.prototype.post = function (req, res, next) {
396414 var cert = null
397415 waterfall ( [
398416 function ( callback ) {
399- if ( options [ ' spkac' ] && options [ ' spkac' ] . length > 0 ) {
417+ if ( options . spkac && options . spkac . length > 0 ) {
400418 webid ( 'tls' ) . generate ( {
401- spkac : options [ ' spkac' ] ,
419+ spkac : options . spkac ,
402420 agent : agent // TODO generate agent
403421 } , callback )
404422 } else {
@@ -407,19 +425,22 @@ IdentityProvider.prototype.post = function (req, res, next) {
407425 } ,
408426 function ( newCert , callback ) {
409427 cert = newCert
410-
411428 self . create ( options , cert , callback )
412429 }
413430 ] , function ( err ) {
414431 if ( err ) {
415432 err . status = err . status || 500
433+ debug ( 'Error creating ' + options . user + ': ' + err . message )
416434 return next ( err )
417435 }
418436
437+ debug ( options . username + ': account created, now setting the cookies and response' )
419438 // Set the cookies on success
420- req . session . agent = agent
439+ console . log ( agent )
440+ req . session . userId = agent ;
441+ req . session . identified = true ;
421442 res . set ( 'User' , agent )
422- res . status ( 201 )
443+ res . status ( 200 )
423444
424445 if ( cert ) {
425446 res . set ( 'Content-Type' , 'application/x-x509-user-cert' )
0 commit comments