PHP code example of aherstein / laravel-saml2-post

1. Go to this page and download the library: Download aherstein/laravel-saml2-post 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/ */

    

aherstein / laravel-saml2-post example snippets


'providers' => [
        ...
    	SamlPost\Saml2\Saml2ServiceProvider::class,
]

'alias' => [
        ...
        'Saml2' => SamlPost\Saml2\Facades\Saml2Auth::class,
]

$metadata['http://laravel_url/saml2/metadata'] = array(
    'AssertionConsumerService' => 'http://laravel_url/saml2/acs',
    'SingleLogoutService' => 'http://laravel_url/saml2/sls',
    //the following two affect what the $Saml2user->getUserId() will return
    'NameIDFormat' => 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent',
    'simplesaml.nameidattribute' => 'uid' 
);

    public function boot()
    {
        $this->registerPolicies();
        
        Auth::extend('saml', function ($app, $name, array $config) {
            return new Saml2Guard(Auth::createUserProvider($config['provider']));
        });
        
        Auth::provider('samldriver', function ($app, array $config) {
            return new Saml2UserProvider();
        });

'guards' => [

...

'saml' => [
    'driver' => 'session',
    'provider' => 'samlusers',
],

'providers' => [

...

'samlusers' => [
    'driver' => 'samldriver',
    'model' => SamlPost\Saml2\Saml2User::class,
],

    protected $listen = [
        'SamlPost\Saml2\Events\Saml2LoginEvent' => [
            'App\Listeners\LoginListener',
        ],
        'SamlPost\Saml2\Events\Saml2LogoutEvent' => [
            'App\Listeners\LogoutListener',
        ],
    ];

Route::middleware(['auth:saml'])->group(function () {
    // Secured routes go here
});

    public function handle(Saml2LoginEvent $event)
    {
        $user = $event->getSaml2User();
        $auth = $event->getSaml2Auth();

        // Store SAML response data in session
        $this->request->session()->put('isLoggedIn', $auth->isAuthenticated());
        $this->request->session()->put('samlData', $user);
        $this->request->session()->put('user', $user->getAttributes());
    }

public function handle(Saml2LogoutEvent $event)
{
    // Clear out SAML data from session
    $this->request->session()->put('isLoggedIn', false);
    $this->request->session()->put('samlData', null);
    $this->request->session()->put('user', null);
}
blade
    <form method="post" action="{{$baseUri}}" accept-charset="utf-8">
        <input type="submit" value="Log In via SSO"/>
        @foreach ($samlParameters as $k => $v)
            <input type="hidden" name="{{$k}}" value="{{$v}}"/>
        @endforeach
    </form>