/
XenaClient.php
300 lines (273 loc) · 9.35 KB
/
XenaClient.php
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
<?php
/**
* Note : Code is released under the GNU LGPL
*
* Please do not change the header of this file
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU Lesser General Public License for more details.
*/
/**
* Light PHP wrapper for the OAuth 2.0 protocol.
*
* This client is based on the OAuth2 specification draft v2.15
* http://tools.ietf.org/html/draft-ietf-oauth-v2-15
*
* @author Pierrick Charron <pierrick@webstart.fr>
* @author Anis Berejeb <anis.berejeb@gmail.com>
* @version 1.2-dev
*/
/**
* Modifications for XENA specific OAuth2 client
*
* All credits for this code belongs to the original author.
*
* The class "XenaOAuth2Client" is a stripped down version of the original class("Client"-class)
* to clarify the use of OAuth2 authentication and the Xena API.
* This code is entirely for demonstration purposes and should not be considered as production ready code!
*
* Please note that there is no SSL verification in this code, neither any use of nonces!
*
* Modified by Thomas Eg <thomas@xena.biz> on 25. July 2017
*
*/
class XenaOAuth2Client
{
/**
* URL's
* Can be found via https://login.xena.biz/.well-known/openid-configuration
*/
const AUTHORIZATION_ENDPOINT = 'https://login.xena.biz/connect/authorize';
const TOKEN_ENDPOINT = 'https://login.xena.biz/connect/token';
/**
* HTTP Methods
*/
const HTTP_METHOD_GET = 'GET';
const HTTP_METHOD_POST = 'POST';
const HTTP_METHOD_PUT = 'PUT';
const HTTP_METHOD_DELETE = 'DELETE';
/**
* HTTP Form content types
*/
const HTTP_FORM_CONTENT_TYPE_APPLICATION = 0;
const HTTP_FORM_CONTENT_TYPE_MULTIPART = 1;
/**
* Client ID
*
* @var string
*/
protected $client_id = null;
/**
* Client Secret
*
* @var string
*/
protected $client_secret = null;
/**
* Access Token
*
* @var string
*/
protected $access_token = null;
/**
* Access Token Secret
*
* @var string
*/
protected $access_token_secret = null;
/**
* cURL options
*
* @var array
*/
protected $curl_options = array();
/**
* Construct
*
* @param string $client_id Client ID
* @param string $client_secret Client Secret
* @return void
*/
public function __construct($client_id, $client_secret)
{
if (!extension_loaded('curl')) {
throw new Exception('The PHP extension curl must be installed to use this library.', 0x01);
}
$this->client_id = $client_id;
$this->client_secret = $client_secret;
}
/**
* Get the client Id
*
* @return string Client ID
*/
public function getClientId()
{
return $this->client_id;
}
/**
* Get the client Secret
*
* @return string Client Secret
*/
public function getClientSecret()
{
return $this->client_secret;
}
/**
* getAuthenticationUrl
*
* @param string $redirect_uri Redirection URI
* @return string URL used for authentication
*/
public function getAuthenticationUrl($redirect_uri)
{
$parameters = array(
'response_type' => 'code id_token',
'client_id' => $this->client_id,
'redirect_uri' => $redirect_uri,
'nonce' => 'stuff', //NOT FOR PRODUCTION! Needed for protection against replay attacks, but it requires persistant storage
// and are therefor not implemented in this demo
'response_mode' => 'form_post',
'scope' => 'testapi openid' //Scopes are currently subject to change! "openid" is mandatory!
);
return self::AUTHORIZATION_ENDPOINT . '?' . http_build_query($parameters, null, '&');
}
/**
* getAccessToken
*
* @param int $grant_type Grant Type ('authorization_code', 'password', 'client_credentials', 'refresh_token', or a custom code (@see GrantType Classes)
* @param array $parameters Array sent to the server (depend on which grant type you're using)
* @return array Array of parameters required by the grant_type (CF SPEC)
*/
public function getAccessToken(array $parameters)
{
$parameters['grant_type'] = 'authorization_code';
$parameters['client_id'] = $this->client_id;
$parameters['client_secret'] = $this->client_secret;
$http_headers = array();
return $this->executeRequest(self::TOKEN_ENDPOINT, $parameters, self::HTTP_METHOD_POST, $http_headers, self::HTTP_FORM_CONTENT_TYPE_APPLICATION);
}
/**
* setToken
*
* @param string $token Set the access token
* @return void
*/
public function setAccessToken($token)
{
$this->access_token = $token;
}
/**
* Set an option for the curl transfer
*
* @param int $option The CURLOPT_XXX option to set
* @param mixed $value The value to be set on option
* @return void
*/
public function setCurlOption($option, $value)
{
$this->curl_options[$option] = $value;
}
/**
* Set multiple options for a cURL transfer
*
* @param array $options An array specifying which options to set and their values
* @return void
*/
public function setCurlOptions($options)
{
$this->curl_options = array_merge($this->curl_options, $options);
}
/**
* Fetch a protected ressource
*
* @param string $protected_ressource_url Protected resource URL
* @param array $parameters Array of parameters
* @param string $http_method HTTP Method to use (POST, PUT, GET, HEAD, DELETE)
* @param array $http_headers HTTP headers
* @param int $form_content_type HTTP form content type to use
* @return array
*/
public function fetch($protected_resource_url, $parameters = array(), $http_method = self::HTTP_METHOD_GET)
{
$http_headers = array();
$http_headers['Authorization'] = 'Bearer ' . $this->access_token;
$http_headers['Accept'] = 'application/json';
return $this->executeRequest($protected_resource_url, $parameters, $http_method, $http_headers);
}
/**
* Execute a request (with curl)
*
* @param string $url URL
* @param mixed $parameters Array of parameters
* @param string $http_method HTTP Method
* @param array $http_headers HTTP Headers
* @param int $form_content_type HTTP form content type to use
* @return array
*/
private function executeRequest($url, $parameters = array(), $http_method = self::HTTP_METHOD_GET, array $http_headers = null, $form_content_type = self::HTTP_FORM_CONTENT_TYPE_MULTIPART)
{
$curl_options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_CUSTOMREQUEST => $http_method
);
switch($http_method) {
case self::HTTP_METHOD_POST:
$curl_options[CURLOPT_POST] = true;
/* No break */
case self::HTTP_METHOD_PUT:
if(is_array($parameters) && self::HTTP_FORM_CONTENT_TYPE_APPLICATION === $form_content_type) {
$parameters = http_build_query($parameters, null, '&');
}
$curl_options[CURLOPT_POSTFIELDS] = $parameters;
break;
case self::HTTP_METHOD_DELETE:
case self::HTTP_METHOD_GET:
if (is_array($parameters)) {
$url .= '?' . http_build_query($parameters, null, '&');
} elseif ($parameters) {
$url .= '?' . $parameters;
}
break;
default:
break;
}
$curl_options[CURLOPT_URL] = $url;
if (is_array($http_headers)) {
$header = array();
foreach($http_headers as $key => $parsed_urlvalue) {
$header[] = "$key: $parsed_urlvalue";
}
$curl_options[CURLOPT_HTTPHEADER] = $header;
}
$ch = curl_init();
curl_setopt_array($ch, $curl_options);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if (!empty($this->curl_options)) {
curl_setopt_array($ch, $this->curl_options);
}
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
if ($curl_error = curl_error($ch)) {
throw new Exception($curl_error, 0x02);
} else {
$json_decode = json_decode($result, true);
}
curl_close($ch);
return array(
'result' => (null === $json_decode) ? $result : $json_decode,
'code' => $http_code,
'content_type' => $content_type
);
}
}