Single sign-on using SAML with Laravel 5

Tram Ho

Introduce

These are memos when using Single sign-on using SAML for Laravel 5.

Most of the content is similar to README of laravel-saml2.

Environment

Works from Laravel 5.4 and later.

Setting

Install laravel-saml2

composer require aacotroneo/laravel-saml2

For versions of Laravel before 5.5, it is necessary to add a Service Provider to configapp.php as shown below:

config app.php

Implement vendor: publish php artisan vendor:publish --provider="AacotroneoSaml2Saml2ServiceProvider" appconfigsaml2_settings.php will be created.

Create Laravel authenticataion

Create Auth for Laravel

Install SAML

Register metadata into Idp

Confirm the Laravel metadata SAML (SAML SP) by following the link below, then register on the IdP side.

http://localhost/Laravel-app-name/public/saml2/metadata

Set up the IdP metadata for Laravel

Set up the IdP metadata into saml2_settings.php of Laravel.

Specify the environment variables entityId, singleSignOnService, singleLogoutService, x509cert.

This site is using Google IdP.

app config saml2_settings.php

Change routesMiddleware

Because the Login session has not been created when adding VerifyCsrfToken, we have to change the routesMiddleware of saml2_settings.php as shown below:

app config saml2_settings.php 'routesMiddleware' => ['saml'],

Add new SAML Middle ware group to Kernel.php.

The content of the saml group after removing VerifyCsrfToken from the web group.

app Http Kernel.php

Create the authentication section

Create Event listeners for SAML login and logout

Describe the event when SAML login and logout.

Because of the association with Laravel’s Auth, the following will be done:

  • When SAML login, Auth also login
  • When SAML logout, Auth also logout

Add boot method to EventServiceProvider.php app Providers EventServiceProvider.php

Synchronize the properties obtained with the environment. If a 401 error occurs when validating then use the corresponding 401 view as the link below: appresourcesviewserrors401.blade.php

Create Middleware SAML authentication

Create Middleware to authenticate SAML. By using this Middleware, a SAML login is required to access the pages.

Use artisan command to create Middleware named SamlAuth.

php artisan make:middleware SamlAuth

Edit SamlAuth.php as follows:

app Http Middleware SamlAuth.php

Register SamlAuth in Kernel.php’s routeMiddleware.

app Http Kernel.php

Use authentication

Use Middleware authentication.

Below is an example of using middleware for a route group.

Check activity

Create data user with email and IdP in table Users.

Access to / home, will appear IdP login screen.

Proceed to login by email address.

Refer

Laravel 5 で SAML シ ン グ ル サ イ ン オ ン す る

Share the news now

Source : Viblo