Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 83c2ecd

Browse files
committed
1 parent 8abac5c commit 83c2ecd

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,108 @@
3131
- PHP 7.2+
3232
- the [Sodium](http://php.net/manual/book.sodium.php) extension for token encryption
3333
- 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+
```

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"homepage": "https://github.com/chillerlan/php-oauth-core",
55
"license": "MIT",
66
"type": "library",
7-
"minimum-stability": "dev",
7+
"minimum-stability": "stable",
88
"keywords": [
99
"oauth", "oauth1", "oauth2", "client", "core"
1010
],
@@ -22,8 +22,8 @@
2222
"require": {
2323
"php": "^7.2",
2424
"chillerlan/php-traits": "^1.1",
25-
"chillerlan/php-httpinterface": "dev-master",
26-
"chillerlan/php-magic-apiclient": "dev-master",
25+
"chillerlan/php-httpinterface": "^1.1",
26+
"chillerlan/php-magic-apiclient": "^1.0",
2727
"psr/log": "^1.0",
2828
"psr/simple-cache": "^1.0"
2929
},

0 commit comments

Comments
 (0)