From 1727107117584858fd0e40a68cf6bcb2734a2d4f Mon Sep 17 00:00:00 2001 From: Cyrille Giquello Date: Tue, 14 May 2024 11:13:51 +0200 Subject: [PATCH] WIP #20 add scopes to OAuthRequestAccess --- examples-web/OAuthRequestAccess.php | 132 +++++++++++++++++++++------- 1 file changed, 100 insertions(+), 32 deletions(-) diff --git a/examples-web/OAuthRequestAccess.php b/examples-web/OAuthRequestAccess.php index 7b18b4a..51c0970 100644 --- a/examples-web/OAuthRequestAccess.php +++ b/examples-web/OAuthRequestAccess.php @@ -1,14 +1,27 @@ null , 'accessToken' => null, 'refreshToken' => null, + 'scopes' => ['read_prefs','read_gpx'] , ]; -// Update data +// Retrieve data from cookie if( isset($_COOKIE[constant('COOKIE')])) { $data = unserialize($_COOKIE[constant('COOKIE')]); } +// Some tools, like reseting stuff + +if( isset($_REQUEST['action']) ) +{ + switch( $_REQUEST['action'] ) + { + case 'clear_auth': + $data['accessCode'] = null ; + $data['oauth2state'] = null ; + $data['accessToken'] = null ; + $data['refreshToken'] = null ; + $data['scopes'] = ['read_prefs','read_gpx']; + break ; + } +} + +// Update data from request + $data['app_id'] = isset($_REQUEST['app_id']) ? $_REQUEST['app_id'] : $data['app_id']; $data['app_secret'] = isset($_REQUEST['app_secret']) ? $_REQUEST['app_secret'] : $data['app_secret']; $data['accessCode'] = isset($_REQUEST['accessCode']) ? $_REQUEST['accessCode'] : $data['accessCode']; +$data['scopes'] = isset($_REQUEST['scopes']) ? $_REQUEST['scopes'] : $data['scopes']; // Processing OAuth @@ -45,7 +78,7 @@ if( isset($data['app_id'])) { - $osmProvider = new \JBelien\OAuth2\Client\Provider\OpenStreetMap([ + $osmProvider = new OpenStreetMap([ 'clientId' => $data['app_id'], 'clientSecret' => $data['app_secret'], 'redirectUri' => $data['app_redirect'], @@ -59,7 +92,7 @@ //$data['accessToken'] = $osmProvider->getAccessToken('client_credentials'); // Options are optional, defaults to 'read_prefs' only - $options = ['scope' => 'read_prefs read_gpx']; + $options = ['scope' => implode(' ',$data['scopes']) ]; $data['authorizationUrl'] = $osmProvider->getAuthorizationUrl($options); $data['oauth2state'] = $osmProvider->getState(); } @@ -70,28 +103,28 @@ $accessToken = $osmProvider->getAccessToken( 'authorization_code', ['code' => $data['accessCode'] ] ); - $data['accessToken'] = $accessToken->getToken(); - $data['refreshToken'] = $accessToken->getRefreshToken(); + //$data['accessToken'] = $accessToken->getToken(); + //$data['refreshToken'] = $accessToken->getRefreshToken(); + $data['accessToken'] = serialize($accessToken); } } -setcookie(constant('COOKIE'), serialize($data), time() + 3600); - -print_r($data); +// Save data back in cookie +setcookie(constant('COOKIE'), serialize($data), time() + 3600); ?> - OSM OAuth request access + OSM OAuth2 access request -

OSM OAuth request access

+

OSM OAuth2 access request

    -
  1. The first step is to create an application. - - /oauth2/applications - . -
  2. -
  3. Then fill them here: + +
  4. +

    Application

    +

    At first we have to create an application ; +
    + Because OAuth2 need a redirect and we do not want to instanciate a https endpoint, you must set the "redirect url" to "urn:ietf:wg:oauth:2.0:oob" ; +
    + Create the app here: + /oauth2/applications ; +

    +

    + And copy the "client id" and "client secret" to paste them below :

    - +
    - -
    + +
    + Select scope(s): +
      +
    • /> read_prefs: Lire les préférences de l’utilisateur
    • +
    • /> write_prefs: Modifier les préférences de l’utilisateur
    • +
    • /> write_diary: Créer des entrées de journal, des commentaires et se faire des amis
    • +
    • /> write_api: Modifier la carte
    • +
    • /> read_gpx: Lire les traces GPX privées
    • +
    • /> write_gpx: Mettre à jour les traces GPX
    • +
    • /> write_notes: Modifier les notes
    • +
    • /> write_redactions: Caviarder les données cartographiques
    • +
    • /> openid: Se connecter à OpenStreetMap
    • +
    +
    +
    -

  5. - Visit url - and copy here the code : +

    Authorization

    +

    Then we have to ask a "code" to obtain an "access token" ; +
    + If asked scope(s) does not match with application's scopes, a error message will be displayed (in french: Le scope demandé n'est pas valide, est inconnu, ou est mal formé) ; +
    + Then visit the authorization url: ; +

    + And copy the displayed code to paste it below :

    +
    + + +
  6. +
  7. - - - +

    Access

    + +

    The access token : getToken(); ?>

    getResourceOwner($data['accessToken']); + echo '
    ', print_r($accessToken,true) ,'
    '; + ?> +

    Resource owner

    + getResourceOwner($accessToken); echo '
    ', print_r($resourceOwner,true) ,'
    '; - } ?>
  8. + +