khan-zia/ laravel-saml-idp
使用SAML 2.0将您的Laravel应用程序打造为身份提供者。
Requires
- php: ^7.4 || ^8.0
- illuminate/database: ^7.0|^8.0
- illuminate/routing: ^7.0|^8.0
- illuminate/support: ^7.0|^8.0
- simplesamlphp/saml2: 4.0
README
Laravel SAML IDP
本库仅支持Laravel 7.x及以上版本。
安装
composer require khan-zia/laravel-saml-idp
配置
php artisan vendor:publish --tag="samlidp_config"
文件系统配置
// config/filesystem.php 'disks' => [ ... 'samlidp' => [ 'driver' => 'local', 'root' => storage_path() . '/samlidp', ] ],
使用以下命令为您的基本身份提供者(IdP)创建自签名证书。如果您将证书名称或密钥名称更改为默认名称以外的名称,您需要更新您的config/samlidp.php
配置文件以反映这些新文件名。
php artisan samlidp:cert [--days <days> --keyname <name> --certname <name>]
Options: --days=<days> Days to add for the expiration date [default: 7800] --keyname=<name> Name of the certificate key file [default: key.pem] --certname=<name> Name of the certificate file [default: cert.pem]
使用方法
在您的登录视图中,可能是resources/views/auth/login.blade.php
,在CSRF指令下方添加SAMLRequest指令
@csrf @samlidp
SAMLRequest指令将在通过HTTP请求发送SAMLRequest时自动填写隐藏的输入,因此启动SAML身份验证尝试。为了启动SAML身份验证,登录和重定向过程需要干预。这是通过在身份验证时触发的Laravel事件来完成的。
配置
在发布配置文件后,您需要设置服务提供者。服务提供者的键是基64编码的消费服务(ACS)URL。您可以从服务提供者获取此信息,但您需要将URL进行基64编码并将其放置在配置中。这是由于配置点符号。
您可以使用此命令帮助生成新的SAML服务提供者
php artisan samlidp:sp
config/samlidp.php
文件中的示例SP
<?php return [ // The URI to your login page 'login_uri' => 'login', // The URI to the saml metadata file, this describes your idP 'issuer_uri' => 'saml/metadata', // List of all Service Providers 'sp' => [ // Base64 encoded ACS URL 'aHR0cHM6Ly9teWZhY2Vib29rd29ya3BsYWNlLmZhY2Vib29rLmNvbS93b3JrL3NhbWwucGhw' => [ // ACS URL of the Service Provider 'destination' => 'https://example.com/saml/acs', // Simple Logout URL of the Service Provider 'logout' => 'https://example.com/saml/sls', ] ] ];
在SLO完成后从IdP注销
如果您希望在SLO完成后从IdP注销,请在.env
中将LOGOUT_AFTER_SLO
设置为true
,然后在IdP上执行注销操作。
// .env
LOGOUT_AFTER_SLO=true
注销后重定向到SLO启动器
如果您希望将用户返回到启动SLO的SP,您可以为/saml/logout
路由提供一个额外的查询参数,例如
https://idp.com/saml/logout?redirect_to=mysp.com
在所有SP注销后,用户将被重定向到mysp.com
。为了使此操作正常工作,您需要将sp_slo_redirects
选项添加到您的config/samlidp.php
配置文件中,例如
<?php // config/samlidp.php return [ // If you need to redirect after SLO depending on SLO initiator // key is beginning of HTTP_REFERER value from SERVER, value is redirect path 'sp_slo_redirects' => [ 'mysp.com' => 'https://mysp.com', ], ];
属性(可选)
服务提供者可能需要发送更多附加属性通过断言。甚至可能需要相同的信息,但作为不同的声明类型。
默认情况下,此包将发送以下声明类型
ClaimTypes::EMAIL_ADDRESS
作为auth()->user()->email
ClaimTypes::GIVEN_NAME
作为auth()->user()->name
这是因为Laravel迁移默认情况下只提供SAML 2.0可用的电子邮件和姓名字段。
要添加额外的声明类型,您可以订阅断言事件
ziakhan\SamlIdp\Events\Assertion
订阅事件
在您的App\Providers\EventServiceProvider
类中,向现有的$listen
属性添加...
protected $listen = [ 'App\Events\Event' => [ 'App\Listeners\EventListener', ], 'ziakhan\SamlIdp\Events\Assertion' => [ 'App\Listeners\SamlAssertionAttributes' ] ];
示例监听器
<?php namespace App\Listeners; use LightSaml\ClaimTypes; use LightSaml\Model\Assertion\Attribute; use ziakhan\SamlIdp\Events\Assertion; class SamlAssertionAttributes { public function handle(Assertion $event) { $event->attribute_statement ->addAttribute(new Attribute(ClaimTypes::PPID, auth()->user()->id)) ->addAttribute(new Attribute(ClaimTypes::NAME, auth()->user()->name)); } }
鸣谢
此库最初是基于codegreencreative/laravel-samlidp的分支开始的,但现在已被大幅修改。据我们所知,当至少80%的代码库被修改后,这些鸣谢可能会被删除。