@@ -6,14 +6,18 @@ var fs = require('fs');
66var glob = require ( 'glob' ) ;
77var path = require ( 'path' ) ;
88var $rdf = require ( 'rdflib' ) ;
9+ var request = require ( 'sync-request' ) ;
910var S = require ( 'string' ) ;
11+ var url = require ( 'url' ) ;
1012
1113var debug = require ( './logging' ) . ACL ;
1214var file = require ( './fileStore.js' ) ;
1315var ns = require ( './vocab/ns.js' ) . ns ;
1416var 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 }
@@ -206,8 +209,6 @@ function allow(mode, req, res) {
206209 if ( i === 0 ) {
207210 if ( path . dirname ( path . dirname ( relativePath ) ) == '.' ) {
208211 filepath = options . root ;
209- } else if ( S ( relativePath ) . endsWith ( "/" ) ) {
210- filepath = options . root + path . dirname ( path . dirname ( relativePath ) ) ;
211212 } else {
212213 filepath = options . root + path . dirname ( relativePath ) ;
213214 }
@@ -217,7 +218,7 @@ function allow(mode, req, res) {
217218 } else if ( path . dirname ( path . dirname ( relativePath ) ) === '.' ) {
218219 filepath = options . root ;
219220 } else {
220- filepath = options . root + path . dirname ( path . dirname ( relativePath ) ) ;
221+ filepath = options . root + path . dirname ( relativePath ) ;
221222 }
222223 }
223224
@@ -233,7 +234,7 @@ function allow(mode, req, res) {
233234 } ;
234235}
235236
236- function allowOrigin ( mode , userId , res , aclGraph , subject ) {
237+ function allowOrigin ( mode , req , userId , aclGraph , subject ) {
237238 debug ( "In allow origin" ) ;
238239 var ownerStatements = aclGraph . each ( subject , ns . acl ( "owner" ) ,
239240 aclGraph . sym ( userId ) ) ;
@@ -267,8 +268,7 @@ function allowOrigin(mode, userId, res, aclGraph, subject) {
267268 }
268269 var groupURI = rdfVocab . debrack ( agentClassElem . toString ( ) ) ;
269270 var groupGraph = $rdf . graph ( ) ;
270- var groupFetcher = $rdf . fetcher ( groupGraph , 3000 , false ) ;
271- groupFetcher . nowOrWhenFetched ( groupURI , null , function ( ok , err ) { } ) ;
271+ fetchDocument ( groupGraph , groupURI , req ) ;
272272 var typeStatements = groupGraph . each ( agentClassElem , ns . rdf ( "type" ) , ns . foaf ( "Group" ) ) ;
273273 if ( groupGraph . statements . length > 0 && typeStatements . length > 0 ) {
274274 var memberStatements = groupGraph . each ( agentClassElem , ns . foaf ( "member" ) ,
@@ -284,11 +284,46 @@ function allowOrigin(mode, userId, res, aclGraph, subject) {
284284 }
285285}
286286
287+ function fetchDocument ( graph , uri , req ) {
288+ var options = req . app . locals . ldp ;
289+ var uriBase = file . uriBase ( req ) ;
290+
291+ var body = "" ;
292+ if ( S ( uri ) . startsWith ( uriBase ) ) {
293+ try {
294+ var oldPath = req . url ;
295+ var newPath = S ( uri ) . chompLeft ( uriBase ) . s ;
296+ var documentPath = file . uriToFilename ( newPath , options . root ) ;
297+ var documentUri = url . parse ( documentPath ) ;
298+ documentPath = documentUri . pathname ;
299+
300+ //Overwrite url attribute to call allow with the correct path.
301+ //Otherwise enters infinite recuresion.
302+ req . url = newPath ;
303+ var readAllowed = allow ( 'Read' , req ) ;
304+ //Restore value of req.url
305+ req . url = oldPath ;
306+
307+ if ( readAllowed && readAllowed . status === 200 ) {
308+ body = fs . readFileSync ( documentPath , { encoding : 'utf8' } ) ;
309+ }
310+ } catch ( err ) { }
311+ } else {
312+ var response = request ( 'GET' , uri , {
313+ headers : {
314+ 'Accept' : 'text/turtle'
315+ }
316+ } ) ;
317+ body = response . getBody ( 'utf8' ) ;
318+ }
319+ $rdf . parse ( body , graph , uri , 'text/turtle' ) ;
320+ }
321+
287322function getUserId ( req ) {
288323 var userId ;
289324 if ( req . get ( 'On-Behalf-Of' ) ) {
290325 var delegator = rdfVocab . debrack ( req . get ( 'On-Behalf-Of' ) ) ;
291- if ( verifyDelegator ( delegator , req . session . userId ) ) {
326+ if ( verifyDelegator ( delegator , req . session . userId , req ) ) {
292327 debug ( "Request User ID (delegation) :" + delegator ) ;
293328 userId = delegator ;
294329 } else {
@@ -301,17 +336,13 @@ function getUserId(req) {
301336 return userId ;
302337}
303338
304- function verifyDelegator ( delegator , delegatee ) {
339+ function verifyDelegator ( delegator , delegatee , req ) {
305340 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 ) ;
341+ fetchDocument ( delegatorGraph , delegator , req ) ;
342+ var delegatesStatements = delegatorGraph
343+ . each ( delegatorGraph . sym ( delegator ) ,
344+ delegatorGraph . sym ( "http://www.w3.org/ns/auth/acl#delegates" ) ,
345+ undefined ) ;
315346 for ( var delegateeIndex in delegatesStatements ) {
316347 var delegateeValue = delegatesStatements [ delegateeIndex ] ;
317348 if ( rdfVocab . debrack ( delegateeValue . toString ( ) ) === delegatee ) {
@@ -354,11 +385,11 @@ exports.allowAppendThenWriteHandler = function(req, res, next) {
354385 if ( ! options . webid ) {
355386 return next ( ) ;
356387 } else {
357- var allowAppendValue = allow ( "Append" , req , res , next ) ;
388+ var allowAppendValue = allow ( "Append" , req ) ;
358389 if ( allowAppendValue . status == 200 ) {
359390 return next ( ) ;
360391 } else {
361- var allowWriteValue = allow ( "Write" , req , res , next ) ;
392+ var allowWriteValue = allow ( "Write" , req ) ;
362393 if ( allowWriteValue . status == 200 ) {
363394 return next ( ) ;
364395 } else {
0 commit comments