Edit File: Auth.inc
<?php namespace B2\Http; use B2\Entities\B2Api; /** * Author: Shlomi * Date: Sep 3, 2017 5:55:22 PM * Founder: Shlomi Bazak (SBZsoft) (c) * File: auth.inc * * Description: * */ // No direct access. defined("__NSS__") or die("Restrected Access."); class Auth { private $_orgAccountId; private $_accountId; private $_applicationKey; private $_client; private $_authToken; private $_apiUrl; private $_downloadUrl; private $_authTime; const AUTH_EXPIRES_AFTER = 85000; // 86400 is the max valid token - we use less than that. public function __construct($accountId, $applicationKey, B2Client $client){ $this->_orgAccountId = $accountId; $this->_applicationKey = $applicationKey; $this->_client = $client; $this->authenticate(); } public function authenticate(){ $this->_authToken = null; $response = $this->_client->_requestJson( B2Client::METHOD_GET, "https://api.backblazeb2.com/b2api/v1/" . B2Api::AUTHORIZE_ACCOUNT, ['headers' => ['Authorization' => "Basic " . base64_encode("{$this->_orgAccountId}:{$this->_applicationKey}")]] ); $this->_accountId = $response['accountId']; $this->_authToken = $response['authorizationToken']; $this->_apiUrl = $response['apiUrl'].'/b2api/v1/'; $this->_downloadUrl = $response['downloadUrl']; $this->_authTime = time(); } /** * * @return string accountId */ public function getAccountId(){ return $this->_accountId; } /** * * @return string applicationKey */ public function getApplicationKey(){ return $this->_applicationKey; } /** * * @return string authToken Null if authentication is unsuccessful */ public function getAuthToken(){ if($this->hasTokenExpired()) $this->authenticate(); return $this->_authToken; } /** * * @return string apiUrl Null if authentication is unsuccessful */ public function getApiUrl(){ return $this->_apiUrl; } /** * * @return string downloadUrl Null if authentication is unsuccessful */ public function getDownloadUrl(){ return $this->_downloadUrl; } /** * Get last successfull authentication timestamp * @return int timestamp of the last authentication */ public function getAuthTime(){ return $this->_authTime; } /** * Check if token is expired * @return bool true iff the token has expired. */ public function hasTokenExpired(){ return (self::AUTH_EXPIRES_AFTER-(time()-$this->getAuthTime()))<=0; } }