-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolidStorageHandler.php
More file actions
71 lines (55 loc) · 1.93 KB
/
Copy pathSolidStorageHandler.php
File metadata and controls
71 lines (55 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Pdsinterop\PhpSolid;
use Laminas\Diactoros\Response;
use Pdsinterop\Solid\Auth\WAC;
use Pdsinterop\Solid\Resources\Server as ResourceServer;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class SolidStorageHandler
{
public function handle(ServerRequestInterface $rawRequest): ResponseInterface
{
try {
StorageServer::initializeStorage();
$filesystem = StorageServer::getFileSystem();
} catch (\Exception $e) {
return (new Response())->withStatus(404, "Not found");
}
$resourceServer = new ResourceServer($filesystem, new Response(), null);
$solidNotifications = new SolidNotifications();
$resourceServer->setNotifications($solidNotifications);
$wac = new WAC($filesystem);
$baseUrl = Util::getServerBaseUrl();
$resourceServer->setBaseUrl($baseUrl);
$wac->setBaseUrl($baseUrl);
try {
$webId = StorageServer::getWebId($rawRequest);
} catch (\Exception $e) {
return $resourceServer->getResponse()
->withStatus(400, "Bad request");
}
if (!isset($webId)) {
return $resourceServer->getResponse()
->withStatus(409, "Invalid token");
}
$origin = $rawRequest->getHeaderLine("Origin");
// FIXME: Read allowed clients from the profile instead;
$ownerWebId = StorageServer::getOwnerWebId();
$owner = User::getUserByWebId($ownerWebId);
$allowedClients = $owner['allowedClients'] ?? [];
$allowedOrigins = array_merge(
($owner['allowedOrigins'] ?? []),
(TRUSTED_APPS ?? [])
);
$allowedOrigins = array_unique($allowedOrigins);
if (!isset($origin) || ($origin === "")) {
$allowedOrigins[] = "app://unset"; // FIXME: this should not be here.
$origin = "app://unset";
}
if (!$wac->isAllowed($rawRequest, $webId, $origin, $allowedOrigins)) {
return (new Response())->withStatus(403, "Access denied!");
}
$response = $resourceServer->respondToRequest($rawRequest);
return $wac->addWACHeaders($rawRequest, $response, $webId);
}
}