From 5edfc91939ba6f54a4b51353b9f4f8749599d4cc Mon Sep 17 00:00:00 2001 From: smiley Date: Sun, 19 May 2024 22:13:46 +0200 Subject: [PATCH] :sparkles: self-contained examples --- examples/example-oauth1.php | 85 +++++++++++++++++++++++++++++++++ examples/example-oauth2.php | 93 +++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 examples/example-oauth1.php create mode 100644 examples/example-oauth2.php diff --git a/examples/example-oauth1.php b/examples/example-oauth1.php new file mode 100644 index 0000000..3d8d49a --- /dev/null +++ b/examples/example-oauth1.php @@ -0,0 +1,85 @@ + + * @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('
%s
', print_r($provider->me(), true)); + + // convert the token to JSON and display it + $tokenJSON = $provider->getAccessTokenFromStorage()->toJSON(); + + printf('', $tokenJSON); +} +// bonus: handle errors +elseif(isset($_GET['error'])){ + throw new RuntimeException($_GET['error']); +} +// step 1 (optional): display a login link +else{ + echo 'Connect with '.$name.'!'; +} diff --git a/examples/example-oauth2.php b/examples/example-oauth2.php new file mode 100644 index 0000000..e5650d1 --- /dev/null +++ b/examples/example-oauth2.php @@ -0,0 +1,93 @@ + + * @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('
%s
', print_r($provider->me(), true)); + + // convert the token to JSON and display it + $tokenJSON = $provider->getAccessTokenFromStorage()->toJSON(); + + printf('', $tokenJSON); +} +// bonus: handle errors +elseif(isset($_GET['error'])){ + throw new RuntimeException($_GET['error']); +} +// step 1 (optional): display a login link +else{ + echo 'Connect with '.$name.'!'; +}