-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.php
More file actions
183 lines (167 loc) · 5.31 KB
/
Server.php
File metadata and controls
183 lines (167 loc) · 5.31 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
174
175
176
177
178
179
180
181
182
183
<?php
namespace Pdsinterop\PhpSolid;
use Pdsinterop\Solid\Auth\Factory\AuthorizationServerFactory;
use Laminas\Diactoros\Response;
use Pdsinterop\Solid\Auth\Server as SolidAuthServer;
use Pdsinterop\Solid\Auth\Factory\ConfigFactory;
use Pdsinterop\Solid\Auth\Config\Client as ConfigClient;
use Pdsinterop\Solid\Auth\Utils\ReplayDetector;
use Pdsinterop\Solid\Auth\Utils\DPop;
use Pdsinterop\Solid\Auth\Utils\Bearer;
use Pdsinterop\Solid\Auth\Utils\JtiValidator;
use Pdsinterop\Solid\Auth\TokenGenerator;
use Pdsinterop\PhpSolid\ClientRegistration;
use Pdsinterop\PhpSolid\JtiStore;
class Server {
public static function generateKeySet() {
$config = array(
"digest_alg" => "sha256",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create the private and public key
$key = openssl_pkey_new($config);
$publicKey = openssl_pkey_get_details($key)['key'];
// Extract the private key from $key to $privateKey
openssl_pkey_export($key, $privateKey);
$encryptionKey = base64_encode(random_bytes(32));
$result = array(
"privateKey" => $privateKey,
"publicKey" => $publicKey,
"encryptionKey" => $encryptionKey
);
return $result;
}
public static function getAuthServer() {
$authServerConfig = self::getAuthServerConfig();
$authServerFactory = new AuthorizationServerFactory($authServerConfig);
$authServer = $authServerFactory->create();
$response = new Response();
$server = new SolidAuthServer($authServer, $authServerConfig, $response);
return $server;
}
public static function getAuthServerConfig() {
$keys = self::getKeys();
$configClient = self::getConfigClient();
$endpoints = self::getEndpoints();
$authServerConfigFactory = new ConfigFactory(
$configClient,
$keys['encryptionKey'],
$keys['privateKey'],
$keys['publicKey'],
$endpoints
);
$authServerConfig = $authServerConfigFactory->create();
return $authServerConfig;
}
public static function getConfigClient() {
$clientId = $_GET['client_id'] ?? $_POST['client_id'] ?? null;
if ($clientId) {
$registeredClient = ClientRegistration::getRegistration($clientId);
}
if (isset($registeredClient)) {
return new ConfigClient(
$clientId,
$registeredClient['client_secret'] ?? '',
$registeredClient['redirect_uris'],
$registeredClient['client_name']
);
} else {
return new ConfigClient(
'',
'',
array(),
''
);
}
}
public static function getDpop() {
$replayDetector = new ReplayDetector(
function($jti, $targetUri) {
if (JtiStore::hasJti($jti)) {
return true;
} else {
JtiStore::saveJti($jti);
return false;
}
return false;
}
);
$jtiValidator = new JtiValidator($replayDetector);
return new DPop($jtiValidator);
}
public static function getBearer() {
$replayDetector = new ReplayDetector(
function($jti, $targetUri) {
if (JtiStore::hasJti($jti)) {
return true;
} else {
JtiStore::saveJti($jti);
return false;
}
return false;
}
);
$jtiValidator = new JtiValidator($replayDetector);
return new Bearer($jtiValidator);
}
public static function getEndpoints() {
return array(
"issuer" => BASEURL,
"jwks_uri" => BASEURL . "/jwks/",
"check_session_iframe" => BASEURL . "/session/",
"end_session_endpoint" => BASEURL . "/logout/",
"authorization_endpoint" => BASEURL . "/authorize/",
"token_endpoint" => BASEURL . "/token/",
"userinfo_endpoint" => BASEURL . "/userinfo/",
"registration_endpoint" => BASEURL . "/register/"
);
}
public static function getKeys() {
$keys = array(
'encryptionKey' => file_get_contents(KEYDIR . "encryption.key"),
'privateKey' => file_get_contents(KEYDIR . "private.key"),
'publicKey' => file_get_contents(KEYDIR . "public.key")
);
return $keys;
}
public static function getTokenGenerator() {
$dpopValidFor = new \DateInterval('PT10M');
return new TokenGenerator(
self::getAuthServerConfig(),
$dpopValidFor,
self::getDpop()
);
}
public static function respond($response) {
$statusCode = $response->getStatusCode();
$response->getBody()->rewind();
$headers = $response->getHeaders();
$body = json_decode($response->getBody()->getContents());
header("HTTP/1.1 $statusCode");
foreach ($headers as $header => $values) {
foreach ($values as $value) {
if ($header == "Location") {
$value = preg_replace("|%26%2334%3B|", "%22", $value); // odoo weird encoding
}
header($header . ":" . $value);
}
}
echo json_encode($body, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);
}
public static function respondPodPro($response, $body) {
$statusCode = $response->getStatusCode();
$response->getBody()->rewind();
$headers = $response->getHeaders();
header("HTTP/1.1 $statusCode");
foreach ($headers as $header => $values) {
foreach ($values as $value) {
if ($header == "Location") {
$value = preg_replace("|%26%2334%3B|", "%22", $value); // odoo weird encoding
}
header($header . ":" . $value);
}
}
echo json_encode($body, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);
}
}