maghonemi/laravel-samlidp

使用 SAML 2.0 将您的 Laravel 应用程序打造成身份提供者。已将 ds:DigestMethod 的哈希值更改为 256。

v6.0.1 2021-02-28 16:37 UTC

README

Latest Version on Packagist Total Downloads

Laravel (5.6+) SAML idP

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

安装

使用 composer 安装此包

composer require Maghonemi/laravel-samlidp

发布配置

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 指令将在发送 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。

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

Maghonemi\SamlIdp\Events\Assertion

订阅事件

在您的 App\Providers\EventServiceProvider 类中,将以下内容添加到现有的 $listen 属性...

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

示例监听器

<?php

namespace App\Listeners;

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