diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 75f532bd..514d1f87 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -29,7 +29,28 @@ class Client * @var string */ static private $api_key; - + + /** + * Store Hash to connect to (oAuth) + * + * @var string + */ + static private $store_hash; + + /** + * oAuth Client ID to connect to the store API with + * + * @var string + */ + static private $client_id; + + /** + * oAuth Access Token + * + * @var string + */ + static private $oauth_token; + /** * Connection instance * @@ -50,6 +71,27 @@ class Client * @var string */ static private $path_prefix = '/api/v2'; + + /** + * oAuth API path prefix to be added to store URL for requests + * + * @var string + */ + static private $oauth_path_prefix = 'https://api.bigcommerce.com/stores/'; + + /** + * oAuth API Version to use (current at time is v2) + * + * @var string + */ + static private $api_version = '/v2'; + + /** + * Connect via oAuth. Default is basic authentication. + * + * @param bool + */ + static private $use_oauth = false; /** * Full URL path to the configured store API. @@ -60,36 +102,64 @@ class Client /** * Configure the API client with the required credentials. + * Default is Basic Authentication. * * Requires a settings array to be passed in with the following keys: - * + * Basic Authentication: * - store_url * - username * - api_key * + * oAuth Authentication: + * - store_hash + * - client_id + * - oauth_token + * * @param array $settings + * @param bool $oauth * @throws \Exception */ - public static function configure(array $settings) - { - if (!isset($settings['store_url'])) { - throw new Exception("'store_url' must be provided"); - } - - if (!isset($settings['username'])) { - throw new Exception("'username' must be provided"); - } - - if (!isset($settings['api_key'])) { - throw new Exception("'api_key' must be provided"); - } - - self::$username = $settings['username']; - self::$api_key = $settings['api_key']; - self::$store_url = rtrim($settings['store_url'], '/'); - self::$api_path = self::$store_url . self::$path_prefix; - self::$connection = false; - } + public static function configure(array $settings, $use_oauth = false) + { + if ($use_oauth) { + if (!isset($settings['store_hash'])) { + throw new Exception("'store_hash' must be provided for oAuth Authentication"); + } + + if (!isset($settings['client_id'])) { + throw new Exception("'client_id' must be provided for oAuth Authentication"); + } + + if (!isset($settings['oauth_token'])) { + throw new Exception("'oauth_token' must be provided for oAuth Authentication"); + } + + self::$store_hash = $settings['store_hash']; + self::$client_id = $settings['client_id']; + self::$oauth_token = $settings['oauth_token']; + self::$api_path = self::$oauth_path_prefix .self::$store_hash .self::$api_version; + self::$connection = false; + self::$use_oauth = true; + } else { + if (!isset($settings['store_url'])) { + throw new Exception("'store_url' must be provided"); + } + + if (!isset($settings['username'])) { + throw new Exception("'username' must be provided"); + } + + if (!isset($settings['api_key'])) { + throw new Exception("'api_key' must be provided"); + } + + self::$username = $settings['username']; + self::$api_key = $settings['api_key']; + self::$store_url = rtrim($settings['store_url'], '/'); + self::$api_path = self::$store_url . self::$path_prefix; + self::$connection = false; + } + } /** * Configure the API client to throw exceptions when HTTP errors occur. @@ -157,9 +227,15 @@ public static function getLastError() private static function connection() { if (!self::$connection) { - self::$connection = new Connection(); - self::$connection->authenticate(self::$username, self::$api_key); - } + if (self::$use_oauth) { + self::$connection = new Connection(); + self::$connection->authenticateOauth(self::$client_id, self::$oauth_token); + + } else { + self::$connection = new Connection(); + self::$connection->authenticateBasic(self::$username, self::$api_key); + } + } return self::$connection; } diff --git a/src/Bigcommerce/Api/Connection.php b/src/Bigcommerce/Api/Connection.php index a9daa336..c3abe008 100644 --- a/src/Bigcommerce/Api/Connection.php +++ b/src/Bigcommerce/Api/Connection.php @@ -40,7 +40,7 @@ class Connection private $failOnError = false; /** - * Manually follow location redirects. Used if CURLOPT_FOLLOWLOCATION + * Manually follow location redirects. Used if FOLLOWLOCATION * is unavailable due to open_basedir restriction. * @var boolean */ @@ -69,6 +69,20 @@ class Connection * as XML. Defaults to false (using JSON). */ private $useXml = false; + + /** + * oAuth Client ID + * + * @var string + */ + private $client_id; + + /** + * oAuth Access Token + * + * @var string + */ + private $oauth_token; /** * Initializes the connection object. @@ -118,11 +132,20 @@ public function failOnError($option = true) /** * Sets the HTTP basic authentication. */ - public function authenticate($username, $password) + public function authenticateBasic($username, $password) { curl_setopt($this->curl, CURLOPT_USERPWD, "$username:$password"); } - + + /** + * Sets the HTTP oAuth autentication. + */ + public function authenticateOauth($client_id, $oauth_token) + { + $this->client_id = $client_id; + $this->oauth_token = $oauth_token; + } + /** * Set a default timeout for the request. The client will error if the * request takes longer than this to respond. @@ -183,6 +206,11 @@ private function initializeRequest() $this->responseHeaders = array(); $this->lastError = false; $this->addHeader('Accept', $this->getContentType()); + + if (isset($this->client_id) && isset($this->oauth_token)) { + $this->addHeader('X-Auth-Client', $this->client_id); + $this->addHeader('X-Auth-Token', $this->oauth_token); + } curl_setopt($this->curl, CURLOPT_POST, false); curl_setopt($this->curl, CURLOPT_PUT, false); @@ -299,7 +327,7 @@ public function get($url, $query = false) */ public function post($url, $body) { - $this->addHeader('Content-Type', $this->getContentType()); + //$this->addHeader('Content-Type', $this->getContentType()); if (!is_string($body)) { $body = json_encode($body); @@ -346,7 +374,7 @@ public function head($url) */ public function put($url, $body) { - $this->addHeader('Content-Type', $this->getContentType()); + //$this->addHeader('Content-Type', $this->getContentType()); if (!is_string($body)) { $body = json_encode($body); diff --git a/src/Bigcommerce/Api/Resource.php b/src/Bigcommerce/Api/Resource.php index 92481f3e..02faa754 100644 --- a/src/Bigcommerce/Api/Resource.php +++ b/src/Bigcommerce/Api/Resource.php @@ -8,7 +8,7 @@ class Resource * * @var stdclass */ - protected $fields; + public $fields; /** * @var int