Skip to content

Commit f102e51

Browse files
author
Martin Martinez Rivera
committed
Added WebID delegation code.
1 parent f144345 commit f102e51

1 file changed

Lines changed: 61 additions & 21 deletions

File tree

‎acl.js‎

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ function allow(mode, req, res) {
2424
var relativePath = file.uriToRelativeFilename(req.path, options.base);
2525
var depth = relativePath.split('/');
2626

27+
//Get user from request
28+
var userId = getUserId(req);
29+
2730
//Handle glob requests
2831
if (req.method === 'GET' && glob.hasMagic(filepath)) {
2932
return {
@@ -40,7 +43,7 @@ function allow(mode, req, res) {
4043
relativePath = path.relative(options.base, filepath);
4144

4245
debug("Checking " + accessType + "<" + mode + "> to " +
43-
pathUri + " for WebID: " + req.session.userId);
46+
pathUri + " for WebID: " + userId);
4447
debug("Looking for policies in " + pathAcl);
4548

4649
var aclData;
@@ -76,7 +79,7 @@ function allow(mode, req, res) {
7679
if (rdfVocab.brack(origin) === originsControlElem.toString()) {
7780
debug("Found policy for origin: " +
7881
originsControlElem.toString());
79-
originControlValue = allowOrigin(mode, req, res, aclGraph, controlElem);
82+
originControlValue = allowOrigin(mode, userId, res, aclGraph, controlElem);
8083
if (originControlValue) {
8184
return originControlValue;
8285
}
@@ -86,27 +89,27 @@ function allow(mode, req, res) {
8689
} else {
8790
debug("No origin found, moving on.");
8891
}
89-
originControlValue = allowOrigin(mode, req, res, aclGraph, controlElem);
92+
originControlValue = allowOrigin(mode, userId, res, aclGraph, controlElem);
9093
if (originControlValue) {
9194
return originControlValue;
9295
}
9396

9497
var ownerStatements = aclGraph.each(accessElem,
95-
ns.acl("owner"), aclGraph.sym(req.session.userId));
98+
ns.acl("owner"), aclGraph.sym(userId));
9699
for(var ownerIndex in ownerStatements) {
97100
debug(mode + " access allowed (as owner)" +
98-
" for: " + req.session.userId);
101+
" for: " + userId);
99102
return {
100103
status: 200,
101104
err: null
102105
};
103106
}
104107

105108
var agentStatements = aclGraph.each(controlElem,
106-
ns.acl("agent"), aclGraph.sym(req.session.userId));
109+
ns.acl("agent"), aclGraph.sym(userId));
107110
for(var agentIndex in agentStatements) {
108111
debug(mode + " access allowed (as agent)" +
109-
" for: " + req.session.userId);
112+
" for: " + userId);
110113
return {
111114
status: 200,
112115
err: null
@@ -137,9 +140,9 @@ function allow(mode, req, res) {
137140
typeStatements.length > 0) {
138141
var memberStatements = groupGraph.each(
139142
agentClassElem, ns.foaf("member"),
140-
groupGraph.sym(req.session.userId));
143+
groupGraph.sym(userId));
141144
for(var memberIndex in memberStatements) {
142-
debug(req.session.userId +
145+
debug(userId +
143146
" listed as member of the group " + groupURI);
144147
return {
145148
status: 200,
@@ -168,7 +171,7 @@ function allow(mode, req, res) {
168171
if (rdfVocab.brack(origin) === originsElem.toString()) {
169172
debug("Found policy for origin: " +
170173
originsElem.toString());
171-
originValue = allowOrigin(mode, req, res, aclGraph, modeElem);
174+
originValue = allowOrigin(mode, userId, res, aclGraph, modeElem);
172175
if (originValue) {
173176
return originValue;
174177
}
@@ -178,24 +181,24 @@ function allow(mode, req, res) {
178181
} else {
179182
debug("No origin found, moving on.");
180183
}
181-
originValue = allowOrigin(mode, req, res, aclGraph, modeElem);
184+
originValue = allowOrigin(mode, userId, res, aclGraph, modeElem);
182185
if (originValue) {
183186
return originValue;
184187
}
185188
}
186189
}
187190

188-
if (req.session.userId.length === 0 || req.session.identified === false) {
191+
if (userId.length === 0 || req.session.identified === false) {
189192
debug("Authentication required");
190193
return {
191194
status: 401,
192195
err: "Access to " + pathUri + " requires authorization"
193196
};
194197
}
195-
debug(mode + " access denied for: " + req.session.userId);
198+
debug(mode + " access denied for: " + userId);
196199
return {
197200
status: 403,
198-
err: "Access denied for " + req.session.userId
201+
err: "Access denied for " + userId
199202
};
200203
}
201204

@@ -230,21 +233,21 @@ function allow(mode, req, res) {
230233
};
231234
}
232235

233-
function allowOrigin(mode, req, res, aclGraph, subject) {
236+
function allowOrigin(mode, userId, res, aclGraph, subject) {
234237
debug("In allow origin");
235238
var ownerStatements = aclGraph.each(subject, ns.acl("owner"),
236-
aclGraph.sym(req.session.userId));
239+
aclGraph.sym(userId));
237240
for (var ownerIndex in ownerStatements) {
238-
debug(mode + " access allowed (as owner) for: " + req.session.userId);
241+
debug(mode + " access allowed (as owner) for: " + userId);
239242
return {
240243
status: 200,
241244
err: null
242245
};
243246
}
244247
var agentStatements = aclGraph.each(subject, ns.acl("agent"),
245-
aclGraph.sym(req.session.userId));
248+
aclGraph.sym(userId));
246249
for (var agentIndex in agentStatements) {
247-
debug(mode + " access allowed (as agent) for: " + req.session.userId);
250+
debug(mode + " access allowed (as agent) for: " + userId);
248251
return {
249252
status: 200,
250253
return: null
@@ -269,9 +272,9 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
269272
var typeStatements = groupGraph.each(agentClassElem, ns.rdf("type"), ns.foaf("Group"));
270273
if (groupGraph.statements.length > 0 && typeStatements.length > 0) {
271274
var memberStatements = groupGraph.each(agentClassElem, ns.foaf("member"),
272-
groupGraph.sym(req.session.userId));
275+
groupGraph.sym(userId));
273276
for (var memberIndex in memberStatements) {
274-
debug(req.session.userId + " listed as member of the group " + groupURI);
277+
debug(userId + " listed as member of the group " + groupURI);
275278
return {
276279
status: 200,
277280
err: null
@@ -281,6 +284,43 @@ function allowOrigin(mode, req, res, aclGraph, subject) {
281284
}
282285
}
283286

287+
function getUserId(req) {
288+
var userId;
289+
if (req.get('On-Behalf-Of')) {
290+
var delegator = rdfVocab.debrack(req.get('On-Behalf-Of'));
291+
if (verifyDelegator(delegator, req.session.userId)) {
292+
debug("Request User ID (delegation) :" + delegator);
293+
userId = delegator;
294+
} else {
295+
debug("Delegation denied for " + req.session.userId + " by " + delegator);
296+
userId = req.session.userId;
297+
}
298+
} else {
299+
userId = req.session.userId;
300+
}
301+
return userId;
302+
}
303+
304+
function verifyDelegator(delegator, delegatee) {
305+
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);
315+
for(var delegateeIndex in delegatesStatements) {
316+
var delegateeValue = delegatesStatements[delegateeIndex];
317+
if (rdfVocab.debrack(delegateeValue.toString()) === delegatee) {
318+
return true;
319+
}
320+
}
321+
return false;
322+
}
323+
284324
function allowIfACLEnabled(mode, req, res, next) {
285325
var options = req.app.locals.ldp;
286326
if (!options.webid) {

0 commit comments

Comments
 (0)