Drupal 8/9: Integrating with auth0
In this article we will cover how to integrate Drupal 8/9 website with Auth0 Single Sign-On (SSO) platform. Auth0 team announced the end of life of their official Drupal 8 module. The Github repository would be available until March 8, 2022 further, it will also be removed. Alternate suggested by the auth0 team is to use the OpenID Connect / OAuth client Drupal module but as of Jan 2022, it does not have support for the Auth0 IDP.
Hence site owners are requested to migrate to alternative solutions as soon as possible 🚀 🏃.
The alternative ! 😌
The approach we will look is to make use of the auth0/auth0-php open source library provided by the Auth0 team to replicate the same functionality as provided by the Community Module.
Getting started 💻
Make sure that you already have an account created in Auth0, next Create an application and fill in the required details. Detailed info on how to create an application can be found on the following Application Settings page.
#1 The flow diagram
#2 Add the needed composer library
The following PHP library can be used to access Auth0 authentication and management API’s.
composer require auth0/auth0-php:7.5
Note :: This example have been tested with 7.x version of the auth0/auth0-php library.
#3 Create a Drupal module
We start with creating a custom module, in this example i will name it as authzero. For detailed implementation check the GIT repository.
Routing
Create a “authzero.routing.yml” file with 3 routes in it.
- Route 1(/auth0/login) — Handler to redirect user to auth0 universal login page when any user visits the Drupal site.
- Route 2(/auth0/callback) — Callback handler to verify and login user after auth0 has successfully authenticated.
- Route 3 (/auth0/logout) — Handler to logout user from Drupal and the Auth0 platform
Controller
We have defined 3 routes so far, so let us create their corresponding callback methods in a Controller class. For detailed implementation check the GIT repository.
The code is simple to understand just go through each function, and check what we are trying to achieve.
#4 Working
On opening the Drupal site, we are redirected to the Auth0 universal login page. Enter the username and password.
Successfully logged into Drupal site.
For ease in handling auth0 settings, create a config form to accept values or store them as environment variables.
Notes
The auth0/auth0-php library comes with a bundle of features, which can be used to integrate any PHP application with Auth0.
I am maintaining a working D8/9 module on Github, if you would like to contribute to it or have some better suggestion create issue or fork the repository.
Github :: https://github.com/vishwac09/authzero
Composer :: https://packagist.org/packages/vishwac09/authzero
Thanks, #visor