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