Skip to content

Commit bd8dac6

Browse files
committed
Added example and update docs
1 parent 0a2c84d commit bd8dac6

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

doc/services/identity/v3/tokens.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,37 @@ Revoke token
4343

4444
.. sample:: identity/v3/tokens/revoke_token.php
4545
.. refdoc:: OpenStack/Identity/v3/Service.html#method_revokeToken
46+
47+
Cache authentication token
48+
--------------------------
49+
50+
Use case
51+
~~~~~~~~
52+
53+
Before one can start calling any API, a very first step that this SDK will do is to authenticate with Identity service
54+
using user's credential.
55+
56+
If the user's credential is valid, Identity service responses with an authentication token embedded in X-Auth-Token
57+
header and services catalog. The SDK will then use this authentication token and services catalog in all subsequent API
58+
calls.
59+
60+
This setup typically works well for command line type of applications. However, for web-based applications, performance
61+
is undesirable since authentication step adds ~100ms to the response time.
62+
63+
In order to improve performance, SDK allows users to export and store authentication token and re-use it when it is
64+
still valid.
65+
66+
67+
Generate token and persist to file
68+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69+
70+
.. sample:: identity/v3/tokens/export_authentication_token.php
71+
72+
73+
For scalability, it is recommended that cached tokens are stored in persistent storage such as memcache or redis instead
74+
of local file.
75+
76+
Initialize Open Stack using cached authentication token
77+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78+
79+
.. sample:: identity/v3/tokens/use_cached_authentication_token.php
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
$params = [
6+
'authUrl' => '{authUrl}',
7+
'region' => '{region}',
8+
'user' => [
9+
'name' => '{username}',
10+
'password' => '{password}',
11+
'domain' => ['id' => '{domainId}']
12+
],
13+
'scope' => [
14+
'project' => ['id' => '{projectId}']
15+
]
16+
];
17+
18+
$openstack = new OpenStack\OpenStack($params);
19+
20+
$identity = $openstack->identityV3();
21+
22+
$token = $identity->generateToken($params);
23+
24+
// Display token expiry
25+
echo sprintf('Token expires at %s'. PHP_EOL, $token->expires->format('c'));
26+
27+
// Save token to file
28+
file_put_contents('token.json', json_encode($token->export()));
29+
30+
31+
// Alternatively, one may persist token to memcache or redis
32+
// Redis and memcache then can purge the entry when token expires.
33+
$memcache->set('token', $token->export, $token->expires->format('U'));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
require 'vendor/autoload.php';
4+
5+
6+
7+
$params = [
8+
'authUrl' => '{authUrl}',
9+
'region' => '{region}',
10+
'user' => [
11+
'name' => '{username}',
12+
'password' => '{password}',
13+
'domain' => ['id' => '{domainId}']
14+
],
15+
'scope' => [
16+
'project' => ['id' => '{projectId}']
17+
]
18+
];
19+
20+
$token = json_decode(file_get_contents('token.json'), true);
21+
22+
// Inject cached token to params if token is still fresh
23+
if ((new \DateTimeImmutable($token['expires_at'])) > (new \DateTimeImmutable('now'))) {
24+
$params['cachedToken'] = $token;
25+
}
26+
27+
$openstack = new OpenStack\OpenStack($params);

src/OpenStack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class OpenStack
3131
* ['logger'] = (LoggerInterface) Must set if debugLog is true [OPTIONAL]
3232
* ['messageFormatter'] = (MessageFormatter) Must set if debugLog is true [OPTIONAL]
3333
* ['requestOptions'] = (array) Guzzle Http request options [OPTIONAL]
34-
* ['cachedCredential'] = (array) Cached token credential [OPTIONAL]
34+
* ['cachedToken'] = (array) Cached token credential [OPTIONAL]
3535
*
3636
* @param Builder $builder
3737
*/

0 commit comments

Comments
 (0)