67 lines
2 KiB
PHP
67 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
|
use Illuminate\Support\Facades\Event;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Slides\Saml2\Events\SignedIn;
|
|
use Slides\Saml2\Events\SignedOut;
|
|
|
|
class EventServiceProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* The event listener mappings for the application.
|
|
*
|
|
* @var array<class-string, array<int, class-string>>
|
|
*/
|
|
protected $listen = [];
|
|
|
|
/**
|
|
* Register any events for your application.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function boot()
|
|
{
|
|
Event::listen('Slides\Saml2\Events\SignedIn', function (SignedIn $event) {
|
|
|
|
$messageId = $event->getAuth()->getLastMessageId();
|
|
|
|
$fromCache = Cache::get(config('cache.prefix') . "_saml_message_id_" . $messageId);
|
|
if ($fromCache) {
|
|
logger()->error(
|
|
'SAML MESSAGE ID HANDLER',
|
|
['error' => 'Message ID : ' . $messageId . " replayed !"]
|
|
);
|
|
abort(403);
|
|
} else {
|
|
Cache::put(config('cache.prefix') . "_saml_message_id_" . $messageId, $messageId, 28800);
|
|
// Cache for 8h.
|
|
}
|
|
|
|
$user = $event->getSaml2User();
|
|
$email = $user->getNameId();
|
|
|
|
$lUser = User::where('email', '=', $email)->first();
|
|
|
|
if (!$lUser) {
|
|
abort(403);
|
|
}
|
|
|
|
Auth::guard('backend')->login($lUser);
|
|
session(['sessionIndex' => $user->getSessionIndex()]);
|
|
$lUser->last_connection = now();
|
|
$lUser->saveQuietly();
|
|
});
|
|
|
|
//phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
|
Event::listen('Slides\Saml2\Events\SignedOut', function (SignedOut $event) {
|
|
Auth::guard('backend')->logout();
|
|
Session::save();
|
|
});
|
|
}
|
|
}
|