brpassos/laravel-samlidp

由 Brpassos/laravel-samlidp 创建。使用 SAML 2.0 将您的 PHP Laravel 应用程序打造为一个身份提供者。此包允许您使用 SAML 2.0 标准实现自己的身份提供者(idP),以便与支持 SAML 2.0 服务提供者(SP)一起使用。

v6.1.0 2022-09-19 15:11 UTC

README

Latest Version on Packagist Total Downloads

买我一杯咖啡

Laravel SAML IdP

此包允许您使用 SAML 2.0 标准实现自己的身份提供者(idP),以便与支持 SAML 2.0 服务提供者(SP)一起使用。

从版本 ^5.1 开始,支持 Laravel 9。

在此版本中,我们将支持 Laravel ^7.0 或 ^8.0。

如果您正在寻找 Laravel ^5.6,请参阅 v1.0

如果您正在寻找 Laravel ^6.0,请使用 v2.0

安装

composer require Brpassos/laravel-samlidp

配置

php artisan vendor:publish --tag="samlidp_config"

文件系统配置

// config/filesystem.php

'disks' => [

        ...

        'samlidp' => [
            'driver' => 'local',
            'root' => storage_path() . '/samlidp',
        ]
],

使用以下命令为您自己的 IdP 创建一个自签名证书。如果您将 certname 或 keyname 改为除默认名称之外的其他名称,您需要更新您的 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 指令将在 SAMLRequest 通过 HTTP 请求发送时自动填写隐藏的输入,因此启动 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',
        ]
    ],
    // List of guards saml idp will catch Authenticated, Login and Logout events (thanks @abublihi)
    'guards' => ['web']
];

在 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?return_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 可以使用的电子邮件和名称字段。

要添加额外的声明类型,您可以订阅断言事件

Brpassos\SamlIdp\Events\Assertion

订阅事件

在您的 App\Providers\EventServiceProvider 类中,向已经存在的 $listen 属性添加...

protected $listen = [
    'App\Events\Event' => [
        'App\Listeners\EventListener',
    ],
    'Brpassos\SamlIdp\Events\Assertion' => [
        'App\Listeners\SamlAssertionAttributes'
    ]
];

示例监听器

<?php

namespace App\Listeners;

use LightSaml\ClaimTypes;
use LightSaml\Model\Assertion\Attribute;
use Brpassos\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));
    }
}

摘要算法(可选)

查看 \RobRichards\XMLSecLibs\XMLSecurityDSig 了解所有摘要选项。

<?php

return [
    // Defind what digital algorithm you want to use
    'digest_algorithm' => \RobRichards\XMLSecLibs\XMLSecurityDSig::SHA1,
];

买我一杯咖啡