单一登录使用Laravel上的SimpleSamlPhp包装器

时间:2016-07-27 07:03:12

标签: php laravel-5.2 simplesamlphp

在我的laravel应用程序中实现单点登录。我决定使用这个插件https://github.com/aacotroneo/laravel-saml2,它基本上是着名的SimpleSamlPhp的包装器。

我通过编辑器下载了代码,并根据给定的信息Remember that you don't need to implement those routes, but you'll need to add them to your IDP configuration. For example, if you use simplesamlphp, add the following to /metadata/sp-remote.php

$metadata['http://laravel_url/saml/metadata'] = array(
 'AssertionConsumerService' => 'http://laravel_url/saml/acs',
 'SingleLogoutService' => 'http://laravel_url/saml/sls',
 //the following two affect what the $Saml2user->getUserId() will return
 'NameIDFormat' => 'urn:oasis:names:tc:SAML:2.0:nameid-format:persistent',
 'simplesaml.nameidattribute' => 'uid'  
);

我找不到metadata/sp-remote.php,任何想法?就http://laravel_url/saml/acs而言,我是否需要在服务器上部署saml?因为目前插件代码在laravel核心架构代码层次结构中位于vendors

2 个答案:

答案 0 :(得分:8)

首先是一些背景:

任何SAML交互都有两个部分 - 身份提供者(" IDP")和服务提供者(" SP")。如果您愿意,IDP是主身份验证器,各种应用程序(SP)连接到该身份验证器。

这个想法是用户访问您的应用程序,该应用程序又作为服务提供商与身份提供商进行通信以获取您的凭据。而且由于多个应用/ SP会对同一个IDP进行竞争,因此您可以获得单点登录的好处。

在设置阶段,在SP和IDP之间交换元数据配置以在它们之间建立信任。这不是用户级数据 - 它的应用程序级数据,允许他们交谈。

行。那么现在问你的问题:

您正在使用的软件包允许您的Laravel应用程序与IDP通信,但在此之前您需要交换一些元数据。您应用的元数据是上面的代码段。这需要进入 IDP 配置,您可以在此处找到metadata/sp-remote(或更准确地说metadata/saml20-sp-remote,您可以将其粘贴到此处。

如果你还没有这样做,我建议在这里使用[https://simplesamlphp.org/docs/stable/][1]作为IDP,因为Laravel套件几乎可以开箱即用。

最后一个提示:如果您使用的是SAML2,那么我发现您需要更改元数据键以引用saml2而不是上面的saml。即$metadata['http://laravel_url/saml2/metadata']而非$metadata['http://laravel_url/saml/metadata']

答案 1 :(得分:1)

我希望这会帮助其他人。我在saml2_settings.php文件夹中添加了config

更新路线:

'logoutRoute' => '/logout',
'loginRoute' => '/homepage',
'errorRoute' => '/error',

更新了x509cert(publickey.cer)和privateKey

更新了'entityId',添加了元数据xml的URL。 更新了singleLogoutServicesaml2_settings.php文件中其余的所需详细信息。

添加了两个侦听器 1)登录事件 2)退出事件

像这样更新路线文件:

\Illuminate\Support\Facades\Event::listen('Aacotroneo\Saml2\Events\Saml2LogoutEvent', function ($event) {
    \Illuminate\Support\Facades\Auth::logout();
    \Illuminate\Support\Facades\Session::save();
    return redirect("login");
});

\Illuminate\Support\Facades\Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function (\Aacotroneo\Saml2\Events\Saml2LoginEvent $event) {

    $user = $event->getSaml2User();
    $userData = [
        'id' => $user->getUserId(),
        'attributes' => $user->getAttributes(),
        'assertion' => $user->getRawSamlAssertion()
    ];


      // add the login for auto login based on your settings
    /// REDIRECT the user to homepage
    }
});