24slides/laravel-saml2 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

24slides/laravel-saml2

最新稳定版本:2.4.2

Composer 安装命令:

composer require 24slides/laravel-saml2

包简介

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

README 文档

README

⚠️ THIS REPOSITORY IS DEPRECATED ⚠️

This package is no longer maintained by 24Slides. It will not receive any further updates, bug fixes, or security patches. We strongly recommend using an alternative, actively maintained SAML package for Laravel. You are free to fork this repository and continue its development independently.

[Laravel 5.4+] SAML Service Provider

Latest Stable Version Software License Build Status Quality Score Code Coverage Total Downloads

An integration to add SSO to your service via SAML2 protocol based on OneLogin toolkit.

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

Requirements

  • Laravel 5.4+
  • PHP 7.0+

Getting Started

Installing

Step 1. Install dependency
composer require 24slides/laravel-saml2

If you are using Laravel 5.5 and higher, the service provider will be automatically registered.

For older versions, you have to add the service provider and alias to your config/app.php:

'providers' => [
    ...
    Slides\Saml2\ServiceProvider::class,
]

'alias' => [
    ...
    'Saml2' => Slides\Saml2\Facades\Auth::class,
]
Step 2. Publish the configuration file.
php artisan vendor:publish --provider="Slides\Saml2\ServiceProvider"
Step 3. Run migrations
php artisan migrate

Configuring

Once you publish saml2.php to app/config, you need to configure your SP. Most of options are inherited from OneLogin Toolkit, so you can check documentation there.

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 IDIdentity Provider Entity ID
  • 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

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

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

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

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

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

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

Azure AD

You need to retrieve the following parameters:

  • Login URL
  • Azure AD Identifier
  • Logout URL
  • Certificate (Base64)
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

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

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

If you want to test it locally, you may use ngrok.

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

Run the following in the package folder:

vendor/bin/phpunit

Security

As this project is no longer maintained, security vulnerabilities will not be fixed by 24Slides. The email address previously listed for reporting is no longer monitored for this project.

Credits

License

The MIT License (MIT). Please see License File for more information.

24slides/laravel-saml2 适用场景与选型建议

24slides/laravel-saml2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.74M 次下载、GitHub Stars 达 271, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「SSO」 「laravel」 「saml」 「SAML2」 「onelogin」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 24slides/laravel-saml2 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 24slides/laravel-saml2 我们能提供哪些服务?
定制开发 / 二次开发

基于 24slides/laravel-saml2 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.74M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 271
  • 点击次数: 32
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 271
  • Watchers: 10
  • Forks: 78
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04