PluginProbe ʕ •ᴥ•ʔ
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin / 4.9.0
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin v4.9.0
4.9.0 0.9.6 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.2 1.7.0 1.7.1 1.8.0 1.8.1 1.9.0 2.0.0 2.0.1 2.1.1 2.2.1 2.3.1 2.4.0 2.5.0 2.5.1 2.6.0 2.7.0 2.8.0 2.9.0 3.0.1 3.0.2 3.0.3 3.1.0 3.10.0 3.11.0 3.11.1 3.2.0 3.2.1 3.3.0 3.4.0 3.5.0 3.5.1 3.5.2 3.6.1 3.7.0 3.8.0 3.8.2 3.9.0 4.0.1 4.1.0 4.1.1 4.2.0 4.3.0 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.8.0 trunk 0.10.0 0.10.1 0.11.1 0.11.2 0.3.1 0.3.2 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.5.2 0.6 0.7 0.8 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5
wp-mail-smtp / vendor_prefixed / google / auth / src / FetchAuthTokenCache.php
wp-mail-smtp / vendor_prefixed / google / auth / src Last commit date
Cache 5 days ago CredentialSource 5 days ago Credentials 5 days ago HttpHandler 5 days ago Middleware 5 days ago AccessToken.php 5 days ago ApplicationDefaultCredentials.php 5 days ago CacheTrait.php 5 days ago CredentialsLoader.php 5 days ago ExternalAccountCredentialSourceInterface.php 5 days ago FetchAuthTokenCache.php 5 days ago FetchAuthTokenInterface.php 5 days ago GCECache.php 5 days ago GetQuotaProjectInterface.php 5 days ago GetUniverseDomainInterface.php 5 days ago Iam.php 5 days ago IamSignerTrait.php 5 days ago OAuth2.php 5 days ago ProjectIdProviderInterface.php 5 days ago ServiceAccountSignerTrait.php 5 days ago SignBlobInterface.php 5 days ago UpdateMetadataInterface.php 5 days ago UpdateMetadataTrait.php 5 days ago
FetchAuthTokenCache.php
262 lines
1 <?php
2
3 /*
4 * Copyright 2010 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 namespace WPMailSMTP\Vendor\Google\Auth;
19
20 use WPMailSMTP\Vendor\Psr\Cache\CacheItemPoolInterface;
21 /**
22 * A class to implement caching for any object implementing
23 * FetchAuthTokenInterface
24 */
25 class FetchAuthTokenCache implements FetchAuthTokenInterface, GetQuotaProjectInterface, GetUniverseDomainInterface, SignBlobInterface, ProjectIdProviderInterface, UpdateMetadataInterface
26 {
27 use CacheTrait;
28 /**
29 * @var FetchAuthTokenInterface
30 */
31 private $fetcher;
32 /**
33 * @var int
34 */
35 private $eagerRefreshThresholdSeconds = 10;
36 /**
37 * @param FetchAuthTokenInterface $fetcher A credentials fetcher
38 * @param array<mixed> $cacheConfig Configuration for the cache
39 * @param CacheItemPoolInterface $cache
40 */
41 public function __construct(FetchAuthTokenInterface $fetcher, ?array $cacheConfig = null, ?CacheItemPoolInterface $cache = null)
42 {
43 $this->fetcher = $fetcher;
44 $this->cache = $cache;
45 $this->cacheConfig = \array_merge(['lifetime' => 1500, 'prefix' => '', 'cacheUniverseDomain' => $fetcher instanceof Credentials\GCECredentials], (array) $cacheConfig);
46 }
47 /**
48 * @return FetchAuthTokenInterface
49 */
50 public function getFetcher()
51 {
52 return $this->fetcher;
53 }
54 /**
55 * Implements FetchAuthTokenInterface#fetchAuthToken.
56 *
57 * Checks the cache for a valid auth token and fetches the auth tokens
58 * from the supplied fetcher.
59 *
60 * @param callable $httpHandler callback which delivers psr7 request
61 * @return array<mixed> the response
62 * @throws \Exception
63 */
64 public function fetchAuthToken(?callable $httpHandler = null)
65 {
66 if ($cached = $this->fetchAuthTokenFromCache()) {
67 return $cached;
68 }
69 $auth_token = $this->fetcher->fetchAuthToken($httpHandler);
70 $this->saveAuthTokenInCache($auth_token);
71 return $auth_token;
72 }
73 /**
74 * @return string
75 */
76 public function getCacheKey()
77 {
78 return $this->getFullCacheKey($this->fetcher->getCacheKey());
79 }
80 /**
81 * @return array<mixed>|null
82 */
83 public function getLastReceivedToken()
84 {
85 return $this->fetcher->getLastReceivedToken();
86 }
87 /**
88 * Get the client name from the fetcher.
89 *
90 * @param callable $httpHandler An HTTP handler to deliver PSR7 requests.
91 * @return string
92 */
93 public function getClientName(?callable $httpHandler = null)
94 {
95 if (!$this->fetcher instanceof SignBlobInterface) {
96 throw new \RuntimeException('Credentials fetcher does not implement ' . 'Google\\Auth\\SignBlobInterface');
97 }
98 return $this->fetcher->getClientName($httpHandler);
99 }
100 /**
101 * Sign a blob using the fetcher.
102 *
103 * @param string $stringToSign The string to sign.
104 * @param bool $forceOpenSsl Require use of OpenSSL for local signing. Does
105 * not apply to signing done using external services. **Defaults to**
106 * `false`.
107 * @return string The resulting signature.
108 * @throws \RuntimeException If the fetcher does not implement
109 * `Google\Auth\SignBlobInterface`.
110 */
111 public function signBlob($stringToSign, $forceOpenSsl = \false)
112 {
113 if (!$this->fetcher instanceof SignBlobInterface) {
114 throw new \RuntimeException('Credentials fetcher does not implement ' . 'Google\\Auth\\SignBlobInterface');
115 }
116 // Pass the access token from cache for credentials that sign blobs
117 // using the IAM API. This saves a call to fetch an access token when a
118 // cached token exists.
119 if ($this->fetcher instanceof Credentials\GCECredentials || $this->fetcher instanceof Credentials\ImpersonatedServiceAccountCredentials) {
120 $cached = $this->fetchAuthTokenFromCache();
121 $accessToken = $cached['access_token'] ?? null;
122 return $this->fetcher->signBlob($stringToSign, $forceOpenSsl, $accessToken);
123 }
124 return $this->fetcher->signBlob($stringToSign, $forceOpenSsl);
125 }
126 /**
127 * Get the quota project used for this API request from the credentials
128 * fetcher.
129 *
130 * @return string|null
131 */
132 public function getQuotaProject()
133 {
134 if ($this->fetcher instanceof GetQuotaProjectInterface) {
135 return $this->fetcher->getQuotaProject();
136 }
137 return null;
138 }
139 /*
140 * Get the Project ID from the fetcher.
141 *
142 * @param callable $httpHandler Callback which delivers psr7 request
143 * @return string|null
144 * @throws \RuntimeException If the fetcher does not implement
145 * `Google\Auth\ProvidesProjectIdInterface`.
146 */
147 public function getProjectId(?callable $httpHandler = null)
148 {
149 if (!$this->fetcher instanceof ProjectIdProviderInterface) {
150 throw new \RuntimeException('Credentials fetcher does not implement ' . 'Google\\Auth\\ProvidesProjectIdInterface');
151 }
152 // Pass the access token from cache for credentials that require an
153 // access token to fetch the project ID. This saves a call to fetch an
154 // access token when a cached token exists.
155 if ($this->fetcher instanceof Credentials\ExternalAccountCredentials) {
156 $cached = $this->fetchAuthTokenFromCache();
157 $accessToken = $cached['access_token'] ?? null;
158 return $this->fetcher->getProjectId($httpHandler, $accessToken);
159 }
160 return $this->fetcher->getProjectId($httpHandler);
161 }
162 /*
163 * Get the Universe Domain from the fetcher.
164 *
165 * @return string
166 */
167 public function getUniverseDomain() : string
168 {
169 if ($this->fetcher instanceof GetUniverseDomainInterface) {
170 if ($this->cacheConfig['cacheUniverseDomain']) {
171 return $this->getCachedUniverseDomain($this->fetcher);
172 }
173 return $this->fetcher->getUniverseDomain();
174 }
175 return GetUniverseDomainInterface::DEFAULT_UNIVERSE_DOMAIN;
176 }
177 /**
178 * Updates metadata with the authorization token.
179 *
180 * @param array<mixed> $metadata metadata hashmap
181 * @param string $authUri optional auth uri
182 * @param callable $httpHandler callback which delivers psr7 request
183 * @return array<mixed> updated metadata hashmap
184 * @throws \RuntimeException If the fetcher does not implement
185 * `Google\Auth\UpdateMetadataInterface`.
186 */
187 public function updateMetadata($metadata, $authUri = null, ?callable $httpHandler = null)
188 {
189 if (!$this->fetcher instanceof UpdateMetadataInterface) {
190 throw new \RuntimeException('Credentials fetcher does not implement ' . 'Google\\Auth\\UpdateMetadataInterface');
191 }
192 $cached = $this->fetchAuthTokenFromCache($authUri);
193 if ($cached) {
194 // Set the access token in the `Authorization` metadata header so
195 // the downstream call to updateMetadata know they don't need to
196 // fetch another token.
197 if (isset($cached['access_token'])) {
198 $metadata[self::AUTH_METADATA_KEY] = ['Bearer ' . $cached['access_token']];
199 } elseif (isset($cached['id_token'])) {
200 $metadata[self::AUTH_METADATA_KEY] = ['Bearer ' . $cached['id_token']];
201 }
202 }
203 $newMetadata = $this->fetcher->updateMetadata($metadata, $authUri, $httpHandler);
204 if (!$cached && ($token = $this->fetcher->getLastReceivedToken())) {
205 $this->saveAuthTokenInCache($token, $authUri);
206 }
207 return $newMetadata;
208 }
209 /**
210 * @param string|null $authUri
211 * @return array<mixed>|null
212 */
213 private function fetchAuthTokenFromCache($authUri = null)
214 {
215 // Use the cached value if its available.
216 //
217 // TODO: correct caching; update the call to setCachedValue to set the expiry
218 // to the value returned with the auth token.
219 //
220 // TODO: correct caching; enable the cache to be cleared.
221 // if $authUri is set, use it as the cache key
222 $cacheKey = $authUri ? $this->getFullCacheKey($authUri) : $this->fetcher->getCacheKey();
223 $cached = $this->getCachedValue($cacheKey);
224 if (\is_array($cached)) {
225 if (empty($cached['expires_at'])) {
226 // If there is no expiration data, assume token is not expired.
227 // (for JwtAccess and ID tokens)
228 return $cached;
229 }
230 if (\time() + $this->eagerRefreshThresholdSeconds < $cached['expires_at']) {
231 // access token is not expired
232 return $cached;
233 }
234 }
235 return null;
236 }
237 /**
238 * @param array<mixed> $authToken
239 * @param string|null $authUri
240 * @return void
241 */
242 private function saveAuthTokenInCache($authToken, $authUri = null)
243 {
244 if (isset($authToken['access_token']) || isset($authToken['id_token'])) {
245 // if $authUri is set, use it as the cache key
246 $cacheKey = $authUri ? $this->getFullCacheKey($authUri) : $this->fetcher->getCacheKey();
247 $this->setCachedValue($cacheKey, $authToken);
248 }
249 }
250 private function getCachedUniverseDomain(GetUniverseDomainInterface $fetcher) : string
251 {
252 $cacheKey = $this->getFullCacheKey($fetcher->getCacheKey() . 'universe_domain');
253 // @phpstan-ignore-line
254 if ($universeDomain = $this->getCachedValue($cacheKey)) {
255 return $universeDomain;
256 }
257 $universeDomain = $fetcher->getUniverseDomain();
258 $this->setCachedValue($cacheKey, $universeDomain);
259 return $universeDomain;
260 }
261 }
262