pkeogan/laravel-saml2

SAML 支持使 Laravel 应用程序成为 SAML IDP 和 SAML SP。

dev-master 2018-11-09 09:36 UTC

This package is auto-updated.

Last update: 2024-09-08 10:27:52 UTC


README

该仓库最初是 kingstarter/laravel-saml 的分支。它已经从那个基础上发展起来,现在为任何 Laravel 应用程序提供了以下功能:1 - 成为 IDP 2 - 生成用于签名消息、签名断言和加密属性的证书。这些证书使用从配置文件中输入的数据 3 - 配置要发送的属性(从配置文件) 4 - 为每个 SP 配置是否应签名消息和/或断言 5 - 提供在注销页面上通过 iframe 注销任何服务提供者的能力

此包使维护 IDP 变得非常容易。文档需要一点工作,如果您愿意帮助,请告诉我。

安装

基本包安装

使用 composer

composer require "pkeogan/laravel-saml2":"dev-master"

Laravel 5.4

将服务提供者添加到 config/app.php

    Pkeogan\LaravelSaml\LaravelSamlServiceProvider::class,

Laravel 5.5+

此包支持 Laravel 的包自动发现,应在需要时通过 composer 自动加载。如果包未自动发现,请运行

    php artisan package:discover

配置

有一个需要发布的配置文件和需要扩展的 config/filesystem.php 文件。以下命令将发布 config/saml.php 文件。

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

将发布 config/saml.php 文件。

文件系统配置

config/filesystem.php 中需要添加以下条目

    'disks' => [

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

    ],

填写配置文件

WIP

生成元数据和证书

一旦填写了正确的配置,请运行以下命令以生成元数据和证书。请注意,如果没有找到证书,系统将生成它们。如果您想覆盖证书,请使用 --cert 标志

php artisan laravel-saml:generate-meta

SAML SP 条目

在 saml.php 配置文件中,需要填写 SAML 服务提供者数组。

    'sp' => [

        //Tableau
        'https://sso.online.tableau.com/public/sp/SSO?alias=xxxx-xxxx-xxxx-xxxx-xxxxxxxx' => [
            'entity-id' => 'https://sso.online.tableau.com/public/sp/metadata?alias=xxxx-xxxx-xxxx-xxxx-xxxxxxxx',
            'certificate' => 'MIICozC........dUvTnGP18g=='
        ],

        //A nifty testing service provider
        'https://sptest.iamshowcase.com/acs' => [

        ]

    ],

使用 SAML 包

要使用 SAML 包,需要修改一些文件。在您的登录视图中,可能是 resources/views/auth/login.blade.php,在 CSRF 字段下方添加一个 SAMLRequest 字段(这实际上是一个很好的地方)

    {{-- The hidden CSRF field for secure authentication --}}
    {{ csrf_field() }}
    {{-- Add a hidden SAML Request field for SAML authentication --}}
 	@if(isset($_GET['SAMLRequest']))
        <input type="hidden" id="SAMLRequest" name="SAMLRequest" value="{{ $_GET['SAMLRequest'] }}">
    @elseif(isset($saml))
        <input type="hidden" id="SAMLRequest" name="SAMLRequest" value="{{ $saml }}">
    @endif
    @if( config('saml.logout_apps_via_iframe') && session('samlLogout') ) 
	  	@include('saml::logout')
  	@endif

当通过 http 请求发送 SAMLRequest 时,SAMLRequest 字段将自动填充,从而启动 SAML 身份验证尝试。要启动 SAML 身份验证,需要修改登录和重定向函数。在 app/Http/Controllers/Auth/LoginController.php 中,将 use AuthenticatesUsers 更改为 use SamlAuthenticatesUsers

use App\Http\Controllers\Controller;
use KingStarter\LaravelSaml\Http\Traits\SamlAuthenticatesUsers;

class LoginController extends Controller
{
...

      /**
     * The user has been authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function authenticated(Request $request, $user)
    {
        if(Auth::check() && isset($request['SAMLRequest'])) {
            $this->handleSamlLoginRequest($request);
        }

        return redirect()->intended($this->redirectPath());
    }

.....

为了允许在某人已经登录的情况下进行后续的直接重定向,我们需要在 app/Http/Middleware/RedirectIfAuthenticated.php 中添加一些行

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Pkeogan\LaravelSaml\Http\Traits\SamlAuth;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class RedirectIfAuthenticated
{
        use SamlAuth;

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle(Request $request, Closure $next, $guard = null)
    {
		if(Auth::check() && isset($request['SAMLRequest'])){
            $saml = $this->handleSamlLoginRequest($request);
			return new Response(view('saml::post')->withSaml($saml));
        } 
		
        if(Auth::guard($guard)->check() && !isset($request['SAMLRequest']) ) {
            return redirect('/');
        }
        return $next($request);
    }
}

绑定:HTTP-POST

如果您使用 HTTP POST 绑定,则需要允许 saml 通过 POST 获取登录请求。

在 web.php 中添加新路由

....
Auth::routes();
Route::post('/postLogin', 'Auth\LoginController@showLoginForm');

您还需要将 csrf 免除添加到 App\Http\Middleware\VerifyCsrfToken

class VerifyCsrfToken extends Middleware { /** * 应排除 CSRF 验证的 URI。 * * @var array */ protected $except = [ 'login/saml', 'logout/saml' ]; }

连接调试

您可以通过 config/saml.php 中的 debug_saml_request 设置启用日志记录。

    // Allow debugging within SamlAuth trait to get SP data  
    // during SAML authentication request
    'debug_saml_request' => true,

确保在您的 .env 文件中将环境日志变量 APP_LOG_LEVEL 设置为 debug。它将记录到 storage/logs/laravel.log