PHP code example of jdlien / laravel-saml

1. Go to this page and download the library: Download jdlien/laravel-saml library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jdlien / laravel-saml example snippets


use Jdlien\LaravelSaml\Saml;

Saml::configureIdpUsing(function (string $idpName): array {
    // Look up the idp config from your DB, tenant store, etc.
    return [
        'entityId' => '...',
        'singleSignOnService' => ['url' => '...'],
        // ... see config/saml.php for the full shape
    ];
});



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Jdlien\LaravelSaml\Saml;
use App\Models\User;

class SamlController extends Controller
{
    public function login() {}
    public function acs() {}
    public function logout() {}
    public function sls() {}
    public function metadata() {}
}

use App\Http\Controllers\SamlController;

Route::get('saml/login', [SamlController::class, 'login'])->name('saml.login');
Route::post('saml/acs', [SamlController::class, 'acs'])->name('saml.acs');
Route::get('saml/logout', [SamlController::class, 'logout'])->name('saml.logout');
Route::get('saml/sls', [SamlController::class, 'sls'])->name('saml.sls');
Route::get('saml/metadata', [SamlController::class, 'metadata'])->name('saml.metadata');

  // bootstrap/app.php
  ->withMiddleware(function (Middleware $middleware) {
      $middleware->validateCsrfTokens(except: [
          'saml/acs',
      ]);
  })
  

public function login(Request $request)
{
    return Saml::redirect();
}

public function acs(Request $request)
{
    $samlUser = Saml::getAuthenticatedUser();

    $user = User::firstOrCreate(['email' => $samlUser->getUserId()]);
    Auth::login($user);

    // getIntendedUrl() validates the SAML RelayState — see "Security" below.
    return redirect($samlUser->getIntendedUrl() ?? '/home');
}

public function logout(Request $request)
{
    return Saml::redirectToLogout();
}

public function sls(Request $request)
{
    $redirect = Saml::handleLogoutRequest();

    Auth::logout();

    // IdP-initiated logout: handleLogoutRequest() returns a RedirectResponse
    // that sends a LogoutResponse back to the IdP. Honor it.
    return $redirect ?? redirect('/');
}

public function metadata(Request $request)
{
    return Saml::getMetadataXML();

    // Or as a streamed download:
    // return Saml::getMetadataXMLAsStreamResponse('my-app-saml-metadata.xml');
}
bash
php artisan vendor:publish --tag=saml-config
bash
php artisan make:controller SamlController