Laravel Passport SSO Login with Drupal OAuth Client | Drupal SSO Login

Laravel Passport SSO login with Drupal OAuth 2.0/OpenID connect module gives the ability to enable OAuth 2.0/OIDC Single Sign On to Drupal Site. The OAuth Client module is compatible with all OAuth 2.0/OpenID providers. Here we go through a guide to configure the SSO login between Drupal and Laravel Passport. At the end of this guide, users of Laravel Passport SSO should be able to log in using the Admin Credentials and register to the Drupal site, you can download module click here

If you have any doubts or queries you can contact us at drupalsupport@xecurify.com. We will help you to configure the module.

Step 1: Setup Laravel Passport as OAuth Provider

  • Create a laravel project on your local machine using command and set it up:
  • composer create-project --prefer-dist laravel/laravel blog

  • Change the directory to blog using cd blog command. Install laravel passport
  • composer require laravel/passport

  • Go to config/app.php and add below provider
  • Laravel\Passport\PassportServiceProvider::class

  • Run php artisan migrate command
  • Run php artisan passport:install command
  • Go to app/User.php model class, add HasApiTokens trait to the code:
  • 
         
        namespace App;
        use Laravel\Passport\HasApiTokens;
        use Illuminate\Contracts\Auth\MustVerifyEmail;
        use Illuminate\Foundation\Auth\User as Authenticatable;
        use Illuminate\Notifications\Notifiable;
         
        class User extends Authenticatable
        {
          use HasApiTokens, Notifiable;
         
          
            /**
             * The attributes that are mass assignable.
             *
             * @var array
            */
            protected $fillable = [
                'name', 'email', 'password',
            ];
         
            /**
             * The attributes that should be hidden for arrays.
             *
             * @var array
             */
            protected $hidden = [
                'password', 'remember_token',
            ];
         
            /**
             * The attributes that should be cast to native types.
             *
             * @var array
             */
           
            protected $casts = [
                'email_verified_at' => 'datetime',
            ];
        }
        
        
        
  • Go to app/Providers/AuthServiceProvider.php, add use Laravel\Passport\Passport; , Passport::routes(); routes to the service code is given below:
  •     
        namespace App\Providers;
        use Laravel\Passport\Passport;
        use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
        use Illuminate\Support\Facades\Gate;
         
        class AuthServiceProvider extends ServiceProvider
        {
        
            /**
             * The policy mappings for the application.
             *
             * @var array
             */
            protected $policies = [
                // 'App\Model' => 'App\Policies\ModelPolicy',
            ];
         
            /**
             * Register any authentication / authorization services.
             *
             * @return void
             */
            public function boot()
            {
                $this->registerPolicies();
                Passport::routes();
                //
            }
        }
        
  • Go to config/auth.php change the api driver token to passport as we are going to use Passport library.
  • 
        'guards' => [
                'web' => [
                    'driver' => 'session',
                    'provider' => 'users',
                ],
                'api' => [
                    'driver' => 'passport',
                    'provider' => 'users',
                    'hash' => false,
                ],
            ],
        
  • To get Client ID and Client Secret run following commands: php artisan passport:client It will ask you the following questions:
  • 
         Which user ID should the client be assigned to?:
         > 1
         
         What should we name the client?:
         > Demo OAuth2 Client Account
         
         Where should we redirect the request after authorization?
         [http://localhost/auth/callback]:
         
          > http://localhost/oauth2_client/callback.php
          
        New client created successfully.
        
        Client ID: 1
        Client secret: zMm0tQ9Cp7LbjK3QTgPy1pssoT1X0u7sg0YWUW01
        

  • To create a UserInfo endpoint manually, in the app/Http/Controllers, create a file UserController.php:
  • 
        <?php
        namespace App\Http\Controllers;
        use App\Http\Controllers\Controller;
        use Illuminate\Http\Request;
        use App\Models\User;
        use Auth;
        class UserController extends Controller
        {
            public function get(Request $request)
            {
              $user_id = Auth::id();
              $user = User::find($user_id);
              return $user;
            }
        }
        
  • Also, register the API route by adding the below line in routes/api.php file:
  • 
        //For Laravel below 8 and migrated to the 8 version:
        Route::middleware('auth:api')->get('/user/get', 'UserController@get');
         

    OR

    
        //For Laravel 8 new users:
        use App\Http\Controllers\UserController;                                                                       
         Route::middleware('auth:api')->get('/user/get', 'App\Http\Controllers\UserController@get');
        
  • Now you can connect OAuth client with Laravel Passport (OAuth Provider) and the following endpoints are:
  • Authorization endpoint: http://your-laravel-site-url/oauth/authorize
    Token endpoint: http://your-laravel-site-url/oauth/token
    User info endpoint: http://your-laravel-site-url/api/user/get

You have successfully configured Laravel Passport as OAuth Provider for achieving Laravel Passport login into your Drupal Site.

Step 2: Configure miniOrange Drupal OAuth Client module.

  • Login in your Drupal site’s admin console and click on Extend from the top navigation bar.
  • Select the Install new module option to install a new module on your Drupal site.
  • Drupal OAuth Client module - Install
  • Upload the downloaded zip file of the Module and click on the Install button to continue.
  • Drupal OAuth Client module - Upload
  • Select Enable newly added modules.
  • Drupal OAuth Client module - Update manager
  • Scroll down till you find miniOrange OAuth Client. Click on the checkbox next to it and click on the Install button to enable the module.
  • Drupal OAuth Client module - Enable module
  • Click on Configuration from the top navigation bar and Select Drupal OAuth client Configuration.
  • Drupal OAuth Client - Configuration
  • Click on the Configure OAuth Client tab and select your OAuth Provider from the Select Application dropdown. In case you do not find your OAuth Provider listed in the dropdown, please select Custom OAuth Provider and continue.
  • Drupal OAuth Client - Configure module
  • Enter your OAuth Provider's information in their respective fields and click on the Save button to continue. Also, copy the Callback/Redirect URL and save it on your OAuth Provider.
  • Drupal OAuth Client - Configure OAuth tab
  • Now click on the Test Configuration option. This Test Configuration link will give you the list of the attributes that are coming from your OAuth Provider.
  • Copy the email and the name attributes and save them under the Attribute & Role Mapping tab in the Email Attribute and Name Attribute text field respectively.
  • Please note: This step is mandatory for your login to work. Click on the Save button to save your attribute configurations.


    Drupal OAuth Client Attribute Mapping
  • If your OAuth Provider supports only HTTPS Root URL or Base URL (for eg. Azure, Azure B2C) you can change it under Sign In Settings tab.
  • Drupal OAuth Client Redirect URL
  • Now logout and go to your Drupal site’s login page, you will automatically find a Login withYour OAuth Provider link there. If you want to add your login link to other pages as well, please follow the steps given in the below image:

  • Drupal OAuth Client login link
  • If you want to check out our complete list of features and our various licensing plans, you can go to the Upgrade Plan tab in the module.
  • If you want to purchase any of the paid version of the module, you have to register/login with us in Register/Login tab.
  • Still, if you are facing any difficulty or if you have any questions in mind, you can reach out to us by submitting a query in the Support tab of a module or by sending us a mail at info@xecurify.com.
  • Congratulations, you have successfully configured the miniOrange Drupal OAuth Client module.

Need Help?

Contact us on drupalsupport@xecurify.com for quick guidance (via email/meeting) or call us at +1 978 658 9387.

On your requirement and our team will help you to select the best suitable solution/plan as per your requirement.


Hello there!

Need Help? We are right here!

support
Contact miniOrange Support
success

Thanks for your inquiry.

If you dont hear from us within 24 hours, please feel free to send a follow up email to info@xecurify.com