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