forked from grommunio/mapi-header-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.keycloak.php
More file actions
335 lines (288 loc) · 10 KB
/
class.keycloak.php
File metadata and controls
335 lines (288 loc) · 10 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
/*
* SPDX-License-Identifier: AGPL-3.0-only
* SPDX-FileCopyrightText: Copyright 2023 grommunio GmbH
*
* Performs several actions against a KeyCloak server.
*/
// include the token grant class
require_once 'class.token.php';
class KeyCloak {
public $access_token;
public $refresh_token;
public $id_token;
public $redirect_url;
public $last_refresh_time;
private static $_instance;
public $grant;
public $error;
protected $realm_id;
protected $client_id;
protected $secret;
protected $realm_url;
protected $realm_admin_url;
protected $public_key;
protected $is_public;
/**
* The constructor reads all required values from the KeyCloak configuration file.
* This includes values for realm_id, client_id, client_secret, server_url etc.
*
* @param mixed $keycloak_config
*/
public function __construct($keycloak_config) {
if (gettype($keycloak_config) === 'string') {
$keycloak_config = json_decode($keycloak_config);
}
// redirect_url
$url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url_exp = explode('?', $url);
$url = $url_exp[0];
if ($url[-1] == '/') {
$url = substr($url, 0, -1);
}
$this->redirect_url = $url;
// keycloak Realm ID
$this->realm_id = $keycloak_config['realm'] ?? 'grommunio';
// keycloak client ID
$this->client_id = $keycloak_config['resource'] ?? 'gramm';
// @type {bool} checks if client is a public client and extracts the public key
$this->is_public = $keycloak_config['public-client'] ?? false;
$this->public_key = $this->is_public == false ? "" : "-----BEGIN PUBLIC KEY-----\n" . chunk_split((string) $keycloak_config['realm-public-key'], 64, "\n") . "\n-----END PUBLIC KEY-----\n";
// client secret => obtained if client is not a public client
if (!$this->is_public) {
$this->secret = $keycloak_config['credentials']['secret'] ?? $keycloak_config['secret'] ?? null;
}
// keycloak server url
$auth_server_url = $keycloak_config['auth-server-url'] ?? 'null';
// Root realm URL.
$this->realm_url = $auth_server_url . 'realms/' . $this->realm_id;
// Root realm admin URL.
$this->realm_admin_url = $auth_server_url . 'admin/realms/' . $this->realm_id;
}
/**
* Static method to instantiate and return a KeyCloak instance from the
* default configuration file.
*
* @return KeyCloak
*/
public static function getInstance() {
if (!defined('GROMOX_CONFIG_PATH')) {
define('GROMOX_CONFIG_PATH', '/etc/gromox/');
}
if (is_null(KeyCloak::$_instance) && file_exists(GROMOX_CONFIG_PATH . 'keycloak.json')) {
// Read the keycloak config adapter into an instance of the keyclaok class
$keycloak_file = file_get_contents(GROMOX_CONFIG_PATH . 'keycloak.json');
$keycloak_json = json_decode($keycloak_file, true);
KeyCloak::$_instance = new KeyCloak($keycloak_json);
}
return KeyCloak::$_instance;
}
/**
* Returns the last known refresh time.
*
* @return long
*/
public function get_last_refresh_time() {
return $this->last_refresh_time;
}
/**
* Sets the last refresh time.
*
* @param long $time
*/
public function set_last_refresh_time($time) {
$this->last_refresh_time = $time;
}
/**
* Oauth 2.0 Authorization flow is used to obtain Access token,
* refresh token and ID token from keycloak server by sending
* https post request (curl) to a /token web endpoint.
* The keycloak server will respond with grant back to the
* grommunio server.
*
* We implement three of this protocol:
* 1. OAuth 2.0 resource owner password credential grant.
* 2. OAuth 2.0 Client Code credential grant.
* 3. Refresh token grant.
*
* The password grant takes two argument:
*
* @param string $username The username
* @param string $password The cleartext password
*
* @return bool indicating if the request was successful nor not
*/
public function password_grant_req($username, $password) {
$params = ['grant_type' => 'password', 'username' => $username, 'password' => $password];
return $this->request($params);
}
/**
* The Oauth 2.0 client credential code grant is the next type request used to
* request access token from keycloak. The logon on the Authentication server url
* (keycloak), on successful authentication. the server replies with the credential
* grant code. This code will be used to request the tokens.
*
* @param string $code The code from a successful login redirected from Keycloak
* @param null|string $session_host
*
* @return bool indicating if the request was successful nor not
*/
public function client_credential_grant_req($code, $session_host = null) {
// TODO: $session_host not used here
$params = ['grant_type' => 'authorization_code', 'code' => $code, 'client_id' => $this->client_id, 'redirect_uri' => $this->redirect_url];
return $this->request($params);
}
/**
* The Oauth 2.0 refresh token grant is the next type request used to
* request access token from keycloak. If the client has a valid refresh token
* which has not expired. It can send a request to the server to obtain new tokens.
*
* @return bool indicating if the request was successful nor not
*/
public function refresh_grant_req() {
// Ensure grant exists, grant is not expired, and we have a refresh token
if (!$this->grant || !$this->refresh_token) {
$this->grant = null;
return false;
}
$params = ['grant_type' => 'refresh_token', 'refresh_token' => $this->refresh_token->get_payload()];
return $this->request($params);
}
/**
* Performs a token request to the KeyCloak server with predefined parameters.
*
* @param array $params predefined parameters used for the request
*
* @return bool indicating if the request was successful or not
*/
protected function request($params) {
$headers = ['Content-Type: application/x-www-form-urlencoded'];
if ($this->is_public) {
$params['client_id'] = $this->client_id;
}
else {
array_push($headers, 'Authorization: Basic ' . base64_encode($this->client_id . ':' . $this->secret));
}
$params['scope'] = 'openid';
$response = $this->http_curl_request('POST', '/protocol/openid-connect/token', $headers, http_build_query($params));
if ($response['code'] < 200 || $response['code'] > 299) {
$this->error = $response['body'];
$this->grant = null;
return false;
}
$this->grant = $response['body'];
if (gettype($this->grant) === 'string') {
$this->grant = json_decode($this->grant, true);
}
else {
$this->grant = json_encode($this->grant);
}
$this->access_token = isset($this->grant['access_token']) ? new Token($this->grant['access_token']) : null;
$this->refresh_token = isset($this->grant['refresh_token']) ? new Token($this->grant['refresh_token']) : null;
$this->id_token = isset($this->grant['id_token']) ? new Token($this->grant['id_token']) : null;
return true;
}
/**
* Validates the grant represented by the access and refresh tokens in the grant.
* If the refresh token has expired too, return false.
*
* @return bool
*/
public function validate_grant() {
if ($this->validate_token($this->access_token) && $this->validate_token($this->refresh_token)) {
return true;
}
return $this->refresh_grant_req();
}
/**
* Validates a token with the server.
*
* @param mixed $token
*
* @return bool
*/
protected function validate_token($token) {
if (isset($token)) {
$path = "/protocol/openid-connect/token/introspect";
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$params = ['token' => $token->get_payload()];
if ($this->is_public) {
$params['client_id'] = $this->client_id;
}
else {
array_push($headers, 'Authorization: Basic ' . base64_encode($this->client_id . ':' . $this->secret));
}
$response = $this->http_curl_request('POST', $path, $headers, http_build_query($params));
if ($response['code'] < 200 || $response['code'] > 299) {
return false;
}
try {
$data = json_decode((string) $response['body'], true);
}
catch (Exception) {
return false;
}
return !array_key_exists('error', $data);
}
return false;
}
/**
* Indicates if the access token is expired.
*
* @return bool
*/
public function is_expired() {
if (!$this->access_token) {
return true;
}
return $this->access_token->is_expired();
}
/**
* Builds a KeyCloak login url used with the client credential code.
*
* @param string $redirect_url Redirect URL to be parameterized in the URL
*
* @return string
*/
public function login_url($redirect_url) {
$uuid = bin2hex(openssl_random_pseudo_bytes(32));
return $this->realm_url . '/protocol/openid-connect/auth?scope=openid&client_id=' . urlencode((string) $this->client_id) . '&state=' . urlencode($uuid) . '&redirect_uri=' . urlencode($redirect_url) . '&response_type=code';
}
/**
* Builds a KeyCloak logout url.
*
* @return string
*/
public function logout() {
$params = '?id_token_hint=' . $this->id_token->get_payload() . '&refresh_token=' . $this->refresh_token->get_payload();
return $this->realm_url . '/protocol/openid-connect/logout' . $params;
}
/*
* Send HTTP request via CURL.
*
* @param string $method The HTTP request to use. (Default to GET)
* @param array $headers The HTTP headers to be passed into the request
* @param string $data The data to be passed into the body of the request
* @param string $domain
*
* @return array associative array with 'code' for response code and 'body' for request body
*/
protected function http_curl_request($method, $domain, $headers = [], $data = '') {
$request = curl_init();
curl_setopt($request, CURLOPT_URL, $this->realm_url . $domain);
if (strcmp(strtoupper((string) $method), 'POST') == 0) {
curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_POSTFIELDS, $data);
array_push($headers, 'Content-Length: ' . strlen((string) $data));
}
curl_setopt($request, CURLOPT_HTTPHEADER, $headers);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($request);
$response_code = curl_getinfo($request, CURLINFO_HTTP_CODE);
curl_close($request);
return [
'code' => $response_code,
'body' => $response,
];
}
}