forked from commerceguys/guzzle-oauth2-plugin
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
197 additions
and
188 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 |
---|---|---|
|
@@ -4,8 +4,6 @@ sudo: false | |
|
||
php: | ||
- 5.6 | ||
- 5.5 | ||
- 5.4 | ||
- 7.0 | ||
- hhvm | ||
|
||
|
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 |
---|---|---|
@@ -1,19 +1,26 @@ | ||
guzzle-oauth2-plugin | ||
==================== | ||
![logo](http://www.sainsburys.co.uk/homepage/images/sainsburys.png) | ||
|
||
Provides an OAuth2 plugin (subscriber) for [Guzzle](http://guzzlephp.org/). | ||
Sainsbury's guzzle-oauth2-plugin | ||
================================ | ||
|
||
[![Build Status](https://travis-ci.org/commerceguys/guzzle-oauth2-plugin.svg)](https://travis-ci.org/commerceguys/guzzle-oauth2-plugin) | ||
[![Code Coverage](https://scrutinizer-ci.com/g/commerceguys/guzzle-oauth2-plugin/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/commerceguys/guzzle-oauth2-plugin/?branch=master) | ||
Provides an OAuth2 plugin (middleware) for [Guzzle](http://guzzlephp.org/). | ||
|
||
Version 2.x (on the `master` branch) is intended for Guzzle 5: | ||
[![Build Status](https://travis-ci.org/Sainsburys/guzzle-oauth2-plugin.svg?branch=release%2F2.0)](https://travis-ci.org/Sainsburys/guzzle-oauth2-plugin) | ||
[![Code Coverage](https://scrutinizer-ci.com/g/Sainsburys/guzzle-oauth2-plugin/badges/coverage.png?b=release%2F2.0)](https://scrutinizer-ci.com/g/Sainsburys/guzzle-oauth2-plugin/?branch=release%2F2.0) | ||
|
||
Version 3.x (on the `master` branch) is intended for Guzzle 6: | ||
```json | ||
"commerceguys/guzzle-oauth2-plugin": "~2.0" | ||
"sainsburys/guzzle-oauth2-plugin": "~3.0" | ||
``` | ||
|
||
Guzzle 3 compatibility continues in the [`1.0`](https://github.com/commerceguys/guzzle-oauth2-plugin/tree/1.0) branch: | ||
Version 2.x (on the `release/2.0` branch) is intended for Guzzle 5: | ||
```json | ||
"commerceguys/guzzle-oauth2-plugin": "~1.0" | ||
"sainsburys/guzzle-oauth2-plugin": "~2.0" | ||
``` | ||
|
||
Version 1.x (on the `release/1.0` branch) is intended for Guzzle 3 [Unmaintained]: | ||
```json | ||
"sainsburys/guzzle-oauth2-plugin": "~1.0" | ||
``` | ||
|
||
## Features | ||
|
@@ -29,39 +36,36 @@ First make sure you have all the dependencies in place by running `composer inst | |
|
||
## Example | ||
```php | ||
use GuzzleHttp\Client; | ||
use CommerceGuys\Guzzle\Oauth2\GrantType\RefreshToken; | ||
use CommerceGuys\Guzzle\Oauth2\GrantType\PasswordCredentials; | ||
use CommerceGuys\Guzzle\Oauth2\Oauth2Subscriber; | ||
use GuzzleHttp\HandlerStack; | ||
use Sainsburys\Guzzle\Oauth2\GrantType\RefreshToken; | ||
use Sainsburys\Guzzle\Oauth2\GrantType\PasswordCredentials; | ||
use Sainsburys\Guzzle\Oauth2\Middleware\OAuthMiddleware; | ||
|
||
$base_url = 'https://example.com'; | ||
$base_uri = 'https://example.com'; | ||
|
||
$oauth2Client = new Client(['base_url' => $base_url]); | ||
$handlerStack = HandlerStack::create(); | ||
$client = new Client(['handler'=> $handlerStack, 'base_uri' => $base_uri, 'auth' => 'oauth2']); | ||
|
||
$config = [ | ||
'username' => '[email protected]', | ||
'password' => 'test password', | ||
'client_id' => 'test-client', | ||
PasswordCredentials::CONFIG_USERNAME => '[email protected]', | ||
PasswordCredentials::CONFIG_PASSWORD => 'test password', | ||
PasswordCredentials::CONFIG_CLIENT_ID => 'test-client', | ||
PasswordCredentials::CONFIG_TOKEN_URL => '/oauth/token', | ||
'scope' => 'administration', | ||
]; | ||
|
||
$token = new PasswordCredentials($oauth2Client, $config); | ||
$refreshToken = new RefreshToken($oauth2Client, $config); | ||
|
||
$oauth2 = new Oauth2Subscriber($token, $refreshToken); | ||
$token = new PasswordCredentials($client, $config); | ||
$refreshToken = new RefreshToken($client, $config); | ||
$middleware = new OAuthMiddleware($client, $token, $refreshToken); | ||
|
||
$client = new Client([ | ||
'defaults' => [ | ||
'auth' => 'oauth2', | ||
'subscribers' => [$oauth2], | ||
], | ||
]); | ||
$handlerStack->push($middleware->onBefore()); | ||
$handlerStack->push($middleware->onFailure(5)); | ||
|
||
$response = $client->get('https://example.com/api/user/me'); | ||
$response = $client->get('/api/user/me'); | ||
|
||
print_r($response->json()); | ||
|
||
// Use $oauth2->getAccessToken(); and $oauth2->getRefreshToken() to get tokens | ||
// Use $middleware->getAccessToken(); and $middleware->getRefreshToken() to get tokens | ||
// that can be persisted for subsequent requests. | ||
|
||
``` |
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.