Skip to content

Commit b0596c9

Browse files
committed
Moving the creation of ACLChechker into a factory method
This is a solution that is used in both https://github.com/solid/node-solid-server/tree/feature/quota-ui and https://github.com/solid/node-solid-server/tree/bug/root-index-special-handling
1 parent ec068f3 commit b0596c9

File tree

3 files changed

+54
-43
lines changed

3 files changed

+54
-43
lines changed

lib/acl-checker.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const debug = require('./debug').ACL
55
const HTTPError = require('./http-error')
66
const aclCheck = require('@solid/acl-check')
77
const { URL } = require('url')
8+
const { promisify } = require('util')
9+
const fs = require('fs')
810

911
const DEFAULT_ACL_SUFFIX = '.acl'
1012
const ACL = rdf.Namespace('http://www.w3.org/ns/auth/acl#')
@@ -144,6 +146,49 @@ class ACLChecker {
144146
isAcl (resource) {
145147
return resource.endsWith(this.suffix)
146148
}
149+
150+
static createFromLDPAndRequest (resource, ldp, req) {
151+
const trustedOrigins = ldp.getTrustedOrigins(req)
152+
return new ACLChecker(resource, {
153+
agentOrigin: req.get('origin'),
154+
// host: req.get('host'),
155+
fetch: fetchFromLdp(ldp.resourceMapper),
156+
fetchGraph: (uri, options) => {
157+
// first try loading from local fs
158+
return ldp.getGraph(uri, options.contentType)
159+
// failing that, fetch remote graph
160+
.catch(() => ldp.fetchGraph(uri, options))
161+
},
162+
suffix: ldp.suffixAcl,
163+
strictOrigin: ldp.strictOrigin,
164+
trustedOrigins
165+
})
166+
}
167+
}
168+
169+
/**
170+
* Returns a fetch document handler used by the ACLChecker to fetch .acl
171+
* resources up the inheritance chain.
172+
* The `fetch(uri, callback)` results in the callback, with either:
173+
* - `callback(err, graph)` if any error is encountered, or
174+
* - `callback(null, graph)` with the parsed RDF graph of the fetched resource
175+
* @return {Function} Returns a `fetch(uri, callback)` handler
176+
*/
177+
function fetchFromLdp (mapper) {
178+
return async function fetch (url, graph = rdf.graph()) {
179+
// Convert the URL into a filename
180+
let path, contentType
181+
try {
182+
({ path, contentType } = await mapper.mapUrlToFile({ url }))
183+
} catch (err) {
184+
throw new HTTPError(404, err)
185+
}
186+
// Read the file from disk
187+
const body = await promisify(fs.readFile)(path, { 'encoding': 'utf8' })
188+
// Parse the file as Turtle
189+
rdf.parse(body, graph, url, contentType)
190+
return graph
191+
}
147192
}
148193

149194
// Returns the index of the last slash before the given position

lib/handlers/allow.js

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
module.exports = allow
22

3-
const $rdf = require('rdflib')
43
const path = require('path')
54
const ACL = require('../acl-checker')
65
const debug = require('../debug.js').ACL
7-
const fs = require('fs')
8-
const { promisify } = require('util')
9-
const HTTPError = require('../http-error')
106

117
function allow (mode, checkPermissionsForDirectory) {
128
return async function allowHandler (req, res, next) {
@@ -48,20 +44,7 @@ function allow (mode, checkPermissionsForDirectory) {
4844
trustedOrigins.push(ldp.serverUri)
4945
}
5046
// Obtain and store the ACL of the requested resource
51-
req.acl = new ACL(rootUrl + resourcePath, {
52-
agentOrigin: req.get('origin'),
53-
// host: req.get('host'),
54-
fetch: fetchFromLdp(ldp.resourceMapper),
55-
fetchGraph: (uri, options) => {
56-
// first try loading from local fs
57-
return ldp.getGraph(uri, options.contentType)
58-
// failing that, fetch remote graph
59-
.catch(() => ldp.fetchGraph(uri, options))
60-
},
61-
suffix: ldp.suffixAcl,
62-
strictOrigin: ldp.strictOrigin,
63-
trustedOrigins: trustedOrigins
64-
})
47+
req.acl = ACL.createFromLDPAndRequest(rootUrl + resourcePath, ldp, req)
6548

6649
// Ensure the user has the required permission
6750
const userId = req.session.userId
@@ -74,28 +57,3 @@ function allow (mode, checkPermissionsForDirectory) {
7457
next(error)
7558
}
7659
}
77-
78-
/**
79-
* Returns a fetch document handler used by the ACLChecker to fetch .acl
80-
* resources up the inheritance chain.
81-
* The `fetch(uri, callback)` results in the callback, with either:
82-
* - `callback(err, graph)` if any error is encountered, or
83-
* - `callback(null, graph)` with the parsed RDF graph of the fetched resource
84-
* @return {Function} Returns a `fetch(uri, callback)` handler
85-
*/
86-
function fetchFromLdp (mapper) {
87-
return async function fetch (url, graph = $rdf.graph()) {
88-
// Convert the URL into a filename
89-
let path, contentType
90-
try {
91-
({ path, contentType } = await mapper.mapUrlToFile({ url }))
92-
} catch (err) {
93-
throw new HTTPError(404, err)
94-
}
95-
// Read the file from disk
96-
const body = await promisify(fs.readFile)(path, {'encoding': 'utf8'})
97-
// Parse the file as Turtle
98-
$rdf.parse(body, graph, url, contentType)
99-
return graph
100-
}
101-
}

lib/ldp.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,14 @@ class LDP {
464464
return ensureNotExists(this, URI.joinPaths(containerURI, filename).toString())
465465
}
466466

467+
getTrustedOrigins (req) {
468+
let trustedOrigins = [this.resourceMapper.resolveUrl(req.hostname)].concat(this.trustedOrigins)
469+
if (this.multiuser) {
470+
trustedOrigins.push(this.serverUri)
471+
}
472+
return trustedOrigins
473+
}
474+
467475
static mimeTypeIsRdf (mimeType) {
468476
return RDF_MIME_TYPES.has(mimeType)
469477
}

0 commit comments

Comments
 (0)