2023-12-15 15:53:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
|
|
|
|
|
use App\Listeners\PasswordHasReset;
|
2026-04-17 16:50:06 +02:00
|
|
|
use App\Models\User;
|
2023-12-15 15:53:55 +00:00
|
|
|
use Illuminate\Auth\Events\PasswordReset;
|
|
|
|
|
use Illuminate\Auth\Events\Registered;
|
|
|
|
|
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
|
|
|
|
|
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
|
2026-04-17 16:50:06 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
|
use Slides\Saml2\Events\SignedIn;
|
2023-12-15 15:53:55 +00:00
|
|
|
|
|
|
|
|
class EventServiceProvider extends ServiceProvider
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* The event to listener mappings for the application.
|
|
|
|
|
*
|
|
|
|
|
* @var array<class-string, array<int, class-string>>
|
|
|
|
|
*/
|
|
|
|
|
protected $listen = [
|
|
|
|
|
Registered::class => [
|
|
|
|
|
SendEmailVerificationNotification::class,
|
|
|
|
|
],
|
|
|
|
|
PasswordReset::class => [
|
|
|
|
|
PasswordHasReset::class
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if events and listeners should be automatically discovered.
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function shouldDiscoverEvents(): bool
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2026-04-17 16:50:06 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register any events for your application.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function boot(): void
|
|
|
|
|
{
|
|
|
|
|
Event::listen('Slides\Saml2\Events\SignedIn', function (SignedIn $event) {
|
|
|
|
|
$messageId = $event->getAuth()->getLastMessageId();
|
|
|
|
|
|
|
|
|
|
if (Cache::get(config('cache.prefix') . "_saml_message_id_" . $messageId)) {
|
|
|
|
|
logger()->error(
|
|
|
|
|
'SAML MESSAGE ID HANDLER',
|
|
|
|
|
['error' => 'Message ID : ' . $messageId . " replayed !"]
|
|
|
|
|
);
|
|
|
|
|
abort(403);
|
|
|
|
|
} else {
|
|
|
|
|
// Cache for 8h.
|
|
|
|
|
Cache::put(config('cache.prefix') . "_saml_message_id_" . $messageId, $messageId, 28800);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$user = $event->getSaml2User();
|
|
|
|
|
$email = $user->getNameId();
|
|
|
|
|
$lUser = User::query()->where('email', $email)->first();
|
|
|
|
|
|
|
|
|
|
if (!$lUser) {
|
|
|
|
|
abort(403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Auth::guard('backend')->login($lUser);
|
|
|
|
|
session(['sessionIndex' => $user->getSessionIndex()]);
|
|
|
|
|
$lUser->last_connection = now();
|
|
|
|
|
$lUser->saveQuietly();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Event::listen('Slides\Saml2\Events\SignedOut', function () {
|
|
|
|
|
Auth::guard('backend')->logout();
|
|
|
|
|
Session::save();
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-12-15 15:53:55 +00:00
|
|
|
}
|