Skip to content

Commit

Permalink
:octocat: use FileStorage in get-token examples
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Apr 2, 2024
1 parent 0589105 commit f1ab4b0
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 8 deletions.
21 changes: 14 additions & 7 deletions examples/OAuthExampleProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}

Expand All @@ -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,
Expand All @@ -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{
Expand Down
5 changes: 4 additions & 1 deletion examples/get-token/Imgur.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 4 additions & 0 deletions examples/get-token/LastFM.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 4 additions & 0 deletions examples/get-token/MailChimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 4 additions & 0 deletions examples/get-token/SteamOpenID.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
$token = $provider->getAccessToken($_GET);

// save the token [...]
$factory->getFileStorage()->storeAccessToken($token, $name);

// access granted, redirect
header('Location: ?granted='.$name);
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions examples/get-token/_flow-oauth1.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/**
* @var \chillerlan\OAuth\Core\OAuth1Interface $provider
* @var \OAuthExampleProviderFactory $factory
* @var array|null $PARAMS
*/

Expand All @@ -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();

Expand Down
5 changes: 5 additions & 0 deletions examples/get-token/_flow-oauth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/**
* @var \chillerlan\OAuth\Core\OAuth2Interface $provider
* @var \OAuthExampleProviderFactory $factory
* @var array|null $PARAMS
* @var array|null $SCOPES
*/
Expand All @@ -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();

Expand Down

0 comments on commit f1ab4b0

Please sign in to comment.