Skip to content

add option to check Origin in acl #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 78 additions & 4 deletions src/WAC.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,23 @@ public function addWACHeaders($request, $response, $webId) {
* see: https://github.com/solid/web-access-control-spec
*/

public function isAllowed($request, $webId) {
public function isAllowed($request, $webId, $origin=false) {
$requestedGrants = $this->getRequestedGrants($request);
$uri = $request->getUri();
$parentUri = $this->getParentUri($uri);

if (
$this->isGranted($requestedGrants['resource'], $uri, $webId) &&
$this->isGranted($requestedGrants['parent'], $parentUri, $webId)
$this->isUserGranted($requestedGrants['resource'], $uri, $webId) &&
$this->isUserGranted($requestedGrants['parent'], $parentUri, $webId) &&
$this->isOriginGranted($requestedGrants['resource'], $uri, $origin) &&
$this->isOriginGranted($requestedGrants['parent'], $parentUri, $origin)
) {
return true;
}
return false;
}

private function isGranted($requestedGrants, $uri, $webId) {
private function isUserGranted($requestedGrants, $uri, $webId) {
if (!$requestedGrants) {
return true;
}
Expand Down Expand Up @@ -90,6 +93,37 @@ private function isGranted($requestedGrants, $uri, $webId) {
return false;
}

private function isOriginGranted($requestedGrants, $uri, $origin) {
if (!$requestedGrants) {
return true;
}
if (!$origin) {
return true;
}

$path = $uri->getPath();
if ($this->basePath) {
$path = str_replace($this->basePath, '', $path);
}

//error_log("REQUESTED GRANT: " . join(" or ", $requestedGrants) . " on $uri");
$grants = $this->getOriginGrants($path, $origin);
//error_log("GRANTED GRANTS for $origin: " . json_encode($grants));
if (is_array($grants)) {
foreach ($requestedGrants as $requestedGrant) {
if ($grants['accessTo'] && $grants['accessTo'][$requestedGrant] && $this->arePathsEqual($grants['accessTo'][$requestedGrant], $uri)) {
return true;
} else if ($grants['default'][$requestedGrant]) {
if ($this->arePathsEqual($grants['default'][$requestedGrant], $uri)) {
return false; // only use default for children, not for an exact match;
}
return true;
}
}
}
return false;
}

private function getUserGrants($resourcePath, $webId) {
$aclPath = $this->getAclPath($resourcePath);
if (!$aclPath) {
Expand Down Expand Up @@ -130,6 +164,46 @@ private function getUserGrants($resourcePath, $webId) {
return $grants;
}

private function getOriginGrants($resourcePath, $origin) {
$aclPath = $this->getAclPath($resourcePath);
if (!$aclPath) {
return array();
}
$acl = $this->filesystem->read($aclPath);

$graph = new \EasyRdf_Graph();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new \EasyRdf_Graph() and $_SERVER['REQUEST_SCHEME'] should eventually come from outside the class, but fine for now.

$graph->parse($acl, Format::TURTLE, $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME']);

// error_log("GET GRANTS for $origin");

$grants = $this->getPublicGrants($resourcePath);

$matching = $graph->resourcesMatching('http://www.w3.org/ns/auth/acl#origin');
//error_log("MATCHING " . sizeof($matching));
// Find all grants machting our origin;
foreach ($matching as $match) {
$grantedOrigin = $match->get("<http://www.w3.org/ns/auth/acl#origin>");
if ($grantedOrigin == $origin) {
$accessTo = $match->get("<http://www.w3.org/ns/auth/acl#accessTo>");
//error_log("$origin accessTo $accessTo");
$default = $match->get("<http://www.w3.org/ns/auth/acl#default>");
$modes = $match->all("<http://www.w3.org/ns/auth/acl#mode>");
if ($default) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if if - foreach - if - foreach is better or foreach - if - if... Since this is already nested in a foreach... and/or maybe use yield to speed things up? 🤔

Not sure...

foreach ($modes as $mode) {
$grants["default"][$mode->getUri()] = $default->getUri();
}
}
if ($accessTo) {
foreach ($modes as $mode) {
$grants["accessTo"][$mode->getUri()] = $accessTo->getUri();
}
}
}
}

return $grants;
}

private function getAclPath($path) {
// get the filename from the request
$filename = basename($path);
Expand Down