Skip to content

Commit aa10ba3

Browse files
authored
Merge pull request #1160 from solid/fix/#1159
Fixed acl files for external webids
2 parents 4cac168 + 27a4ddd commit aa10ba3

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

lib/acl-checker.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const aclCheck = require('@solid/acl-check')
77
const { URL } = require('url')
88
const { promisify } = require('util')
99
const fs = require('fs')
10+
const Url = require('url')
11+
const httpFetch = require('node-fetch')
1012

1113
const DEFAULT_ACL_SUFFIX = '.acl'
1214
const ACL = rdf.Namespace('http://www.w3.org/ns/auth/acl#')
@@ -156,7 +158,7 @@ class ACLChecker {
156158
return new ACLChecker(resource, {
157159
agentOrigin: req.get('origin'),
158160
// host: req.get('host'),
159-
fetch: fetchFromLdp(ldp.resourceMapper),
161+
fetch: fetchLocalOrRemote(ldp.resourceMapper, ldp.serverUri),
160162
fetchGraph: (uri, options) => {
161163
// first try loading from local fs
162164
return ldp.getGraph(uri, options.contentType)
@@ -178,17 +180,27 @@ class ACLChecker {
178180
* - `callback(null, graph)` with the parsed RDF graph of the fetched resource
179181
* @return {Function} Returns a `fetch(uri, callback)` handler
180182
*/
181-
function fetchFromLdp (mapper) {
183+
function fetchLocalOrRemote (mapper, serverUri) {
182184
return async function fetch (url, graph = rdf.graph()) {
183185
// Convert the URL into a filename
184-
let path, contentType
185-
try {
186-
({ path, contentType } = await mapper.mapUrlToFile({ url }))
187-
} catch (err) {
188-
throw new HTTPError(404, err)
186+
let body, path, contentType
187+
188+
if (Url.parse(url).host.includes(Url.parse(serverUri).host)) {
189+
// Fetch the acl from local
190+
try {
191+
({ path, contentType } = await mapper.mapUrlToFile({ url }))
192+
} catch (err) {
193+
throw new HTTPError(404, err)
194+
}
195+
// Read the file from disk
196+
body = await promisify(fs.readFile)(path, { 'encoding': 'utf8' })
197+
} else {
198+
// Fetch the acl from the internet
199+
const response = await httpFetch(url)
200+
body = await response.text()
201+
contentType = response.headers.get('content-type')
189202
}
190-
// Read the file from disk
191-
const body = await promisify(fs.readFile)(path, { 'encoding': 'utf8' })
203+
192204
// Parse the file as Turtle
193205
rdf.parse(body, graph, url, contentType)
194206
return graph

0 commit comments

Comments
 (0)