Skip to content

Commit 5edfc91

Browse files
committed
✨ self-contained examples
1 parent 1f3ec4f commit 5edfc91

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

examples/example-oauth1.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* A full self-contained OAuth1 example
4+
*
5+
* @created 19.05.2024
6+
* @author smiley <[email protected]>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
declare(strict_types=1);
11+
12+
use chillerlan\OAuth\Core\OAuthInterface;
13+
use chillerlan\OAuth\OAuthOptions;
14+
use chillerlan\OAuth\Providers\Discogs;
15+
use chillerlan\OAuth\Storage\SessionStorage;
16+
use GuzzleHttp\Client;
17+
use GuzzleHttp\Psr7\HttpFactory;
18+
19+
require_once __DIR__.'/../vendor/autoload.php';
20+
21+
#error_reporting(E_ALL);
22+
#ini_set('display_errors', 1);
23+
ini_set('date.timezone', 'UTC');
24+
25+
// invoke the oauth options instance
26+
$options = new OAuthOptions([
27+
'key' => '[client id]',
28+
'secret' => '[client secret]',
29+
'callbackURL' => '[callback URL]',
30+
'sessionStart' => true,
31+
]);
32+
33+
// the PSR-18 HTTP client
34+
$http = new Client([
35+
'verify' => '/path/to/cacert.pem',
36+
'headers' => [
37+
'User-Agent' => OAuthInterface::USER_AGENT,
38+
],
39+
]);
40+
41+
// the PSR-17 factory/factories
42+
$httpFactory = new HttpFactory;
43+
// the storage instance
44+
$storage = new SessionStorage($options);
45+
// the provider
46+
$provider = new Discogs($options, $http, $httpFactory, $httpFactory, $httpFactory, $storage);
47+
48+
// execute the oauth flow
49+
$name = $provider->getName();
50+
51+
// step 2: redirect to the provider's login screen
52+
if(isset($_GET['login']) && $_GET['login'] === $name){
53+
header('Location: '.$provider->getAuthorizationURL());
54+
}
55+
// step 3: receive the access token
56+
elseif(isset($_GET['oauth_token'], $_GET['oauth_verifier'])){
57+
$token = $provider->getAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']);
58+
59+
// save the token in a permanent storage
60+
// [...]
61+
62+
// access granted, redirect
63+
header('Location: ?granted='.$name);
64+
}
65+
// step 4: verify the token and use the API
66+
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
67+
// use the file storage from now on
68+
// [...]
69+
70+
// dump the AuthenticatedUser instance
71+
printf('<pre>%s</pre>', print_r($provider->me(), true));
72+
73+
// convert the token to JSON and display it
74+
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();
75+
76+
printf('<textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $tokenJSON);
77+
}
78+
// bonus: handle errors
79+
elseif(isset($_GET['error'])){
80+
throw new RuntimeException($_GET['error']);
81+
}
82+
// step 1 (optional): display a login link
83+
else{
84+
echo '<a href="?login='.$name.'">Connect with '.$name.'!</a>';
85+
}

examples/example-oauth2.php

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
/**
3+
* A full self-contained OAuth2 example
4+
*
5+
* @created 19.05.2024
6+
* @author smiley <[email protected]>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
declare(strict_types=1);
11+
12+
use chillerlan\OAuth\Core\OAuthInterface;
13+
use chillerlan\OAuth\OAuthOptions;
14+
use chillerlan\OAuth\Providers\GitHub;
15+
use chillerlan\OAuth\Storage\SessionStorage;
16+
use GuzzleHttp\Client;
17+
use GuzzleHttp\Psr7\HttpFactory;
18+
19+
require_once __DIR__.'/../vendor/autoload.php';
20+
21+
#error_reporting(E_ALL);
22+
#ini_set('display_errors', 1);
23+
ini_set('date.timezone', 'UTC');
24+
25+
// invoke the oauth options instance
26+
$options = new OAuthOptions([
27+
'key' => '[client id]',
28+
'secret' => '[client secret]',
29+
'callbackURL' => '[callback URL]',
30+
'sessionStart' => true,
31+
]);
32+
33+
// the PSR-18 HTTP client
34+
$http = new Client([
35+
'verify' => '/path/to/cacert.pem',
36+
'headers' => [
37+
'User-Agent' => OAuthInterface::USER_AGENT,
38+
],
39+
]);
40+
41+
// the PSR-17 factory/factories
42+
$httpFactory = new HttpFactory;
43+
// the storage instance
44+
$storage = new SessionStorage($options);
45+
// the provider
46+
$provider = new GitHub($options, $http, $httpFactory, $httpFactory, $httpFactory, $storage);
47+
48+
// execute the oauth flow
49+
$name = $provider->getName();
50+
51+
// step 2: redirect to the provider's login screen
52+
if(isset($_GET['login']) && $_GET['login'] === $name){
53+
54+
// a set of scopes for this authorization request
55+
$scopes = [
56+
GitHub::SCOPE_USER,
57+
GitHub::SCOPE_PUBLIC_REPO,
58+
GitHub::SCOPE_GIST,
59+
];
60+
61+
header('Location: '.$provider->getAuthorizationURL(scopes: $scopes));
62+
}
63+
// step 3: receive the access token
64+
elseif(isset($_GET['code'], $_GET['state'])){
65+
$token = $provider->getAccessToken($_GET['code'], $_GET['state']);
66+
67+
// save the token in a permanent storage
68+
// [...]
69+
70+
// access granted, redirect
71+
header('Location: ?granted='.$name);
72+
}
73+
// step 4: verify the token and use the API
74+
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
75+
// use the file storage from now on
76+
// [...]
77+
78+
// dump the AuthenticatedUser instance
79+
printf('<pre>%s</pre>', print_r($provider->me(), true));
80+
81+
// convert the token to JSON and display it
82+
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();
83+
84+
printf('<textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $tokenJSON);
85+
}
86+
// bonus: handle errors
87+
elseif(isset($_GET['error'])){
88+
throw new RuntimeException($_GET['error']);
89+
}
90+
// step 1 (optional): display a login link
91+
else{
92+
echo '<a href="?login='.$name.'">Connect with '.$name.'!</a>';
93+
}

0 commit comments

Comments
 (0)