Skip to content

Commit

Permalink
WIP #20 console command for access token
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrille37 committed May 14, 2024
1 parent ee84f97 commit 453bdd2
Show file tree
Hide file tree
Showing 6 changed files with 513 additions and 327 deletions.
2 changes: 1 addition & 1 deletion examples-console/math-gravityCenter-relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Cyrille37\OSM\Yapafo\Objects\Relation ;

$osmApi = new OSM_Api([
'url' => OSM_Api::URL_PROD_UK,
'url' => OSM_Api::URL_PROD_UK.OSM_Api::URL_PATH_API,
// Because we change the Url, have to erase potential Access Token in Config
'access_token' => null,
//'log'=>['level'=>LogLevel::NOTICE]
Expand Down
2 changes: 1 addition & 1 deletion examples-console/math-gravityCenter-way.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Cyrille37\OSM\Yapafo\Objects\Way ;

$osmApi = new OSM_Api([
'url' => OSM_Api::URL_PROD_UK,
'url' => OSM_Api::URL_PROD_UK.OSM_Api::URL_PATH_API,
// Because we change the Url, have to erase potential Access Token in Config
'access_token' => null,
//'log'=>['level'=>LogLevel::NOTICE]
Expand Down
2 changes: 1 addition & 1 deletion examples-console/tool-osmXmlToCSV-02.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Psr\Log\LogLevel;

$osmapi = new OSM_Api([
'url' => OSM_Api::URL_PROD_UK,
'url' => OSM_Api::URL_PROD_UK.OSM_Api::URL_PATH_API,
// Because we change the Url, have to erase potential Access Token in Config
'access_token' => null,
'log'=>['level'=>LogLevel::WARNING]
Expand Down
7 changes: 4 additions & 3 deletions examples-web/OAuthRequestAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use JBelien\OAuth2\Client\Provider\OpenStreetMap ;
use League\OAuth2\Client\Token\AccessToken ;
use Cyrille37\OSM\Yapafo\OSM_Api;

// To store data locally
define('COOKIE','osmoauth2');
Expand All @@ -34,7 +35,7 @@
'app_id' => null ,
'app_secret' => null,
//'app_redirect' => 'https://example.com',
'app_redirect' => 'urn:ietf:wg:oauth:2.0:oob',
'app_redirect' => OSM_Api::OAUTH2_NO_REDIRECT_URL,
'authorizationUrl' => null ,
'accessCode' => null,
'oauth2state' => null ,
Expand Down Expand Up @@ -167,8 +168,8 @@
<li><input type="checkbox" name="scopes[]" value="write_prefs" <?php echo in_array('write_prefs',$data['scopes']) ? 'checked' : '' ?> /> write_prefs: Modifier les préférences de l’utilisateur</li>
<li><input type="checkbox" name="scopes[]" value="write_diary" <?php echo in_array('write_diary',$data['scopes']) ? 'checked' : '' ?> /> write_diary: Créer des entrées de journal, des commentaires et se faire des amis</li>
<li><input type="checkbox" name="scopes[]" value="write_api" <?php echo in_array('write_api',$data['scopes']) ? 'checked' : '' ?> /> write_api: Modifier la carte</li>
<li><input type="checkbox" name="scopes[]" value="read_gpx" <?php echo in_array('read_gpx',$data['scopes']) ? 'checked' : '' ?> /> read_gpx: Lire les traces GPX privées</li>
<li><input type="checkbox" name="scopes[]" value="write_gpx" <?php echo in_array('write_gpx',$data['scopes']) ? 'checked' : '' ?> /> write_gpx: Mettre à jour les traces GPX</li>
<li><input type="checkbox" name="scopes[]" value="read_gpx" <?php echo in_array('read_gpx',$data['scopes']) ? 'checked' : '' ?> /> read_gpx: Lire les traces GPS privées</li>
<li><input type="checkbox" name="scopes[]" value="write_gpx" <?php echo in_array('write_gpx',$data['scopes']) ? 'checked' : '' ?> /> write_gpx: Mettre à jour les traces GPS</li>
<li><input type="checkbox" name="scopes[]" value="write_notes" <?php echo in_array('write_notes',$data['scopes']) ? 'checked' : '' ?> /> write_notes: Modifier les notes</li>
<li><input type="checkbox" name="scopes[]" value="write_redactions" <?php echo in_array('write_redactions',$data['scopes']) ? 'checked' : '' ?> /> write_redactions: Caviarder les données cartographiques</li>
<li><input type="checkbox" name="scopes[]" value="openid" <?php echo in_array('openid',$data['scopes']) ? 'checked' : '' ?> /> openid: Se connecter à OpenStreetMap</li>
Expand Down
215 changes: 215 additions & 0 deletions oauth/oauth-console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#!/usr/bin/env php
<?php
/**
*
* Credits & Thanks to:
* - Grant Horwood for styling the tty output: https://dev.to/gbhorwood/writing-command-line-scripts-in-php-part-5-styling-output-text-1bcp
*/
error_reporting(-1);

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

use Cyrille37\OSM\Yapafo\OSM_Api;
use JBelien\OAuth2\Client\Provider\OpenStreetMap as OAuth2OsmProvider;
use League\OAuth2\Client\Token\AccessToken;

define('EOL', "\n");
define('TAB', "\t");

/**
* Escape character
*/
define('ESC', "\033");

/**
* ANSI colours
*/
define('ANSI_BLACK', ESC . "[30m");
define('ANSI_RED', ESC . "[31m");
define('ANSI_GREEN', ESC . "[32m");
define('ANSI_YELLOW', ESC . "[33m");
define('ANSI_BLUE', ESC . "[34m");
define('ANSI_MAGENTA', ESC . "[35m");
define('ANSI_CYAN', ESC . "[36m");
define('ANSI_WHITE', ESC . "[37m");

/**
* ANSI background colours
*/
define('ANSI_BACKGROUND_BLACK', ESC . "[40m");
define('ANSI_BACKGROUND_RED', ESC . "[41m");
define('ANSI_BACKGROUND_GREEN', ESC . "[42m");
define('ANSI_BACKGROUND_YELLOW', ESC . "[43m");
define('ANSI_BACKGROUND_BLUE', ESC . "[44m");
define('ANSI_BACKGROUND_MAGENTA', ESC . "[45m");
define('ANSI_BACKGROUND_CYAN', ESC . "[46m");
define('ANSI_BACKGROUND_WHITE', ESC . "[47m");

/**
* ANSI styles
*/
define('ANSI_BOLD', ESC . "[1m");
define('ANSI_ITALIC', ESC . "[3m"); // limited support.
define('ANSI_UNDERLINE', ESC . "[4m");
define('ANSI_STRIKETHROUGH', ESC . "[9m");

/**
* Clear all ANSI styling
*/
define('ANSI_CLOSE', ESC . "[0m");

$console = new OAuthConsole();
$console->hello();
if (!$console->select_osm_instance()) {
echo 'Abandon.', EOL;
exit;
}
if (!$console->select_app_scopes()) {
echo 'Abandon.', EOL;
exit;
}
if (!$console->get_app_creds()) {
echo 'Abandon.', EOL;
exit;
}
if (!$console->get_auth_code()) {
echo 'Abandon.', EOL;
exit;
}
$console->get_access_token();


class OAuthConsole
{
protected $base_url;
protected $osmOAuth2Provider;
protected $scopes;
protected $app_id;
protected $app_secret;
protected $authorizationUrl;
protected $oauth2state;
protected $accessCode;

public function hello()
{
echo EOL, TAB, ANSI_BOLD, '*** OSM Access Token creation ***', ANSI_CLOSE, EOL, EOL;
echo 'We’re going to create an ', ANSI_BOLD, 'Access Token', ANSI_CLOSE, ' to give you a special user access on the ', ANSI_BOLD, 'OpenStreetMap', ANSI_CLOSE, ' database.', EOL;
echo 'Use ', ANSI_BOLD, 'Ctrl+C', ANSI_CLOSE, ' will stop this command.', EOL;
}

public function select_osm_instance()
{
echo EOL, 'What OSM instance do you need ?', EOL;
echo TAB, ANSI_BOLD, '1. OSM Production', ANSI_CLOSE, ' (', OSM_Api::URL_PROD_UK, EOL;
echo TAB, ANSI_BOLD, '2. OSM Developpement', ANSI_CLOSE, ' (', OSM_Api::URL_DEV_UK, EOL;

$done = false;
while (true) {
$choice = readline('Your choice: ');
switch (trim($choice)) {
case '1':
$this->base_url = OSM_Api::URL_PROD_UK;
$done = true;
break 2;
case '2':
$this->base_url = OSM_Api::URL_DEV_UK;
$done = true;
break 2;
}
}
return $done;
}

public function select_app_scopes()
{
echo EOL, 'What permissions (scopes) do you need ? ', EOL;
echo 'If the application already exists, be sure to select at least less or same permissions as the application`s scopes.', EOL;
foreach (OSM_Api::SCOPES4HUMANS as $c => $scope) {
echo TAB, ANSI_BOLD, $c, '. ', $scope['value'], ANSI_CLOSE, ' - ', $scope['desc'], ';', EOL;
}
echo TAB, '0. when done to close the list.', EOL;

$scopes = ['read_prefs', 'write_prefs'];

while (true) {
echo ANSI_BOLD, 'Actual list', ANSI_CLOSE, ': ', implode(', ', $scopes), EOL;
$choice = trim(readline('Your choice: '));
if ($choice == '0') {
if (empty($scopes)) {
echo 'Abort, empty permissions set', EOL;
return false;
}
$this->scopes = $scopes;
return true;
} else if (isset(OSM_Api::SCOPES4HUMANS[$choice])) {
$v = OSM_Api::SCOPES4HUMANS[$choice]['value'];
$k = array_search($v, $scopes);
if ($k !== false)
unset($scopes[$k]);
else
$scopes[] = $v;
}
}
return false;
}

public function get_app_creds()
{
echo EOL, 'We need application credentials ', ANSI_BOLD, 'app_id', ANSI_CLOSE, ' and ', ANSI_BOLD, 'app_secret', ANSI_CLOSE, '.', EOL;
echo 'If you don\'t have them, create a new application at url ', $this->base_url, '/oauth2/applications', EOL;
echo 'and be sure to select at least the permissions you have previously chosen.', EOL;

// readline does not manage ANSI codes

$app_id = trim(readline('Application ID: '));
if (empty($app_id))
return false;
$this->app_id = $app_id;

$app_secret = trim(readline('Application Secret: '));
if (empty($app_secret))
return false;
$this->app_secret = $app_secret;

$this->osmOAuth2Provider = new OAuth2OsmProvider([
'clientId' => $this->app_id,
'clientSecret' => $this->app_secret,
'redirectUri' => OSM_Api::OAUTH2_NO_REDIRECT_URL,
'osm_base_url' => $this->base_url,
]);

return true;
}

public function get_auth_code()
{
$options = ['scope' => implode(' ', $this->scopes)];
$this->authorizationUrl = $this->osmOAuth2Provider->getAuthorizationUrl($options);
$this->oauth2state = $this->osmOAuth2Provider->getState();

echo EOL, 'Now you have to visit url ', $this->authorizationUrl, ' to agree permissions and get the ', ANSI_BOLD, 'Code', ANSI_CLOSE, ' mandatory to request an Access Token.', EOL;
$accessCode = trim(readline('The Code: '));
if (empty($accessCode))
return false;

$this->accessCode = $accessCode;

return true;
}

public function get_access_token()
{
/** @var \League\OAuth2\Client\Token\AccessToken $accessToken */
$accessToken = $this->osmOAuth2Provider->getAccessToken(
'authorization_code',
['code' => $this->accessCode]
);
$resourceOwner = $this->osmOAuth2Provider->getResourceOwner($accessToken);

echo EOL, 'Here is the Access Token: ', ANSI_BOLD, $accessToken->getToken(), ANSI_CLOSE, "\n";
echo 'which authorize access as user ', ANSI_BOLD, $resourceOwner->getDisplayName(), ANSI_CLOSE, "\n";
echo 'with permission to ', implode(', ', $this->scopes), EOL;

return true;
}
}
Loading

0 comments on commit 453bdd2

Please sign in to comment.