PluginProbe
Gianism / 4.3.4
Gianism v4.3.4
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / vendor / google / auth / src / FetchAuthTokenCache.php

FetchAuthTokenCache.php in Gianism 4.3.4, at vendor/google/auth/src/FetchAuthTokenCache.php

232 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2010 Google Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 namespace Google\Auth;
19
20 use Psr\Cache\CacheItemPoolInterface;
21
22 /**
23 * A class to implement caching for any object implementing
24 * FetchAuthTokenInterface
25 */
26 class FetchAuthTokenCache implements
27 FetchAuthTokenInterface,
28 GetQuotaProjectInterface,
29 SignBlobInterface,
30 ProjectIdProviderInterface,
31 UpdateMetadataInterface
32 {
33 use CacheTrait;
34
35 /**
36 * @var FetchAuthTokenInterface
37 */
38 private $fetcher;
39
40 /**
41 * @var array
42 */
43 private $cacheConfig;
44
45 /**
46 * @var CacheItemPoolInterface
47 */
48 private $cache;
49
50 /**
51 * @param FetchAuthTokenInterface $fetcher A credentials fetcher
52 * @param array $cacheConfig Configuration for the cache
53 * @param CacheItemPoolInterface $cache
54 */
55 public function __construct(
56 FetchAuthTokenInterface $fetcher,
57 array $cacheConfig = null,
58 CacheItemPoolInterface $cache
59 ) {
60 $this->fetcher = $fetcher;
61 $this->cache = $cache;
62 $this->cacheConfig = array_merge([
63 'lifetime' => 1500,
64 'prefix' => '',
65 ], (array) $cacheConfig);
66 }
67
68 /**
69 * Implements FetchAuthTokenInterface#fetchAuthToken.
70 *
71 * Checks the cache for a valid auth token and fetches the auth tokens
72 * from the supplied fetcher.
73 *
74 * @param callable $httpHandler callback which delivers psr7 request
75 * @return array the response
76 * @throws \Exception
77 */
78 public function fetchAuthToken(callable $httpHandler = null)
79 {
80 // Use the cached value if its available.
81 //
82 // TODO: correct caching; update the call to setCachedValue to set the expiry
83 // to the value returned with the auth token.
84 //
85 // TODO: correct caching; enable the cache to be cleared.
86 $cacheKey = $this->fetcher->getCacheKey();
87 $cached = $this->getCachedValue($cacheKey);
88 if (is_array($cached)) {
89 if (empty($cached['expires_at'])) {
90 // If there is no expiration data, assume token is not expired.
91 // (for JwtAccess and ID tokens)
92 return $cached;
93 }
94 if (time() < $cached['expires_at']) {
95 // access token is not expired
96 return $cached;
97 }
98 }
99
100 $auth_token = $this->fetcher->fetchAuthToken($httpHandler);
101
102 if (isset($auth_token['access_token']) ||
103 isset($auth_token['id_token'])) {
104 $this->setCachedValue($cacheKey, $auth_token);
105 }
106
107 return $auth_token;
108 }
109
110 /**
111 * @return string
112 */
113 public function getCacheKey()
114 {
115 return $this->getFullCacheKey($this->fetcher->getCacheKey());
116 }
117
118 /**
119 * @return array|null
120 */
121 public function getLastReceivedToken()
122 {
123 return $this->fetcher->getLastReceivedToken();
124 }
125
126 /**
127 * Get the client name from the fetcher.
128 *
129 * @param callable $httpHandler An HTTP handler to deliver PSR7 requests.
130 * @return string
131 */
132 public function getClientName(callable $httpHandler = null)
133 {
134 return $this->fetcher->getClientName($httpHandler);
135 }
136
137 /**
138 * Sign a blob using the fetcher.
139 *
140 * @param string $stringToSign The string to sign.
141 * @param bool $forceOpenSsl Require use of OpenSSL for local signing. Does
142 * not apply to signing done using external services. **Defaults to**
143 * `false`.
144 * @return string The resulting signature.
145 * @throws \RuntimeException If the fetcher does not implement
146 * `Google\Auth\SignBlobInterface`.
147 */
148 public function signBlob($stringToSign, $forceOpenSsl = false)
149 {
150 if (!$this->fetcher instanceof SignBlobInterface) {
151 throw new \RuntimeException(
152 'Credentials fetcher does not implement ' .
153 'Google\Auth\SignBlobInterface'
154 );
155 }
156
157 return $this->fetcher->signBlob($stringToSign, $forceOpenSsl);
158 }
159
160 /**
161 * Get the quota project used for this API request from the credentials
162 * fetcher.
163 *
164 * @return string|null
165 */
166 public function getQuotaProject()
167 {
168 if ($this->fetcher instanceof GetQuotaProjectInterface) {
169 return $this->fetcher->getQuotaProject();
170 }
171 }
172
173 /*
174 * Get the Project ID from the fetcher.
175 *
176 * @param callable $httpHandler Callback which delivers psr7 request
177 * @return string|null
178 * @throws \RuntimeException If the fetcher does not implement
179 * `Google\Auth\ProvidesProjectIdInterface`.
180 */
181 public function getProjectId(callable $httpHandler = null)
182 {
183 if (!$this->fetcher instanceof ProjectIdProviderInterface) {
184 throw new \RuntimeException(
185 'Credentials fetcher does not implement ' .
186 'Google\Auth\ProvidesProjectIdInterface'
187 );
188 }
189
190 return $this->fetcher->getProjectId($httpHandler);
191 }
192
193 /**
194 * Updates metadata with the authorization token.
195 *
196 * @param array $metadata metadata hashmap
197 * @param string $authUri optional auth uri
198 * @param callable $httpHandler callback which delivers psr7 request
199 * @return array updated metadata hashmap
200 * @throws \RuntimeException If the fetcher does not implement
201 * `Google\Auth\UpdateMetadataInterface`.
202 */
203 public function updateMetadata(
204 $metadata,
205 $authUri = null,
206 callable $httpHandler = null
207 ) {
208 if (!$this->fetcher instanceof UpdateMetadataInterface) {
209 throw new \RuntimeException(
210 'Credentials fetcher does not implement ' .
211 'Google\Auth\UpdateMetadataInterface'
212 );
213 }
214
215 // Set the `Authentication` header from the cache, so it is not set
216 // again by the fetcher
217 $result = $this->fetchAuthToken($httpHandler);
218
219 if (isset($result['access_token'])) {
220 $metadata[self::AUTH_METADATA_KEY] = [
221 'Bearer ' . $result['access_token']
222 ];
223 }
224
225 return $this->fetcher->updateMetadata(
226 $metadata,
227 $authUri,
228 $httpHandler
229 );
230 }
231 }
232