Package Data | |
---|---|
Maintainer Username: | nirajp |
Maintainer Contact: | aacotroneo@gmail.com (aacotroneo) |
Package Create Date: | 2016-10-06 |
Package Last Update: | 2019-07-16 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-19 03:16:30 |
Package Statistics | |
---|---|
Total Downloads: | 15,736 |
Monthly Downloads: | 42 |
Daily Downloads: | 1 |
Total Stars: | 2 |
Total Watchers: | 2 |
Total Forks: | 3 |
Total Open Issues: | 0 |
Forked from aacotroneo/laravel-saml2. Idea is to enhance the existing code to support configuration for multiple IDPs.
A 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 requests.
To install Saml2 as a Composer package to be used with Laravel 5, simply add this to your composer.json:
"nirajp/laravel-saml2": "*"
..and run composer update
. Once it's installed, you can register the service provider in app/config/app.php
in the providers
array. If you want, you can add the alias saml2:
'providers' => [
...
'Aacotroneo\Saml2\Saml2ServiceProvider',
]
'alias' => [
...
'Saml2' => 'Aacotroneo\Saml2\Facades\Saml2Auth',
]
Then publish the config files with php artisan vendor:publish
. This will add the files app/config/saml2_settings.php
& app/config/saml2/test_idp_settings.php
.
The test_idp_settings.php config is handled almost directly by OneLogin so you may get further references there, but will cover here what's really necessary. There are some other config about routes you may want to check, they are pretty strightforward.
Define names of all the IDPs you want to configure in saml2_settings.php. Keep 'test' as the first IDP, and add real IDPs after that.
'idpNames' => ['test', 'myidp1', 'myidp2'],
You will need to create a separate configuration file for each IDP under app/config/saml2
folder. e.g. myidp1_idp_settings.php
. You can use test_idp_settings.php
as the starting point.
The only real difference between this config and the one that OneLogin uses, is that the SP entityId, assertionConsumerService url and singleLogoutService URL are injected by the library. They are taken from routes 'saml_metadata', 'saml_acs' and 'saml_sls' respectively.
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'
);
You can check that metadata if you actually navigate to 'http://laravel_url/saml/metadata'
When you want your user to login, just call Saml2Auth::login()
or redirect to route 'saml2_login'. Just remember that it does not use any session storage, so if you ask it to login it will redirect to the IDP whether the user is logged in or not. For example, you can change your authentication middleware.
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);
};
The Saml2::login will redirect the user to the IDP and will came back to an endpoint the library serves at /saml2/acs. That will process the response and fire an event when ready. The next step for you is to handle that event. You just need to login the user or refuse.
Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function (Saml2LoginEvent $event) {
$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);
});
Now there are two ways the user can log out.
For case 1 call Saml2Auth::logout();
or redirect the user to the route 'saml_logout' which does just that. Do not close the session inmediately as you need to receive a response confirmation from the IDP (redirection). That response will be handled by the library at /saml2/sls and will fire an event for you to complete the operation.
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)
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.