Skip to content

Commit ad1856d

Browse files
author
nicola
committed
adding support for multiple hosts
1 parent 55f559c commit ad1856d

15 files changed

Lines changed: 93 additions & 67 deletions

File tree

‎bin/ldnode.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ var argv = require('nomnom')
5050
flag: true
5151
})
5252
.option('idp', {
53-
help: 'Allow registration of WebIDs (e.g. \'accounts/\')',
53+
help: 'Allow registration of WebIDs',
5454
abbr: 'idp',
55-
full: 'identity-provider'
55+
full: 'identity-provider',
56+
flag: true
5657
})
5758
.option('secret', {
5859
help: 'HTTP Session secret key (e.g. "your secret phrase")',

‎lib/acl.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ ACL.prototype.isAcl = function (resource) {
4141
}
4242

4343
ACL.prototype.can = function (user, mode, resource, callback, options) {
44-
debug('Can ' + user || 'an agent' + ' ' + mode + ' ' + resource + '?')
44+
debug('Can ' + (user || 'an agent') + ' ' + mode + ' ' + resource + '?')
4545
var self = this
4646
var accessType = 'accessTo'
4747
var acls = possibleACLs(resource, self.suffix)
@@ -264,15 +264,16 @@ function possibleACLs (uri, suffix) {
264264
return urls
265265
}
266266

267-
function fetchDocument (ldp, baseUri) {
267+
function fetchDocument (host, ldp, baseUri) {
268268
return function (uri, callback) {
269269
var graph = $rdf.graph();
270270
async.waterfall([
271271
function (cb) {
272272
// URL is local
273273
var newPath = S(uri).chompLeft(baseUri).s;
274274
// TODO prettify this
275-
var documentPath = utils.uriToFilename(newPath, ldp.root);
275+
var root = !ldp.idp ? ldp.root : ldp.root + host + '/';
276+
var documentPath = utils.uriToFilename(newPath, root);
276277
var documentUri = url.parse(documentPath);
277278
documentPath = documentUri.pathname;
278279
return ldp.readFile(documentPath, cb);
@@ -281,7 +282,6 @@ function fetchDocument (ldp, baseUri) {
281282
try {
282283
$rdf.parse(body, graph, uri, 'text/turtle');
283284
} catch(err) {
284-
console.log(err)
285285
return cb(err, graph);
286286
}
287287
return cb(null, graph);
@@ -297,7 +297,7 @@ function getUserId (req, callback) {
297297
}
298298

299299
var delegator = rdfVocab.debrack(onBehalfOf);
300-
verifyDelegator(delegator, req.session.userId, function(err, res) {
300+
verifyDelegator(req.hostname, delegator, req.session.userId, function(err, res) {
301301
if (res) {
302302
debug("Request User ID (delegation) :" + delegator);
303303
return callback(null, delegator);
@@ -306,8 +306,8 @@ function getUserId (req, callback) {
306306
});
307307
};
308308

309-
function verifyDelegator (ldp, baseUri, delegator, delegatee, callback) {
310-
fetchDocument(ldp, baseUri)(delegator, function(err, delegatorGraph) {
309+
function verifyDelegator (host, ldp, baseUri, delegator, delegatee, callback) {
310+
fetchDocument(host, ldp, baseUri)(delegator, function(err, delegatorGraph) {
311311

312312
// TODO handle error
313313
var delegatesStatements = delegatorGraph
@@ -343,7 +343,7 @@ function allow (mode) {
343343
var baseUri = utils.uriBase(req)
344344

345345
var acl = new ACL({
346-
fetch: fetchDocument(ldp, baseUri),
346+
fetch: fetchDocument(req.hostname, ldp, baseUri),
347347
match: match,
348348
suffix: ldp.suffixAcl
349349
})

‎lib/handlers/delete.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function handler(req, res, next) {
1212

1313
var ldp = req.app.locals.ldp;
1414

15-
ldp.delete(req.path, function(err) {
15+
ldp.delete(req.hostname, req.path, function(err) {
1616
if (err) {
1717
debug("DELETE -- error: " + err);
1818
return next(err);

‎lib/handlers/get.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ function get (req, res, next) {
5151

5252
debug(req.method + ' requesting -- ' + req.originalUrl);
5353

54-
// Get resource or container
55-
ldp.get(req.path, baseUri, includeBody, possibleRDFType,
54+
ldp.get(req.hostname, req.path, baseUri, includeBody, possibleRDFType,
5655
function (err, stream, contentType, container) {
5756

5857
// use globHandler if magic is detected
@@ -115,7 +114,8 @@ function get (req, res, next) {
115114

116115
function globHandler (req, res, next) {
117116
var ldp = req.app.locals.ldp;
118-
var filename = utils.uriToFilename(req.path, ldp.root);
117+
var root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/';
118+
var filename = utils.uriToFilename(req.path, root);
119119
var uri = utils.uriBase(req);
120120

121121
var globOptions = {
@@ -136,7 +136,7 @@ function globHandler (req, res, next) {
136136
var globGraph = $rdf.graph();
137137

138138
async.each(matches, function (match, done) {
139-
var baseUri = utils.filenameToBaseUri(match, uri, ldp.root);
139+
var baseUri = utils.filenameToBaseUri(match, uri, root);
140140
fs.readFile(match, {encoding: 'utf8'}, function (err, fileData) {
141141
if (err) {
142142
debug('GET -- Error in globHandler' + err);
@@ -178,7 +178,8 @@ function aclAllow (match, req, res, callback) {
178178
return callback(true);
179179
}
180180

181-
var relativePath = '/' + path.relative(ldp.root, match);
181+
var root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/';
182+
var relativePath = '/' + path.relative(root, match);
182183
res.locals.path = relativePath;
183184
acl.allow('Read', req, res, function (err) {
184185
callback(err);

‎lib/handlers/patch.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ function handler(req, res, next) {
1414
debug('PATCH -- ' + req.originalUrl);
1515
debug('PATCH -- text length: ' + (req.text ? req.text.length : 'undefined2'));
1616
res.header('MS-Author-Via' , 'SPARQL' );
17-
18-
var filename = utils.uriToFilename(req.path, ldp.root);
17+
18+
var root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/';
19+
var filename = utils.uriToFilename(req.path, root);
1920
var targetContentType = mime.lookup(filename);
2021
var patchContentType = req.get('content-type').split(';')[0].trim(); // Ignore parameters
2122
var targetURI = utils.uriAbs(req) + req.originalUrl;

‎lib/handlers/post.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ function handler(req, res, next) {
4646
}));
4747
}
4848

49-
var containerPath = utils.uriToFilename(req.path, ldp.root);
49+
var root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/';
50+
var containerPath = utils.uriToFilename(req.path, root);
5051
debug("POST -- Container path: " + containerPath);
5152

5253
// Not a container
@@ -88,7 +89,7 @@ function handler(req, res, next) {
8889
var resourceBaseUri = utils.filenameToBaseUri(
8990
resourcePath,
9091
uri,
91-
ldp.root);
92+
root);
9293

9394
try {
9495
$rdf.parse(
@@ -119,6 +120,7 @@ function handler(req, res, next) {
119120
containerCallback);
120121
} else {
121122
ldp.createNewResource(
123+
req.hostname,
122124
uri,
123125
resourcePath,
124126
resourceGraph,

‎lib/handlers/put.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function handler(req, res, next) {
1616
debug("PUT -- Text length: " + (req.text ? req.text.length : 'undefined'));
1717
res.header('MS-Author-Via' , 'SPARQL' );
1818

19-
ldp.put(req.path, req.text, function(err) {
19+
20+
ldp.put(req.hostname, req.path, req.text, function(err) {
2021
if (err) {
2122
debug("PUT -- Write error: " + err.message);
2223
err.message = "Can't write file: " + err.message;

‎lib/header.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ function addLinks(res, fileMetadata) {
3333
function linksHandler(req, res, next) {
3434
var uri = utils.uriBase(req);
3535
var ldp = req.app.locals.ldp;
36-
var filename = utils.uriToFilename(req.url, ldp.root);
36+
var root = !ldp.idp ? ldp.root : ldp.root + req.hostname + '/';
37+
var filename = utils.uriToFilename(req.url, root);
3738

3839
filename = path.join(filename, req.path);
3940
if (ldp.isMetadataFile(filename)) {

‎lib/identity-provider.js‎

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var waterfall = async.waterfall
1111
var debug = require('./debug').idp
1212
var express = require('express')
1313
var bodyParser = require('body-parser')
14+
var errorHandler = require('./handlers/error');
15+
var url = require('url')
1416

1517
function defaultBuildURI (account, host, port) {
1618
var hostAndPort = (host || 'localhost') + (port || '')
@@ -45,7 +47,10 @@ IdentityProvider.prototype.putGraph = function (uri, graph) {
4547
var self = this
4648
return function (callback) {
4749
var content = $rdf.serialize(content, graph, uri, 'text/turtle')
48-
return self.store.put(uri, content, callback)
50+
var parsed = url.parse(uri)
51+
var host = parsed.hostname
52+
var path = parsed.path
53+
return self.store.put(host, path, content, callback)
4954
}
5055
}
5156

@@ -63,17 +68,28 @@ IdentityProvider.prototype.create = function (options, cert, callback) {
6368
var rootAcl = createRootAcl(options)
6469

6570
// TODO create workspaces
66-
67-
parallel([
68-
self.putGraph(options.card, card),
69-
self.putGraph(options.card + self.suffixAcl, cardAcl),
70-
self.putGraph(options.url + self.suffixAcl, rootAcl)
71-
], function (err) {
72-
if (err) {
73-
err.statusCode = 500
71+
// remove port from host
72+
var host = self.host.split(':')[0]
73+
var subdomain = options.username + '.' + host
74+
self.store.exists(subdomain, '/', function (err) {
75+
// if page exists
76+
if (!err || err.status !== 404) {
77+
var error = new Error('Account already exists')
78+
error.status = 406
79+
return callback(error)
7480
}
7581

76-
callback(err)
82+
parallel([
83+
self.putGraph(options.card, card),
84+
self.putGraph(options.card + self.suffixAcl, cardAcl),
85+
self.putGraph(options.url + self.suffixAcl, rootAcl)
86+
], function (err) {
87+
if (err) {
88+
err.status = 500
89+
}
90+
91+
callback(err)
92+
})
7793
})
7894
}
7995

@@ -266,7 +282,7 @@ IdentityProvider.prototype.post = function (req, res, next) {
266282

267283
if (!req.body || !req.body.username) {
268284
var err = new Error('You must enter an account name!')
269-
err.statusCode = 406
285+
err.status = 406
270286
return next(err)
271287
}
272288

@@ -318,5 +334,6 @@ IdentityProvider.prototype.middleware = function (corsSettings) {
318334
router.all('/*', function (req, res) {
319335
res.redirect('https://solid.github.io/solid-signup/?domain=https://' + this.host + '/accounts')
320336
})
337+
router.use(errorHandler.handler)
321338
return router
322339
}

‎lib/ldp.js‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var fs = require('fs');
77
var $rdf = require('rdflib');
88
var async = require('async');
99
var fs = require('fs');
10+
var url = require('url');
1011
var mkdirp = require('fs-extra').mkdirp;
1112
var uuid = require('node-uuid');
1213
var debug = require('./debug');
@@ -116,6 +117,10 @@ LDP.prototype.readContainerMeta = function (directory, callback) {
116117
};
117118

118119
LDP.prototype.listContainer = function (filename, uri, containerData, contentType, callback) {
120+
var ldp = this
121+
var host = url.parse(uri).hostname
122+
var root = !ldp.idp ? ldp.root : ldp.root + host + '/';
123+
119124
function addStats (resourceGraph, baseUri, stats) {
120125
resourceGraph.add(
121126
resourceGraph.sym(baseUri),
@@ -191,7 +196,7 @@ LDP.prototype.listContainer = function (filename, uri, containerData, contentTyp
191196
}
192197

193198
var fileSubject = file + (stats.isDirectory() ? '/' : '');
194-
var fileBaseUri = utils.filenameToBaseUri(fileSubject, uri, ldp.root);
199+
var fileBaseUri = utils.filenameToBaseUri(fileSubject, uri, root);
195200

196201
// Add fileStats to resource Graph
197202
addStats(resourceGraph, fileSubject, stats);
@@ -259,8 +264,7 @@ LDP.prototype.listContainer = function (filename, uri, containerData, contentTyp
259264
});
260265
}
261266

262-
var ldp = this;
263-
var baseUri = utils.filenameToBaseUri(filename, uri, ldp.root);
267+
var baseUri = utils.filenameToBaseUri(filename, uri, root);
264268
var resourceGraph = $rdf.graph();
265269

266270
try {
@@ -325,9 +329,10 @@ LDP.prototype.listContainer = function (filename, uri, containerData, contentTyp
325329
});
326330
};
327331

328-
LDP.prototype.put = function (resourcePath, contents, callback) {
332+
LDP.prototype.put = function (host, resourcePath, contents, callback) {
329333
var ldp = this;
330-
var filePath = utils.uriToFilename(resourcePath, ldp.root);
334+
var root = !ldp.idp ? ldp.root : ldp.root + host + '/';
335+
var filePath = utils.uriToFilename(resourcePath, root, host);
331336

332337
// PUT requests not supported on containers. Use POST instead
333338
if (filePath[filePath.length - 1] === '/') {
@@ -359,9 +364,14 @@ LDP.prototype.put = function (resourcePath, contents, callback) {
359364
});
360365
};
361366

362-
LDP.prototype.get = function (reqPath, baseUri, includeBody, contentType, callback) {
367+
LDP.prototype.exists = function (host, reqPath, callback) {
368+
this.get(host, reqPath, undefined, false, undefined, callback)
369+
}
370+
371+
LDP.prototype.get = function (host, reqPath, baseUri, includeBody, contentType, callback) {
363372
var ldp = this;
364-
var filename = utils.uriToFilename(reqPath, ldp.root);
373+
var root = !ldp.idp ? ldp.root : ldp.root + host + '/';
374+
var filename = utils.uriToFilename(reqPath, root);
365375
ldp.stat(filename, function (err, stats) {
366376
// File does not exist
367377
if (err) {
@@ -414,9 +424,10 @@ LDP.prototype.get = function (reqPath, baseUri, includeBody, contentType, callba
414424
});
415425
};
416426

417-
LDP.prototype.delete = function(resourcePath, callback) {
427+
LDP.prototype.delete = function(host, resourcePath, callback) {
418428
var ldp = this;
419-
var filename = utils.uriToFilename(resourcePath, ldp.root);
429+
var root = !ldp.idp ? ldp.root : ldp.root + host + '/';
430+
var filename = utils.uriToFilename(resourcePath, root);
420431
ldp.stat(filename, function(err, stats) {
421432
if (err) {
422433
return callback(new HttpError({
@@ -501,9 +512,10 @@ LDP.prototype.releaseResourceUri = function (uri) {
501512
delete this.usedURIs[uri];
502513
};
503514

504-
LDP.prototype.createNewResource = function(uri, resourcePath, resourceGraph, callback) {
515+
LDP.prototype.createNewResource = function(host, uri, resourcePath, resourceGraph, callback) {
505516
var ldp = this;
506-
var resourceURI = path.relative(ldp.root, resourcePath);
517+
var root = !ldp.idp ? ldp.root : ldp.root + host + '/';
518+
var resourceURI = path.relative(root, resourcePath);
507519
var rawResource = $rdf.serialize(
508520
undefined,
509521
resourceGraph,

0 commit comments

Comments
 (0)