diff --git a/composer.json b/composer.json index 5676473..6870a51 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "autoload": { "psr-4": { - "ChrisWhite\\B2\\": "src/" + "ILAB\\B2\\": "src/" } }, "autoload-dev": { diff --git a/src/AuthCacheInterface.php b/src/AuthCacheInterface.php new file mode 100644 index 0000000..1756014 --- /dev/null +++ b/src/AuthCacheInterface.php @@ -0,0 +1,20 @@ +applicationKeyId = $applicationKeyId; $this->applicationKey = $applicationKey; @@ -36,7 +39,7 @@ public function __construct($applicationKeyId, $applicationKey, array $options = $this->client = new HttpClient(['exceptions' => false]); } - $this->authorizeAccount(); + $this->authorizeAccount($authCache); } /** @@ -412,20 +415,36 @@ public function deleteFile(array $options) /** * Authorize the B2 account in order to get an auth token and API/download URLs. * + * @param AuthCacheInterface $authCache * @throws \Exception */ - protected function authorizeAccount() + protected function authorizeAccount($authCache = null) { - $response = $this->client->request('GET', 'https://api.backblazeb2.com/b2api/v2/b2_authorize_account', [ - 'headers' => [ - 'Authorization' => 'Basic ' . base64_encode($this->applicationKeyId . ":" . $this->applicationKey) - ] - ]); + $auth = base64_encode($this->applicationKeyId . ":" . $this->applicationKey); + + $authData = []; + if (!empty($authCache)) { + $authData = $authCache->cachedB2Auth($auth); + } - $this->accountId = $response['accountId']; - $this->authToken = $response['authorizationToken']; - $this->apiUrl = $response['apiUrl'].'/b2api/v2'; - $this->downloadUrl = $response['downloadUrl']; + if (empty($authData)) { + $authData = $this->client->request('GET', 'https://api.backblazeb2.com/b2api/v2/b2_authorize_account', [ + 'headers' => [ + 'Authorization' => 'Basic ' . $auth + ] + ]); + + if (!empty($authData) && !empty($authCache)) { + $authCache->cacheB2Auth($auth, $authData); + } + } + + if (!empty($authData)) { + $this->accountId = $authData['accountId']; + $this->authToken = $authData['authorizationToken']; + $this->apiUrl = $authData['apiUrl'].'/b2api/v2'; + $this->downloadUrl = $authData['downloadUrl']; + } } /**