pdmfc/laravel-saml2idp-php-8-support

使用SAML 2.0将您的Laravel应用程序打造成一个身份提供者。

dev-master 2024-02-15 18:36 UTC

This package is not auto-updated.

Last update: 2024-09-16 19:51:20 UTC


README

laravel SAMLIDP PHP库的分支

FOR LARAVEL 7

使用2.0.0版本

FOR LARAVEL 6

使用1.0.0版本

安装

在composer仓库部分添加此内容

{
    "type": "vcs",
    "url": "https://github.com/pdmfc/laravel-saml2idp-php-8-support.git"
}

使用composer要求此包

composer require pdmfc/laravel-saml2idp-php-8-support:*

配置

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的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',
        ]
    ]

];

在SLO后从IdP注销

如果您希望在SLO完成后从IdP注销,请将LOGOUT_AFTER_SLO设置为true在您的.env执行注销操作。

// .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可用的电子邮件和名称字段。

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

PDMFC\Saml2Idp\Events\Assertion

订阅事件

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

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

示例监听器

<?php

namespace App\Listeners;

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