diff options
author | Robin Appelman <robin@icewind.nl> | 2018-03-16 15:20:16 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2018-03-16 15:20:16 +0100 |
commit | 41954d29034cef1a6c9bf2f488d6517f32566678 (patch) | |
tree | 10c5c9f4a55bd998658637e176195ca6f643316d /lib/private | |
parent | af89db3407e599ea47d62a851759fa929cdd4713 (diff) | |
download | nextcloud-server-41954d29034cef1a6c9bf2f488d6517f32566678.tar.gz nextcloud-server-41954d29034cef1a6c9bf2f488d6517f32566678.zip |
verify cached swift token
Signed-off-by: Robin Appelman <robin@icewind.nl>
Diffstat (limited to 'lib/private')
-rw-r--r-- | lib/private/Files/ObjectStore/Swift.php | 11 | ||||
-rw-r--r-- | lib/private/Files/ObjectStore/SwiftFactory.php | 25 |
2 files changed, 24 insertions, 12 deletions
diff --git a/lib/private/Files/ObjectStore/Swift.php b/lib/private/Files/ObjectStore/Swift.php index 4451fbcc750..18184bc94f4 100644 --- a/lib/private/Files/ObjectStore/Swift.php +++ b/lib/private/Files/ObjectStore/Swift.php @@ -36,16 +36,15 @@ class Swift implements IObjectStore { */ private $params; - /** - * @var \OpenStack\ObjectStore\v1\Models\Container|null - */ - private $container = null; - /** @var SwiftFactory */ private $swiftFactory; public function __construct($params, SwiftFactory $connectionFactory = null) { - $this->swiftFactory = $connectionFactory ?: new SwiftFactory(\OC::$server->getMemCacheFactory()->createDistributed('swift::'), $params); + $this->swiftFactory = $connectionFactory ?: new SwiftFactory( + \OC::$server->getMemCacheFactory()->createDistributed('swift::'), + $params, + \OC::$server->getLogger() + ); $this->params = $params; } diff --git a/lib/private/Files/ObjectStore/SwiftFactory.php b/lib/private/Files/ObjectStore/SwiftFactory.php index 9513cdc3ab8..2a1536b10c5 100644 --- a/lib/private/Files/ObjectStore/SwiftFactory.php +++ b/lib/private/Files/ObjectStore/SwiftFactory.php @@ -30,6 +30,7 @@ use GuzzleHttp\HandlerStack; use OCP\Files\StorageAuthException; use OCP\Files\StorageNotAvailableException; use OCP\ICache; +use OCP\ILogger; use OpenStack\Common\Error\BadResponseError; use OpenStack\Common\Auth\Token; use OpenStack\Identity\v2\Service as IdentityV2Service; @@ -44,10 +45,12 @@ class SwiftFactory { private $params; /** @var Container|null */ private $container = null; + private $logger; - public function __construct(ICache $cache, array $params) { + public function __construct(ICache $cache, array $params, ILogger $logger) { $this->cache = $cache; $this->params = $params; + $this->logger = $logger; } private function getCachedToken(string $cacheKey) { @@ -92,10 +95,7 @@ class SwiftFactory { $cacheKey = $userName . '@' . $this->params['url'] . '/' . $this->params['bucket']; $token = $this->getCachedToken($cacheKey); - $hasToken = is_array($token) && (new \DateTimeImmutable($token['expires_at'])) > (new \DateTimeImmutable('now')); - if ($hasToken) { - $this->params['cachedToken'] = $token; - } + $this->params['cachedToken'] = $token; $httpClient = new Client([ 'base_uri' => TransportUtils::normalizeUrl($this->params['url']), @@ -120,7 +120,20 @@ class SwiftFactory { $this->params['authUrl'] = $this->params['url']; $client = new OpenStack($this->params); - if (!isset($this->params['cachedToken'])) { + $cachedToken = $this->params['cachedToken']; + $hasValidCachedToken = false; + if (is_array($cachedToken)) { + $token = $authService->generateTokenFromCache($cachedToken); + if (is_null($token->catalog)) { + $this->logger->warning('Invalid cached token for swift, no catalog set: ' . json_encode($cachedToken)); + } else if ($token->hasExpired()) { + $this->logger->debug('Cached token for swift expired'); + } else { + $hasValidCachedToken = true; + } + } + + if (!$hasValidCachedToken) { try { $token = $authService->generateToken($this->params); $this->cacheToken($token, $cacheKey); |