PHPackages                             roileo/laravel-saml2 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. roileo/laravel-saml2

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

roileo/laravel-saml2
====================

SAML2 Service Provider integration to your Laravel 8+ application, based on OneLogin toolkit

022[1 issues](https://github.com/roiLeo/laravel-saml2/issues)PHPCI passing

Since Feb 27Pushed 4mo agoCompare

[ Source](https://github.com/roiLeo/laravel-saml2)[ Packagist](https://packagist.org/packages/roileo/laravel-saml2)[ RSS](/packages/roileo-laravel-saml2/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

\[Laravel 8+\] SAML Service Provider
------------------------------------

[](#laravel-8-saml-service-provider)

[![Latest Stable Version](https://camo.githubusercontent.com/4f8fcf6d3dceadc7aa43cbba4937a9520820629e7bfe758a481316428abf558d/68747470733a2f2f706f7365722e707567782e6f72672f726f696c656f2f6c61726176656c2d73616d6c322f762f737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/roileo/laravel-saml2#dev-master)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/95559b6fdb02e54855bd686f61abf183c638c9eee9d449b670add926898be930/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f726f694c656f2f6c61726176656c2d73616d6c322f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/roiLeo/laravel-saml2/build-status/master)[![Quality Score](https://camo.githubusercontent.com/5fc692db2dd4a28daa489a0be74202d36c1968ec27612f0c8feffe3085bd0a14/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f726f696c656f2f6c61726176656c2d73616d6c322e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/roileo/laravel-saml2)[![Code Coverage](https://camo.githubusercontent.com/9e3d32c3808505acc1f3f1ec9773b2dd9e4e47b55eb13a96ddd371e97fcba8bb/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f726f696c656f2f6c61726176656c2d73616d6c322e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/roileo/laravel-saml2)[![Total Downloads](https://camo.githubusercontent.com/948567d9d3b3335446338988c96783dc7ccee62190e4be18d6e90fb18d444e5b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f696c656f2f6c61726176656c2d73616d6c322e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/roileo/laravel-saml2)

An integration to add SSO to your service via SAML2 protocol based on [OneLogin](https://github.com/onelogin/php-saml) toolkit.

This package turns your application into Service Provider with the support of multiple Identity Providers.

Requirements
------------

[](#requirements)

- Laravel 8+
- PHP 7.4+

Getting Started
---------------

[](#getting-started)

### Installing

[](#installing)

##### Step 1. Install dependency

[](#step-1-install-dependency)

```
composer require roileo/laravel-saml2

```

##### Step 2. Publish the configuration file.

[](#step-2-publish-the-configuration-file)

```
php artisan vendor:publish --provider="Slides\Saml2\ServiceProvider"

```

##### Step 3. Run migrations

[](#step-3-run-migrations)

```
php artisan migrate

```

### Configuring

[](#configuring)

Once you publish `saml2.php` to `app/config`, you need to configure your SP. Most of options are inherited from [OneLogin Toolkit](https://github.com/onelogin/php-saml), so you can check documentation there.

#### Identity Providers (IdPs)

[](#identity-providers-idps)

To distinguish between identity providers there is an entity called Tenant that represent each IdP.

When request comes to an application, the middleware parses UUID and resolves the Tenant.

You can easily manage tenants using the following console commands:

- `artisan saml2:create-tenant`
- `artisan saml2:update-tenant`
- `artisan saml2:delete-tenant`
- `artisan saml2:restore-tenant`
- `artisan saml2:list-tenants`
- `artisan saml2:tenant-credentials`

> To learn their options, run a command with `-h` parameter.

Each Tenant has the following attributes:

- **UUID** — a unique identifier that allows to resolve a tenannt and configure SP correspondingly
- **Key** — a custom key to use for application needs
- **Entity ID** — [Identity Provider Entity ID](https://spaces.at.internet2.edu/display/InCFederation/Entity+IDs)
- **Login URL** — Identity Provider Single Sign On URL
- **Logout URL** — Identity Provider Logout URL
- **x509 certificate** — The certificate provided by Identity Provider in **base64** format
- **Metadata** — Custom parameters for your application needs

#### Default routes

[](#default-routes)

The following routes are registered by default:

- `GET saml2/{uuid}/login`
- `GET saml2/{uuid}/logout`
- `GET saml2/{uuid}/metadata`
- `POST saml2/{uuid}/acs`
- `POST saml2/{uuid}/sls`

You may disable them by setting `saml2.useRoutes` to `false`.

> `/saml2` prefix can be changed via `saml2.routesPrefix` config parameter.

Usage
-----

[](#usage)

### Authentication events

[](#authentication-events)

The simplest way to handle SAML authentication is to add listeners on `Slides\Saml2\SignedIn` and `Slides\Saml2\SignedOut` events.

```
Event::listen(\Slides\Saml2\Events\SignedIn::class, function (\Slides\Saml2\Events\SignedIn $event) {
    $messageId = $event->getAuth()->getLastMessageId();

    // your own code preventing reuse of a $messageId to stop replay attacks
    $samlUser = $event->getSaml2User();

    $userData = [
        'id' => $samlUser->getUserId(),
        'attributes' => $samlUser->getAttributes(),
        'assertion' => $samlUser->getRawSamlAssertion()
    ];

    $user = // find user by ID or attribute

    // Login a user.
    Auth::login($user);
});
```

### Middleware

[](#middleware)

To define a middleware for default routes, add its name to `config/saml2.php`:

```
/*
|--------------------------------------------------------------------------
| Built-in routes prefix
|--------------------------------------------------------------------------
|
| Here you may define the prefix for built-in routes.
|
*/

'routesMiddleware' => ['saml'],
```

Then you need to define necessary middlewares for your group in `app/Http/Kernel.php`:

```
protected $middlewareGroups = [
    'web' => [
        ...
    ],
    'api' => [
        ...
    ],
    'saml' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
    ],
```

### Logging out

[](#logging-out)

There are two ways the user can logout:

- By logging out in your app. In this case you SHOULD notify the IdP first so it'll close the global session.
- By logging out of the global SSO Session. In this case the IdP will notify you on `/saml2/{uuid}/slo` endpoint (already provided).

For the first case, call `Saml2Auth::logout();` or redirect the user to the route `saml.logout` which does just that. Do not close the session immediately as you need to receive a response confirmation from the IdP (redirection). That response will be handled by the library at `/saml2/sls` and will fire an event for you to complete the operation.

For the second case you will only receive the event. Both cases receive the same event.

Note that for the second case, you may have to manually save your session to make the logout stick (as the session is saved by middleware, but the OneLogin library will redirect back to your IdP before that happens):

```
Event::listen('Slides\Saml2\Events\SignedOut', function (SignedOut $event) {
    Auth::logout();
    Session::save();
});
```

### SSO-friendly links

[](#sso-friendly-links)

Sometimes, you need to create links to your application with support of SSO lifecycle. It means you expect a user to be signed in once you click on that link.

The most popular example is generating links from emails, where you need to make sure when user goes to your application from email, he will be logged in. To solve this issue, you can use helpers that allow you create SSO-friendly routes and URLs — `saml_url()` and `saml_route()`.

To generate a link, you need to call one of functions and pass UUID of the tenant as a second parameter, unless your session knows that user was resolved by SSO.

> To retrieve UUID based on user, you should implement logic that links your internal user to a tenant.

Then, it generates a link like this:

```
https://yourdomain/saml/63fffdd1-f416-4bed-b3db-967b6a56896b/login?returnTo=https://yourdomain.com/your/actual/link

```

Basically, when user clicks on a link, it initiates SSO login process and redirects it back to your needed URL.

Examples
--------

[](#examples)

### Azure AD

[](#azure-ad)

At this point, we assume you have an application on Azure AD that supports Single Sign On.

##### Step 1. Retrieve Identity Provider credentials

[](#step-1-retrieve-identity-provider-credentials)

[![Azure AD](https://camo.githubusercontent.com/9e25a97344b90b27d5f10460fce273a5258ca3e3c0618b5e50b4833ab8c19e2b/68747470733a2f2f692e696d6775722e636f6d2f784b4c737778422e706e67)](https://camo.githubusercontent.com/9e25a97344b90b27d5f10460fce273a5258ca3e3c0618b5e50b4833ab8c19e2b/68747470733a2f2f692e696d6775722e636f6d2f784b4c737778422e706e67)

You need to retrieve the following parameters:

- Login URL
- Azure AD Identifier
- Logout URL
- Certificate (Base64)

##### Step 2. Create a Tenant

[](#step-2-create-a-tenant)

Based on information you received below, create a Tenant, like this:

```
php artisan saml2:create-tenant \
  --key=azure_testing \
  --entityId=https://sts.windows.net/fb536a7a-7251-4895-a09a-abd8e614c70b/ \
  --loginUrl=https://login.microsoftonline.com/fb536a7a-7251-4895-a09a-abd8e614c70b/saml2 \
  --logoutUrl=https://login.microsoftonline.com/common/wsfederation?wa=wsignout1.0 \
  --x509cert="MIIC0jCCAbqgAw...CapVR4ncDVjvbq+/S" \
  --metadata="customer:11235,anotherfield:value" // you might add some customer parameters here to simplify logging in your customer afterwards

```

Once you successfully created the tenant, you will receive the following output:

```
The tenant #1 (63fffdd1-f416-4bed-b3db-967b6a56896b) was successfully created.

Credentials for the tenant
--------------------------

 Identifier (Entity ID): https://yourdomain.com/saml/63fffdd1-f416-4bed-b3db-967b6a56896b/metadata
 Reply URL (Assertion Consumer Service URL): https://yourdomain.com/saml/63fffdd1-f416-4bed-b3db-967b6a56896b/acs
 Sign on URL: https://yourdomain.com/saml/63fffdd1-f416-4bed-b3db-967b6a56896b/login
 Logout URL: https://yourdomain.com/saml/63fffdd1-f416-4bed-b3db-967b6a56896b/logout
 Relay State: / (optional)

```

##### Step 3. Configure Identity Provider

[](#step-3-configure-identity-provider)

Using the output below, assign parameters to your IdP on application Single-Sign-On settings page.

[![Azure AD](https://camo.githubusercontent.com/2184746b3850950da5929d6200916d41d3ee2ae2ee8701c83a49933574adcc61/68747470733a2f2f692e696d6775722e636f6d2f33686b6a464c5a2e706e67)](https://camo.githubusercontent.com/2184746b3850950da5929d6200916d41d3ee2ae2ee8701c83a49933574adcc61/68747470733a2f2f692e696d6775722e636f6d2f33686b6a464c5a2e706e67)

##### Step 4. Make sure your application accessible by Azure AD

[](#step-4-make-sure-your-application-accessible-by-azure-ad)

Test your application directly from Azure AD and make sure it's accessible worldwide.

###### Running locally

[](#running-locally)

If you want to test it locally, you may use [ngrok](https://ngrok.com/).

In case if you have a problem with URL creation in your application, you can overwrite host header in your nginx host config file by adding the following parameters:

```
fastcgi_param HTTP_HOST your.ngrok.io;
fastcgi_param HTTPS on;

```

> Replace `your.ngrok.io` with your actual ngrok URL

Tests
-----

[](#tests)

Run the following in the package folder:

```
vendor/bin/phpunit

```

Security
--------

[](#security)

WTFPL

Credits
-------

[](#credits)

- [aacotroneo](https://github.com/aacotroneo)
- [brezzhnev](https://github.com/brezzhnev)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance52

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity19

Early-stage or recently created project

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/010d0c8a3ff594764fce9b9ee02aa04f7919ea1e05e1cefc3ed4b358f450890e?d=identicon)[roiLeo](/maintainers/roiLeo)

---

Top Contributors

[![aacotroneo](https://avatars.githubusercontent.com/u/10578291?v=4)](https://github.com/aacotroneo "aacotroneo (90 commits)")[![breart](https://avatars.githubusercontent.com/u/9466810?v=4)](https://github.com/breart "breart (86 commits)")[![roiLeo](https://avatars.githubusercontent.com/u/9987732?v=4)](https://github.com/roiLeo "roiLeo (15 commits)")[![vopolonc](https://avatars.githubusercontent.com/u/44755807?v=4)](https://github.com/vopolonc "vopolonc (8 commits)")[![RobertBoes](https://avatars.githubusercontent.com/u/2871897?v=4)](https://github.com/RobertBoes "RobertBoes (7 commits)")[![dmyers](https://avatars.githubusercontent.com/u/207171?v=4)](https://github.com/dmyers "dmyers (5 commits)")[![aguinaldotupy](https://avatars.githubusercontent.com/u/44652991?v=4)](https://github.com/aguinaldotupy "aguinaldotupy (5 commits)")[![danmichaelo](https://avatars.githubusercontent.com/u/434495?v=4)](https://github.com/danmichaelo "danmichaelo (4 commits)")[![omitobi](https://avatars.githubusercontent.com/u/16482234?v=4)](https://github.com/omitobi "omitobi (3 commits)")[![soltmar](https://avatars.githubusercontent.com/u/14175459?v=4)](https://github.com/soltmar "soltmar (3 commits)")[![r-senchuk](https://avatars.githubusercontent.com/u/5601288?v=4)](https://github.com/r-senchuk "r-senchuk (2 commits)")[![s3sam](https://avatars.githubusercontent.com/u/25058465?v=4)](https://github.com/s3sam "s3sam (2 commits)")[![snipe](https://avatars.githubusercontent.com/u/197404?v=4)](https://github.com/snipe "snipe (2 commits)")[![BrendanTWhite](https://avatars.githubusercontent.com/u/2833789?v=4)](https://github.com/BrendanTWhite "BrendanTWhite (2 commits)")[![darynmitchell](https://avatars.githubusercontent.com/u/3178872?v=4)](https://github.com/darynmitchell "darynmitchell (2 commits)")[![abublihi](https://avatars.githubusercontent.com/u/10172039?v=4)](https://github.com/abublihi "abublihi (2 commits)")[![matijakovacevic](https://avatars.githubusercontent.com/u/2469719?v=4)](https://github.com/matijakovacevic "matijakovacevic (2 commits)")[![perifer](https://avatars.githubusercontent.com/u/34488?v=4)](https://github.com/perifer "perifer (2 commits)")[![olivM](https://avatars.githubusercontent.com/u/855?v=4)](https://github.com/olivM "olivM (1 commits)")[![garethellis36](https://avatars.githubusercontent.com/u/6451455?v=4)](https://github.com/garethellis36 "garethellis36 (1 commits)")

### Embed Badge

![Health badge](/badges/roileo-laravel-saml2/health.svg)

```
[![Health](https://phpackages.com/badges/roileo-laravel-saml2/health.svg)](https://phpackages.com/packages/roileo-laravel-saml2)
```

###  Alternatives

[namshi/jose

JSON Object Signing and Encryption library for PHP.

1.8k99.6M101](/packages/namshi-jose)[league/oauth1-client

OAuth 1.0 Client Library

99698.8M106](/packages/league-oauth1-client)[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[gesdinet/jwt-refresh-token-bundle

Implements a refresh token system over Json Web Tokens in Symfony

70516.4M35](/packages/gesdinet-jwt-refresh-token-bundle)[league/oauth2-google

Google OAuth 2.0 Client Provider for The PHP League OAuth2-Client

41721.2M118](/packages/league-oauth2-google)[illuminate/auth

The Illuminate Auth package.

9327.3M1.0k](/packages/illuminate-auth)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
