Skip to main content

Laravel Tutorial

Welcome to the Laravel setup tutorial! This tutorial will guide you through the steps to integrate AuthSezam with Laravel.

I - Technical Prerequisites

To set up OIDC on a Laravel, you need :

  • Knowledge of Laravel
  • Access to the following variables:
    • $clientId: OIDC client ID*
    • $clientSecret: OIDC client secret*
    • $baseUrl : url of OIDC serveur*
    • $realm : realm of OIDC serveur*
  • Transmit to us :
    • $redirectUrl : the redirect URL you want once the connection is established

*Provided by us in a separate document

II - Installing the Components

composer require laravel/socialite
composer require socialiteproviders/keycloak

AuthSezam is compatible with the Keycloak provider plugin.

III - Configuration

  • Add in .env :
CLIENT_ID=$clientId
CLIENT_SECRET=$clientSecret
REDIRECT_URI=$redirectUrl
BASE_URL=$baseUrl
REALM=$realm
  • Open the config/services.php file and add the configuration :
'keycloak' => [
'client_id' => env('CLIENT_ID'),
'client_secret' => env('CLIENT_SECRET'),
'redirect' => env('REDIRECT_URI'),
'base_url' => env('BASE_URL'),
'realms' => env('REALM'),
],
  • Open the app/Providers/AppServiceProvider.php and add on boot function :
    public function boot(): void
{
Event::listen(function (\SocialiteProviders\Manager\SocialiteWasCalled $event) {
$event->extendSocialite('keycloak', \SocialiteProviders\Keycloak\Provider::class);
});
}
  • In your terminal :
php artisan make:controller AuthController
  • Open the app/Http/Controllers/AuthController.php and add :
<?php

namespace App\Http\Controllers;

use Laravel\Socialite\Facades\Socialite;
use Illuminate\Http\Request;

class AuthController extends Controller
{
public function redirectToProvider()
{
return Socialite::driver('keycloak')->redirect();
}

public function handleProviderCallback(Request $request)
{

$user = Socialite::driver('keycloak')->user();
return view('user.profile', ['user' => $user]);

}
}

  • Open the routes/web and add :
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;

Route::get('/', function () {
return view('login');
});

Route::get('/redirect-to-provider', [AuthController::class, 'redirectToProvider'])->name('socialite.redirect');

Route::get('/callback/authsezam', [AuthController::class, 'handleProviderCallback']);

IV - Testing and Validation

  • To test the configuration, you can create two files:
    • views/login.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Connexion</title>
</head>
<body style="display: flex; justify-content: center; align-items: center; height: 100vh;">
<div>
<form action="{{ route('socialite.redirect') }}" method="GET">
<button type="submit" style="background-color: #0C3F77; color: white; padding: 15px 30px; border: none; border-radius: 5px; font-size: 16px; cursor: pointer; transition: background-color 0.3s;">
Login with AuthSezam
</button>
</form>
</div>

</body>
</html>

  • views/user/profile.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profil Utilisateur</title>
</head>
<body style="display: flex; justify-content: center; align-items: center; height: 100vh; flex-direction: column; text-align: center;">
<h1 style="margin-bottom: 20px;">Profil Utilisateur</h1>
<p style="margin-bottom: 10px;">Nom : {{ $user->name }}</p>
<p>Email : {{ $user->email }}</p>
</body>
</html>

Launch your Laravel server and test your connection.