From f1ab4b0c5ec7b17e1d992a9c3c57f1f354fce5bb Mon Sep 17 00:00:00 2001 From: smiley Date: Tue, 2 Apr 2024 17:06:59 +0200 Subject: [PATCH] :octocat: use FileStorage in get-token examples --- examples/OAuthExampleProviderFactory.php | 21 ++++++++++++++------- examples/get-token/Imgur.php | 5 ++++- examples/get-token/LastFM.php | 4 ++++ examples/get-token/MailChimp.php | 4 ++++ examples/get-token/SteamOpenID.php | 4 ++++ examples/get-token/_flow-oauth1.php | 5 +++++ examples/get-token/_flow-oauth2.php | 5 +++++ 7 files changed, 40 insertions(+), 8 deletions(-) diff --git a/examples/OAuthExampleProviderFactory.php b/examples/OAuthExampleProviderFactory.php index 16dc2e9..78733ac 100644 --- a/examples/OAuthExampleProviderFactory.php +++ b/examples/OAuthExampleProviderFactory.php @@ -38,6 +38,7 @@ class OAuthExampleProviderFactory{ protected DotEnv $dotEnv; protected LoggerInterface $logger; protected OAuthOptions|SettingsContainerInterface $options; + protected OAuthStorageInterface $fileStorage; public function __construct( protected OAuthProviderFactory $factory, @@ -47,8 +48,10 @@ public function __construct( ){ ini_set('date.timezone', 'UTC'); - $this->dotEnv = (new DotEnv($this->cfgDir, $envFile, false))->load(); - $this->logger = $this->initLogger($logLevel); + $this->dotEnv = (new DotEnv($this->cfgDir, $envFile, false))->load(); + $this->logger = $this->initLogger($logLevel); + $this->fileStorage = $this->initFileStorage(); + $this->factory->setLogger($this->logger); } @@ -67,6 +70,14 @@ protected function initLogger(string|null $logLevel):LoggerInterface{ return $logger; } + protected function initFileStorage():OAuthStorageInterface{ + $options = new OAuthOptions; + + $options->fileStoragePath = $this->cfgDir.'/.filestorage'; + + return new FileStorage('oauth-example', $options, $this->logger); + } + public function getProvider( string $providerFQN, string $envVar, @@ -90,11 +101,7 @@ public function getProvider( } public function getFileStorage():OAuthStorageInterface{ - $options = new OAuthOptions; - - $options->fileStoragePath = $this->cfgDir.'/.filestorage'; - - return new FileStorage('oauth-example', $options, $this->logger); + return $this->fileStorage; } public function getEnvVar(string $var):mixed{ diff --git a/examples/get-token/Imgur.php b/examples/get-token/Imgur.php index e40eff1..2ed417d 100644 --- a/examples/get-token/Imgur.php +++ b/examples/get-token/Imgur.php @@ -40,13 +40,16 @@ // so we set the expiry to a sane period to allow auto-refreshing $token->expires = (time() + 2592000); // 30 days // save the token [...] - $storage->storeAccessToken($token); + $factory->getFileStorage()->storeAccessToken($token, $name); // 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 + $provider->setStorage($factory->getFileStorage()); + $me = print_r($provider->me(), true); $tokenJSON = $provider->getAccessTokenFromStorage()->toJSON(); diff --git a/examples/get-token/LastFM.php b/examples/get-token/LastFM.php index fcc0b70..e722c07 100644 --- a/examples/get-token/LastFM.php +++ b/examples/get-token/LastFM.php @@ -32,12 +32,16 @@ $token = $provider->getAccessToken($_GET['token']); // save the token [...] + $factory->getFileStorage()->storeAccessToken($token, $name); // 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 + $provider->setStorage($factory->getFileStorage()); + $me = print_r($provider->me(), true); $tokenJSON = $provider->getAccessTokenFromStorage()->toJSON(); diff --git a/examples/get-token/MailChimp.php b/examples/get-token/MailChimp.php index 9f241f3..cebca56 100644 --- a/examples/get-token/MailChimp.php +++ b/examples/get-token/MailChimp.php @@ -38,12 +38,16 @@ $token = $provider->getTokenMetadata($token); // save the token [...] + $factory->getFileStorage()->storeAccessToken($token, $name); // 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 + $provider->setStorage($factory->getFileStorage()); + $me = print_r($provider->me(), true); $tokenJSON = $provider->getAccessTokenFromStorage()->toJSON(); diff --git a/examples/get-token/SteamOpenID.php b/examples/get-token/SteamOpenID.php index 732cf99..3cd043f 100644 --- a/examples/get-token/SteamOpenID.php +++ b/examples/get-token/SteamOpenID.php @@ -33,6 +33,7 @@ $token = $provider->getAccessToken($_GET); // save the token [...] + $factory->getFileStorage()->storeAccessToken($token, $name); // access granted, redirect header('Location: ?granted='.$name); @@ -43,6 +44,9 @@ } // step 4: verify the token and use the API elseif(isset($_GET['granted']) && $_GET['granted'] === $name){ + // use the file storage from now on + $provider->setStorage($factory->getFileStorage()); + $token = $provider->getAccessTokenFromStorage(); // the user's steamid is stored as access token $response = $provider->request('/ISteamUser/GetPlayerSummaries/v2', ['steamids' => $token->accessToken]); $data = print_r(MessageUtil::decodeJSON($response), true); diff --git a/examples/get-token/_flow-oauth1.php b/examples/get-token/_flow-oauth1.php index 964a861..5dde231 100644 --- a/examples/get-token/_flow-oauth1.php +++ b/examples/get-token/_flow-oauth1.php @@ -11,6 +11,7 @@ /** * @var \chillerlan\OAuth\Core\OAuth1Interface $provider + * @var \OAuthExampleProviderFactory $factory * @var array|null $PARAMS */ @@ -25,12 +26,16 @@ $token = $provider->getAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']); // save the token [...] + $factory->getFileStorage()->storeAccessToken($token, $name); // 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 + $provider->setStorage($factory->getFileStorage()); + $me = print_r($provider->me(), true); $tokenJSON = $provider->getAccessTokenFromStorage()->toJSON(); diff --git a/examples/get-token/_flow-oauth2.php b/examples/get-token/_flow-oauth2.php index 805a947..4437bce 100644 --- a/examples/get-token/_flow-oauth2.php +++ b/examples/get-token/_flow-oauth2.php @@ -13,6 +13,7 @@ /** * @var \chillerlan\OAuth\Core\OAuth2Interface $provider + * @var \OAuthExampleProviderFactory $factory * @var array|null $PARAMS * @var array|null $SCOPES */ @@ -34,12 +35,16 @@ $token = $provider->getAccessToken($_GET['code'], $state); // save the token [...] + $factory->getFileStorage()->storeAccessToken($token, $name); // 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 + $provider->setStorage($factory->getFileStorage()); + $me = print_r($provider->me(), true); $tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();