Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to guzzle 6 #29

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
24 changes: 23 additions & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
build_failure_conditions:
- 'issues.label("coding-style").new.exists'
- 'issues.severity(>= MAJOR).new.exists'

tools:
external_code_coverage: true
external_code_coverage: false
php_code_sniffer:
config: { standard: 'PSR2' }

build:
environment:
php:
version: 5.5.12
ini:
display_errors: true
date.timezone: Europe/London
memory_limit: 2G
tests:
override:
-
command: 'bin/phpunit --coverage-clover=coverage.clover'
coverage:
file: 'coverage.clover'
format: 'php-clover'
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ sudo: false
php:
- 5.6
- 5.5
- 5.4
- 7.0
- hhvm

Expand Down
45 changes: 17 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
guzzle-oauth2-plugin
====================

Provides an OAuth2 plugin (subscriber) for [Guzzle](http://guzzlephp.org/).
Provides an OAuth2 plugin (middleware) for [Guzzle](http://guzzlephp.org/).

[![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)

Version 2.x (on the `master` branch) is intended for Guzzle 5:
```json
"commerceguys/guzzle-oauth2-plugin": "~2.0"
```

Guzzle 3 compatibility continues in the [`1.0`](https://github.com/commerceguys/guzzle-oauth2-plugin/tree/1.0) branch:
```json
"commerceguys/guzzle-oauth2-plugin": "~1.0"
```
This version is intended for Guzzle 6:

## Features

Expand All @@ -32,36 +24,33 @@ First make sure you have all the dependencies in place by running `composer inst
use GuzzleHttp\Client;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use GuzzleHttp\HandlerStack;

use CommerceGuys\Guzzle\Oauth2\GrantType\RefreshToken;
use CommerceGuys\Guzzle\Oauth2\GrantType\PasswordCredentials;
use CommerceGuys\Guzzle\Oauth2\Oauth2Subscriber;
use CommerceGuys\Guzzle\Oauth2\OAuthMiddleware;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use CommerceGuys\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::USERNAME => '[email protected]',
PasswordCredentials::PASSWORD => 'test password',
PasswordCredentials::CLIENT_ID => 'test-client',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

```
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"description": "An OAuth2 plugin (subscriber) for Guzzle",
"license": "MIT",
"require": {
"guzzlehttp/guzzle": "~5.0",
"firebase/php-jwt": "~2.0"
"guzzlehttp/guzzle": "~6.0",
"firebase/php-jwt": "~3.0"
},
"autoload": {
"psr-4": {
Expand Down
Loading