Skip to content

Commit 643c6de

Browse files
author
nicola
committed
Merge branch 'master' of https://github.com/linkeddata/ldnode
2 parents ef887a7 + f0f808c commit 643c6de

24 files changed

Lines changed: 885 additions & 841 deletions

‎.travis.yml‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,3 @@ language: node_js
33
node_js:
44
- "0.12"
55

6-
before_script:
7-
- npm install --quiet -g grunt-cli karma bower
8-
- make &
9-
- sleep 5
10-
11-
after_script: killall node
12-

‎Makefile‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# see also JSON validator http://jsonformatter.curiousconcept.com for the package.json file
22

33
run-for-test:
4-
node server.js -p 3456 -v --uriBase http://localhost:3456/test/ --fileBase `pwd`/test/
4+
node bin/ldnode.js -p 3456 -v --uriBase http://localhost:3456/test/ --fileBase `pwd`/test/
55

66
run-for-test-login:
7-
node server.js -p 3456 -v --webid --uriBase https://localhost:3456/test/ --fileBase `pwd`/test/
7+
node bin/ldnode.js -p 3456 -v --webid --uriBase https://localhost:3456/test/ --fileBase `pwd`/test/
88

99
run-for-live-test:
1010
NODE_PATH=.:../tabulator-firefox/content/js/rdf/dist/ node server.js \

