forked from pdsinterop/php-solid-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerConfig.php
More file actions
173 lines (141 loc) · 4.8 KB
/
Copy pathServerConfig.php
File metadata and controls
173 lines (141 loc) · 4.8 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php declare(strict_types=1);
namespace Pdsinterop\Solid;
class ServerConfig
{
////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\
private $path;
private $serverConfig;
private $userConfig;
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public function __construct($path)
{
$this->path = $path;
$this->serverConfigFile = $this->path . "serverConfig.json";
$this->userConfigFile = $this->path . "user.json";
$this->serverConfig = $this->loadConfig();
$this->userConfig = $this->loadUserConfig();
}
public function getAllowedOrigins()
{
$allowedOrigins = [];
$serverConfig = $this->serverConfig;
foreach ($serverConfig as $value) {
if (isset($value['redirect_uris'])) {
foreach ($value['redirect_uris'] as $url) {
$allowedOrigins[] = parse_url($url)['host'];
}
}
}
return array_unique($allowedOrigins);
}
private function loadConfig()
{
if ( ! file_exists($this->serverConfigFile)) {
$keySet = $this->generateKeySet();
$this->serverConfig = [
"encryptionKey" => $keySet['encryptionKey'],
"privateKey" => $keySet['privateKey'],
];
$this->saveConfig();
}
return json_decode(file_get_contents($this->serverConfigFile), true);
}
private function saveConfig()
{
file_put_contents($this->serverConfigFile, json_encode($this->serverConfig, JSON_PRETTY_PRINT));
}
private function loadUserConfig()
{
if ( ! file_exists($this->userConfigFile)) {
$this->userConfig = [
"allowedClients" => [],
];
$this->saveUserConfig();
}
return json_decode(file_get_contents($this->userConfigFile), true);
}
private function saveUserConfig()
{
file_put_contents($this->userConfigFile, json_encode($this->userConfig, JSON_PRETTY_PRINT));
}
/* Server data */
public function getPrivateKey()
{
return $this->serverConfig['privateKey'];
}
public function getEncryptionKey()
{
return $this->serverConfig['encryptionKey'];
}
public function getClientConfigById($clientId)
{
$clients = (array) $this->serverConfig['clients'];
if (array_key_exists($clientId, $clients)) {
return $clients[$clientId];
}
return null;
}
public function saveClientConfig($clientConfig)
{
$clientId = uuidv4();
$this->serverConfig['clients'][$clientId] = $clientConfig;
$this->saveConfig();
return $clientId;
}
public function saveClientRegistration($origin, $clientData)
{
$originHash = md5($origin);
$existingRegistration = $this->getClientRegistration($originHash);
if ($existingRegistration && isset($existingRegistration['client_name'])) {
return $originHash;
}
$clientData['client_name'] = $origin;
$clientData['client_secret'] = md5(random_bytes(32));
$this->serverConfig['client-' . $originHash] = $clientData;
$this->saveConfig();
return $originHash;
}
public function getClientRegistration($clientId)
{
if (isset($this->serverConfig['client-' . $clientId])) {
return $this->serverConfig['client-' . $clientId];
} else {
return [];
}
}
/* User specific data */
public function getAllowedClients()
{
return $this->userConfig['allowedClients'];
}
public function addAllowedClient($userId, $clientId)
{
$this->userConfig['allowedClients'][] = $clientId;
$this->userConfig['allowedClients'] = array_unique($this->userConfig['allowedClients']);
$this->saveUserConfig();
}
public function removeAllowedClient($userId, $clientId)
{
$this->userConfig['allowedClients'] = array_diff($this->userConfig['allowedClients'], [$clientId]);
$this->saveUserConfig();
}
////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
private function generateKeySet()
{
$config = [
"digest_alg" => "sha256",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
];
// Create the private and public key
$key = openssl_pkey_new($config);
// Extract the private key from $key to $privateKey
openssl_pkey_export($key, $privateKey);
$encryptionKey = base64_encode(random_bytes(32));
$result = [
"privateKey" => $privateKey,
"encryptionKey" => $encryptionKey,
];
return $result;
}
}