PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.3
WP-Stateless – Google Cloud Storage v3.0.3
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / Google / vendor / google / auth / src / FetchAuthTokenCache.php

FetchAuthTokenCache.php in WP-Stateless – Google Cloud Storage 3.0.3, at lib/Google/vendor/google/auth/src/FetchAuthTokenCache.php

192 lines 5.5 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 {
32 use CacheTrait;
33
34 /**
35 * @var FetchAuthTokenInterface
36 */
37 private $fetcher;
38
39 /**
40 * @var array
41 */
42 private $cacheConfig;
43
44 /**
45 * @var CacheItemPoolInterface
46 */
47 private $cache;
48
49 /**
50 * @param FetchAuthTokenInterface $fetcher A credentials fetcher
51 * @param array $cacheConfig Configuration for the cache
52 * @param CacheItemPoolInterface $cache
53 */
54 public function __construct(
55 FetchAuthTokenInterface $fetcher,
56 array $cacheConfig = null,
57 CacheItemPoolInterface $cache
58 ) {
59 $this->fetcher = $fetcher;
60 $this->cache = $cache;
61 $this->cacheConfig = array_merge([
62 'lifetime' => 1500,
63 'prefix' => '',
64 ], (array) $cacheConfig);
65 }
66
67 /**
68 * Implements FetchAuthTokenInterface#fetchAuthToken.
69 *
70 * Checks the cache for a valid auth token and fetches the auth tokens
71 * from the supplied fetcher.
72 *
73 * @param callable $httpHandler callback which delivers psr7 request
74 * @return array the response
75 * @throws \Exception
76 */
77 public function fetchAuthToken(callable $httpHandler = null)
78 {
79 // Use the cached value if its available.
80 //
81 // TODO: correct caching; update the call to setCachedValue to set the expiry
82 // to the value returned with the auth token.
83 //
84 // TODO: correct caching; enable the cache to be cleared.
85 $cacheKey = $this->fetcher->getCacheKey();
86 $cached = $this->getCachedValue($cacheKey);
87 if (is_array($cached)) {
88 if (empty($cached['expires_at'])) {
89 // If there is no expiration data, assume token is not expired.
90 // (for JwtAccess and ID tokens)
91 return $cached;
92 }
93 if (time() < $cached['expires_at']) {
94 // access token is not expired
95 return $cached;
96 }
97 }
98
99 $auth_token = $this->fetcher->fetchAuthToken($httpHandler);
100
101 if (isset($auth_token['access_token']) ||
102 isset($auth_token['id_token'])) {
103 $this->setCachedValue($cacheKey, $auth_token);
104 }
105
106 return $auth_token;
107 }
108
109 /**
110 * @return string
111 */
112 public function getCacheKey()
113 {
114 return $this->getFullCacheKey($this->fetcher->getCacheKey());
115 }
116
117 /**
118 * @return array|null
119 */
120 public function getLastReceivedToken()
121 {
122 return $this->fetcher->getLastReceivedToken();
123 }
124
125 /**
126 * Get the client name from the fetcher.
127 *
128 * @param callable $httpHandler An HTTP handler to deliver PSR7 requests.
129 * @return string
130 */
131 public function getClientName(callable $httpHandler = null)
132 {
133 return $this->fetcher->getClientName($httpHandler);
134 }
135
136 /**
137 * Sign a blob using the fetcher.
138 *
139 * @param string $stringToSign The string to sign.
140 * @param bool $forceOpenSsl Require use of OpenSSL for local signing. Does
141 * not apply to signing done using external services. **Defaults to**
142 * `false`.
143 * @return string The resulting signature.
144 * @throws \RuntimeException If the fetcher does not implement
145 * `Google\Auth\SignBlobInterface`.
146 */
147 public function signBlob($stringToSign, $forceOpenSsl = false)
148 {
149 if (!$this->fetcher instanceof SignBlobInterface) {
150 throw new \RuntimeException(
151 'Credentials fetcher does not implement ' .
152 'Google\Auth\SignBlobInterface'
153 );
154 }
155
156 return $this->fetcher->signBlob($stringToSign, $forceOpenSsl);
157 }
158
159 /**
160 * Get the quota project used for this API request from the credentials
161 * fetcher.
162 *
163 * @return string|null
164 */
165 public function getQuotaProject()
166 {
167 if ($this->fetcher instanceof GetQuotaProjectInterface) {
168 return $this->fetcher->getQuotaProject();
169 }
170 }
171
172 /*
173 * Get the Project ID from the fetcher.
174 *
175 * @param callable $httpHandler Callback which delivers psr7 request
176 * @return string|null
177 * @throws \RuntimeException If the fetcher does not implement
178 * `Google\Auth\ProvidesProjectIdInterface`.
179 */
180 public function getProjectId(callable $httpHandler = null)
181 {
182 if (!$this->fetcher instanceof ProjectIdProviderInterface) {
183 throw new \RuntimeException(
184 'Credentials fetcher does not implement ' .
185 'Google\Auth\ProvidesProjectIdInterface'
186 );
187 }
188
189 return $this->fetcher->getProjectId($httpHandler);
190 }
191 }
192