‎README.md‎

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Linked Data Platform server based on [rdflib.js](https://github.com/linkeddata/r
1111
- [x] Access control using RDF ACLs
1212
- [x] WebID Authentication
1313
- [x] Real-time live updates (using websokets)
14-
- [ ] Mount as express' router
14+
- [x] Mount as express' router
1515

1616

1717
## Install
@@ -20,7 +20,49 @@ Linked Data Platform server based on [rdflib.js](https://github.com/linkeddata/r
2020
npm install
2121
```
2222

23-
## Command line tool
23+
## Usage
24+
25+
### Library
26+
#### Simple
27+
28+
```javascript
29+
var ldnode = require('ldnode')
30+
31+
var ldp = ldnode.createServer({
32+
uriBase: "http://example.com/test/",
33+
fileBase: __dirname + '/test/'
34+
})
35+
ldp.listen(1234, function() {
36+
// Started Linked Data Platform
37+
})
38+
```
39+
40+
#### Advanced
41+
42+
You can integrate it with your existing express app
43+
44+
```javascript
45+
var ldnode = require('ldnode')
46+
var app = require('express')()
47+
app.use('/test', ldnode(opts))
48+
...
49+
```
50+
51+
##### Logging
52+
53+
If you are running your own app
54+
55+
```bash
56+
$ DEBUG="ldnode:*" node app.js
57+
```
58+
59+
or simply
60+
61+
```bash
62+
$ ldnode -v
63+
```
64+
65+
### Command line tool
2466

2567
npm install -g ldnode
2668

@@ -55,8 +97,9 @@ The command line tool has the following options
5597
The tests assume that there is a running ldnode.
5698

5799
```bash
58-
# on a terminal
59-
make
60-
# on another terminal
61-
npm test
100+
$ npm test
62101
```
102+
103+
## License
104+
105+
MIT

‎acl.js‎

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@ var path = require('path');
88
var $rdf = require('rdflib');
99
var S = require('string');
1010

11+
var debug = require('./logging').ACL;
1112
var file = require('./fileStore.js');
12-
var logging = require('./logging.js');
13-
var options = require('./options.js');
14-
1513
var ns = require('./vocab/ns.js').ns;
1614
var rdfVocab = require('./vocab/rdf.js');
1715

1816
var aclExtension = ".acl";
1917

2018
function allow(mode, req, res) {
19+
var options = req.app.locals.ldp;
2120
var origin = req.get('origin');
2221
origin = origin ? origin : '';
2322

2423
var accessType = "accessTo";
2524

26-
var filepath = file.uriToFilename(req.path);
27-
var relativePath = file.uriToRelativeFilename(req.path);
25+
var filepath = file.uriToFilename(req.path, options.fileBase);
26+
var relativePath = file.uriToRelativeFilename(req.path, options.fileBase);
2827
var depth = relativePath.split('/');
2928

3029
//Handle glob requests
@@ -38,27 +37,27 @@ function allow(mode, req, res) {
3837
for (var i = 0; i < depth.length; i++) {
3938
var pathAcl = S(filepath).endsWith(aclExtension) ?
4039
filepath : filepath + aclExtension;
41-
var pathUri = file.filenameToBaseUri(filepath);
40+
var pathUri = file.filenameToBaseUri(filepath, options.uriBase, options.fileBase);
4241
relativePath = path.relative(options.fileBase, filepath);
4342

44-
logging.log("ACL -- Checking " + accessType + "<" + mode + "> to " +
43+
debug("Checking " + accessType + "<" + mode + "> to " +
4544
pathUri + " for WebID: " + req.session.userId);
46-
logging.log("ACL -- Looking for policies in " + pathAcl);
45+
debug("Looking for policies in " + pathAcl);
4746

4847
var aclData;
4948
var aclGraph = $rdf.graph();
5049
try {
5150
aclData = fs.readFileSync(pathAcl, {encoding: 'utf8'});
5251
$rdf.parse(aclData, aclGraph, pathUri, 'text/turtle');
5352
} catch (parseErr) {
54-
logging.log("ACL -- Error parsing ACL policy: " + parseErr);
53+
debug("Error parsing ACL policy: " + parseErr);
5554
//Resetting graph to prevent the code from taking the next if brach.
5655
aclGraph = $rdf.graph();
5756
}
5857

5958

6059
if (aclGraph.statements.length > 0) {
61-
logging.log("ACL -- Found policies in " + pathAcl);
60+
debug("Found policies in " + pathAcl);
6261
var controlStatements = aclGraph.each(undefined, ns.acl("mode"),
6362
ns.acl("Control"));
6463
for(var controlIndex in controlStatements) {
@@ -72,11 +71,11 @@ function allow(mode, req, res) {
7271
var originsControl = aclGraph.each(modeElem, ns.acl("origin"), undefined);
7372
var originControlValue;
7473
if (origin.length > 0 && originsControl.length > 0) {
75-
logging.log("ACL -- Origin set to: " + rdfVocab.brack(origin));
74+
debug("Origin set to: " + rdfVocab.brack(origin));
7675
for(var originsControlIndex in originsControl) {
7776
var originsControlElem = originsControl[originsControlIndex];
7877
if (rdfVocab.brack(origin) === originsControlElem.toString()) {
79-
logging.log("ACL -- Found policy for origin: " +
78+
debug("Found policy for origin: " +
8079
originsControlElem.toString());
8180
originControlValue = allowOrigin(mode, req, res, aclGraph, controlElem);
8281
if (originControlValue) {
@@ -86,7 +85,7 @@ function allow(mode, req, res) {
8685
}
8786
continue;
8887
} else {
89-
logging.log("ACL -- No origin found, moving on.");
88+
debug("No origin found, moving on.");
9089
}
9190
originControlValue = allowOrigin(mode, req, res, aclGraph, controlElem);
9291
if (originControlValue) {
@@ -96,7 +95,7 @@ function allow(mode, req, res) {
9695
var ownerStatements = aclGraph.each(accessElem,
9796
ns.acl("owner"), aclGraph.sym(req.session.userId));
9897
for(var ownerIndex in ownerStatements) {
99-
logging.log("ACL -- " + mode + " access allowed (as owner)" +
98+
debug(mode + " access allowed (as owner)" +
10099
" for: " + req.session.userId);
101100
return {
102101
status: 200,
@@ -107,7 +106,7 @@ function allow(mode, req, res) {
107106
var agentStatements = aclGraph.each(controlElem,
108107
ns.acl("agent"), aclGraph.sym(req.session.userId));
109108
for(var agentIndex in agentStatements) {
110-
logging.log("ACL -- " + mode + " access allowed (as agent)" +
109+
debug(mode + " access allowed (as agent)" +
111110
" for: " + req.session.userId);
112111
return {
113112
status: 200,
@@ -119,9 +118,9 @@ function allow(mode, req, res) {
119118
ns.acl("agentClass"), undefined);
120119
for (var agentClassIndex in agentClassStatements) {
121120
var agentClassElem = agentClassStatements[agentClassIndex];
122-
logging.log("ACL -- Found agentClass policy");
121+
debug("Found agentClass policy");
123122
if (agentClassElem.sameTerm(ns.foaf("Agent"))) {
124-
logging.log("ACL -- " + mode +
123+
debug(mode +
125124
" access allowed as FOAF agent");
126125
return {
127126
status: 200,
@@ -141,7 +140,7 @@ function allow(mode, req, res) {
141140
agentClassElem, ns.foaf("member"),
142141
groupGraph.sym(req.session.userId));
143142
for(var memberIndex in memberStatements) {
144-
logging.log("ACL -- " + req.session.userId +
143+
debug(req.session.userId +
145144
" listed as member of the group " + groupURI);
146145
return {
147146
status: 200,
@@ -156,19 +155,19 @@ function allow(mode, req, res) {
156155
var modeStatements = aclGraph.each(undefined, ns.acl("mode"), ns.acl(mode));
157156
for(var modeIndex in modeStatements) {
158157
var modeElem = modeStatements[modeIndex];
159-
logging.log("ACL -- Found " + accessType + " policy for <" + mode + ">");
158+
debug("Found " + accessType + " policy for <" + mode + ">");
160159
var accessTypeStatements = aclGraph.each(modeElem, ns.acl(accessType),
161160
aclGraph.sym(pathUri));
162161
for(var accessTypeIndex in accessTypeStatements) {
163162
var accessTypeElem = accessTypeStatements[accessTypeIndex];
164163
var origins = aclGraph.each(modeElem, ns.acl("origin"), undefined);
165164
var originValue;
166165
if (origin.length > 0 && origins.length > 0) {
167-
logging.log("ACL -- Origin set to: " + rdfVocab.brack(origin));
166+
debug("Origin set to: " + rdfVocab.brack(origin));
168167
for(var originsIndex in origins) {
169168
var originsElem = origins[originsIndex];
170169
if (rdfVocab.brack(origin) === originsElem.toString()) {
171-
logging.log("ACL -- Found policy for origin: " +
170+
debug("Found policy for origin: " +
172171
originsElem.toString());
173172
originValue = allowOrigin(mode, req, res, aclGraph, modeElem);
174173
if (originValue) {
@@ -178,7 +177,7 @@ function allow(mode, req, res) {
178177
}
179178
continue;
180179
} else {
181-
logging.log("ACL -- No origin found, moving on.");
180+
debug("No origin found, moving on.");
182181
}
183182
originValue = allowOrigin(mode, req, res, aclGraph, modeElem);
184183
if (originValue) {
@@ -188,13 +187,13 @@ function allow(mode, req, res) {
188187
}
189188

190189
if (req.session.userId.length === 0 || req.session.identified === false) {
191-
logging.log("ACL -- Authentication required");
190+
debug("Authentication required");
192191
return {
193192
status: 401,
194193
err: "Access to " + pathUri + " requires authorization"
195194
};
196195
}
197-
logging.log("ACL -- " + mode + " access denied for: " + req.session.userId);
196+
debug(mode + " access denied for: " + req.session.userId);
198197
return {
199198
status: 403,
200199
err: "Access denied for " + req.session.userId
@@ -225,19 +224,19 @@ function allow(mode, req, res) {
225224
}
226225
}
227226

228-
logging.log("ACL -- No ACL policies present - access allowed");
227+
debug("No ACL policies present - access allowed");
229228
return {
230229
status: 200,
231230
err: null
232231
};
233232
}
234233

235234
function allowOrigin(mode, req, res, aclGraph, subject) {
236-
logging.log("ACL -- In allow origin");
235+
debug("In allow origin");
237236
var ownerStatements = aclGraph.each(subject, ns.acl("owner"),
238237
aclGraph.sym(req.session.userId));
239238
for (var ownerIndex in ownerStatements) {
240-
logging.log("ACL -- " + mode + " access allowed (as owner) for: " + req.session.userId);
239+
debug(mode + " access allowed (as owner) for: " + req.session.userId);
241240
return {
242241
status: 200,
243242
err: null
@@ -246,7 +245,7 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
246245
var agentStatements = aclGraph.each(subject, ns.acl("agent"),
247246
aclGraph.sym(req.session.userId));
248247
for (var agentIndex in agentStatements) {
249-
logging.log("ACL -- " + mode + " access allowed (as agent) for: " + req.session.userId);
248+
debug(mode + " access allowed (as agent) for: " + req.session.userId);
250249
return {
251250
status: 200,
252251
return: null
@@ -256,9 +255,9 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
256255
for (var agentClassIndex in agentClassStatements) {
257256
var agentClassElem = agentClassStatements[agentClassIndex];
258257
//Check for FOAF groups
259-
logging.log("ACL -- Found agentClass policy");
258+
debug("Found agentClass policy");
260259
if (agentClassElem.sameTerm(ns.foaf("Agent"))) {
261-
logging.log("ACL -- " + mode + " allowed access as FOAF agent");
260+
debug(mode + " allowed access as FOAF agent");
262261
return {
263262
status: 200,
264263
err: null
@@ -273,7 +272,7 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
273272
var memberStatements = groupGraph.each(agentClassElem, ns.foaf("member"),
274273
groupGraph.sym(req.session.userId));
275274
for (var memberIndex in memberStatements) {
276-
logging.log("ACL -- " + req.session.userId + " listed as member of the group " + groupURI);
275+
debug(req.session.userId + " listed as member of the group " + groupURI);
277276
return {
278277
status: 200,
279278
err: null
@@ -284,6 +283,7 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
284283
}
285284

286285
function allowIfACLEnabled(mode, req, res, next) {
286+
var options = req.app.locals.ldp;
287287
if (!options.webid) {
288288
return next();
289289
} else {
@@ -311,6 +311,7 @@ exports.allowAppendHandler = function(req, res, next) {
311311
};
312312

313313
exports.allowAppendThenWriteHandler = function(req, res, next) {
314+
var options = req.app.locals.ldp;
314315
if (!options.webid) {
315316
return next();
316317
} else {

‎bin/ldnode.js‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var fs = require('fs');
2+
var debug = require('../logging').server;
3+
var ldnode = require('../index');
4+
5+
var argv = require('optimist')
6+
.boolean('cors')
7+
.boolean('v')
8+
.boolean('live')
9+
.argv;
10+
11+
if (argv.h || argv.help || argv['?']) {
12+
console.log([
13+
"usage: ldnode [path] [options]",
14+
"",
15+
"options:",
16+
" --uriBase Address, port, and default path of the server. (Example: http://localhost:3000/test/)",
17+
" --fileBase Base location to serve resources. Requests whose paths do not have fileBase as a prefix will be ignored",
18+
" --live Offer and support live updates",
19+
" -p Port to use",
20+
" -v Log messages to console",
21+
" --changesSuffix The suffix that will be used to identify the requests that will subscribe to changes to the object requested. Defaults to ,changes",
22+
" --cors Enable CORS via the 'Access-Control-Allow-Origin' header",
23+
" -c Set cache time (in seconds). e.g. -c10 for 10 seconds.",
24+
" To disable caching, use -c-1.",
25+
" --changesSuffix sss Change the URI suffix used for the URI of a change stream",
26+
" --SSESuffix sss Change the URI suffix used for the URI of a SSE stream",
27+
"",
28+
" -S --ssl Enable https.",
29+
" -C --cert Path to ssl cert file (default: cert.pem).",
30+
" -K --key Path to ssl key file (default: key.pem).",
31+
"",
32+
" --webid Enable WebID authentication",
33+
" --privateKey Path to the private key used to enable webid authentication",
34+
" --cert Path to the private key used to enable webid authentication",
35+
" -h --help Print this list and exit."
36+
].join('\n'));
37+
process.exit();
38+
}
39+
40+
// Command line handling
41+
// Signal handling
42+
if (process.platform !== 'win32') {
43+
// Signal handlers don't work on Windows.
44+
process.on('SIGINT', function() {
45+
debug("http-server stopped.");
46+
process.exit();
47+
});
48+
}
49+
process.env.DEBUG = argv.v ? '*' : false;
50+
var app = ldnode.createServer(argv);
51+
app.listen(argv.p, function() {
52+
debug('LDP started on port ' + argv.p);
53+
});

0 commit comments

Comments
 (0)