OAuth2 client module

Note

This part is accessible from the webtools, so if you created your project with the -a option or with the create-project command. The OAuth module is not installed by default. It uses HybridAuth library.

Installation

In the root of your project:

composer require phpmv/ubiquity-oauth

Note

It is also possible to add the ubiquity-oauth dependency using the Composer part of the administration module.

../_images/composer-add-1.png

OAuth configuration

Global configuration

../_images/oauth-part-0.png

Click on the Global configuration button, and modify the callback URL, which corresponds to the local callback url after a successful connection.

../_images/oauth-part-callback.png

OAuth controller

Click on the Create Oauth controller button and assign to the route the value previously given to the callback:

../_images/create-oauth-controller.png

Validate and reset the router cache:

../_images/create-oauth-controller-created.png

Providers

Note

For an OAuth authentication, it is necessary to create an application at the provider beforehand, and to take note of the keys of the application (id and secret).

Click on the Add provider button and select Google:

../_images/provider-config.png

Check the connection by clicking on the Check button:

../_images/google-check.png

Post Login Information:

../_images/google-check-infos.png

OAuthController customization

The controller created is the following:

app/controllers/OAuthTest.php
namespace controllers;
use Hybridauth\Adapter\AdapterInterface;
 /**
 * Controller OAuthTest
 */
class OAuthTest extends \Ubiquity\controllers\auth\AbstractOAuthController{

     public function index(){
     }

     /**
     * @get("oauth/{name}")
     */
     public function _oauth(string $name):void {
             parent::_oauth($name);
     }

     protected function onConnect(string $name,AdapterInterface $provider){
             //TODO
     }
}
  • The _oauth method corresponds to the callback url
  • The onConnect method is triggered on connection and can be overridden.

Example :

  • Possible retrieval of an associated user in the database
  • or creation of a new user
  • Adding the logged-in user and redirection
app/controllers/OAuthTest.php
     protected function onConnect(string $name, AdapterInterface $provider) {
             $userProfile = $provider->getUserProfile();
             $key = md5($name . $userProfile->identifier);
             $user = DAO::getOne(User::class, 'oauth= ?', false, [
                     $key
             ]);
             if (! isset($user)) {
                     $user = new User();
                     $user->setOauth($key);
                     $user->setLogin($userProfile->displayName);
                     DAO::save($user);
             }
             USession::set('activeUser', $user);
             header('location:/');
     }