Skip to content

Commit

Permalink
:octocat: optimize/clean-up get-token examples
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Mar 25, 2024
1 parent ddf4eac commit 272c636
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 81 deletions.
2 changes: 1 addition & 1 deletion examples/get-token/Foursquare.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
/** @var \OAuthExampleProviderFactory $factory */
$provider = $factory->getProvider(Foursquare::class, $ENVVAR);

require_once __DIR__.'/_flow-oauth2-no-state.php';
require_once __DIR__.'/_flow-oauth2.php';

exit;
11 changes: 6 additions & 5 deletions examples/get-token/Imgur.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
$username = $token->extraParams['account_username'];
$id = $token->extraParams['account_id'];

// set the expiry to a sane period
// imgur sends the token with an expiry of 10 years,
// 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);
Expand All @@ -46,10 +47,10 @@
}
// step 4: verify the token and use the API
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
echo '<pre>'.print_r($provider->me(), true).'</pre>'.
'<textarea cols="120" rows="3" onclick="this.select();">'.
$storage->getAccessToken($name)->toJSON().
'</textarea>';
$me = print_r($provider->me(), true);
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();

printf('<pre>%s</pre><textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $me, $tokenJSON);
}
// step 1 (optional): display a login link
else{
Expand Down
8 changes: 4 additions & 4 deletions examples/get-token/LastFM.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
}
// step 4: verify the token and use the API
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
echo '<pre>'.print_r($provider->me(), true).'</pre>'.
'<textarea cols="120" rows="3" onclick="this.select();">'.
$provider->getAccessTokenFromStorage()->toJSON().
'</textarea>';
$me = print_r($provider->me(), true);
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();

printf('<pre>%s</pre><textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $me, $tokenJSON);
}
// step 1 (optional): display a login link
else{
Expand Down
8 changes: 4 additions & 4 deletions examples/get-token/MailChimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
}
// step 4: verify the token and use the API
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
echo '<pre>'.print_r($provider->me(), true).'</pre>'.
'<textarea cols="120" rows="3" onclick="this.select();">'.
$provider->getAccessTokenFromStorage()->toJSON().
'</textarea>';
$me = print_r($provider->me(), true);
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();

printf('<pre>%s</pre><textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $me, $tokenJSON);
}
// step 1 (optional): display a login link
else{
Expand Down
2 changes: 1 addition & 1 deletion examples/get-token/Mixcloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
/** @var \OAuthExampleProviderFactory $factory */
$provider = $factory->getProvider(Mixcloud::class, $ENVVAR);

require_once __DIR__.'/_flow-oauth2-no-state.php';
require_once __DIR__.'/_flow-oauth2.php';

exit;
10 changes: 5 additions & 5 deletions examples/get-token/MusicBrainz.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
}
// step 4: verify the token and use the API
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
$response = $provider->request(sprintf('/artist/%s', '573510d6-bb5d-4d07-b0aa-ea6afe39e28d'), ['inc' => 'url-rels work-rels']);
$path = sprintf('/artist/%s', '573510d6-bb5d-4d07-b0aa-ea6afe39e28d');
$response = $provider->request($path, ['inc' => 'url-rels work-rels']);
$data = print_r(MessageUtil::decodeJSON($response), true);
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();

echo '<pre>'.print_r(MessageUtil::decodeJSON($response), true).'</pre>'.
'<textarea cols="120" rows="3" onclick="this.select();">'.
$provider->getAccessTokenFromStorage()->toJSON().
'</textarea>';
printf('<pre>%s</pre><textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $data, $tokenJSON);
}
// step 1 (optional): display a login link
else{
Expand Down
2 changes: 1 addition & 1 deletion examples/get-token/SoundCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
/** @var \OAuthExampleProviderFactory $factory */
$provider = $factory->getProvider(SoundCloud::class, $ENVVAR);

require_once __DIR__.'/_flow-oauth2-no-state.php';
require_once __DIR__.'/_flow-oauth2.php';

exit;
17 changes: 10 additions & 7 deletions examples/get-token/SteamOpenID.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

require_once __DIR__.'/../provider-example-common.php';

/** @var \OAuthExampleProviderFactory $factory */
/**
* @var \OAuthExampleProviderFactory $factory
* @var \chillerlan\OAuth\Providers\SteamOpenID $provider
*/
$provider = $factory->getProvider(SteamOpenID::class, $ENVVAR);
$name = $provider->serviceName;

Expand All @@ -26,6 +29,7 @@
}
// step 3: receive the access token
elseif(isset($_GET['openid_sig']) && isset($_GET['openid_signed'])){
// the SteamOpenID provider takes the whole $_GET array as it uses multiple of the query parameters
$token = $provider->getAccessToken($_GET);

// save the token [...]
Expand All @@ -39,13 +43,12 @@
}
// step 4: verify the token and use the API
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
$token = $provider->getAccessTokenFromStorage(); // the user's steamid is stored as access token
$response = $provider->request('/ISteamUser/GetPlayerSummaries/v2', ['steamids' => $token->accessToken]);
$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);
$tokenJSON = $token->toJSON();

echo '<pre>'.print_r(MessageUtil::decodeJSON($response), true).'</pre>'.
'<textarea cols="120" rows="3" onclick="this.select();">'
.$token->toJSON().
'</textarea>';
printf('<pre>%s</pre><textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $data, $tokenJSON);
}
// step 1 (optional): display a login link
else{
Expand Down
8 changes: 4 additions & 4 deletions examples/get-token/_flow-oauth1.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
}
// step 4: verify the token and use the API
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
echo '<pre>'.print_r($provider->me(), true).'</pre>'.
'<textarea cols="120" rows="3" onclick="this.select();">'.
$provider->getAccessTokenFromStorage()->toJSON().
'</textarea>';
$me = print_r($provider->me(), true);
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();

printf('<pre>%s</pre><textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $me, $tokenJSON);
}
// step 1 (optional): display a login link
else{
Expand Down
43 changes: 0 additions & 43 deletions examples/get-token/_flow-oauth2-no-state.php

This file was deleted.

20 changes: 14 additions & 6 deletions examples/get-token/_flow-oauth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*/
declare(strict_types=1);

use chillerlan\OAuth\Core\CSRFToken;

/**
* @var \chillerlan\OAuth\Core\OAuth2Interface $provider
* @var array|null $PARAMS
Expand All @@ -22,8 +24,14 @@
header('Location: '.$provider->getAuthURL($PARAMS, $SCOPES));
}
// step 3: receive the access token
elseif(isset($_GET['code']) && isset($_GET['state'])){
$token = $provider->getAccessToken($_GET['code'], $_GET['state']);
elseif(isset($_GET['code'])){
$state = null;

if($provider instanceof CSRFToken && isset($_GET['state'])){
$state = $_GET['state'];
}

$token = $provider->getAccessToken($_GET['code'], $state);

// save the token [...]

Expand All @@ -32,10 +40,10 @@
}
// step 4: verify the token and use the API
elseif(isset($_GET['granted']) && $_GET['granted'] === $name){
echo '<pre>'.print_r($provider->me(), true).'</pre>'.
'<textarea cols="120" rows="3" onclick="this.select();">'.
$provider->getAccessTokenFromStorage()->toJSON().
'</textarea>';
$me = print_r($provider->me(), true);
$tokenJSON = $provider->getAccessTokenFromStorage()->toJSON();

printf('<pre>%s</pre><textarea cols="120" rows="5" onclick="this.select();">%s</textarea>', $me, $tokenJSON);
}
// step 1 (optional): display a login link
else{
Expand Down

0 comments on commit 272c636

Please sign in to comment.