Skip to content

Commit b4a2c88

Browse files
author
nicola
committed
merging current master use regexp instead of string matching
2 parents 1202fa2 + 8f275c2 commit b4a2c88

8 files changed

Lines changed: 206 additions & 106 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Linked Data Platform server based on [rdflib.js](https://github.com/linkeddata/r
2323
npm install
2424
```
2525

26+
Due to the use of the sync-request library to implement the ACl feature, ldnode requires a version of node >= 0.12
27+
2628
## Usage
2729

2830
The library provides two APIs:
@@ -120,6 +122,14 @@ There are some scripts in the [package.json](https://github.com/linkeddata/ldnod
120122
- `npm run ldp-webid`: run ldnode with SSL and WebID+TLS enabled (remember it runs in HTTPS)
121123
- `npm run ldp-ssl`: same as the above without WebID+TLS support
122124

125+
### Package scripts
126+
127+
There are some scripts in the [package.json](https://github.com/linkeddata/ldnode/blob/master/package.json):
128+
129+
- `npm start`: starts a very basic ldnode with default configs
130+
- `npm run ldp-webid`: run ldnode with SSL and WebID+TLS enabled (remember it runs in HTTPS)
131+
- `npm run ldp-ssl`: same as the above without WebID+TLS support
132+
123133
## Tests
124134

125135
The tests assume that there is a running ldnode.

‎acl.js‎

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ var fs = require('fs');
66
var glob = require('glob');
77
var path = require('path');
88
var $rdf = require('rdflib');
9+
var request = require('sync-request');
910
var S = require('string');
11+
var url = require('url');
1012

1113
var debug = require('./logging').ACL;
1214
var file = require('./fileStore.js');
1315
var ns = require('./vocab/ns.js').ns;
1416
var rdfVocab = require('./vocab/rdf.js');
1517

16-
function allow(mode, req, res) {
18+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
19+
20+
function allow(mode, req) {
1721
var options = req.app.locals.ldp;
1822
var origin = req.get('origin');
1923
origin = origin ? origin : '';
@@ -79,7 +83,7 @@ function allow(mode, req, res) {
7983
if (rdfVocab.brack(origin) === originsControlElem.toString()) {
8084
debug("Found policy for origin: " +
8185
originsControlElem.toString());
82-
originControlValue = allowOrigin(mode, userId, res, aclGraph, controlElem);
86+
originControlValue = allowOrigin(mode, req, userId, aclGraph, controlElem);
8387
if (originControlValue) {
8488
return originControlValue;
8589
}
@@ -89,7 +93,7 @@ function allow(mode, req, res) {
8993
} else {
9094
debug("No origin found, moving on.");
9195
}
92-
originControlValue = allowOrigin(mode, userId, res, aclGraph, controlElem);
96+
originControlValue = allowOrigin(mode, req, userId, aclGraph, controlElem);
9397
if (originControlValue) {
9498
return originControlValue;
9599
}
@@ -132,8 +136,7 @@ function allow(mode, req, res) {
132136

133137
var groupURI = rdfVocab.debrack(agentClassElem.toString());
134138
var groupGraph = $rdf.graph();
135-
var groupFetcher = $rdf.fetcher(groupGraph, 3000, false);
136-
groupFetcher.nowOrWhenFetched(groupURI, null, function(ok, err) {});
139+
fetchDocument(groupGraph, groupURI, req);
137140
var typeStatements = groupGraph.each(agentClassElem,
138141
ns.rdf("type"), ns.foaf("Group"));
139142
if (groupGraph.statements.length > 0 &&
@@ -171,7 +174,7 @@ function allow(mode, req, res) {
171174
if (rdfVocab.brack(origin) === originsElem.toString()) {
172175
debug("Found policy for origin: " +
173176
originsElem.toString());
174-
originValue = allowOrigin(mode, userId, res, aclGraph, modeElem);
177+
originValue = allowOrigin(mode, req, userId, aclGraph, modeElem);
175178
if (originValue) {
176179
return originValue;
177180
}
@@ -181,7 +184,7 @@ function allow(mode, req, res) {
181184
} else {
182185
debug("No origin found, moving on.");
183186
}
184-
originValue = allowOrigin(mode, userId, res, aclGraph, modeElem);
187+
originValue = allowOrigin(mode, req, userId, aclGraph, modeElem);
185188
if (originValue) {
186189
return originValue;
187190
}
@@ -206,8 +209,6 @@ function allow(mode, req, res) {
206209
if (i === 0) {
207210
if (path.dirname(path.dirname(relativePath)) == '.') {
208211
filepath = options.root;
209-
} else if (S(relativePath).endsWith("/")) {
210-
filepath = options.root + path.dirname(path.dirname(relativePath));
211212
} else {
212213
filepath = options.root + path.dirname(relativePath);
213214
}
@@ -217,7 +218,7 @@ function allow(mode, req, res) {
217218
} else if (path.dirname(path.dirname(relativePath)) === '.') {
218219
filepath = options.root;
219220
} else {
220-
filepath = options.root + path.dirname(path.dirname(relativePath));
221+
filepath = options.root + path.dirname(relativePath);
221222
}
222223
}
223224

@@ -233,7 +234,7 @@ function allow(mode, req, res) {
233234
};
234235
}
235236

236-
function allowOrigin(mode, userId, res, aclGraph, subject) {
237+
function allowOrigin(mode, req, userId, aclGraph, subject) {
237238
debug("In allow origin");
238239
var ownerStatements = aclGraph.each(subject, ns.acl("owner"),
239240
aclGraph.sym(userId));
@@ -267,8 +268,7 @@ function allowOrigin(mode, userId, res, aclGraph, subject) {
267268
}
268269
var groupURI = rdfVocab.debrack(agentClassElem.toString());
269270
var groupGraph = $rdf.graph();
270-
var groupFetcher = $rdf.fetcher(groupGraph, 3000, false);
271-
groupFetcher.nowOrWhenFetched(groupURI, null, function(ok, err) {});
271+
fetchDocument(groupGraph, groupURI, req);
272272
var typeStatements = groupGraph.each(agentClassElem, ns.rdf("type"), ns.foaf("Group"));
273273
if (groupGraph.statements.length > 0 && typeStatements.length > 0) {
274274
var memberStatements = groupGraph.each(agentClassElem, ns.foaf("member"),
@@ -284,11 +284,46 @@ function allowOrigin(mode, userId, res, aclGraph, subject) {
284284
}
285285
}
286286

287+
function fetchDocument(graph, uri, req) {
288+
var options = req.app.locals.ldp;
289+
var uriBase = file.uriBase(req);
290+
291+
var body = "";
292+
if (S(uri).startsWith(uriBase)) {
293+
try {
294+
var oldPath = req.url;
295+
var newPath = S(uri).chompLeft(uriBase).s;
296+
var documentPath = file.uriToFilename(newPath, options.root);
297+
var documentUri = url.parse(documentPath);
298+
documentPath = documentUri.pathname;
299+
300+
//Overwrite url attribute to call allow with the correct path.
301+
//Otherwise enters infinite recuresion.
302+
req.url = newPath;
303+
var readAllowed = allow('Read', req);
304+
//Restore value of req.url
305+
req.url = oldPath;
306+
307+
if (readAllowed && readAllowed.status === 200) {
308+
body = fs.readFileSync(documentPath, {encoding: 'utf8'});
309+
}
310+
} catch (err) {}
311+
} else {
312+
var response = request('GET', uri, {
313+
headers: {
314+
'Accept': 'text/turtle'
315+
}
316+
});
317+
body = response.getBody('utf8');
318+
}
319+
$rdf.parse(body, graph, uri, 'text/turtle');
320+
}
321+
287322
function getUserId(req) {
288323
var userId;
289324
if (req.get('On-Behalf-Of')) {
290325
var delegator = rdfVocab.debrack(req.get('On-Behalf-Of'));
291-
if (verifyDelegator(delegator, req.session.userId)) {
326+
if (verifyDelegator(delegator, req.session.userId, req)) {
292327
debug("Request User ID (delegation) :" + delegator);
293328
userId = delegator;
294329
} else {
@@ -301,17 +336,13 @@ function getUserId(req) {
301336
return userId;
302337
}
303338

304-
function verifyDelegator(delegator, delegatee) {
339+
function verifyDelegator(delegator, delegatee, req) {
305340
var delegatorGraph = $rdf.graph();
306-
var delegatorFetcher = $rdf.fetcher(delegatorGraph, 3000, false);
307-
debug("delegator: " + delegator);
308-
delegatorFetcher.nowOrWhenFetched(delegator, undefined, function(ok, err) {
309-
debug("ok: " + ok);
310-
debug("error: " + err);
311-
});
312-
var delegatesStatements = delegatorGraph.each(delegatorGraph.sym(delegator),
313-
delegatorGraph.sym("http://www.w3.org/ns/auth/acl#delegates"),
314-
undefined);
341+
fetchDocument(delegatorGraph, delegator, req);
342+
var delegatesStatements = delegatorGraph
343+
.each(delegatorGraph.sym(delegator),
344+
delegatorGraph.sym("http://www.w3.org/ns/auth/acl#delegates"),
345+
undefined);
315346
for(var delegateeIndex in delegatesStatements) {
316347
var delegateeValue = delegatesStatements[delegateeIndex];
317348
if (rdfVocab.debrack(delegateeValue.toString()) === delegatee) {
@@ -354,11 +385,11 @@ exports.allowAppendThenWriteHandler = function(req, res, next) {
354385
if (!options.webid) {
355386
return next();
356387
} else {
357-
var allowAppendValue = allow("Append", req, res, next);
388+
var allowAppendValue = allow("Append", req);
358389
if (allowAppendValue.status == 200) {
359390
return next();
360391
} else {
361-
var allowWriteValue = allow("Write", req, res, next);
392+
var allowWriteValue = allow("Write", req);
362393
if(allowWriteValue.status == 200) {
363394
return next();
364395
} else {

‎fileStore.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ var fs = require('fs');
55
var path = require('path');
66
var S = require('string');
77

8+
var turtleExtension = ".ttl";
9+
810
function uriToFilename(uri, base) {
911
var filename = path.join(base, uri);
1012
// Make sure filename ends with '/' if filename exists and is a directory.
@@ -38,8 +40,20 @@ function uriBase(req) {
3840
return uriAbs(req) + (req.baseUrl || '');
3941
}
4042

43+
function getResourceLink(filename, uri, base, suffix, otherSuffix) {
44+
var link = filenameToBaseUri(filename, uri, base);
45+
if (S(link).endsWith(suffix)) {
46+
return link;
47+
} else if (S(link).endsWith(otherSuffix)) {
48+
return S(link).chompRight(otherSuffix).s + suffix;
49+
} else {
50+
return link+suffix;
51+
}
52+
}
53+
4154
exports.uriToFilename = uriToFilename;
4255
exports.uriToRelativeFilename = uriToRelativeFilename;
4356
exports.filenameToBaseUri = filenameToBaseUri;
4457
exports.uriAbs = uriAbs;
45-
exports.uriBase = uriBase;
58+
exports.uriBase = uriBase;
59+
exports.getResourceLink = getResourceLink;

‎handlers/get.js‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ function get(req, res, includeBody) {
5252
}
5353

5454
var filename = file.uriToFilename(req.path, options.root);
55+
var baseUri = file.uriBase(req);
56+
var aclLink = file.getResourceLink(filename, baseUri,
57+
options.root, options.suffixAcl,
58+
metaExtension);
59+
var metaLink = file.getResourceLink(filename, baseUri,
60+
options.root, metaExtension,
61+
options.suffixAcl);
62+
header.addLink(res, aclLink, 'acl');
63+
header.addLink(res, metaLink, 'describedBy');
5564

5665
// Check if file exists
5766
fs.stat(filename, function(err, stats) {
@@ -96,7 +105,7 @@ function get(req, res, includeBody) {
96105

97106
// Consider acl and meta files text/turtle
98107
if (path.extname(filename) === options.suffixAcl ||
99-
path.basename(filename) === options.suffixAcl ||
108+
path.basename(filename) === turtleExtension ||
100109
path.basename(filename) === metaExtension) {
101110
contentType = 'text/turtle';
102111
}
@@ -136,7 +145,6 @@ function get(req, res, includeBody) {
136145
match,
137146
{ encoding: "utf8" });
138147

139-
//TODO integrate ACL
140148
if (S(match).endsWith(".ttl") && aclAllow(match)) {
141149
$rdf.parse(
142150
fileData,
@@ -203,7 +211,6 @@ function get(req, res, includeBody) {
203211
return res.status(500).send(err);
204212
}
205213

206-
//TODO rdflib callbacks
207214
$rdf.serialize(
208215
undefined,
209216
resourceGraph,

‎handlers/post.js‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,17 @@ function handler(req, res) {
3434
// Error on invalid content-type
3535
if (contentType != 'text/turtle' &&
3636
contentType != 'text/n3' &&
37-
contentType != 'application/rdf+xml') {
38-
//TODO Handle json and nquad content types
37+
contentType != 'application/rdf+xml' &&
38+
contentType != 'application/json+ld' &&
39+
contentType != 'application/nquads' &&
40+
contentType != 'application/n-quads') {
3941
debug("POST -- Invalid Content Type: " + contentType);
4042
return res.status(415).send("Invalid Content Type");
4143
}
4244

4345

4446
var containerPath = file.uriToFilename(req.path, options.root);
4547
debug("POST -- Container path: " + containerPath);
46-
47-
// Container not found/invalid
48-
if (metadata.isMetadataFile(containerPath)) {
49-
debug("POST -- Invalid container.");
50-
return res.sendStatus(404);
51-
}
5248

5349
// Not a container
5450
if (containerPath[containerPath.length - 1] != '/') {
@@ -75,7 +71,6 @@ function handler(req, res) {
7571
}
7672

7773
var resourceGraph = $rdf.graph();
78-
// TODO make sure correct text is selected
7974
var requestText = req.convertedText || req.text;
8075
var uri = file.uriBase(req);
8176
var resourceBaseUri = file.filenameToBaseUri(
@@ -136,5 +131,4 @@ function handler(req, res) {
136131
}
137132
}
138133

139-
140134
exports.handler = handler;

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"request": "^2.58.0",
4848
"response-time": "^2.3.1",
4949
"string": "^3.3.0",
50+
"sync-request": "^2.0.1",
5051
"webid": "^0.2.2"
5152
},
5253
"devDependencies": {

0 commit comments

Comments
 (0)