@@ -8,6 +8,7 @@ var path = require('path');
88var $rdf = require ( 'rdflib' ) ;
99var request = require ( 'sync-request' ) ;
1010var S = require ( 'string' ) ;
11+ var url = require ( 'url' ) ;
1112
1213var debug = require ( './logging' ) . ACL ;
1314var file = require ( './fileStore.js' ) ;
@@ -82,7 +83,7 @@ function allow(mode, req, res) {
8283 if ( rdfVocab . brack ( origin ) === originsControlElem . toString ( ) ) {
8384 debug ( "Found policy for origin: " +
8485 originsControlElem . toString ( ) ) ;
85- originControlValue = allowOrigin ( mode , userId , res , aclGraph , controlElem ) ;
86+ originControlValue = allowOrigin ( mode , req , res , userId , aclGraph , controlElem ) ;
8687 if ( originControlValue ) {
8788 return originControlValue ;
8889 }
@@ -92,7 +93,7 @@ function allow(mode, req, res) {
9293 } else {
9394 debug ( "No origin found, moving on." ) ;
9495 }
95- originControlValue = allowOrigin ( mode , userId , res , aclGraph , controlElem ) ;
96+ originControlValue = allowOrigin ( mode , req , res , userId , aclGraph , controlElem ) ;
9697 if ( originControlValue ) {
9798 return originControlValue ;
9899 }
@@ -135,8 +136,7 @@ function allow(mode, req, res) {
135136
136137 var groupURI = rdfVocab . debrack ( agentClassElem . toString ( ) ) ;
137138 var groupGraph = $rdf . graph ( ) ;
138- var groupFetcher = $rdf . fetcher ( groupGraph , 3000 , false ) ;
139- groupFetcher . nowOrWhenFetched ( groupURI , null , function ( ok , err ) { } ) ;
139+ fetchDocument ( groupGraph , groupURI , req ) ;
140140 var typeStatements = groupGraph . each ( agentClassElem ,
141141 ns . rdf ( "type" ) , ns . foaf ( "Group" ) ) ;
142142 if ( groupGraph . statements . length > 0 &&
@@ -174,7 +174,7 @@ function allow(mode, req, res) {
174174 if ( rdfVocab . brack ( origin ) === originsElem . toString ( ) ) {
175175 debug ( "Found policy for origin: " +
176176 originsElem . toString ( ) ) ;
177- originValue = allowOrigin ( mode , userId , res , aclGraph , modeElem ) ;
177+ originValue = allowOrigin ( mode , req , res , userId , aclGraph , modeElem ) ;
178178 if ( originValue ) {
179179 return originValue ;
180180 }
@@ -184,7 +184,7 @@ function allow(mode, req, res) {
184184 } else {
185185 debug ( "No origin found, moving on." ) ;
186186 }
187- originValue = allowOrigin ( mode , userId , res , aclGraph , modeElem ) ;
187+ originValue = allowOrigin ( mode , req , res , userId , aclGraph , modeElem ) ;
188188 if ( originValue ) {
189189 return originValue ;
190190 }
@@ -236,7 +236,7 @@ function allow(mode, req, res) {
236236 } ;
237237}
238238
239- function allowOrigin ( mode , userId , res , aclGraph , subject ) {
239+ function allowOrigin ( mode , req , res , userId , aclGraph , subject ) {
240240 debug ( "In allow origin" ) ;
241241 var ownerStatements = aclGraph . each ( subject , ns . acl ( "owner" ) ,
242242 aclGraph . sym ( userId ) ) ;
@@ -270,18 +270,8 @@ function allowOrigin(mode, userId, res, aclGraph, subject) {
270270 }
271271 var groupURI = rdfVocab . debrack ( agentClassElem . toString ( ) ) ;
272272 var groupGraph = $rdf . graph ( ) ;
273- // var groupFetcher = $rdf.fetcher(groupGraph, 5000, false);
274- // debug("Fetching Group statements at " + groupURI);
275- // groupFetcher.nowOrWhenFetched(groupURI, undefined, function(ok) {
276- // debug("Group statements: " + groupGraph.statements);
277- // var typeStatements = groupGraph.each(agentClassElem, ns.rdf("type"), ns.foaf("Group"));
278- // debug(typeStatements);
279- // });
280- var response = request ( 'GET' , 'https://localhost:3457/test/resources/acl/testDir/group#' ) ;
281- debug ( response . getBody ( 'utf8' ) ) ;
282- $rdf . parse ( response . getBody ( 'utf8' ) , groupGraph , groupURI , 'text/turtle' ) ;
273+ fetchDocument ( groupGraph , groupURI , req ) ;
283274 var typeStatements = groupGraph . each ( agentClassElem , ns . rdf ( "type" ) , ns . foaf ( "Group" ) ) ;
284- debug ( typeStatements ) ;
285275 if ( groupGraph . statements . length > 0 && typeStatements . length > 0 ) {
286276 var memberStatements = groupGraph . each ( agentClassElem , ns . foaf ( "member" ) ,
287277 groupGraph . sym ( userId ) ) ;
@@ -296,11 +286,37 @@ function allowOrigin(mode, userId, res, aclGraph, subject) {
296286 }
297287}
298288
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 documentPath = file . uriToFilename ( S ( uri ) . chompLeft ( uriBase ) . s ,
297+ options . root ) ;
298+ var documentUri = url . parse ( documentPath ) ;
299+ documentPath = documentUri . pathname ;
300+ body = fs . readFileSync ( documentPath , { encoding : 'utf8' } ) ;
301+ } catch ( err ) {
302+ body = "" ;
303+ }
304+ } else {
305+ var response = request ( 'GET' , uri , {
306+ headers : {
307+ 'Accept' : 'text/turtle'
308+ }
309+ } ) ;
310+ body = response . getBody ( 'utf8' ) ;
311+ }
312+ $rdf . parse ( body , graph , uri , 'text/turtle' ) ;
313+ }
314+
299315function getUserId ( req ) {
300316 var userId ;
301317 if ( req . get ( 'On-Behalf-Of' ) ) {
302318 var delegator = rdfVocab . debrack ( req . get ( 'On-Behalf-Of' ) ) ;
303- if ( verifyDelegator ( delegator , req . session . userId ) ) {
319+ if ( verifyDelegator ( delegator , req . session . userId , req ) ) {
304320 debug ( "Request User ID (delegation) :" + delegator ) ;
305321 userId = delegator ;
306322 } else {
@@ -313,17 +329,13 @@ function getUserId(req) {
313329 return userId ;
314330}
315331
316- function verifyDelegator ( delegator , delegatee ) {
332+ function verifyDelegator ( delegator , delegatee , req ) {
317333 var delegatorGraph = $rdf . graph ( ) ;
318- var delegatorFetcher = $rdf . fetcher ( delegatorGraph , 3000 , false ) ;
319- debug ( "delegator: " + delegator ) ;
320- delegatorFetcher . nowOrWhenFetched ( delegator , undefined , function ( ok , err ) {
321- debug ( "ok: " + ok ) ;
322- debug ( "error: " + err ) ;
323- } ) ;
324- var delegatesStatements = delegatorGraph . each ( delegatorGraph . sym ( delegator ) ,
325- delegatorGraph . sym ( "http://www.w3.org/ns/auth/acl#delegates" ) ,
326- undefined ) ;
334+ fetchDocument ( delegatorGraph , delegator , req ) ;
335+ var delegatesStatements = delegatorGraph
336+ . each ( delegatorGraph . sym ( delegator ) ,
337+ delegatorGraph . sym ( "http://www.w3.org/ns/auth/acl#delegates" ) ,
338+ undefined ) ;
327339 for ( var delegateeIndex in delegatesStatements ) {
328340 var delegateeValue = delegatesStatements [ delegateeIndex ] ;
329341 if ( rdfVocab . debrack ( delegateeValue . toString ( ) ) === delegatee ) {
0 commit comments