1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenStack\Identity\v3; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use OpenStack\Common\Auth\IdentityService; |
7
|
|
|
use OpenStack\Common\Error\BadResponseError; |
8
|
|
|
use OpenStack\Common\Service\AbstractService; |
9
|
|
|
use OpenStack\Identity\v3\Models; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Represents the Keystone v3 service. |
13
|
|
|
* |
14
|
|
|
* @property \OpenStack\Identity\v3\Api $api |
15
|
|
|
*/ |
16
|
|
|
class Service extends AbstractService implements IdentityService |
17
|
|
|
{ |
18
|
5 |
|
public static function factory(ClientInterface $client): self |
19
|
|
|
{ |
20
|
5 |
|
return new static($client, new Api()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Authenticates credentials, giving back a token and a base URL for the service. |
25
|
|
|
* |
26
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postTokens} |
27
|
|
|
* |
28
|
|
|
* @return array Returns a {@see Models\Token} as the first element, a string base URL as the second |
29
|
|
|
*/ |
30
|
6 |
|
public function authenticate(array $options): array |
31
|
|
|
{ |
32
|
6 |
|
$authOptions = array_intersect_key($options, $this->api->postTokens()['params']); |
33
|
|
|
|
34
|
6 |
|
if (!empty($options['cachedToken'])) { |
35
|
|
|
$token = $this->generateTokenFromCache($options['cachedToken']); |
36
|
2 |
|
|
37
|
2 |
|
if ($token->hasExpired()) { |
38
|
2 |
|
throw new \RuntimeException(sprintf('Cached token has expired. You may need to re-generate a new token.')); |
39
|
2 |
|
} |
40
|
|
|
} else { |
41
|
2 |
|
$token = $this->generateToken($authOptions); |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
$name = $options['catalogName']; |
45
|
1 |
|
$type = $options['catalogType']; |
46
|
1 |
|
$region = $options['region']; |
47
|
|
|
$interface = isset($options['interface']) ? $options['interface'] : Enum::INTERFACE_PUBLIC; |
48
|
|
|
|
49
|
|
|
if ($baseUrl = $token->catalog->getServiceUrl($name, $type, $region, $interface)) { |
50
|
|
|
return [$token, $baseUrl]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
throw new \RuntimeException(sprintf("No service found with type [%s] name [%s] region [%s] interface [%s]", |
54
|
|
|
$type, $name, $region, $interface)); |
55
|
|
|
} |
56
|
8 |
|
|
57
|
|
|
public function generateTokenFromCache(array $cache): Models\Token |
58
|
8 |
|
{ |
59
|
|
|
return $this->model(Models\Token::class)->populateFromArray($cache); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Generates a new authentication token |
64
|
|
|
* |
65
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postTokens} |
66
|
|
|
* |
67
|
|
|
* @return Models\Token |
68
|
|
|
*/ |
69
|
1 |
|
public function generateToken(array $options): Models\Token |
70
|
|
|
{ |
71
|
1 |
|
return $this->model(Models\Token::class)->create($options); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Retrieves a token object and populates its unique identifier object. This operation will not perform a GET or |
76
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
77
|
|
|
* |
78
|
|
|
* @param string $id The unique ID of the token to retrieve |
79
|
|
|
* |
80
|
|
|
* @return Models\Token |
81
|
2 |
|
*/ |
82
|
|
|
public function getToken(string $id): Models\Token |
83
|
|
|
{ |
84
|
2 |
|
return $this->model(Models\Token::class, ['id' => $id]); |
85
|
1 |
|
} |
86
|
1 |
|
|
87
|
1 |
|
/** |
88
|
|
|
* Validates a token, identified by its ID, and returns TRUE if its valid, FALSE if not. |
89
|
|
|
* |
90
|
|
|
* @param string $id The unique ID of the token |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function validateToken(string $id): bool |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
$this->execute($this->api->headTokens(), ['tokenId' => $id]); |
98
|
|
|
return true; |
99
|
1 |
|
} catch (BadResponseError $e) { |
100
|
1 |
|
return false; |
101
|
1 |
|
} |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Revokes a token, identified by its ID. After this operation completes, users will not be able to use this token |
106
|
|
|
* again for authentication. |
107
|
|
|
* |
108
|
|
|
* @param string $id The unique ID of the token |
109
|
|
|
*/ |
110
|
|
|
public function revokeToken(string $id) |
111
|
1 |
|
{ |
112
|
|
|
$this->execute($this->api->deleteTokens(), ['tokenId' => $id]); |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Creates a new service according to the provided options. |
117
|
|
|
* |
118
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postServices} |
119
|
|
|
* |
120
|
|
|
* @return Models\Service |
121
|
|
|
*/ |
122
|
|
|
public function createService(array $options): Models\Service |
123
|
|
|
{ |
124
|
|
|
return $this->model(Models\Service::class)->create($options); |
|
|
|
|
125
|
1 |
|
} |
126
|
|
|
|
127
|
1 |
|
/** |
128
|
|
|
* Returns a generator which will yield a collection of service objects. The elements which generators yield can be |
129
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
130
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
131
|
|
|
* |
132
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getServices} |
133
|
|
|
* |
134
|
|
|
* @return \Generator |
135
|
|
|
*/ |
136
|
|
|
public function listServices(array $options = []): \Generator |
137
|
|
|
{ |
138
|
1 |
|
return $this->model(Models\Service::class)->enumerate($this->api->getServices(), $options); |
|
|
|
|
139
|
|
|
} |
140
|
1 |
|
|
141
|
|
|
/** |
142
|
|
|
* Retrieves a service object and populates its unique identifier object. This operation will not perform a GET or |
143
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
144
|
|
|
* |
145
|
|
|
* @param string $id The unique ID of the service |
146
|
|
|
* |
147
|
|
|
* @return Models\Service |
148
|
|
|
*/ |
149
|
|
|
public function getService(string $id): Models\Service |
150
|
2 |
|
{ |
151
|
|
|
return $this->model(Models\Service::class, ['id' => $id]); |
152
|
2 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Creates a new endpoint according to the provided options. |
156
|
|
|
* |
157
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postEndpoints} |
158
|
|
|
* |
159
|
|
|
* @return Models\Endpoint |
160
|
|
|
*/ |
161
|
|
|
public function createEndpoint(array $options): Models\Endpoint |
162
|
|
|
{ |
163
|
1 |
|
return $this->model(Models\Endpoint::class)->create($options); |
|
|
|
|
164
|
|
|
} |
165
|
1 |
|
|
166
|
|
|
/** |
167
|
|
|
* Retrieves an endpoint object and populates its unique identifier object. This operation will not perform a GET or |
168
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
169
|
|
|
* |
170
|
|
|
* @param string $id The unique ID of the service |
171
|
|
|
* |
172
|
|
|
* @return Models\Endpoint |
173
|
|
|
*/ |
174
|
|
|
public function getEndpoint(string $id): Models\Endpoint |
175
|
|
|
{ |
176
|
|
|
return $this->model(Models\Endpoint::class, ['id' => $id]); |
177
|
1 |
|
} |
178
|
|
|
|
179
|
1 |
|
/** |
180
|
|
|
* Returns a generator which will yield a collection of endpoint objects. The elements which generators yield can be |
181
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
182
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
183
|
|
|
* |
184
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getEndpoints} |
185
|
|
|
* |
186
|
|
|
* @return \Generator |
187
|
|
|
*/ |
188
|
|
|
public function listEndpoints(array $options = []): \Generator |
189
|
1 |
|
{ |
190
|
|
|
return $this->model(Models\Endpoint::class)->enumerate($this->api->getEndpoints(), $options); |
|
|
|
|
191
|
1 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Creates a new domain according to the provided options. |
195
|
|
|
* |
196
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postDomains} |
197
|
|
|
* |
198
|
|
|
* @return Models\Domain |
199
|
|
|
*/ |
200
|
|
|
public function createDomain(array $options): Models\Domain |
201
|
|
|
{ |
202
|
|
|
return $this->model(Models\Domain::class)->create($options); |
|
|
|
|
203
|
1 |
|
} |
204
|
|
|
|
205
|
1 |
|
/** |
206
|
|
|
* Returns a generator which will yield a collection of domain objects. The elements which generators yield can be |
207
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
208
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
209
|
|
|
* |
210
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getDomains} |
211
|
|
|
* |
212
|
|
|
* @return \Generator |
213
|
|
|
*/ |
214
|
|
|
public function listDomains(array $options = []): \Generator |
215
|
|
|
{ |
216
|
1 |
|
return $this->model(Models\Domain::class)->enumerate($this->api->getDomains(), $options); |
|
|
|
|
217
|
|
|
} |
218
|
1 |
|
|
219
|
|
|
/** |
220
|
|
|
* Retrieves a domain object and populates its unique identifier object. This operation will not perform a GET or |
221
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
222
|
|
|
* |
223
|
|
|
* @param string $id The unique ID of the domain |
224
|
|
|
* |
225
|
|
|
* @return Models\Domain |
226
|
|
|
*/ |
227
|
|
|
public function getDomain(string $id): Models\Domain |
228
|
1 |
|
{ |
229
|
|
|
return $this->model(Models\Domain::class, ['id' => $id]); |
230
|
1 |
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Creates a new project according to the provided options. |
234
|
|
|
* |
235
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postProjects} |
236
|
|
|
* |
237
|
|
|
* @return Models\Project |
238
|
|
|
*/ |
239
|
|
|
public function createProject(array $options): Models\Project |
240
|
|
|
{ |
241
|
|
|
return $this->model(Models\Project::class)->create($options); |
|
|
|
|
242
|
1 |
|
} |
243
|
|
|
|
244
|
1 |
|
/** |
245
|
|
|
* Returns a generator which will yield a collection of project objects. The elements which generators yield can be |
246
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
247
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
248
|
|
|
* |
249
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getProjects} |
250
|
|
|
* |
251
|
|
|
* @return \Generator |
252
|
|
|
*/ |
253
|
|
|
public function listProjects(array $options = []): \Generator |
254
|
|
|
{ |
255
|
1 |
|
return $this->model(Models\Project::class)->enumerate($this->api->getProjects(), $options); |
|
|
|
|
256
|
|
|
} |
257
|
1 |
|
|
258
|
|
|
/** |
259
|
|
|
* Retrieves a project object and populates its unique identifier object. This operation will not perform a GET or |
260
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
261
|
|
|
* |
262
|
|
|
* @param string $id The unique ID of the project |
263
|
|
|
* |
264
|
|
|
* @return Models\Project |
265
|
|
|
*/ |
266
|
|
|
public function getProject(string $id): Models\Project |
267
|
1 |
|
{ |
268
|
|
|
return $this->model(Models\Project::class, ['id' => $id]); |
269
|
1 |
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Creates a new user according to the provided options. |
273
|
|
|
* |
274
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postUsers} |
275
|
|
|
* |
276
|
|
|
* @return Models\User |
277
|
|
|
*/ |
278
|
|
|
public function createUser(array $options): Models\User |
279
|
|
|
{ |
280
|
|
|
return $this->model(Models\User::class)->create($options); |
|
|
|
|
281
|
1 |
|
} |
282
|
|
|
|
283
|
1 |
|
/** |
284
|
|
|
* Returns a generator which will yield a collection of user objects. The elements which generators yield can be |
285
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
286
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
287
|
|
|
* |
288
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getUsers} |
289
|
|
|
* |
290
|
|
|
* @return \Generator |
291
|
|
|
*/ |
292
|
|
|
public function listUsers(array $options = []): \Generator |
293
|
|
|
{ |
294
|
1 |
|
return $this->model(Models\User::class)->enumerate($this->api->getUsers(), $options); |
|
|
|
|
295
|
|
|
} |
296
|
1 |
|
|
297
|
|
|
/** |
298
|
|
|
* Retrieves a user object and populates its unique identifier object. This operation will not perform a GET or |
299
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
300
|
|
|
* |
301
|
|
|
* @param string $id The unique ID of the user |
302
|
|
|
* |
303
|
|
|
* @return Models\User |
304
|
|
|
*/ |
305
|
|
|
public function getUser(string $id): Models\User |
306
|
1 |
|
{ |
307
|
|
|
return $this->model(Models\User::class, ['id' => $id]); |
308
|
1 |
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Creates a new group according to the provided options. |
312
|
|
|
* |
313
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postGroups} |
314
|
|
|
* |
315
|
|
|
* @return Models\Group |
316
|
|
|
*/ |
317
|
|
|
public function createGroup(array $options): Models\Group |
318
|
|
|
{ |
319
|
|
|
return $this->model(Models\Group::class)->create($options); |
|
|
|
|
320
|
1 |
|
} |
321
|
|
|
|
322
|
1 |
|
/** |
323
|
|
|
* Returns a generator which will yield a collection of group objects. The elements which generators yield can be |
324
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
325
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
326
|
|
|
* |
327
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getGroups} |
328
|
|
|
* |
329
|
|
|
* @return \Generator |
330
|
|
|
*/ |
331
|
|
|
public function listGroups(array $options = []): \Generator |
332
|
|
|
{ |
333
|
1 |
|
return $this->model(Models\Group::class)->enumerate($this->api->getGroups(), $options); |
|
|
|
|
334
|
|
|
} |
335
|
1 |
|
|
336
|
|
|
/** |
337
|
|
|
* Retrieves a group object and populates its unique identifier object. This operation will not perform a GET or |
338
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
339
|
|
|
* |
340
|
|
|
* @param string $id The unique ID of the group |
341
|
|
|
* |
342
|
|
|
* @return Models\Group |
343
|
|
|
*/ |
344
|
|
|
public function getGroup($id): Models\Group |
345
|
1 |
|
{ |
346
|
|
|
return $this->model(Models\Group::class, ['id' => $id]); |
347
|
1 |
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Creates a new credential according to the provided options. |
351
|
|
|
* |
352
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postCredentials} |
353
|
|
|
* |
354
|
|
|
* @return Models\Credential |
355
|
|
|
*/ |
356
|
|
|
public function createCredential(array $options): Models\Credential |
357
|
1 |
|
{ |
358
|
|
|
return $this->model(Models\Credential::class)->create($options); |
|
|
|
|
359
|
1 |
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Returns a generator which will yield a collection of credential objects. The elements which generators yield can |
363
|
|
|
* be accessed using a foreach loop. Often the API will not return the full state of the resource in collections; |
364
|
|
|
* you will need to use retrieve() to pull in the full state of the remote resource from the API. |
365
|
|
|
* |
366
|
|
|
* @return \Generator |
367
|
|
|
*/ |
368
|
|
|
public function listCredentials(): \Generator |
369
|
|
|
{ |
370
|
1 |
|
return $this->model(Models\Credential::class)->enumerate($this->api->getCredentials()); |
|
|
|
|
371
|
|
|
} |
372
|
1 |
|
|
373
|
|
|
/** |
374
|
|
|
* Retrieves a credential object and populates its unique identifier object. This operation will not perform a GET |
375
|
|
|
* or HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
376
|
|
|
* |
377
|
|
|
* @param string $id The unique ID of the credential |
378
|
|
|
* |
379
|
|
|
* @return Models\Credential |
380
|
|
|
*/ |
381
|
|
|
public function getCredential(string $id): Models\Credential |
382
|
1 |
|
{ |
383
|
|
|
return $this->model(Models\Credential::class, ['id' => $id]); |
384
|
1 |
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Creates a new role according to the provided options. |
388
|
|
|
* |
389
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postRoles} |
390
|
|
|
* |
391
|
|
|
* @return Models\Role |
392
|
|
|
*/ |
393
|
|
|
public function createRole(array $options): Models\Role |
394
|
|
|
{ |
395
|
|
|
return $this->model(Models\Role::class)->create($options); |
|
|
|
|
396
|
1 |
|
} |
397
|
|
|
|
398
|
1 |
|
/** |
399
|
|
|
* Returns a generator which will yield a collection of role objects. The elements which generators yield can be |
400
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
401
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
402
|
|
|
* |
403
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getRoles} |
404
|
|
|
* |
405
|
|
|
* @return \Generator |
406
|
|
|
*/ |
407
|
|
|
public function listRoles(array $options = []): \Generator |
408
|
|
|
{ |
409
|
|
|
return $this->model(Models\Role::class)->enumerate($this->api->getRoles(), $options); |
|
|
|
|
410
|
1 |
|
} |
411
|
|
|
|
412
|
1 |
|
/** |
413
|
|
|
* Returns a generator which will yield a collection of role assignment objects. The elements which generators |
414
|
|
|
* yield can be accessed using a foreach loop. Often the API will not return the full state of the resource in |
415
|
|
|
* collections; you will need to use retrieve() to pull in the full state of the remote resource from the API. |
416
|
|
|
* |
417
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getRoleAssignments} |
418
|
|
|
* |
419
|
|
|
* @return \Generator |
420
|
|
|
*/ |
421
|
|
|
public function listRoleAssignments(array $options = []): \Generator |
422
|
1 |
|
{ |
423
|
|
|
return $this->model(Models\Assignment::class)->enumerate($this->api->getRoleAssignments(), $options); |
|
|
|
|
424
|
1 |
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Creates a new policy according to the provided options. |
428
|
|
|
* |
429
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::postPolicies} |
430
|
|
|
* |
431
|
|
|
* @return Models\Policy |
432
|
|
|
*/ |
433
|
|
|
public function createPolicy(array $options): Models\Policy |
434
|
|
|
{ |
435
|
|
|
return $this->model(Models\Policy::class)->create($options); |
|
|
|
|
436
|
1 |
|
} |
437
|
|
|
|
438
|
1 |
|
/** |
439
|
|
|
* Returns a generator which will yield a collection of policy objects. The elements which generators yield can be |
440
|
|
|
* accessed using a foreach loop. Often the API will not return the full state of the resource in collections; you |
441
|
|
|
* will need to use retrieve() to pull in the full state of the remote resource from the API. |
442
|
|
|
* |
443
|
|
|
* @param array $options {@see \OpenStack\Identity\v3\Api::getPolicies} |
444
|
|
|
* |
445
|
|
|
* @return \Generator |
446
|
|
|
*/ |
447
|
|
|
public function listPolicies(array $options = []): \Generator |
448
|
|
|
{ |
449
|
1 |
|
return $this->model(Models\Policy::class)->enumerate($this->api->getPolicies(), $options); |
|
|
|
|
450
|
|
|
} |
451
|
1 |
|
|
452
|
|
|
/** |
453
|
|
|
* Retrieves a policy object and populates its unique identifier object. This operation will not perform a GET or |
454
|
|
|
* HEAD request by default; you will need to call retrieve() if you want to pull in remote state from the API. |
455
|
|
|
* |
456
|
|
|
* @param string $id The unique ID of the policy |
457
|
|
|
* |
458
|
|
|
* @return Models\Policy |
459
|
|
|
*/ |
460
|
|
|
public function getPolicy(string $id): Models\Policy |
461
|
|
|
{ |
462
|
|
|
return $this->model(Models\Policy::class, ['id' => $id]); |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: