You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Tested with Guzzle 4, 5, 6 and PHP 5.4, 5.5, 5.6, 7.0, 7.1 and HHVM
5
+
3
6
This is an OAuth 2.0 client for Guzzle which aims to be 100% compatible with Guzzle 4, 5, 6 and all future versions within a single package.
4
7
Although I love Guzzle, its interfaces keep changing, causing massive breaking changes every 12 months or so, so I have created this package
5
8
to help reduce the dependency hell that most third-party Guzzle dependencies bring with them. I wrote the official Guzzle OAuth 2.0 plugin
6
9
which is still on the `oauth2` branch, [over at the official Guzzle repo](https://github.com/guzzle/oauth-subscriber/tree/oauth2), but I
7
10
see that they have dropped support for Guzzle < v6 on `master`, which prompted me to split this back off to a separate package.
8
11
9
-
## Installing
12
+
## Features
13
+
14
+
- Acquires access tokens via one of the supported grant types (code, client credentials,
15
+
user credentials, refresh token). Or you can set an access token yourself.
16
+
- Supports refresh tokens (stores them and uses them to get new access tokens).
17
+
- Handles token expiration (acquires new tokens and retries failed requests).
18
+
- Allows storage and lookup of access tokens via callbacks
19
+
10
20
11
-
This project can be installed using Composer. Add the following to your
12
-
`composer.json`:
21
+
## Installation
22
+
23
+
This project can be installed using Composer. Run `composer require kamermans/guzzle-oauth-subscriber` or add the following to your `composer.json`:
13
24
14
25
```javascript
15
26
{
@@ -18,3 +29,128 @@ This project can be installed using Composer. Add the following to your
18
29
}
19
30
}
20
31
```
32
+
33
+
## Usage
34
+
35
+
This plugin integrates seamlessly with Guzzle, transparently adding authentication to outgoing requests and optionally attempting re-authorization if the access token is no longer valid.
36
+
37
+
There are multiple grant types available like `PasswordCredentials`, `ClientCredentials` and `AuthorizationCode`.
38
+
39
+
### Guzzle 4 & 5 vs Guzzle 6+
40
+
With the Guzzle 6 release, most of the library was refactored or completely rewritten, and as such, the integration of this library is different.
41
+
42
+
#### Emitters (Guzzle 4 & 5)
43
+
Guzzle 4 & 5 use **Event Subscribers**, and this library incudes `OAuth2Subscriber` for that purpose:
44
+
45
+
```php
46
+
$oauth = new OAuth2Subscriber($grant_type);
47
+
48
+
$client = new Client([
49
+
'auth' => 'oauth',
50
+
]);
51
+
52
+
$client->getEmitter()->attach($oauth);
53
+
```
54
+
55
+
#### Middleware (Guzzle 6+)
56
+
Starting with Guzzle 6, **Middleware** is used to integrate OAuth, and this library includes `OAuthMiddleware` for that purpose:
57
+
58
+
```php
59
+
$oauth = new OAuth2Middleware($grant_type);
60
+
61
+
$stack = HandlerStack::create();
62
+
$stack->push($oauth);
63
+
64
+
$client = new Client([
65
+
'auth' => 'oauth',
66
+
'handler' => $stack,
67
+
]);
68
+
```
69
+
70
+
### Client Credentials Example
71
+
Client credentials are normally used in server-to-server authentication. With this grant type, a client is requesting authorization in its own behalf, so there are only two parties involved. At a minimum, a `client_id` and `client_secret` are required, although many services require a `scope` and other parameters.
72
+
73
+
Here's an example of the client credentials method in Guzzle 4 and Guzzle 5:
74
+
75
+
```php
76
+
use kamermans\OAuth2\GrantType\ClientCredentials;
77
+
use kamermans\OAuth2\OAuth2Subscriber;
78
+
79
+
// Authorization client - this is used to request OAuth access tokens
The following OAuth grant types are supported directly, and you can always create your own by implementing `kamermans\OAuth2\GrantType\GrantTypeInterface`:
139
+
-`AuthorizationCode`
140
+
-`ClientCredentials`
141
+
-`PasswordCredentials`
142
+
-`RefreshToken`
143
+
144
+
Each of these takes a Guzzle client as the first argument. This client is used to obtain or refresh your OAuth access token, out of band from the other requests you are making.
145
+
146
+
147
+
### Access Token Persistence
148
+
> Note: OAuth Access tokens should be stored somewhere securely and/or encrypted. If an attacker gains access to your access token, they could have unrestricted access to whatever resources and scopes were allowed!
149
+
150
+
By default, access tokens are not persisted anywhere. There are some built-in mechanisms for caching / persisting tokens (in `kamermans\OAuth2\Persistence`):
-`FileTokenPersitence` Takes the path to a file in which the access token will be saved.
153
+
-`DoctrineCacheTokenPersistence` Takes a `Doctrine\Common\Cache\Cache` object and optionally a key name (default: `guzzle-oauth2-token`) where the access token will be saved.
154
+
155
+
If you want to use your own persistence layer, you should write your own class that implements `TokenPersistenceInterface`.
0 commit comments