Skip to content

Commit 393c2f5

Browse files
author
Martin Martinez Rivera
committed
Merge branch 'martinmr-acl-https'
2 parents d954384 + f16afdd commit 393c2f5

7 files changed

Lines changed: 132 additions & 110 deletions

File tree

‎Makefile‎

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎README.md‎

Lines changed: 16 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:
@@ -118,6 +120,14 @@ or simply
118120
$ ldnode -v
119121
```
120122

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

123133
The tests assume that there is a running ldnode.
@@ -128,6 +138,12 @@ $ npm test
128138
$ DEBUG="ldnode:*" npm test
129139
```
130140

141+
In order to test a single component, you can run
142+
143+
```javascript
144+
npm run test-(acl|formats|params|patch)
145+
```
146+
131147
## License
132148

133149
MIT

‎acl.js‎

Lines changed: 56 additions & 23 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
}
@@ -233,7 +236,7 @@ function allow(mode, req, res) {
233236
};
234237
}
235238

236-
function allowOrigin(mode, userId, res, aclGraph, subject) {
239+
function allowOrigin(mode, req, userId, aclGraph, subject) {
237240
debug("In allow origin");
238241
var ownerStatements = aclGraph.each(subject, ns.acl("owner"),
239242
aclGraph.sym(userId));
@@ -267,8 +270,7 @@ function allowOrigin(mode, userId, res, aclGraph, subject) {
267270
}
268271
var groupURI = rdfVocab.debrack(agentClassElem.toString());
269272
var groupGraph = $rdf.graph();
270-
var groupFetcher = $rdf.fetcher(groupGraph, 3000, false);
271-
groupFetcher.nowOrWhenFetched(groupURI, null, function(ok, err) {});
273+
fetchDocument(groupGraph, groupURI, req);
272274
var typeStatements = groupGraph.each(agentClassElem, ns.rdf("type"), ns.foaf("Group"));
273275
if (groupGraph.statements.length > 0 && typeStatements.length > 0) {
274276
var memberStatements = groupGraph.each(agentClassElem, ns.foaf("member"),
@@ -284,11 +286,46 @@ function allowOrigin(mode, userId, res, aclGraph, subject) {
284286
}
285287
}
286288

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

304-
function verifyDelegator(delegator, delegatee) {
341+
function verifyDelegator(delegator, delegatee, req) {
305342
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);
343+
fetchDocument(delegatorGraph, delegator, req);
344+
var delegatesStatements = delegatorGraph
345+
.each(delegatorGraph.sym(delegator),
346+
delegatorGraph.sym("http://www.w3.org/ns/auth/acl#delegates"),
347+
undefined);
315348
for(var delegateeIndex in delegatesStatements) {
316349
var delegateeValue = delegatesStatements[delegateeIndex];
317350
if (rdfVocab.debrack(delegateeValue.toString()) === delegatee) {
@@ -354,11 +387,11 @@ exports.allowAppendThenWriteHandler = function(req, res, next) {
354387
if (!options.webid) {
355388
return next();
356389
} else {
357-
var allowAppendValue = allow("Append", req, res, next);
390+
var allowAppendValue = allow("Append", req);
358391
if (allowAppendValue.status == 200) {
359392
return next();
360393
} else {
361-
var allowWriteValue = allow("Write", req, res, next);
394+
var allowWriteValue = allow("Write", req);
362395
if(allowWriteValue.status == 200) {
363396
return next();
364397
} else {

‎bin/ldnode.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/env node
1+
#!/usr/bin/env node
22

33
var fs = require('fs');
44
var path = require('path');

‎index.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var request = require('request');
1616
var debug = require('./logging').settings;
1717
var debugSubscription = require('./logging').subscription;
1818
var debugServer = require('./logging').server;
19+
var uuid = require('node-uuid');
1920

2021
// ldnode dependencies
2122
var acl = require('./acl.js');
@@ -40,9 +41,9 @@ function ldnode (argv) {
4041
// Setting options as local variable
4142
app.locals.ldp = opts;
4243

43-
// Session [TODO]
44+
// Session
4445
app.use(session({
45-
secret: opts.secret || 'node-ldp',
46+
secret: opts.secret || uuid.v1(),
4647
saveUninitialized: false,
4748
resave: false
4849
}));

‎package.json‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"request": "^2.58.0",
4747
"response-time": "^2.3.1",
4848
"string": "^3.3.0",
49+
"sync-request": "^2.0.1",
4950
"webid": "^0.2.2"
5051
},
5152
"devDependencies": {
@@ -55,13 +56,15 @@
5556
},
5657
"main": "index.js",
5758
"scripts": {
58-
"start": "node bin/ldnode.js",
59+
"start": "node ./bin/ldnode.js",
5960
"test": "./node_modules/mocha/bin/mocha ./test/*.js",
6061
"test-debug": "DEBUG='ldnode:*' ./node_modules/mocha/bin/mocha ./test/*.js",
6162
"test-acl": "./node_modules/mocha/bin/mocha ./test/acl.js",
6263
"test-params": "./node_modules/mocha/bin/mocha ./test/params.js",
6364
"test-http": "./node_modules/mocha/bin/mocha ./test/http.js",
64-
"test-formats": "./node_modules/mocha/bin/mocha ./test/http.js"
65+
"test-formats": "./node_modules/mocha/bin/mocha ./test/http.js",
66+
"ldp-webid": "node ./bin/ldnode.js --webid --cert ./test/keys/cert.pem --key ./test/keys/key.pem -v",
67+
"ldp-ssl": "node ./bin/ldnode.js --cert ./test/keys/cert.pem --key ./test/keys/key.pem -v"
6568
},
6669
"bin": {
6770
"ldnode": "./bin/ldnode.js"

0 commit comments

Comments
 (0)