Skip to content

Commit

Permalink
Allow to forceRefresh the authentication
Browse files Browse the repository at this point in the history
Useful when you need you find issues with B2 tomes
  • Loading branch information
robregonm authored Jul 10, 2020
1 parent b7e8824 commit a74d311
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Client
protected $apiUrl = '';
protected $downloadUrl;
protected $recommendedPartSize;

protected $authorizationValues;

protected $client;

Expand Down Expand Up @@ -61,10 +63,12 @@ class Client
public function __construct(string $accountId, array $authorizationValues, array $options = [])
{
$this->accountId = $accountId;
$keyId = $authorizationValues['keyId'] ?? $accountId;
$applicationKey = $authorizationValues['applicationKey'];

if (empty($keyId) or empty($applicationKey)) {
if (!isset($authorizationValues['keyId']) or empty($authorizationValues['keyId'])) {
$authorizationValues['keyId'] = $accountId;
}

if (empty($authorizationValues['keyId']) or empty($authorizationValues['applicationKey'])) {
throw new \Exception('Please provide "keyId" and "applicationKey"');
}

Expand All @@ -77,8 +81,10 @@ public function __construct(string $accountId, array $authorizationValues, array

// initialize cache
$this->createCacheContainer();

$this->authorizationValues = $authorizationValues;

$this->authorizeAccount($keyId, $applicationKey);
$this->authorizeAccount(false);
}

private function createCacheContainer()
Expand Down Expand Up @@ -720,17 +726,22 @@ public function createKey($key)
throw new \Exception(__FUNCTION__ . ' has not been implemented yet');
}


/**
* Authorize the B2 account in order to get an auth token and API/download URLs.
* @param string $keyId
* @param string $applicationKey
* @param bool $forceRefresh
*/
protected function authorizeAccount(string $keyId, string $applicationKey)
public function authorizeAccount(bool $forceRefresh = false)
{
$keyId = $this->authorizationValues['keyId'];
$applicationKey = $this->authorizationValues['applicationKey'];

$baseApiUrl = 'https://api.backblazeb2.com';
$versionPath = '/b2api/v' . $this->version;

if ($forceRefresh === true) {
$this->cache->forget('B2-SDK-Authorization');
}

$response = $this->cache->remember('B2-SDK-Authorization', $this->authorizationCacheTime,
function () use ($keyId, $applicationKey, $baseApiUrl, $versionPath) {
return $this->request('GET', $baseApiUrl . $versionPath . '/b2_authorize_account', [
Expand Down

0 comments on commit a74d311

Please sign in to comment.