Skip to content

Commit c413924

Browse files
committed
cleanup user storage route file
1 parent 6402e29 commit c413924

File tree

2 files changed

+78
-67
lines changed

2 files changed

+78
-67
lines changed

lib/Api/SolidStorage.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
namespace Pdsinterop\PhpSolid\Api;
3+
4+
use Pdsinterop\PhpSolid\StorageServer;
5+
use Pdsinterop\PhpSolid\ClientRegistration;
6+
use Pdsinterop\PhpSolid\SolidNotifications;
7+
use Pdsinterop\Solid\Auth\WAC;
8+
use Pdsinterop\Solid\Resources\Server as ResourceServer;
9+
use Laminas\Diactoros\ServerRequestFactory;
10+
use Laminas\Diactoros\Response;
11+
12+
class SolidStorage {
13+
public static function respondToStorage() {
14+
$requestFactory = new ServerRequestFactory();
15+
$rawRequest = $requestFactory->fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
16+
$response = new Response();
17+
18+
StorageServer::initializeStorage();
19+
$filesystem = StorageServer::getFileSystem();
20+
21+
$resourceServer = new ResourceServer($filesystem, $response, null);
22+
$solidNotifications = new SolidNotifications();
23+
$resourceServer->setNotifications($solidNotifications);
24+
25+
$wac = new WAC($filesystem);
26+
27+
$baseUrl = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME'];
28+
29+
$resourceServer->setBaseUrl($baseUrl);
30+
$wac->setBaseUrl($baseUrl);
31+
32+
$webId = StorageServer::getWebId($rawRequest);
33+
34+
if (!isset($webId)) {
35+
$response = $resourceServer->getResponse()
36+
->withStatus(409, "Invalid token");
37+
StorageServer::respond($response);
38+
exit();
39+
}
40+
41+
$origin = $rawRequest->getHeaderLine("Origin");
42+
43+
// FIXME: Read allowed clients from the profile instead;
44+
$owner = StorageServer::getOwner();
45+
46+
$allowedClients = $owner['allowedClients'] ?? [];
47+
$allowedOrigins = [];
48+
foreach ($allowedClients as $clientId) {
49+
$clientRegistration = ClientRegistration::getRegistration($clientId);
50+
if (isset($clientRegistration['client_name'])) {
51+
$allowedOrigins[] = $clientRegistration['client_name'];
52+
}
53+
if (isset($clientRegistration['origin'])) {
54+
$allowedOrigins[] = $clientRegistration['origin'];
55+
}
56+
}
57+
if ($origin =="") {
58+
$allowedOrigins[] = "app://unset"; // FIXME: this should not be here.
59+
$origin = "app://unset";
60+
}
61+
62+
if (!$wac->isAllowed($rawRequest, $webId, $origin, $allowedOrigins)) {
63+
$response = new Response();
64+
$response = $response->withStatus(403, "Access denied!");
65+
StorageServer::respond($response);
66+
exit();
67+
}
68+
69+
$response = $resourceServer->respondToRequest($rawRequest);
70+
$response = $wac->addWACHeaders($rawRequest, $response, $webId);
71+
StorageServer::respond($response);
72+
}
73+
}
74+

www/user/storage.php

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@
66
require_once(__DIR__ . "/../../vendor/autoload.php");
77

88
use Pdsinterop\PhpSolid\Middleware;
9-
use Pdsinterop\PhpSolid\StorageServer;
10-
use Pdsinterop\PhpSolid\ClientRegistration;
11-
use Pdsinterop\PhpSolid\SolidNotifications;
12-
use Pdsinterop\Solid\Auth\WAC;
13-
use Pdsinterop\Solid\Resources\Server as ResourceServer;
14-
use Laminas\Diactoros\ServerRequestFactory;
15-
use Laminas\Diactoros\Response;
9+
use Pdsinterop\PhpSolid\Api\SolidStorage;
1610

17-
$request = explode("?", $_SERVER['REQUEST_URI'], 2)[0];
1811
$method = $_SERVER['REQUEST_METHOD'];
1912

2013
Middleware::cors();
@@ -25,63 +18,7 @@
2518
echo "OK";
2619
return;
2720
break;
21+
default:
22+
SolidStorage::respondToStorage();
23+
break;
2824
}
29-
30-
$requestFactory = new ServerRequestFactory();
31-
$rawRequest = $requestFactory->fromGlobals($_SERVER, $_GET, $_POST, $_COOKIE, $_FILES);
32-
$response = new Response();
33-
34-
StorageServer::initializeStorage();
35-
$filesystem = StorageServer::getFileSystem();
36-
37-
$resourceServer = new ResourceServer($filesystem, $response, null);
38-
$solidNotifications = new SolidNotifications();
39-
$resourceServer->setNotifications($solidNotifications);
40-
41-
$wac = new WAC($filesystem);
42-
43-
$baseUrl = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME'];
44-
45-
$resourceServer->setBaseUrl($baseUrl);
46-
$wac->setBaseUrl($baseUrl);
47-
48-
$webId = StorageServer::getWebId($rawRequest);
49-
50-
if (!isset($webId)) {
51-
$response = $resourceServer->getResponse()
52-
->withStatus(409, "Invalid token");
53-
StorageServer::respond($response);
54-
exit();
55-
}
56-
57-
$origin = $rawRequest->getHeaderLine("Origin");
58-
59-
// FIXME: Read allowed clients from the profile instead;
60-
$owner = StorageServer::getOwner();
61-
62-
$allowedClients = $owner['allowedClients'] ?? [];
63-
$allowedOrigins = [];
64-
foreach ($allowedClients as $clientId) {
65-
$clientRegistration = ClientRegistration::getRegistration($clientId);
66-
if (isset($clientRegistration['client_name'])) {
67-
$allowedOrigins[] = $clientRegistration['client_name'];
68-
}
69-
if (isset($clientRegistration['origin'])) {
70-
$allowedOrigins[] = $clientRegistration['origin'];
71-
}
72-
}
73-
if ($origin =="") {
74-
$allowedOrigins[] = "app://unset"; // FIXME: this should not be here.
75-
$origin = "app://unset";
76-
}
77-
78-
if (!$wac->isAllowed($rawRequest, $webId, $origin, $allowedOrigins)) {
79-
$response = new Response();
80-
$response = $response->withStatus(403, "Access denied!");
81-
StorageServer::respond($response);
82-
exit();
83-
}
84-
85-
$response = $resourceServer->respondToRequest($rawRequest);
86-
$response = $wac->addWACHeaders($rawRequest, $response, $webId);
87-
StorageServer::respond($response);

0 commit comments

Comments
 (0)