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
{{ message }}
This repository was archived by the owner on Mar 23, 2024. It is now read-only.
- a [PSR-18](https://www.php-fig.org/psr/psr-18/) compatible HTTP client library of your choice
30
30
- optional [PSR-17](https://www.php-fig.org/psr/psr-17/) compatible Request-, Response- and UriFactories
31
-
- see [`chillerlan/php-oauth`](https://github.com/chillerlan/php-oauth) for already implemented providers
32
-
33
-
## Getting Started
34
-
In order to instance an [`OAuthInterface`](https://github.com/chillerlan/php-oauth-core/blob/master/src/Core/OAuthInterface.php) you you'll need to invoke a PSR-18 [`ClientInterface`](https://github.com/php-fig/http-client/blob/master/src/ClientInterface.php), a [`OAuthStorageInterface`](https://github.com/chillerlan/php-oauth-core/blob/master/src/Storage/OAuthStorageInterface.php) and `OAuthOptions` (a [`SettingsContainerInterface`](https://github.com/chillerlan/php-settings-container/blob/master/src/SettingsContainerInterface.php)) objects first:
35
-
```php
36
-
use chillerlan\OAuth\Providers\<PROVIDER_NAMESPACE>\<PROVIDER>;
37
-
use chillerlan\OAuth\{OAuthOptions, Storage\SessionStorage};
38
-
use <PSR-18HTTPClient>;
39
-
40
-
// OAuthOptions
41
-
$options = new OAuthOptions([
42
-
// OAuthOptions
43
-
'key' => '<API_KEY>',
44
-
'secret' => '<API_SECRET>',
45
-
'callbackURL' => '<API_CALLBACK_URL>',
46
-
]);
47
-
48
-
// a \Psr\Http\Client\ClientInterface
49
-
$http = new HttpClient;
50
-
51
-
// OAuthStorageInterface
52
-
// a persistent storage is required for authentication!
53
-
$storage = new SessionStorage($options);
54
-
55
-
// an optional \Psr\LoggerInterface logger
56
-
$logger = new Logger;
57
-
58
-
// invoke and use the OAuthInterface
59
-
$provider = new Provider($http, $storage, $options, $logger);
60
-
```
61
-
62
-
## Authentication
63
-
The application flow may differ slightly depending on the provider; there's a working authentication example in the provider repository.
64
-
65
-
### Step 1: optional login link
66
-
Display a login link and provide the user with information what kind of data you're about to access; ask them for permission to save the access token if needed.
67
-
68
-
```php
69
-
echo '<ahref="?login='.$provider->serviceName.'">connect with '.$provider->serviceName.'!</a>';
70
-
```
71
-
72
-
### Step 2: redirect to the provider
73
-
Redirect to the provider's login screen with optional arguments in the authentication URL, like permissions, scopes etc.
and offers basic methods that are common to the OAuth 1/2 interfaces, all supplied in the abstract class [`OAuthProvider`](https://github.com/chillerlan/php-oauth-core/blob/master/src/Core/OAuthProvider.php).
116
53
117
-
## Call the Provider's API
118
-
After successfully receiving the Token, we're ready to make API requests:
119
-
```php
120
-
// import a token to the OAuth token storage if needed
121
-
$storage->storeAccessToken($provider->serviceName, (new AccessToken)->fromJSON($token_json));
122
-
123
-
// make a request
124
-
$response = $provider->request(
125
-
'/some/endpoint',
126
-
['q' => 'param'],
127
-
'POST',
128
-
['data' => 'content'],
129
-
['content-type' => 'whatever']
130
-
);
131
-
132
-
// use the data: $response is a PSR-7 ResponseInterface
[chillerlan/php-httpinterface](https://github.com/chillerlan/php-httpinterface#psr-7-message-helpers) brings some convenience functions to handle a `ResponseInterface` (among other stuff).
64
+
property | description
65
+
-------- | -----------
66
+
`$serviceName` | the classname for the current provider
67
+
`$accessTokenURL` | the provider`s access token URL
68
+
`$authURL` | the provider`s authentication token URL
69
+
`$revokeURL` | an optional URL to revoke an access token (via API)
70
+
`$userRevokeURL` | an optional link to the provider's user control panel where they can revoke the current token
71
+
`$apiDocs` | a link to the provider's API docs
72
+
`$applicationURL` | a link to the API/application credential generation page
73
+
`$endpoints` | an `EndpointMapInterface` for the current provider
138
74
139
-
##Extensions
140
-
In order to use a provider or storage, that is not yet supported, you'll need to implement the respective interfaces:
[OAuth2 is a very straightforward... mess](https://hueniverse.com/oauth-2-0-and-the-road-to-hell-8eec45921529). Please refer to your provider's docs for implementation details.
160
-
```php
161
-
use chillerlan\OAuth\Core\OAuth2Provider;
162
-
163
-
class MyOauth2Provider extends Oauth2Provider implements ClientCredentials, CSRFToken, TokenExpires, TokenRefresh{
There are 2 different `OAuthStorageInterface`, refer to these for implementation details (extend `OAuthStorageAbstract`):
181
-
-[`MemoryStorage`](https://github.com/chillerlan/php-oauth-core/tree/master/src/Storage/MemoryStorage.php): non-persistent, to store an existing token during script runtime and then discard it.
182
-
-[`SessionStorage`](https://github.com/chillerlan/php-oauth-core/tree/master/src/Storage/SessionStorage.php): (half-)persistent, stores a token for as long a user's session is alive, e.g. while authenticating.
The `ClientCredentials` interface indicates that the provider supports the [client credentials grant type](https://tools.ietf.org/html/rfc6749#section-4.4).
The `CSRFToken` interface enables usage of the `<state>` parameter to mitigate [cross-site request forgery](https://tools.ietf.org/html/rfc6749#section-10.12) and automatically enforces it during authorization requests.
The `TokenRefresh` interface indicates if a provider supports usage of [refresh tokens](https://tools.ietf.org/html/rfc6749#section-10.4).
123
+
The option setting `$tokenAutoRefresh` enables automatic refresh of expired tokens when using the `OAuthInterface::request()` or the PSR-18 `OAuthInterface::sendRequest()` methods to call the provider's API.
The `OAuthStorageInterface` serves for storing access tokens and auth states (CSRF) on a per-user basis.
148
+
The included implementations are intended for throwaway use during authentication or script runtime, please refer to these for implementation details (extend `OAuthStorageAbstract`):
149
+
150
+
-[`MemoryStorage`](https://github.com/chillerlan/php-oauth-core/tree/master/src/Storage/MemoryStorage.php): non-persistent, to store an existing token during script runtime and then discard it.
151
+
-[`SessionStorage`](https://github.com/chillerlan/php-oauth-core/tree/master/src/Storage/SessionStorage.php): (half-)persistent, stores a token for as long a user's session is alive, e.g. while authenticating.
152
+
153
+
An example implementation for a persistent database storage with token encryption can be found over here: [`DBStorage`](https://github.com/chillerlan/php-oauth/blob/master/src/Storage/DBStorage.php).
`OAuthOptions` is a [`SettingsContainerInterface`](https://github.com/chillerlan/php-settings-container/blob/master/src/SettingsContainerInterface.php)
206
+
that uses the plug-in traits [`OAuthOptionsTrait`](https://github.com/chillerlan/php-oauth-core/blob/master/src/OAuthOptionsTrait.php)
207
+
and [`HTTPOptionsTrait`](https://github.com/chillerlan/php-httpinterface/blob/master/src/HTTPOptionsTrait.php) to provide settings for a provider.
208
+
209
+
property | type | default | description
210
+
-------- | ---- | ------- | -----------
211
+
`$key` | string | `null` | The application key (or id) given by your provider (see [supported providers](https://github.com/chillerlan/php-oauth-providers#supported-providers))
212
+
`$secret` | string | `null` | The application secret given by your provider
213
+
`$callbackURL` | string | `null` | The callback URL associated with your application
214
+
`$sessionStart` | bool | `true` | Whether or not to start the session when [session storage](https://github.com/chillerlan/php-oauth-core/tree/master/src/Storage/SessionStorage.php) is used
215
+
`$sessionTokenVar` | string | 'chillerlan-oauth-token' | The session array key for token storage
216
+
`$sessionStateVar` | string | 'chillerlan-oauth-state' | The session array key for <state> storage (OAuth2)
217
+
`$tokenAutoRefresh` | bool | `true` | Whether or not to automatically refresh access tokens (OAuth2)
0 commit comments