diff options
author | Morris Jobke <hey@morrisjobke.de> | 2018-03-19 10:10:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-19 10:10:04 +0100 |
commit | 63bc633d89e53c2ff04d3fd521a2a1b18f6737f7 (patch) | |
tree | b28ff0756e3e91e94973a454a16fe8293c5f0c4d /lib/private/Files/ObjectStore | |
parent | 2fab6b2620ba8411777f6b2380a11468f813825e (diff) | |
parent | 41954d29034cef1a6c9bf2f488d6517f32566678 (diff) | |
download | nextcloud-server-63bc633d89e53c2ff04d3fd521a2a1b18f6737f7.tar.gz nextcloud-server-63bc633d89e53c2ff04d3fd521a2a1b18f6737f7.zip |
Merge pull request #8857 from nextcloud/swift-verify-cached-token
verify cached swift token
Diffstat (limited to 'lib/private/Files/ObjectStore')
-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 f22e147445b..6bb01506c4c 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 7bb76782a82..85bba573001 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) { @@ -97,10 +100,7 @@ class SwiftFactory { $cacheKey = $userName . '@' . $this->params['url'] . '/' . $this->params['container']; $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']), @@ -125,7 +125,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); |