1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Micro |
6
|
|
|
* |
7
|
|
|
* @author Raffael Sahli <[email protected]> |
8
|
|
|
* @copyright Copyright (c) 2017 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license MIT https://opensource.org/licenses/MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Micro\Auth\Adapter; |
13
|
|
|
|
14
|
|
|
use \Psr\Log\LoggerInterface; |
15
|
|
|
use \Micro\Auth\Exception; |
16
|
|
|
|
17
|
|
|
class Oidc extends AbstractAdapter |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* OpenID-connect discovery path |
21
|
|
|
*/ |
22
|
|
|
CONST DISCOVERY_PATH = '/.well-known/openid-configuration'; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* OpenID-connect provider url |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $provider_url = 'https://oidc.example.org'; |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Token validation endpoint (rfc7662) |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $token_validation_url; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Identity attribute |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $identity_attribute = 'preferred_username'; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Attributes |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $attributes = []; |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Access token |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $access_token; |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Init adapter |
67
|
|
|
* |
68
|
|
|
* @param LoggerInterface $logger |
69
|
|
|
* @param Iterable $config |
70
|
|
|
* @return void |
|
|
|
|
71
|
|
|
*/ |
72
|
|
|
public function __construct(LoggerInterface $logger, ?Iterable $config=null) |
73
|
|
|
{ |
74
|
|
|
$this->logger = $logger; |
75
|
|
|
$this->setOptions($config); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set options |
81
|
|
|
* |
82
|
|
|
* @param Iterable $config |
83
|
|
|
* @return AdapterInterface |
84
|
|
|
*/ |
85
|
|
|
public function setOptions(? Iterable $config = null) : AdapterInterface |
86
|
|
|
{ |
87
|
|
|
if ($config === null) { |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
foreach($config as $option => $value) { |
92
|
|
|
switch($option) { |
93
|
|
|
case 'provider_url': |
|
|
|
|
94
|
|
|
case 'token_validation_url': |
95
|
|
|
case 'identity_attribute': |
96
|
|
|
$this->{$option} = (string)$value; |
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return parent::setOptions($config); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Authenticate |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function authenticate(): bool |
111
|
|
|
{ |
112
|
|
|
if (!isset($_SERVER['HTTP_AUTHORIZATION'])) { |
113
|
|
|
$this->logger->debug('skip auth adapter ['.get_class($this).'], no http authorization header or access_token param found', [ |
114
|
|
|
'category' => get_class($this) |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} else { |
119
|
|
|
$header = $_SERVER['HTTP_AUTHORIZATION']; |
120
|
|
|
$parts = explode(' ', $header); |
121
|
|
|
|
122
|
|
|
if ($parts[0] == 'Bearer') { |
123
|
|
|
$this->logger->debug('found http bearer authorization header', [ |
124
|
|
|
'category' => get_class($this) |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
return $this->verifyToken($parts[1]); |
128
|
|
|
} else { |
129
|
|
|
$this->logger->debug('http authorization header contains no bearer string or invalid authentication string', [ |
130
|
|
|
'category' => get_class($this) |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get discovery url |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getDiscoveryUrl(): string |
145
|
|
|
{ |
146
|
|
|
return $this->provider_url.self::DISCOVERY_PATH; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get discovery document |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public function getDiscoveryDocument(): array |
156
|
|
|
{ |
157
|
|
|
if ($apc = extension_loaded('apc') && apc_exists($this->provider_url)) { |
|
|
|
|
158
|
|
|
return apc_get($this->provider_url); |
159
|
|
|
} else { |
160
|
|
|
$ch = curl_init(); |
161
|
|
|
$url = $this->getDiscoveryUrl(); |
162
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
163
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
164
|
|
|
|
165
|
|
|
$this->logger->debug('fetch openid-connect discovery document from ['.$url.']', [ |
166
|
|
|
'category' => get_class($this) |
167
|
|
|
]); |
168
|
|
|
|
169
|
|
|
$result = curl_exec($ch); |
170
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
171
|
|
|
curl_close($ch); |
172
|
|
|
|
173
|
|
|
if($code === 200) { |
174
|
|
|
$discovery = json_decode($result, true); |
175
|
|
|
$this->logger->debug('received openid-connect discovery document from ['.$url.']', [ |
176
|
|
|
'category' => get_class($this), |
177
|
|
|
'discovery'=> $discovery |
178
|
|
|
]); |
179
|
|
|
|
180
|
|
|
if ($apc === true) { |
181
|
|
|
apc_store($this->provider_url, $discovery); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $discovery; |
185
|
|
|
} else { |
186
|
|
|
$this->logger->error('failed to receive openid-connect discovery document from ['.$url.'], request ended with status ['.$code.']', [ |
187
|
|
|
'category' => get_class($this), |
188
|
|
|
]); |
189
|
|
|
|
190
|
|
|
throw new Exception('failed to get openid-connect discovery document'); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Token verification |
198
|
|
|
* |
199
|
|
|
* @param string $token |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
protected function verifyToken(string $token): bool |
203
|
|
|
{ |
204
|
|
|
if($this->token_validation_url) { |
205
|
|
|
$this->logger->debug('validate oauth2 token via rfc7662 token validation endpoint ['.$this->token_validation_url.']', [ |
206
|
|
|
'category' => get_class($this), |
207
|
|
|
]); |
208
|
|
|
|
209
|
|
|
$url = str_replace('{token}', $token, $this->token_validation_url); |
210
|
|
|
} else { |
211
|
|
|
$discovery = $this->getDiscoveryDocument(); |
212
|
|
|
if (!(isset($discovery['userinfo_endpoint']))) { |
213
|
|
|
throw new Exception('userinfo_endpoint could not be determained'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$this->logger->debug('validate token via openid-connect userinfo_endpoint ['.$discovery['userinfo_endpoint'].']', [ |
217
|
|
|
'category' => get_class($this), |
218
|
|
|
]); |
219
|
|
|
|
220
|
|
|
$url = $discovery['userinfo_endpoint'].'?access_token='.$token; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$ch = curl_init(); |
224
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
225
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
226
|
|
|
$result = curl_exec($ch); |
227
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
228
|
|
|
curl_close($ch); |
229
|
|
|
$response = json_decode($result, true); |
|
|
|
|
230
|
|
|
|
231
|
|
|
if($code === 200) { |
232
|
|
|
$attributes = json_decode($result, true); |
233
|
|
|
$this->logger->debug('successfully verified oauth2 access token via authorization server', [ |
234
|
|
|
'category' => get_class($this), |
235
|
|
|
]); |
236
|
|
|
|
237
|
|
|
if(!isset($attributes[$this->identity_attribute])) { |
238
|
|
|
throw new Exception('identity attribute '.$this->identity_attribute.' not found in oauth2 response'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
$this->identifier = $attributes['preferred_username']; |
242
|
|
|
|
243
|
|
|
if($this->token_validation_url) { |
244
|
|
|
$this->attributes = $attributes; |
245
|
|
|
} else { |
246
|
|
|
$this->access_token = $token; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return true; |
250
|
|
|
} else { |
251
|
|
|
$this->logger->error('failed verify oauth2 access token via authorization server, received status ['.$code.']', [ |
252
|
|
|
'category' => get_class($this), |
253
|
|
|
]); |
254
|
|
|
|
255
|
|
|
throw new Exception('failed verify oauth2 access token via authorization server'); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get attributes |
262
|
|
|
* |
263
|
|
|
* @return array |
264
|
|
|
*/ |
265
|
|
|
public function getAttributes(): array |
266
|
|
|
{ |
267
|
|
|
if(count($this->attributes) !== 0) { |
268
|
|
|
return $this->attributes; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$discovery = $this->getDiscoveryDocument(); |
272
|
|
|
if (!(isset($discovery['authorization_endpoint']))) { |
273
|
|
|
throw new Exception('authorization_endpoint could not be determained'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$this->logger->debug('fetch user attributes from userinfo_endpoint ['.$discovery['userinfo_endpoint'].']', [ |
277
|
|
|
'category' => get_class($this), |
278
|
|
|
]); |
279
|
|
|
|
280
|
|
|
$url = $discovery['userinfo_endpoint'].'?access_token='.$this->access_token; |
281
|
|
|
$ch = curl_init(); |
282
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
283
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
284
|
|
|
$result = curl_exec($ch); |
285
|
|
|
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
286
|
|
|
curl_close($ch); |
287
|
|
|
$response = json_decode($result, true); |
|
|
|
|
288
|
|
|
|
289
|
|
|
if($code === 200) { |
290
|
|
|
$attributes = json_decode($result, true); |
291
|
|
|
$this->logger->debug('successfully requested user attributes from userinfo_endpoint', [ |
292
|
|
|
'category' => get_class($this), |
293
|
|
|
]); |
294
|
|
|
|
295
|
|
|
return $this->attributes = $attributes; |
296
|
|
|
} else { |
297
|
|
|
$this->logger->error('failed requesting user attributes from userinfo_endpoint, status code ['.$code.']', [ |
298
|
|
|
'category' => get_class($this), |
299
|
|
|
]); |
300
|
|
|
|
301
|
|
|
throw new Exception('failed requesting user attribute from userinfo_endpoint'); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.