-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} | ||
|
@@ -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) { | ||
|
@@ -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(); | ||
$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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if 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); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.