Skip to content

Commit 1425943

Browse files
committed
Fix bug caused by passing an array as a string in Resource controller
In order to create this fix, the ResourceController has been changed to extend the ServerController, so it has access to the ServerConfig. The ServerConfig has a getAllowedOrigins method added that returns all allowed origins it is aware of. In order to facilitate this, some cleanup was also needed for the ServerController.
1 parent d4da6c5 commit 1425943

File tree

3 files changed

+163
-147
lines changed

3 files changed

+163
-147
lines changed

src/Controller/ResourceController.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@
22

33
namespace Pdsinterop\Solid\Controller;
44

5+
use Pdsinterop\Solid\Auth\Utils\DPop;
6+
use Pdsinterop\Solid\Auth\WAC;
57
use Pdsinterop\Solid\Resources\Server;
6-
use Pdsinterop\Solid\Controller\AbstractController;
78
use Psr\Http\Message\ResponseInterface as Response;
89
use Psr\Http\Message\ServerRequestInterface as Request;
910

10-
use Pdsinterop\Solid\Auth\Utils\DPop as DPop;
11-
use Pdsinterop\Solid\Auth\WAC as WAC;
12-
13-
class ResourceController extends AbstractController
11+
class ResourceController extends ServerController
1412
{
1513
////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\
1614

1715
/** @var Server */
18-
private $baseUrl;
1916
private $server;
2017
private $DPop;
2118
private $WAC;
2219
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
2320

2421
final public function __construct(Server $server)
2522
{
26-
$this->baseUrl = isset($_ENV['SERVER_ROOT']) ? $_ENV['SERVER_ROOT'] : "https://localhost";
23+
parent::__construct();
24+
2725
$this->server = $server;
2826
$this->DPop = new DPop();
2927
$this->WAC = new WAC($server->getFilesystem());
@@ -44,11 +42,12 @@ final public function __invoke(Request $request, array $args) : Response
4442
return $this->server->getResponse()->withStatus(409, 'Invalid token');
4543
}
4644

45+
$allowedOrigins = $this->config->getAllowedOrigins();
4746
$origins = $request->getHeader('Origin');
4847

4948
$isAllowed = false;
5049
foreach ($origins as $origin) {
51-
if ($this->WAC->isAllowed($request, $webId, $origin)) {
50+
if ($this->WAC->isAllowed($request, $webId, $origin, $allowedOrigins)) {
5251
$isAllowed = true;
5352
break;
5453
}

src/Controller/ServerController.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
namespace Pdsinterop\Solid\Controller;
44

5-
use Psr\Http\Message\ResponseInterface;
6-
use Psr\Http\Message\ServerRequestInterface;
7-
85
abstract class ServerController extends AbstractController
9-
{
10-
// public $config;
11-
// public $baseUrl;
12-
// public $authServerConfig;
13-
// public $authServerFactory;
14-
// public $tokenGenerator;
15-
public function __construct() {
16-
// parent::__construct();
17-
require_once(__DIR__.'/../../vendor/autoload.php');
6+
{
7+
protected $authServerConfig;
8+
protected $authServerFactory;
9+
protected $baseUrl;
10+
protected $config;
11+
protected $openIdConfiguration;
12+
protected $tokenGenerator;
13+
protected $userId;
14+
15+
private $keys = [];
16+
17+
public function __construct()
18+
{
1819
$this->config = new \Pdsinterop\Solid\ServerConfig(__DIR__.'/../../config/');
1920

2021
$this->authServerConfig = $this->createAuthServerConfig();

src/ServerConfig.php

Lines changed: 143 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,143 @@
1-
<?php declare(strict_types=1);
2-
3-
namespace Pdsinterop\Solid;
4-
5-
class ServerConfig {
6-
private $path;
7-
private $serverConfig;
8-
private $userConfig;
9-
10-
public function __construct($path) {
11-
$this->path = $path;
12-
$this->serverConfigFile = $this->path . "serverConfig.json";
13-
$this->userConfigFile = $this->path . "user.json";
14-
$this->serverConfig = $this->loadConfig();
15-
$this->userConfig = $this->loadUserConfig();
16-
17-
}
18-
private function loadConfig() {
19-
if (!file_exists($this->serverConfigFile)) {
20-
$keySet = $this->generateKeySet();
21-
$this->serverConfig = array(
22-
"encryptionKey" => $keySet['encryptionKey'],
23-
"privateKey" => $keySet['privateKey']
24-
);
25-
$this->saveConfig();
26-
}
27-
return json_decode(file_get_contents($this->serverConfigFile), true);
28-
}
29-
private function saveConfig() {
30-
file_put_contents($this->serverConfigFile, json_encode($this->serverConfig, JSON_PRETTY_PRINT));
31-
}
32-
private function loadUserConfig() {
33-
if (!file_exists($this->userConfigFile)) {
34-
$this->userConfig = array(
35-
"allowedClients" => array()
36-
);
37-
$this->saveUserConfig();
38-
}
39-
return json_decode(file_get_contents($this->userConfigFile), true);
40-
}
41-
private function saveUserConfig() {
42-
file_put_contents($this->userConfigFile, json_encode($this->userConfig, JSON_PRETTY_PRINT));
43-
}
44-
45-
/* Server data */
46-
public function getPrivateKey() {
47-
return $this->serverConfig['privateKey'];
48-
}
49-
50-
public function getEncryptionKey() {
51-
return $this->serverConfig['encryptionKey'];
52-
}
53-
54-
public function getClientConfigById($clientId) {
55-
$clients = (array)$this->serverConfig['clients'];
56-
57-
if (array_key_exists($clientId, $clients)) {
58-
return $clients[$clientId];
59-
}
60-
return null;
61-
}
62-
63-
public function saveClientConfig($clientConfig) {
64-
$clientId = uuidv4();
65-
$this->serverConfig['clients'][$clientId] = $clientConfig;
66-
$this->saveConfig();
67-
return $clientId;
68-
}
69-
70-
public function saveClientRegistration($origin, $clientData) {
71-
$originHash = md5($origin);
72-
$existingRegistration = $this->getClientRegistration($originHash);
73-
if ($existingRegistration && isset($existingRegistration['client_name'])) {
74-
return $originHash;
75-
}
76-
77-
$clientData['client_name'] = $origin;
78-
$clientData['client_secret'] = md5(random_bytes(32));
79-
$this->serverConfig['client-' . $originHash] = $clientData;
80-
$this->saveConfig();
81-
return $originHash;
82-
}
83-
84-
public function getClientRegistration($clientId) {
85-
if (isset($this->serverConfig['client-' . $clientId])) {
86-
return $this->serverConfig['client-' . $clientId];
87-
} else {
88-
return array();
89-
}
90-
}
91-
92-
/* User specific data */
93-
public function getAllowedClients($userId) {
94-
return $this->userConfig['allowedClients'];
95-
}
96-
97-
public function addAllowedClient($userId, $clientId) {
98-
$this->userConfig['allowedClients'][] = $clientId;
99-
$this->userConfig['allowedClients'] = array_unique($this->userConfig['allowedClients']);
100-
$this->saveUserConfig();
101-
}
102-
public function removeAllowedClient($userId, $clientId) {
103-
$this->userConfig['allowedClients'] = array_diff($this->userConfig['allowedClients'], array($clientId));
104-
$this->saveUserConfig();
105-
}
106-
107-
/* Helper functions */
108-
private function generateKeySet() {
109-
$config = array(
110-
"digest_alg" => "sha256",
111-
"private_key_bits" => 2048,
112-
"private_key_type" => OPENSSL_KEYTYPE_RSA,
113-
);
114-
// Create the private and public key
115-
$key = openssl_pkey_new($config);
116-
117-
// Extract the private key from $key to $privateKey
118-
openssl_pkey_export($key, $privateKey);
119-
$encryptionKey = base64_encode(random_bytes(32));
120-
$result = array(
121-
"privateKey" => $privateKey,
122-
"encryptionKey" => $encryptionKey
123-
);
124-
return $result;
125-
}
126-
}
127-
?>
1+
<?php declare(strict_types=1);
2+
3+
namespace Pdsinterop\Solid;
4+
5+
class ServerConfig {
6+
private $path;
7+
private $serverConfig;
8+
private $userConfig;
9+
10+
public function __construct($path) {
11+
$this->path = $path;
12+
$this->serverConfigFile = $this->path . "serverConfig.json";
13+
$this->userConfigFile = $this->path . "user.json";
14+
$this->serverConfig = $this->loadConfig();
15+
$this->userConfig = $this->loadUserConfig();
16+
17+
}
18+
19+
public function getAllowedOrigins()
20+
{
21+
$allowedOrigins = [];
22+
23+
$serverConfig = $this->serverConfig;
24+
foreach ($serverConfig as $value) {
25+
if (isset($value['redirect_uris'])) {
26+
foreach($value['redirect_uris'] as $url) {
27+
$allowedOrigins[] = parse_url($url)['host'];
28+
}
29+
}
30+
}
31+
32+
return array_unique($allowedOrigins);
33+
}
34+
35+
private function loadConfig() {
36+
if (!file_exists($this->serverConfigFile)) {
37+
$keySet = $this->generateKeySet();
38+
$this->serverConfig = array(
39+
"encryptionKey" => $keySet['encryptionKey'],
40+
"privateKey" => $keySet['privateKey']
41+
);
42+
$this->saveConfig();
43+
}
44+
return json_decode(file_get_contents($this->serverConfigFile), true);
45+
}
46+
private function saveConfig() {
47+
file_put_contents($this->serverConfigFile, json_encode($this->serverConfig, JSON_PRETTY_PRINT));
48+
}
49+
private function loadUserConfig() {
50+
if (!file_exists($this->userConfigFile)) {
51+
$this->userConfig = array(
52+
"allowedClients" => array()
53+
);
54+
$this->saveUserConfig();
55+
}
56+
return json_decode(file_get_contents($this->userConfigFile), true);
57+
}
58+
private function saveUserConfig() {
59+
file_put_contents($this->userConfigFile, json_encode($this->userConfig, JSON_PRETTY_PRINT));
60+
}
61+
62+
/* Server data */
63+
public function getPrivateKey() {
64+
return $this->serverConfig['privateKey'];
65+
}
66+
67+
public function getEncryptionKey() {
68+
return $this->serverConfig['encryptionKey'];
69+
}
70+
71+
public function getClientConfigById($clientId) {
72+
$clients = (array)$this->serverConfig['clients'];
73+
74+
if (array_key_exists($clientId, $clients)) {
75+
return $clients[$clientId];
76+
}
77+
return null;
78+
}
79+
80+
public function saveClientConfig($clientConfig) {
81+
$clientId = uuidv4();
82+
$this->serverConfig['clients'][$clientId] = $clientConfig;
83+
$this->saveConfig();
84+
return $clientId;
85+
}
86+
87+
public function saveClientRegistration($origin, $clientData) {
88+
$originHash = md5($origin);
89+
$existingRegistration = $this->getClientRegistration($originHash);
90+
if ($existingRegistration && isset($existingRegistration['client_name'])) {
91+
return $originHash;
92+
}
93+
94+
$clientData['client_name'] = $origin;
95+
$clientData['client_secret'] = md5(random_bytes(32));
96+
$this->serverConfig['client-' . $originHash] = $clientData;
97+
$this->saveConfig();
98+
return $originHash;
99+
}
100+
101+
public function getClientRegistration($clientId) {
102+
if (isset($this->serverConfig['client-' . $clientId])) {
103+
return $this->serverConfig['client-' . $clientId];
104+
} else {
105+
return array();
106+
}
107+
}
108+
109+
/* User specific data */
110+
public function getAllowedClients($userId) {
111+
return $this->userConfig['allowedClients'];
112+
}
113+
114+
public function addAllowedClient($userId, $clientId) {
115+
$this->userConfig['allowedClients'][] = $clientId;
116+
$this->userConfig['allowedClients'] = array_unique($this->userConfig['allowedClients']);
117+
$this->saveUserConfig();
118+
}
119+
public function removeAllowedClient($userId, $clientId) {
120+
$this->userConfig['allowedClients'] = array_diff($this->userConfig['allowedClients'], array($clientId));
121+
$this->saveUserConfig();
122+
}
123+
124+
/* Helper functions */
125+
private function generateKeySet() {
126+
$config = array(
127+
"digest_alg" => "sha256",
128+
"private_key_bits" => 2048,
129+
"private_key_type" => OPENSSL_KEYTYPE_RSA,
130+
);
131+
// Create the private and public key
132+
$key = openssl_pkey_new($config);
133+
134+
// Extract the private key from $key to $privateKey
135+
openssl_pkey_export($key, $privateKey);
136+
$encryptionKey = base64_encode(random_bytes(32));
137+
$result = array(
138+
"privateKey" => $privateKey,
139+
"encryptionKey" => $encryptionKey
140+
);
141+
return $result;
142+
}
143+
}

0 commit comments

Comments
 (0)