|
31 | 31 | - PHP 7.2+
|
32 | 32 | - the [Sodium](http://php.net/manual/book.sodium.php) extension for token encryption
|
33 | 33 | - cURL, PHP's stream wrapper or a HTTP client library of your choice
|
| 34 | +- see [`chillerlan/php-oauth`](https://github.com/chillerlan/php-oauth) |
| 35 | + |
| 36 | +## Getting Started |
| 37 | +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 [`HTTPClientInterface`](https://github.com/chillerlan/php-httpinterface/blob/master/src/HTTPClientInterface.php), [`OAuthStorageInterface`](https://github.com/chillerlan/php-oauth-core/blob/master/src/Storage/OAuthStorageInterface.php) and `OAuthOptions` (a [`ContainerInterface`](https://github.com/chillerlan/php-traits/blob/master/src/ContainerInterface.php)) objects first: |
| 38 | +```php |
| 39 | +use chillerlan\OAuth\Providers\<PROVIDER_NAMESPACE>\<PROVIDER>; |
| 40 | +use chillerlan\OAuth\{ |
| 41 | + OAuthOptions, Storage\SessionTokenStorage |
| 42 | +}; |
| 43 | +use chillerlan\HTTP\CurlClient; |
| 44 | + |
| 45 | +// OAuthOptions |
| 46 | +$options = new OAuthOptions([ |
| 47 | + // OAuthOptions |
| 48 | + 'key' => '<API_KEY>', |
| 49 | + 'secret' => '<API_SECRET>', |
| 50 | + 'callbackURL' => '<API_CALLBACK_URL>', |
| 51 | + |
| 52 | + // HTTPOptions |
| 53 | + 'ca_info' => '/path/to/cacert.pem', // https://curl.haxx.se/ca/cacert.pem |
| 54 | + 'userAgent' => 'my-awesome-oauth-app', |
| 55 | +]); |
| 56 | + |
| 57 | +// HTTPClientInterface |
| 58 | +$http = new CurlClient($options); |
| 59 | + |
| 60 | +// OAuthStorageInterface |
| 61 | +// a persistent storage is required for authentication! |
| 62 | +$storage = new SessionTokenStorage($options); |
| 63 | + |
| 64 | +// optional scopes for OAuth2 providers |
| 65 | +$scopes = [ |
| 66 | + Provider::SCOPE_WHATEVER, |
| 67 | +]; |
| 68 | + |
| 69 | +// an optional \Psr\LoggerInterface logger |
| 70 | +$logger = new Logger; |
| 71 | + |
| 72 | +// invoke and use the OAuthInterface |
| 73 | +$provider = new Provider($http, $storage, $options, $logger, $scopes); |
| 74 | +``` |
| 75 | + |
| 76 | +## Authentication |
| 77 | +The application flow may differ slightly depending on the provider; there's a working authentication example in the provider repository. |
| 78 | + |
| 79 | +### Step 1: optional login link |
| 80 | +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. |
| 81 | + |
| 82 | +```php |
| 83 | +echo '<a href="?login='.$provider->serviceName.'">connect with '.$provider->serviceName.'!</a>'; |
| 84 | +``` |
| 85 | + |
| 86 | +### Step 2: redirect to the provider |
| 87 | +Redirect to the provider's login screen with optional arguments in the authentication URL, like permissions etc. |
| 88 | +```php |
| 89 | +if(isset($_GET['login']) && $_GET['login'] === $provider->serviceName){ |
| 90 | + header('Location: '.$provider->getAuthURL(['extra-param' => 'val'])); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Step 3: receive the token |
| 95 | +Receive the access token, save it, do whatever you need to do, then redirect to step 4 |
| 96 | + |
| 97 | +#### OAuth1 |
| 98 | +```php |
| 99 | +if(isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])){ |
| 100 | + $token = $provider->getAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']); |
| 101 | + |
| 102 | + // save & redirect... |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +#### OAuth2 |
| 107 | +usage of the `<state>` parameter depends on the provider |
| 108 | +```php |
| 109 | +if(isset($_GET['code'])){ |
| 110 | + $token = $provider->getAccessToken($_GET['code'], $_GET['state'] ?? null); |
| 111 | + |
| 112 | + // save & redirect... |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### Step 4: auth Granted |
| 117 | +After receiving the access token, go on and verify it then use the API. |
| 118 | +```php |
| 119 | +if(isset($_GET['granted']) && $_GET['granted'] === $provider->serviceName){ |
| 120 | + $response = $provider->doStuff(); |
| 121 | + |
| 122 | + // ... |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## Call the Provider's API |
| 127 | +After successfully receiving the Token, we're ready to make API requests: |
| 128 | +```php |
| 129 | +// import a token to the OAuth token storage if needed |
| 130 | +$storage->storeAccessToken($provider->serviceName, new AccessToken->__fromJSON($token_json)); |
| 131 | + |
| 132 | +// make a request |
| 133 | +$response = $provider->request('/some/endpoint', ['q' => 'param'], 'POST', ['data' => 'content'], ['content-type' => 'whatever']); |
| 134 | + |
| 135 | +// use the data |
| 136 | +$headers = $response->headers; |
| 137 | +$data = $response->json; |
| 138 | +``` |
0 commit comments