Brak opisu

FetchAuthTokenCache.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. namespace Google\Auth;
  18. use Psr\Cache\CacheItemPoolInterface;
  19. /**
  20. * A class to implement caching for any object implementing
  21. * FetchAuthTokenInterface
  22. */
  23. class FetchAuthTokenCache implements FetchAuthTokenInterface
  24. {
  25. use CacheTrait;
  26. /**
  27. * @var FetchAuthTokenInterface
  28. */
  29. private $fetcher;
  30. /**
  31. * @var array
  32. */
  33. private $cacheConfig;
  34. /**
  35. * @var CacheItemPoolInterface
  36. */
  37. private $cache;
  38. public function __construct(
  39. FetchAuthTokenInterface $fetcher,
  40. array $cacheConfig = null,
  41. CacheItemPoolInterface $cache
  42. ) {
  43. $this->fetcher = $fetcher;
  44. $this->cache = $cache;
  45. $this->cacheConfig = array_merge([
  46. 'lifetime' => 1500,
  47. 'prefix' => '',
  48. ], (array) $cacheConfig);
  49. }
  50. /**
  51. * Implements FetchAuthTokenInterface#fetchAuthToken.
  52. *
  53. * Checks the cache for a valid auth token and fetches the auth tokens
  54. * from the supplied fetcher.
  55. *
  56. * @param callable $httpHandler callback which delivers psr7 request
  57. *
  58. * @return array the response
  59. *
  60. * @throws \Exception
  61. */
  62. public function fetchAuthToken(callable $httpHandler = null)
  63. {
  64. // Use the cached value if its available.
  65. //
  66. // TODO: correct caching; update the call to setCachedValue to set the expiry
  67. // to the value returned with the auth token.
  68. //
  69. // TODO: correct caching; enable the cache to be cleared.
  70. $cacheKey = $this->fetcher->getCacheKey();
  71. $cached = $this->getCachedValue($cacheKey);
  72. if (!empty($cached)) {
  73. return ['access_token' => $cached];
  74. }
  75. $auth_token = $this->fetcher->fetchAuthToken($httpHandler);
  76. if (isset($auth_token['access_token'])) {
  77. $this->setCachedValue($cacheKey, $auth_token['access_token']);
  78. }
  79. return $auth_token;
  80. }
  81. /**
  82. * @return string
  83. */
  84. public function getCacheKey()
  85. {
  86. return $this->getFullCacheKey($this->fetcher->getCacheKey());
  87. }
  88. /**
  89. * @return array|null
  90. */
  91. public function getLastReceivedToken()
  92. {
  93. return $this->fetcher->getLastReceivedToken();
  94. }
  95. }