Skip to content
Open
Show file tree
Hide file tree
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
32 changes: 31 additions & 1 deletion src/WAC.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,42 @@ public function addWACHeaders($request, $response, $webId) {
$wacHeaders[] = "public=\"$publicGrants\"";
}

$response = $response->withAddedHeader("Link", '<.acl>; rel="acl"');
$response = $response->withAddedHeader("Link", '<' . $this->getAclLinkTarget($uri) . '>; rel="acl"');
$response = $response->withHeader("WAC-Allow", implode(",", $wacHeaders));
$this->adapter->setFormat($currentFormat);
return $response;
}

/**
* Returns the URI reference, relative to the resource, of the ACL resource
* that a client must use to read or change the access to that resource.
*
* Every resource has its own ACL resource, whether it exists yet or not:
* `<.acl>` for a container and `<name.acl>` for any other resource. The
* inherited ACL of an ancestor container must not be advertised here. A
* client resolves the link against the resource URI, so advertising
* `<.acl>` for `/foo/bar.ttl` points it at the ACL of the container
* `/foo/`, and a client that then writes "the ACL of bar.ttl" replaces
* the rules for every resource in that container.
*
* @see https://solidproject.org/TR/wac#acl-resource-discovery
*/
public function getAclLinkTarget($uri): string {
$path = $uri->getPath();

if ($path === '' || substr($path, -1) === '/') {
return '.acl';
}

$name = substr($path, strrpos($path, '/') + 1);

if (substr($name, -4) === '.acl') {
return $name;
}

return $name . '.acl';
}

/**
* Checks the requested filename (path+name) and user (webid) to see if the request
* is allowed to continue, according to the web acl
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/WACTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types=1);

namespace Pdsinterop\Solid\Auth;

use Laminas\Diactoros\Uri;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \Pdsinterop\Solid\Auth\WAC
* @covers ::__construct
*/
class WACTest extends TestCase
{
/**
* @testdox WAC should advertise the ACL resource of the resource itself, relative to the resource
*
* @covers ::getAclLinkTarget
*
* @dataProvider provideResourceUris
*/
public function testGetAclLinkTarget(string $resourceUri, string $expected, string $expectedAclUri): void
{
$wac = new WAC(null, null);

$actual = $wac->getAclLinkTarget(new Uri($resourceUri));

$this->assertSame($expected, $actual);
}

/**
* @testdox WAC should advertise an ACL link that resolves to the resource's own ACL resource, not to the ACL of its container
*
* @covers ::getAclLinkTarget
*
* @dataProvider provideResourceUris
*/
public function testAclLinkResolvesToOwnAcl(string $resourceUri, string $target, string $expectedAclUri): void
{
$wac = new WAC(null, null);

$link = $wac->getAclLinkTarget(new Uri($resourceUri));

// A relative reference that is a single path segment resolves against the "directory" of the resource
$resolved = substr($resourceUri, 0, strrpos($resourceUri, '/') + 1) . $link;

$this->assertSame($expectedAclUri, $resolved);
}

public static function provideResourceUris(): array
{
return [
'root container' => ['https://pod.example/', '.acl', 'https://pod.example/.acl'],
'container' => ['https://pod.example/private/', '.acl', 'https://pod.example/private/.acl'],
'resource in container' => ['https://pod.example/private/note.ttl', 'note.ttl.acl', 'https://pod.example/private/note.ttl.acl'],
'resource at root' => ['https://pod.example/note.ttl', 'note.ttl.acl', 'https://pod.example/note.ttl.acl'],
'percent-encoded name' => ['https://pod.example/private/my%20note.ttl', 'my%20note.ttl.acl', 'https://pod.example/private/my%20note.ttl.acl'],
'ACL resource of a resource' => ['https://pod.example/private/note.ttl.acl', 'note.ttl.acl', 'https://pod.example/private/note.ttl.acl'],
'ACL resource of a container' => ['https://pod.example/private/.acl', '.acl', 'https://pod.example/private/.acl'],
];
}
}