在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):aacotroneo/laravel-saml2开源软件地址(OpenSource Url):https://github.com/aacotroneo/laravel-saml2开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Status: Not ActiveThis project is no longer maintained. I'd be glad to transfer ownership, or otherwise you can easily replace it by some of the many forks (let me know if someone wants to list theirs here, or some oher library). The library itself shouldn't change much, but there are occational changes needed to keep up with Laravel and PHP version updates Laravel 5 - Saml2A Laravel package for Saml2 integration as a SP (service provider) based on OneLogin toolkit, which is much lighter and easier to install than simplesamlphp SP. It doesn't need separate routes or session storage to work! The aim of this library is to be as simple as possible. We won't mess with Laravel users, auth, session... We prefer to limit ourselves to a concrete task. Ask the user to authenticate at the IDP and process the response. Same case for SLO (Single Logout) requests. Installation - ComposerYou can install the package via composer:
Or manually add this to your composer.json: composer.json "aacotroneo/laravel-saml2": "*" If you are using Laravel 5.5 and up, the service provider will automatically get registered. For older versions of Laravel (<5.5), you have to add the service provider: config/app.php 'providers' => [
...
Aacotroneo\Saml2\Saml2ServiceProvider::class,
] Then publish the config files with The ConfigurationDefine the IDPsDefine names of all the IDPs you want to configure in config/saml2_settings.php 'idpNames' => ['mytestidp1', 'test', 'myidp2'], Configure laravel-saml2 to know about each IDPYou will need to create a separate configuration file for each IDP under Configuration options are not explained in this project as they come from the OneLogin project, please refer there for details. The only real difference between this config and the one that OneLogin uses, is that the SP If you don't specify URLs in the corresponding IDP config optional values, this library provides defaults values. The library creates the If you want to optionally define values in ENV vars instead of the For example, it can be: .env SAML2_mytestidp1_SP_x509="..."
SAML2_mytestidp1_SP_PRIVATEKEY="..."
// Other SAML2_mytestidp1_* values
SAML2_myidp2_SP_x509="..."
SAML2_myidp2_SP_PRIVATEKEY="..."
// Other SAML2_myidp2_* values URLs To Pass to The IDP configurationAs mentioned above, you don't need to implement the SP You can check the actual routes in the metadata, by navigating to If you configure the optional The routes automatically created by the library for each IDP are:
Example: simplesamlphp IDP configurationIf you use simplesamlphp as a test IDP, and your SP metadata url is For example, it can be: /metadata/sp-remote.php $metadata['http(s)://{laravel_url}/mytestidp1/metadata'] = array(
'AssertionConsumerService' => 'http(s)://{laravel_url}/mytestidp1/acs',
'SingleLogoutService' => 'http(s)://{laravel_url}/mytestidp1/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'
); UsageWhen you want your user to login, just redirect to the login route configured for the particular IDP, For example, it can be: App/Http/Middleware/RedirectIfAuthenticated.php public function handle($request, Closure $next)
{
if ($this->auth->guest())
{
if ($request->ajax())
{
return response('Unauthorized.', 401); // Or, return a response that causes client side js to redirect to '/routesPrefix/myIdp1/login'
}
else
{
$saml2Auth = new Saml2Auth(Saml2Auth::loadOneLoginAuthFromIpdConfig('mytestidp1'));
return $saml2Auth->login(URL::full());
}
}
return $next($request);
} Since Laravel 5.3, you can change your unauthenticated method. For example, it can be: App/Exceptions/Handler.php protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson())
{
return response()->json(['error' => 'Unauthenticated.'], 401); // Or, return a response that causes client side js to redirect to '/routesPrefix/myIdp1/login'
}
$saml2Auth = new Saml2Auth(Saml2Auth::loadOneLoginAuthFromIpdConfig('mytestidp1'));
return $saml2Auth->login('/my/redirect/path');
} For login requests that come through redirects to the login route, For example, it can be: config/saml_settings.php
App/Http/Controllers/MyNamespace/MySaml2Controller.php use Aacotroneo\Saml2\Http\Controllers\Saml2Controller;
class MySaml2Controller extends Saml2Controller
{
public function login()
{
$loginRedirect = '...'; // Determine redirect URL
$this->saml2Auth->login($loginRedirect);
}
} After login is called, the user will be redirected to the IDP login page. Then the IDP, which you have configured with an endpoint the library serves, will call back, e.g. For example, it can be: App/Providers/MyEventServiceProvider.php Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function (Saml2LoginEvent $event) {
$messageId = $event->getSaml2Auth()->getLastMessageId();
// Add 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);
}); Auth persistenceBe careful about necessary Laravel middleware for Auth persistence in Session. For example, it can be: App/Http/Kernel.php protected $middlewareGroups = [
'web' => [
...
],
'api' => [
...
],
'saml' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
], config/saml2_settings.php
Log outNow there are two ways the user can log out.
For case 1, call For case 2, you will only receive the event. Both cases 1 and 2 receive the same event. Note that for case 2, you may have to manually save your session to make the logout stick (as the session is saved by middleware, but the OneLogin library will redirect back to your IDP before that happens) For example, it can be: App/Providers/MyEventServiceProvider.php Event::listen('Aacotroneo\Saml2\Events\Saml2LogoutEvent', function ($event) {
Auth::logout();
Session::save();
}); That's it. Feel free to ask any questions, make PR or suggestions, or open Issues. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论