-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from klever/feature/add_bearer_auth
Feature/add bearer auth
- Loading branch information
Showing
10 changed files
with
273 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ A PHP SDK for communicating with the JustGiving API. Based on the [original SDK] | |
|
||
## Quick start | ||
```php | ||
$guzzleClient = GuzzleClientFactory::build('https://api.justgiving.com/', 'abcde1f2g', 1, '[email protected]', 'myPassword'); | ||
$auth = new AppAuth('abcde123'); | ||
$guzzleClient = (new GuzzleClientFactory($auth))->createClient(); | ||
$client = new JustGivingClient($guzzleClient); | ||
``` | ||
|
||
|
@@ -38,18 +39,22 @@ See the [JustGiving API documentation](https://api.justgiving.com/docs) for more | |
The `JustGivingClient` should be instantiated with a suitable HTTP client passed in as a parameter. This client must adhere to the [PSR-7 interfaces](http://www.php-fig.org/psr/psr-7/), and should return from requests an instance of `JustGivingApiSdk\Support\Response`. | ||
This class implements the PSR-7 `ResponseInterface` and adds support for dealing with JSON responses, as well as making it easy to access response data. | ||
|
||
There is a helper class `GuzzleClientFactory`, that will create a correctly configured [Guzzle](http://docs.guzzlephp.org/en/latest/) client ready to be passed in. The usage of this is: | ||
There is a helper class `GuzzleClientFactory`, that will create a correctly configured [Guzzle](http://docs.guzzlephp.org/en/latest/) client ready to be passed in. | ||
The factory takes an authorisation object to set the auth headers on the HTTP client. The available classes are: | ||
- `AppAuth($appId)` for unprotected endpoints | ||
- `BasicAuth($appId, $username, $password)` for protected endpoints, where you have the username and password | ||
- `BearerAuth($appId, $oAuthSecret, $token)` for protected endpoints, where you have a bearer token (from oAuth) | ||
|
||
The API base URL and version are set automatically, but may be overridden by passing an associative array with keys `root_domain` and `api_version` as the $options argument. | ||
Any options apart from this will be passed to Guzzle as custom options (e.g. custom headers or turning on debug mode). | ||
|
||
```php | ||
$guzzleClient = GuzzleClientFactory::build($rootDomain, $apiKey, $apiVersion, $username = '', $password = '', $options = []); | ||
``` | ||
For example: | ||
```php | ||
$guzzleClient = GuzzleClientFactory::build('https://api.justgiving.com/', 'abcde1f2g', 1, '[email protected]', 'myPassword', ['debug' => true]); | ||
$auth = new BasicAuth('abced123', '[email protected]', 'pass123'); | ||
$guzzleClient = (new GuzzleClientFactory($auth, ['api_version' => 2])->buildClient(); | ||
|
||
$client = new JustGivingClient($guzzleClient); | ||
``` | ||
The `options` array is optional, and is merged with the existing configuration to allow for customisation of the Guzzle client configuration (e.g. turning on debug mode as above). | ||
|
||
### Querying the API | ||
The SDK defines a separate client class for each resource as define by the [API documentation](https://api.justgiving.com/docs), and each of those classes contain methods that correspond to API actions. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Konsulting\JustGivingApiSdk\Exceptions; | ||
|
||
class InvalidPropertyException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Konsulting\JustGivingApiSdk\Support\Auth; | ||
|
||
class AppAuth implements AuthValue | ||
{ | ||
/** | ||
* The application ID. | ||
* | ||
* @see https://developer.justgiving.com/apidocs/documentation#AppId | ||
* @var string | ||
*/ | ||
protected $appId; | ||
|
||
public function __construct($appId) | ||
{ | ||
$this->appId = $appId; | ||
} | ||
|
||
/** | ||
* Get the authentication headers. | ||
* | ||
* @return array | ||
*/ | ||
public function getHeaders() | ||
{ | ||
return ['x-api-key' => $this->appId]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Konsulting\JustGivingApiSdk\Support\Auth; | ||
|
||
interface AuthValue | ||
{ | ||
/** | ||
* Get the authentication headers. | ||
* | ||
* @return array | ||
*/ | ||
public function getHeaders(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Konsulting\JustGivingApiSdk\Support\Auth; | ||
|
||
class BasicAuth implements AuthValue | ||
{ | ||
/** | ||
* The application ID (also known as API key). | ||
* | ||
* @see https://developer.justgiving.com/apidocs/documentation#AppId | ||
* @var string | ||
*/ | ||
protected $appId; | ||
|
||
/** | ||
* The username of the JustGiving user being authenticated. | ||
* | ||
* @var string | ||
*/ | ||
protected $username; | ||
|
||
/** | ||
* The password of the JustGiving user being authenticated. | ||
* | ||
* @var string | ||
*/ | ||
protected $password; | ||
|
||
public function __construct($appId, $username, $password) | ||
{ | ||
$this->username = $username; | ||
$this->password = $password; | ||
$this->appId = $appId; | ||
} | ||
|
||
/** | ||
* Get the authentication headers. | ||
* | ||
* @return array | ||
*/ | ||
public function getHeaders() | ||
{ | ||
return [ | ||
'Authorization' => 'Basic ' . base64_encode($this->username . ":" . $this->password), | ||
'x-api-key' => $this->appId, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Konsulting\JustGivingApiSdk\Support\Auth; | ||
|
||
class BearerAuth implements AuthValue | ||
{ | ||
/** | ||
* The application ID (also known as API key). | ||
* | ||
* @see https://developer.justgiving.com/apidocs/documentation#AppId | ||
* @var string | ||
*/ | ||
protected $appId; | ||
|
||
/** | ||
* The bearer token obtained via oAuth. | ||
* | ||
* @see https://justgivingdeveloper.zendesk.com/hc/en-us/articles/207071499-Getting-a-bearer-token | ||
* @var string | ||
*/ | ||
protected $token; | ||
|
||
/** | ||
* The oAuth secret provided by JustGiving (this currently has to be requested manually). | ||
* | ||
* @see https://justgivingdeveloper.zendesk.com/hc/en-us/articles/115002238925-How-do-I-get-a-secret-key- | ||
* @var string | ||
*/ | ||
protected $oAuthSecret; | ||
|
||
public function __construct($appId, $oAuthSecret, $token) | ||
{ | ||
$this->appId = $appId; | ||
$this->token = $token; | ||
$this->oAuthSecret = $oAuthSecret; | ||
} | ||
|
||
/** | ||
* Get the authentication headers. | ||
* | ||
* @return array | ||
*/ | ||
public function getHeaders() | ||
{ | ||
return [ | ||
'Authorization' => 'Bearer ' . $this->token, | ||
'x-api-key' => $this->appId, | ||
'x-application-key' => $this->oAuthSecret, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.