Skip to content

Commit be9c8d8

Browse files
author
nicola
committed
breaking change, supporting accounts/* on subdomains
1 parent d81b507 commit be9c8d8

3 files changed

Lines changed: 38 additions & 14 deletions

File tree

‎lib/handlers/patch.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"use strict";
33

44
var mime = require('mime');
5+
mime.default_type = 'text/turtle'
56
var fs = require('fs');
67
var $rdf = require('rdflib');
78

@@ -75,6 +76,7 @@ function sparql (filename, targetURI, text, callback) {
7576
try {
7677
$rdf.parse(dataIn, targetKB, targetURI, targetContentType);
7778
} catch(e) {
79+
debug("Patch: Target " + targetContentType + " file syntax error:" + e)
7880
return callback(new HttpError({
7981
status: 500,
8082
message: "Patch: Target " + targetContentType + " file syntax error:" + e
@@ -152,6 +154,7 @@ function sparqlUpdate(filename, targetURI, text, callback) {
152154
try {
153155
$rdf.parse(dataIn, targetKB, targetURI, targetContentType);
154156
} catch(e) {
157+
debug("Patch: Target " + targetContentType + " file syntax error:" + e)
155158
return callback(new HttpError({
156159
status: 500,
157160
message: "Patch: Target " + targetContentType + " file syntax error:" + e

‎lib/identity-provider.js‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var parse = require('./utils').parse
2121

2222
function 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
4141
IdentityProvider.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

301307
IdentityProvider.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')

‎test/identity-provider.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('Identity Provider', function () {
7575
rm('accounts/nicola.localhost')
7676
server.post('/accounts')
7777
.send('username=nicola')
78-
.expect(201)
78+
.expect(200)
7979
.end(function (err) {
8080
rm('accounts/nicola.localhost')
8181
done(err)
@@ -86,7 +86,7 @@ describe('Identity Provider', function () {
8686
rm('accounts/nicola.localhost')
8787
server.post('/accounts')
8888
.send('username=nicola')
89-
.expect(201)
89+
.expect(200)
9090
.expect('Content-Type', 'text/html')
9191
.end(function (err) {
9292
done(new Error('Test not implemented yet'))
@@ -98,7 +98,7 @@ describe('Identity Provider', function () {
9898
rm('accounts/nicola.localhost')
9999
server.post('/accounts')
100100
.send('username=nicola')
101-
.expect(201)
101+
.expect(200)
102102
.end(function (err) {
103103
if (err) {
104104
return done(err)

0 commit comments

Comments
 (0)