guzzle/oauth-subscriber — pull request #20 by MattKetmo (2014-09-22 09:51:35+00:00)
Title: Implement OAuth2 subscriber

Hello guys,

Last week I needed a Guzzle plugin to handle OAuth2 spec for an API. I saw that the topic has already been started with PR #3, but this was still in development for a few months.

Then I saw @kamermans' [implementation](github.com/kamermans/guzzle4-oauth2-subscriber) which was a bit more advanced. In particular, this one allows more customization about the call made to request the access token, which was very useful in my case where the API I tried to fetch does not respect OAuth2 RFC right.

As I needed *right now* I simply forked it, started [fixing a typo](https://github.com/kamermans/guzzle4-oauth2-subscriber/pull/3), plus made several changes (without taking time to contribute there, until now).

Here is what this PR provides more than #3:

- the way to **retrieve the access token is customizable** by implementing the `ClientCredentials\SignerInterface`. For instance, instead of using the Authorization header, we could use a GET or POST param.
- the way to **inject the access token in each request is is customizable** by implementing the `AccessToken\SignerInterface`. By default it uses the `Authorization: Bearer` header, but in my case I needed to put it in the [query string](https://github.com/MattKetmo/oauth-subscriber/blob/oauth2/src/OAuth2/Signer/AccessToken/QueryString.php).
- the way to **parse the access token response** is customisable by implementing the [TokenFactoryInterface](https://github.com/MattKetmo/oauth-subscriber/blob/oauth2/src/OAuth2/Factory/TokenFactoryInterface.php). (in my case the "expires" field was not properly set, and the default implementation was then fautly)
- Plugin offer a way to **persist the access token** retrieved, so that we don't have to ask for each PHP process a new one. Unlike kamermans' implementation, it uses a proper interface (instead of a callback), and already bring a [file persistence implementation](https://github.com/MattKetmo/oauth-subscriber/blob/oauth2/src/OAuth2/Persistence/FileTokenPersistence.php).
- I uses the `GuzzleHttp\Subscriber\OAuth2 ` namespace, instead of `kamermans\`, in order to bring a more "official" plugin into the `guzzle/oauth-subscriber` repository (note that I used OAuth with a capital A).

TODO:

- [ ] Add some tests
- [ ] Complete the README

Here is a example of usage:

```php
$client = new Client([
'base_url' => 'http://api.example.org/'
'defaults' => ['auth' => 'oauth']
]);

$oauth2Client = new Client([
'base_url' => 'http://auth.example.org/'
]);

// Choose the grant type you want to use
$grantType = new ClientCredentials($oauth2Client, [
'client_id' => '1234567',
'client_secret' => 'uigdbkjhVTVkhj453',
]);

$oauth2Subscriber = new OAuth2Subscriber($grantType);

// Optionnal customisation
$oauth2Subscriber
->setClientCredentialsSigner(new PostFormData('clientId', 'clientSecret'))
->setAccessTokenSigner(new AccessTokenQueryString('access_token'))
->setTokenFactory(new CustomTokenFactory())
->setTokenPersistence(new FileTokenPersistence('/tmp/oauth_token.json'))
;

// Finally attach the listener
$client->getEmitter()->attach($this->oauth2Subscriber);
```

Here I am. Let me know if you like it (or have some criticism). The idea is to bring an "official" OAuth2 plugin for Guzzle4 without waiting to long to be merged (but i'll be ok if you prefer another implementation ;))

Btw, if I miss some authors credit, let me know and I will add it.
Thanks.

/cc @mtdowling, @ddeboer, @kamermans