forked from nodeSolidServer/node-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacl.js
More file actions
373 lines (340 loc) · 14.5 KB
/
Copy pathacl.js
File metadata and controls
373 lines (340 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*jslint node: true*/
/*jshint loopfunc:true */
"use strict";
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var $rdf = require('rdflib');
var S = require('string');
var debug = require('./logging').ACL;
var file = require('./fileStore.js');
var ns = require('./vocab/ns.js').ns;
var rdfVocab = require('./vocab/rdf.js');
function allow(mode, req, res) {
var options = req.app.locals.ldp;
var origin = req.get('origin');
origin = origin ? origin : '';
var accessType = "accessTo";
var filepath = file.uriToFilename(req.path, options.root);
var relativePath = file.uriToRelativeFilename(req.path, options.root);
var depth = relativePath.split('/');
//Get user from request
var userId = getUserId(req);
//Handle glob requests
if (req.method === 'GET' && glob.hasMagic(filepath)) {
return {
status: 200,
err: null
};
}
for (var i = 0; i < depth.length; i++) {
var pathAcl = S(filepath).endsWith(options.suffixAcl) ?
filepath : filepath + options.suffixAcl;
var uri = file.uriAbs(req);
var pathUri = file.filenameToBaseUri(filepath, uri, options.root);
relativePath = path.relative(options.root, filepath);
debug("Checking " + accessType + "<" + mode + "> to " +
pathUri + " for WebID: " + userId);
debug("Looking for policies in " + pathAcl);
var aclData;
var aclGraph = $rdf.graph();
try {
aclData = fs.readFileSync(pathAcl, {encoding: 'utf8'});
$rdf.parse(aclData, aclGraph, pathUri, 'text/turtle');
} catch (parseErr) {
debug("Error parsing ACL policy: " + parseErr);
//Resetting graph to prevent the code from taking the next if brach.
aclGraph = $rdf.graph();
}
if (aclGraph.statements.length > 0) {
debug("Found policies in " + pathAcl);
var controlStatements = aclGraph.each(undefined, ns.acl("mode"),
ns.acl("Control"));
for(var controlIndex in controlStatements) {
var controlElem = controlStatements[controlIndex];
var accessStatements = aclGraph.each(controlElem,
ns.acl(accessType), aclGraph.sym(pathUri));
for(var accessIndex in accessStatements) {
var accessElem = accessStatements[accessIndex];
var originsControl = aclGraph.each(modeElem, ns.acl("origin"), undefined);
var originControlValue;
if (origin.length > 0 && originsControl.length > 0) {
debug("Origin set to: " + rdfVocab.brack(origin));
for(var originsControlIndex in originsControl) {
var originsControlElem = originsControl[originsControlIndex];
if (rdfVocab.brack(origin) === originsControlElem.toString()) {
debug("Found policy for origin: " +
originsControlElem.toString());
originControlValue = allowOrigin(mode, userId, res, aclGraph, controlElem);
if (originControlValue) {
return originControlValue;
}
}
}
continue;
} else {
debug("No origin found, moving on.");
}
originControlValue = allowOrigin(mode, userId, res, aclGraph, controlElem);
if (originControlValue) {
return originControlValue;
}
var ownerStatements = aclGraph.each(accessElem,
ns.acl("owner"), aclGraph.sym(userId));
for(var ownerIndex in ownerStatements) {
debug(mode + " access allowed (as owner)" +
" for: " + userId);
return {
status: 200,
err: null
};
}
var agentStatements = aclGraph.each(controlElem,
ns.acl("agent"), aclGraph.sym(userId));
for(var agentIndex in agentStatements) {
debug(mode + " access allowed (as agent)" +
" for: " + userId);
return {
status: 200,
err: null
};
}
var agentClassStatements = aclGraph.each(controlElem,
ns.acl("agentClass"), undefined);
for (var agentClassIndex in agentClassStatements) {
var agentClassElem = agentClassStatements[agentClassIndex];
debug("Found agentClass policy");
if (agentClassElem.sameTerm(ns.foaf("Agent"))) {
debug(mode +
" access allowed as FOAF agent");
return {
status: 200,
err: null
};
}
var groupURI = rdfVocab.debrack(agentClassElem.toString());
var groupGraph = $rdf.graph();
var groupFetcher = $rdf.fetcher(groupGraph, 3000, false);
groupFetcher.nowOrWhenFetched(groupURI, null, function(ok, err) {});
var typeStatements = groupGraph.each(agentClassElem,
ns.rdf("type"), ns.foaf("Group"));
if (groupGraph.statements.length > 0 &&
typeStatements.length > 0) {
var memberStatements = groupGraph.each(
agentClassElem, ns.foaf("member"),
groupGraph.sym(userId));
for(var memberIndex in memberStatements) {
debug(userId +
" listed as member of the group " + groupURI);
return {
status: 200,
err: null
};
}
}
}
}
}
var modeStatements = aclGraph.each(undefined, ns.acl("mode"), ns.acl(mode));
for(var modeIndex in modeStatements) {
var modeElem = modeStatements[modeIndex];
debug("Found " + accessType + " policy for <" + mode + ">");
var accessTypeStatements = aclGraph.each(modeElem, ns.acl(accessType),
aclGraph.sym(pathUri));
for(var accessTypeIndex in accessTypeStatements) {
var accessTypeElem = accessTypeStatements[accessTypeIndex];
var origins = aclGraph.each(modeElem, ns.acl("origin"), undefined);
var originValue;
if (origin.length > 0 && origins.length > 0) {
debug("Origin set to: " + rdfVocab.brack(origin));
for(var originsIndex in origins) {
var originsElem = origins[originsIndex];
if (rdfVocab.brack(origin) === originsElem.toString()) {
debug("Found policy for origin: " +
originsElem.toString());
originValue = allowOrigin(mode, userId, res, aclGraph, modeElem);
if (originValue) {
return originValue;
}
}
}
continue;
} else {
debug("No origin found, moving on.");
}
originValue = allowOrigin(mode, userId, res, aclGraph, modeElem);
if (originValue) {
return originValue;
}
}
}
if (userId.length === 0 || req.session.identified === false) {
debug("Authentication required");
return {
status: 401,
err: "Access to " + pathUri + " requires authorization"
};
}
debug(mode + " access denied for: " + userId);
return {
status: 403,
err: "Access denied for " + userId
};
}
accessType = "defaultForNew";
if (i === 0) {
if (path.dirname(path.dirname(relativePath)) == '.') {
filepath = options.root;
} else if (S(relativePath).endsWith("/")) {
filepath = options.root + path.dirname(path.dirname(relativePath));
} else {
filepath = options.root + path.dirname(relativePath);
}
} else {
if (relativePath.length === 0) {
break;
} else if (path.dirname(path.dirname(relativePath)) === '.') {
filepath = options.root;
} else {
filepath = options.root + path.dirname(path.dirname(relativePath));
}
}
if (!S(filepath).endsWith("/")) {
filepath += "/";
}
}
debug("No ACL policies present - access allowed");
return {
status: 200,
err: null
};
}
function allowOrigin(mode, userId, res, aclGraph, subject) {
debug("In allow origin");
var ownerStatements = aclGraph.each(subject, ns.acl("owner"),
aclGraph.sym(userId));
for (var ownerIndex in ownerStatements) {
debug(mode + " access allowed (as owner) for: " + userId);
return {
status: 200,
err: null
};
}
var agentStatements = aclGraph.each(subject, ns.acl("agent"),
aclGraph.sym(userId));
for (var agentIndex in agentStatements) {
debug(mode + " access allowed (as agent) for: " + userId);
return {
status: 200,
return: null
};
}
var agentClassStatements = aclGraph.each(subject, ns.acl("agentClass"), undefined);
for (var agentClassIndex in agentClassStatements) {
var agentClassElem = agentClassStatements[agentClassIndex];
//Check for FOAF groups
debug("Found agentClass policy");
if (agentClassElem.sameTerm(ns.foaf("Agent"))) {
debug(mode + " allowed access as FOAF agent");
return {
status: 200,
err: null
};
}
var groupURI = rdfVocab.debrack(agentClassElem.toString());
var groupGraph = $rdf.graph();
var groupFetcher = $rdf.fetcher(groupGraph, 3000, false);
groupFetcher.nowOrWhenFetched(groupURI, null, function(ok, err) {});
var typeStatements = groupGraph.each(agentClassElem, ns.rdf("type"), ns.foaf("Group"));
if (groupGraph.statements.length > 0 && typeStatements.length > 0) {
var memberStatements = groupGraph.each(agentClassElem, ns.foaf("member"),
groupGraph.sym(userId));
for (var memberIndex in memberStatements) {
debug(userId + " listed as member of the group " + groupURI);
return {
status: 200,
err: null
};
}
}
}
}
function getUserId(req) {
var userId;
if (req.get('On-Behalf-Of')) {
var delegator = rdfVocab.debrack(req.get('On-Behalf-Of'));
if (verifyDelegator(delegator, req.session.userId)) {
debug("Request User ID (delegation) :" + delegator);
userId = delegator;
} else {
debug("Delegation denied for " + req.session.userId + " by " + delegator);
userId = req.session.userId;
}
} else {
userId = req.session.userId;
}
return userId;
}
function verifyDelegator(delegator, delegatee) {
var delegatorGraph = $rdf.graph();
var delegatorFetcher = $rdf.fetcher(delegatorGraph, 3000, false);
debug("delegator: " + delegator);
delegatorFetcher.nowOrWhenFetched(delegator, undefined, function(ok, err) {
debug("ok: " + ok);
debug("error: " + err);
});
var delegatesStatements = delegatorGraph.each(delegatorGraph.sym(delegator),
delegatorGraph.sym("http://www.w3.org/ns/auth/acl#delegates"),
undefined);
for(var delegateeIndex in delegatesStatements) {
var delegateeValue = delegatesStatements[delegateeIndex];
if (rdfVocab.debrack(delegateeValue.toString()) === delegatee) {
return true;
}
}
return false;
}
function allowIfACLEnabled(mode, req, res, next) {
var options = req.app.locals.ldp;
if (!options.webid) {
return next();
} else {
var allowValue = allow(mode, req, res, next);
if (allowValue.status == 200) {
return next();
} else {
return res.status(allowValue.status).send(allowValue.err);
}
}
}
exports.allow = allow;
exports.allowReadHandler = function(req, res, next) {
allowIfACLEnabled("Read", req, res, next);
};
exports.allowWriteHandler = function(req, res, next) {
allowIfACLEnabled("Write", req, res, next);
};
exports.allowAppendHandler = function(req, res, next) {
allowIfACLEnabled("Append", req, res, next);
};
exports.allowAppendThenWriteHandler = function(req, res, next) {
var options = req.app.locals.ldp;
if (!options.webid) {
return next();
} else {
var allowAppendValue = allow("Append", req, res, next);
if (allowAppendValue.status == 200) {
return next();
} else {
var allowWriteValue = allow("Write", req, res, next);
if(allowWriteValue.status == 200) {
return next();
} else {
return res.status(allowWriteValue.status).send(allowWriteValue.err);
}
}
}
};
exports.allowControlHandler = function(req, res, next) {
allowIfACLEnabled("Control", req, res, next);
};