Skip to content

Commit

Permalink
✨ self-contained examples
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed May 19, 2024
1 parent 1f3ec4f commit 5edfc91
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
85 changes: 85 additions & 0 deletions examples/example-oauth1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* A full self-contained OAuth1 example
*
* @created 19.05.2024
* @author smiley <[email protected]>
* @copyright 2024 smiley
* @license MIT
*/
declare(strict_types=1);

use chillerlan\OAuth\Core\OAuthInterface;
use chillerlan\OAuth\OAuthOptions;
use chillerlan\OAuth\Providers\Discogs;
use chillerlan\OAuth\Storage\SessionStorage;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;

require_once __DIR__.'/../vendor/autoload.php';

#error_reporting(E_ALL);
#ini_set('display_errors', 1);
ini_set('date.timezone', 'UTC');

// invoke the oauth options instance
$options = new OAuthOptions([
'key' => '[client id]',
'secret' => '[client secret]',
'callbackURL' => '[callback URL]',
'sessionStart' => true,
]);

// the PSR-18 HTTP client
$http = new Client([
'verify' => '/path/to/cacert.pem',
'headers' => [
'User-Agent' => OAuthInterface::USER_AGENT,
],
]);

// the PSR-17 factory/factories
$httpFactory = new HttpFactory;
// the storage instance
$storage = new SessionStorage($options);
// the provider
$provider = new Discogs($options, $http, $httpFactory, $httpFactory, $httpFactory, $storage);

// execute the oauth flow
$name = $provider->getName();

// step 2: redirect to the provider's login screen
if(isset($_GET['login']) && $_GET['login'] === $name){
header('Location: '.$provider->getAuthorizationURL());
}
// step 3: receive the access token
elseif(isset($_GET['oauth_token'], $_GET['oauth_verifier'])){
$token = $provider->getAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']);

// save the token in a permanent storage
// [...]

// access granted, redirect
header('Location: ?granted='.$name);
}
// step 4: verify the token and use the API
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
// use the file storage from now on
// [...]

// dump the AuthenticatedUser instance
printf('<pre>%s</pre>', print_r($provider->me(), true));

// convert the token to JSON and display it
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();

printf('<textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $tokenJSON);
}
// bonus: handle errors
elseif(isset($_GET['error'])){
throw new RuntimeException($_GET['error']);
}
// step 1 (optional): display a login link
else{
echo '<a href="?login='.$name.'">Connect with '.$name.'!</a>';
}
93 changes: 93 additions & 0 deletions examples/example-oauth2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* A full self-contained OAuth2 example
*
* @created 19.05.2024
* @author smiley <[email protected]>
* @copyright 2024 smiley
* @license MIT
*/
declare(strict_types=1);

use chillerlan\OAuth\Core\OAuthInterface;
use chillerlan\OAuth\OAuthOptions;
use chillerlan\OAuth\Providers\GitHub;
use chillerlan\OAuth\Storage\SessionStorage;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;

require_once __DIR__.'/../vendor/autoload.php';

#error_reporting(E_ALL);
#ini_set('display_errors', 1);
ini_set('date.timezone', 'UTC');

// invoke the oauth options instance
$options = new OAuthOptions([
'key' => '[client id]',
'secret' => '[client secret]',
'callbackURL' => '[callback URL]',
'sessionStart' => true,
]);

// the PSR-18 HTTP client
$http = new Client([
'verify' => '/path/to/cacert.pem',
'headers' => [
'User-Agent' => OAuthInterface::USER_AGENT,
],
]);

// the PSR-17 factory/factories
$httpFactory = new HttpFactory;
// the storage instance
$storage = new SessionStorage($options);
// the provider
$provider = new GitHub($options, $http, $httpFactory, $httpFactory, $httpFactory, $storage);

// execute the oauth flow
$name = $provider->getName();

// step 2: redirect to the provider's login screen
if(isset($_GET['login']) && $_GET['login'] === $name){

// a set of scopes for this authorization request
$scopes = [
GitHub::SCOPE_USER,
GitHub::SCOPE_PUBLIC_REPO,
GitHub::SCOPE_GIST,
];

header('Location: '.$provider->getAuthorizationURL(scopes: $scopes));
}
// step 3: receive the access token
elseif(isset($_GET['code'], $_GET['state'])){
$token = $provider->getAccessToken($_GET['code'], $_GET['state']);

// save the token in a permanent storage
// [...]

// access granted, redirect
header('Location: ?granted='.$name);
}
// step 4: verify the token and use the API
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
// use the file storage from now on
// [...]

// dump the AuthenticatedUser instance
printf('<pre>%s</pre>', print_r($provider->me(), true));

// convert the token to JSON and display it
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();

printf('<textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $tokenJSON);
}
// bonus: handle errors
elseif(isset($_GET['error'])){
throw new RuntimeException($_GET['error']);
}
// step 1 (optional): display a login link
else{
echo '<a href="?login='.$name.'">Connect with '.$name.'!</a>';
}

0 comments on commit 5edfc91

Please sign in to comment.