Skip to content

Commit

Permalink
Added auth cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jawngee committed Dec 10, 2018
1 parent 05bca31 commit 72fe842
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"autoload": {
"psr-4": {
"ChrisWhite\\B2\\": "src/"
"ILAB\\B2\\": "src/"
}
},
"autoload-dev": {
Expand Down
20 changes: 20 additions & 0 deletions src/AuthCacheInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ILAB\B2;

interface AuthCacheInterface {
/**
* Returns the auth data for the given key
*
* @param $key
* @return array|null
*/
public function cachedB2Auth($key);

/**
* Caches authentication data
* @param $key
* @param $authData
*/
public function cacheB2Auth($key, $authData);
}
47 changes: 33 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ class Client
/**
* Client constructor. Accepts the account ID, application key and an optional array of options.
*
* @param $accountId
* @param $applicationKey
* @param string $applicationKeyId
* @param string $applicationKey
* @param array $options
* @param AuthCacheInterface $authCache
*
* @throws \Exception
*/
public function __construct($applicationKeyId, $applicationKey, array $options = [])
public function __construct($applicationKeyId, $applicationKey, array $options = [], $authCache = null)
{
$this->applicationKeyId = $applicationKeyId;
$this->applicationKey = $applicationKey;
Expand All @@ -36,7 +39,7 @@ public function __construct($applicationKeyId, $applicationKey, array $options =
$this->client = new HttpClient(['exceptions' => false]);
}

$this->authorizeAccount();
$this->authorizeAccount($authCache);
}

/**
Expand Down Expand Up @@ -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'];
}
}

/**
Expand Down

0 comments on commit 72fe842

Please sign in to comment.