marketredesign / laravel-saml2
一个基于OneLogin工具包的Laravel包,用于Saml2集成作为SP(服务提供者),支持多个IDP,源自aacotroneo/laravel-saml2。
Requires
- php: >=5.4.0
- ext-openssl: *
- illuminate/support: >=5.0.0
- onelogin/php-saml: ^3.0.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~4.0
README
从aacotroneo/laravel-saml2分支而来。这个分支旨在提供对多个IDP的支持。它基于nirajp的分支,由@nirajp创建。
Laravel 5 - Saml2
[检查https://github.com/marketredesign/laravel-saml2/tree/remove_mcrypt以获取无mcrypt版本 ]
一个基于OneLogin工具包的Laravel包,用于Saml2集成作为SP(服务提供者),比simplesamlphp SP更轻便,安装也更容易!它不需要单独的路由或会话存储即可工作!
该库的目标是尽可能简单。我们不会与Laravel用户、认证、会话等纠缠。我们更喜欢限制自己只完成具体任务。要求用户在IDP上进行认证并处理响应。SLO请求也是同样的情况。
安装 - Composer
您可以通过composer安装此包
composer require marketredesign/laravel-saml2
如果您正在使用Laravel 5.5及以上版本,服务提供者将自动注册。
对于Laravel的旧版本(<5.5),您必须将服务提供者和别名添加到config/app.php
'providers' => [ ... Aacotroneo\Saml2\Saml2ServiceProvider::class, ] 'alias' => [ ... 'Saml2' => Aacotroneo\Saml2\Facades\Saml2Auth::class, ]
然后使用以下命令发布配置文件:php artisan vendor:publish --provider="Aacotroneo\Saml2\Saml2ServiceProvider"
。这将添加文件app/config/saml2_settings.php
和app/config/saml2/test_idp_settings.php
。此配置几乎直接由OneLogin处理,因此您可以在那里找到更多参考资料,但这里将涵盖真正必要的部分。还有一些关于路由的配置您可能想要检查,它们相当直接。
配置
在saml2_settings.php
中定义您要配置的所有IDP的名称。将test
保留为第一个IDP,并在之后添加真实的IDP。您需要在app/config/saml2
文件夹中为每个IDP创建一个单独的配置文件。使用test_idp_settings.php
作为起点。此配置与OneLogin使用的配置之间的唯一真正区别是,SP entityId、断言消费者服务url和单点登出服务url是由库注入的。它们分别来自路由'saml_metadata'、'saml_acs'和'saml_sls'。
请记住,您不需要实现这些路由,但您需要将它们添加到IDP配置中。例如,如果您使用simplesamlphp,请将以下内容添加到/metadata/sp-remote.php
$metadata['http://laravel_url/saml2/metadata'] = array( 'AssertionConsumerService' => 'http://laravel_url/saml2/acs', 'SingleLogoutService' => 'http://laravel_url/saml2/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' );
您可以通过实际导航到'http://laravel_url/saml2/metadata'来检查这些元数据
用法
当您想要用户登录时,只需调用Saml2Auth::login()
或重定向到路由'saml2_login'。只需记住,它不使用任何会话存储,因此如果要求它登录,它将重定向到IDP,无论用户是否已登录。例如,您可以更改您的认证中间件。
public function handle($request, Closure $next) { if ($this->auth->guest()) { if ($request->ajax()) { return response('Unauthorized.', 401); } else { return Saml2::login(URL::full()); //return redirect()->guest('auth/login'); } } return $next($request); }
自Laravel 5.3以来,您可以在app/Exceptions/Handler.php
中更改您的未认证方法。
protected function unauthenticated($request, AuthenticationException $exception) { if ($request->expectsJson()) { return response()->json(['error' => 'Unauthenticated.'], 401); } return Saml2Auth::login(); }
Saml2::login将重定向用户到IDP,并返回到库在/saml2/acs处提供端点。这将处理响应,并在准备就绪时触发事件。您的下一步是处理该事件。您只需要登录用户或拒绝。
Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function (Saml2LoginEvent $event) { $messageId = $event->getSaml2Auth()->getLastMessageId(); // your own code preventing reuse of a $messageId to stop replay attacks $user = $event->getSaml2User(); $userData = [ 'id' => $user->getUserId(), 'attributes' => $user->getAttributes(), 'assertion' => $user->getRawSamlAssertion() ]; $laravelUser = //find user by ID or attribute //if it does not exist create it and go on or show an error message Auth::login($laravelUser); });
认证持久性
注意在会话中实现认证持久性所需的Laravel中间件。
例如,它可以是
# in App\Http\Kernel
protected $middlewareGroups = [
'web' => [
...
],
'api' => [
...
],
'saml' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
],
并在config/saml2_settings.php
中
/**
* which middleware group to use for the saml routes
* Laravel 5.2 will need a group which includes StartSession
*/
'routesMiddleware' => ['saml'],
注销
现在用户有两种注销方式。
- 1 - 通过在您的应用中注销:在这种情况下,您应该首先通知IDP以关闭全局会话。
- 2 - 通过注销全局SSO会话。在这种情况下,IDP将在/saml2/slo端点通知您(已提供)
对于情况1,调用Saml2Auth::logout();
或将用户重定向到'saml_logout'路由,该路由会执行相同的操作。不要立即关闭会话,因为您需要从IDP接收响应确认(重定向)。该响应将由库在/saml2/sls处处理,并将触发一个事件以完成操作。
对于情况2,您只会收到事件。情况和2都会收到相同的事件。
请注意,对于情况2,您可能需要手动保存您的会话以使注销生效(因为会话是由中间件保存的,但OneLogin库将在发生之前将您重定向回您的IDP)
Event::listen('Aacotroneo\Saml2\Events\Saml2LogoutEvent', function ($event) { Auth::logout(); Session::save(); });
就是这样。请随时提问,提出PR或建议,或打开问题。