Setup Laravel Single Sign-on with Drupal OAuth Client
Overview
Drupal Laravel SSO integration will allow you to configure Single Sign-On ( SSO ) login between your Drupal site and Laravel using OAuth/OpenID protocol. Drupal OAuth 2.0/OpenID connect module gives the ability to enable login using OAuth 2.0/OIDC Single Sign-On to Drupal Site. We provide the Drupal OAuth/OpenID Client module for Drupal 7, Drupal 8, Drupal 9 and Drupal 10, Drupal 11.
Installation Steps
- Using Composer
- Using Drush
- Manual Installation
Configuration Steps
Setup Drupal as OAuth Client
- After installing the module, navigate to the Configuration -> miniOrange OAuth Client Configuration -> Configure OAuth tab and select Custom OAuth 2.0 Provider from the Select Application dropdown list.
- Copy the Callback/Redirect URL and keep it handy.
- Enter the application name in the Display Name text field. For example, Laravel.
Note: If your provider only supports HTTPS Callback/Redirect URLs and you have an HTTP site, please make sure to enable the 'Enforce HTTPS Callback URL' checkbox at the bottom of the tab.
Create Application in Laravel
- 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 below commands
php artisan migrate
php artisan passport:install - 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:
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 and change the API driver token to the passport as we are going to
use the Passport library.
'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'passport', 'provider' => 'users', 'hash' => false, ], ],
Integrating Drupal with Laravel
- Navigate to the Laravel portal and run the following commands to get Client ID and
Client Secret:
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? > Paste the Callback/Redirect URL New client created successfully. Client ID: 1 Client secret: zMm0tQ9Cp7LbjK3QTgPy1pssoT1X0u7sg0YWUW01
- Copy the Client ID and Client secret value.
- Go to the Drupal's Configure OAuth tab and paste the copied Client ID and Client secret into the respective text-fields.
- 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; } }
- 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');
- Copy and paste the following scope and endpoints in Drupal's Configure OAuth tab.
Scope openid email profile Authorize Endpoint /oauth/authorize Access Token Endpoint /oauth/token Get User Info Endpoint /api/user/get
- Click on the Save Configuration button.
Test Configuration of Drupal with Laravel
- Click on the Perform Test Configuration button to check the Single Sign-On (SSO) connection between Drupal and Laravel.
- On a Test Connection popup, if you don't have an active session in Laravel on the same browser, you will be asked to sign in to your Laravel account. After successfully logging into a Laravel account, you will be provided with a list of attributes that are received from the Laravel Passport.
- Select the Email Attribute from the dropdown menu in which the user's email is obtained and click the Done button.
Note: Mapping the Email Attribute is mandatory for your login to work.
How to perform the SSO
- Open a new browser/private window and navigate to the Drupal site login page.
- Click on the Login using Laravel link to initiate the SSO from Drupal.
- If you want to add the SSO link on other pages, please follow the steps given in the image below:
Need Assistance?
If you face any issues during the configuration or if you want some additional features, please contact us at drupalsupport@xecurify.com.
Additional Features:
Troubleshooting:
More FAQs ➔Getting error: 'Username not received. Check your Attribute Mapping configuration.' OR Getting Error: 'Email not received. Check your Attribute Mapping configuration.'
Follow the steps mentioned HERE
I am getting "Client Credentials were not found in
the headers or body"
when I try to perform test configuration
Follow the steps mentioned HERE
After I click on the logout in Drupal, it sends me back to the Drupal homepage. However, when I try to login with other user, it doesn't ask me to login but automatically logs me in with same user
The logout functionality you've mentioned here is the default behavior of a module. It's logging you out of Drupal but not from your Application/Provider. To allow the module to logout from your provider/application account (what you are looking for), you need to make the below configurations: [know more]
I purchased the paid Drupal module and replaced it with the free module, but still I am not able to use paid features.
As you have upgraded to one of our paid versions of the Drupal module and replaced the free module with the paid one, you must first activate the paid module. Please refer to the below steps. [Know more]
[MO_CONTACT_US]