Skip to content

Commit a82a201

Browse files
committed
add option to check Origin in acl
1 parent 4933e28 commit a82a201

1 file changed

Lines changed: 78 additions & 4 deletions

File tree

src/WAC.php

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,23 @@ public function addWACHeaders($request, $response, $webId) {
4949
* see: https://github.com/solid/web-access-control-spec
5050
*/
5151

52-
public function isAllowed($request, $webId) {
52+
public function isAllowed($request, $webId, $origin=false) {
5353
$requestedGrants = $this->getRequestedGrants($request);
5454
$uri = $request->getUri();
5555
$parentUri = $this->getParentUri($uri);
56+
5657
if (
57-
$this->isGranted($requestedGrants['resource'], $uri, $webId) &&
58-
$this->isGranted($requestedGrants['parent'], $parentUri, $webId)
58+
$this->isUserGranted($requestedGrants['resource'], $uri, $webId) &&
59+
$this->isUserGranted($requestedGrants['parent'], $parentUri, $webId) &&
60+
$this->isOriginGranted($requestedGrants['resource'], $uri, $origin) &&
61+
$this->isOriginGranted($requestedGrants['parent'], $parentUri, $origin)
5962
) {
6063
return true;
6164
}
6265
return false;
6366
}
6467

65-
private function isGranted($requestedGrants, $uri, $webId) {
68+
private function isUserGranted($requestedGrants, $uri, $webId) {
6669
if (!$requestedGrants) {
6770
return true;
6871
}
@@ -90,6 +93,37 @@ private function isGranted($requestedGrants, $uri, $webId) {
9093
return false;
9194
}
9295

96+
private function isOriginGranted($requestedGrants, $uri, $origin) {
97+
if (!$requestedGrants) {
98+
return true;
99+
}
100+
if (!$origin) {
101+
return true;
102+
}
103+
104+
$path = $uri->getPath();
105+
if ($this->basePath) {
106+
$path = str_replace($this->basePath, '', $path);
107+
}
108+
109+
//error_log("REQUESTED GRANT: " . join(" or ", $requestedGrants) . " on $uri");
110+
$grants = $this->getOriginGrants($path, $origin);
111+
//error_log("GRANTED GRANTS for $origin: " . json_encode($grants));
112+
if (is_array($grants)) {
113+
foreach ($requestedGrants as $requestedGrant) {
114+
if ($grants['accessTo'] && $grants['accessTo'][$requestedGrant] && $this->arePathsEqual($grants['accessTo'][$requestedGrant], $uri)) {
115+
return true;
116+
} else if ($grants['default'][$requestedGrant]) {
117+
if ($this->arePathsEqual($grants['default'][$requestedGrant], $uri)) {
118+
return false; // only use default for children, not for an exact match;
119+
}
120+
return true;
121+
}
122+
}
123+
}
124+
return false;
125+
}
126+
93127
private function getUserGrants($resourcePath, $webId) {
94128
$aclPath = $this->getAclPath($resourcePath);
95129
if (!$aclPath) {
@@ -130,6 +164,46 @@ private function getUserGrants($resourcePath, $webId) {
130164
return $grants;
131165
}
132166

167+
private function getOriginGrants($resourcePath, $origin) {
168+
$aclPath = $this->getAclPath($resourcePath);
169+
if (!$aclPath) {
170+
return array();
171+
}
172+
$acl = $this->filesystem->read($aclPath);
173+
174+
$graph = new \EasyRdf_Graph();
175+
$graph->parse($acl, Format::TURTLE, $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME']);
176+
177+
// error_log("GET GRANTS for $origin");
178+
179+
$grants = $this->getPublicGrants($resourcePath);
180+
181+
$matching = $graph->resourcesMatching('http://www.w3.org/ns/auth/acl#origin');
182+
//error_log("MATCHING " . sizeof($matching));
183+
// Find all grants machting our origin;
184+
foreach ($matching as $match) {
185+
$grantedOrigin = $match->get("<http://www.w3.org/ns/auth/acl#origin>");
186+
if ($grantedOrigin == $origin) {
187+
$accessTo = $match->get("<http://www.w3.org/ns/auth/acl#accessTo>");
188+
//error_log("$origin accessTo $accessTo");
189+
$default = $match->get("<http://www.w3.org/ns/auth/acl#default>");
190+
$modes = $match->all("<http://www.w3.org/ns/auth/acl#mode>");
191+
if ($default) {
192+
foreach ($modes as $mode) {
193+
$grants["default"][$mode->getUri()] = $default->getUri();
194+
}
195+
}
196+
if ($accessTo) {
197+
foreach ($modes as $mode) {
198+
$grants["accessTo"][$mode->getUri()] = $accessTo->getUri();
199+
}
200+
}
201+
}
202+
}
203+
204+
return $grants;
205+
}
206+
133207
private function getAclPath($path) {
134208
// get the filename from the request
135209
$filename = basename($path);

0 commit comments

Comments
 (0)