1 | <?php |
||
16 | class TokenPool |
||
17 | { |
||
18 | const TOKEN_KEY_FORMAT = 'keystone_token_3_%s'; |
||
19 | |||
20 | /** |
||
21 | * @var Tenant |
||
22 | */ |
||
23 | protected $tenant; |
||
24 | |||
25 | /** |
||
26 | * The cache where tokens are stored. |
||
27 | * |
||
28 | * @var CacheInterface |
||
29 | */ |
||
30 | protected $cache; |
||
31 | |||
32 | /** |
||
33 | * Optional logger. |
||
34 | * |
||
35 | * @var LoggerInterface |
||
36 | */ |
||
37 | protected $logger; |
||
38 | |||
39 | /** |
||
40 | * Internal client to request a new token with. |
||
41 | * |
||
42 | * @var ClientInterface |
||
43 | */ |
||
44 | protected $client; |
||
45 | |||
46 | /** |
||
47 | * The endpoint url that was obtained via the token. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $endpointUrl; |
||
52 | |||
53 | /** |
||
54 | * Cached copy of the token id. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $tokenId; |
||
59 | |||
60 | /** |
||
61 | * @param Tenant $tenant |
||
62 | * @param CacheInterface $cache |
||
63 | * @param LoggerInterface $logger |
||
64 | */ |
||
65 | 14 | public function __construct(Tenant $tenant, CacheInterface $cache, LoggerInterface $logger = null) |
|
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | 9 | public function getEndpointUrl() |
|
77 | { |
||
78 | 9 | if (null === $this->endpointUrl) { |
|
79 | 5 | $this->getToken(); |
|
80 | 5 | } |
|
81 | |||
82 | 9 | return $this->endpointUrl; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | 9 | public function getTokenId() |
|
96 | |||
97 | /** |
||
98 | * Returns a token to use for the keystone service. Uses cached instance |
||
99 | * whenever possible. |
||
100 | * |
||
101 | * @param bool $forceNew Whether to force creating a new token |
||
102 | * |
||
103 | * @return Token |
||
104 | */ |
||
105 | 10 | public function getToken($forceNew = false) |
|
128 | |||
129 | /** |
||
130 | * @throws TokenException |
||
131 | * |
||
132 | * @return Token |
||
133 | */ |
||
134 | 1 | protected function requestToken() |
|
135 | { |
||
136 | 1 | $this->logger->debug('Requesting a new token'); |
|
137 | |||
138 | $data = [ |
||
139 | 'auth' => [ |
||
140 | 'passwordCredentials' => [ |
||
141 | 1 | 'password' => $this->tenant->getPassword(), |
|
142 | 1 | 'username' => $this->tenant->getUsername(), |
|
143 | 1 | ], |
|
144 | 1 | ], |
|
145 | 1 | ]; |
|
146 | |||
147 | 1 | if ($name = $this->tenant->getTenantName()) { |
|
148 | $data['auth']['tenantName'] = $name; |
||
149 | } |
||
150 | |||
151 | try { |
||
152 | 1 | $response = $this->client->request('POST', $this->tenant->getTokenUrl(), [ |
|
153 | 1 | RequestOptions::JSON => $data, |
|
154 | 1 | ]); |
|
155 | |||
156 | 1 | return $this->createToken($response->getBody()->getContents()); |
|
157 | } catch (RequestException $e) { |
||
158 | $this->logger->error(sprintf('Error requesting token: %s', $e->getMessage())); |
||
159 | |||
160 | throw new TokenException($e->getMessage(), $e->getCode(), $e); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param string $jsonData |
||
166 | * |
||
167 | * @throws TokenException |
||
168 | * |
||
169 | * @return Token |
||
170 | */ |
||
171 | 8 | private function createToken($jsonData) |
|
181 | |||
182 | /** |
||
183 | * @param Token $token |
||
184 | * |
||
185 | * @throws TokenException |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 9 | private function getEndpointUrlFromToken(Token $token) |
|
204 | |||
205 | /** |
||
206 | * @return string |
||
207 | */ |
||
208 | 10 | private function getCacheKey() |
|
212 | } |
||
213 |