fix: auth events

This commit is contained in:
HDVinnie
2023-12-11 20:52:19 -05:00
parent 799fa3548a
commit 9656dfe396
5 changed files with 28 additions and 107 deletions

View File

@@ -1,46 +0,0 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Listeners;
use App\Models\FailedLoginAttempt;
use App\Models\Group;
use App\Notifications\FailedLogin;
use Exception;
use Illuminate\Auth\Events\Failed;
class FailedLoginListener
{
/**
* Handle the event.
*
* @throws Exception
*/
public function handle(Failed $event): void
{
$bannedGroup = cache()->rememberForever('banned_group', fn () => Group::where('slug', '=', 'banned')->pluck('id'));
if ($event->user instanceof \App\Models\User
&& $event->user->group_id !== $bannedGroup[0]) {
FailedLoginAttempt::record(
$event->user,
request()->input('username'),
request()->ip()
);
$event->user->notify(new FailedLogin(
request()->ip()
));
}
}
}

View File

@@ -1,33 +0,0 @@
<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Listeners;
use App\Models\User;
use Illuminate\Auth\Events\Login;
use Illuminate\Support\Carbon;
class LoginListener
{
/**
* Handle the event.
*/
public function handle(Login $event): void
{
// Update Login Timestamp
if ($event->user instanceof User) {
$event->user->last_login = Carbon::now();
$event->user->save();
}
}
}

View File

@@ -31,15 +31,6 @@ class FailedLoginAttempt extends Model
'ip_address',
];
public static function record(?User $user, string $username, string $ip): mixed
{
return static::create([
'user_id' => $user?->id,
'username' => $username,
'ip_address' => $ip,
]);
}
/**
* Belongs To A User.
*

View File

@@ -45,14 +45,6 @@ class EventServiceProvider extends ServiceProvider
* @var array<string, array<int, string>>
*/
protected $listen = [
// Auth System
Login::class => [
LoginListener::class,
],
Failed::class => [
FailedLoginListener::class,
],
// Achievements System
Unlocked::class => [
AchievementUnlocked::class,

View File

@@ -6,11 +6,14 @@ use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use App\Models\FailedLoginAttempt;
use App\Models\Group;
use App\Models\User;
use App\Notifications\FailedLogin;
use App\Services\Unit3dAnnounce;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Support\Facades\RateLimiter;
@@ -149,42 +152,56 @@ class FortifyServiceProvider extends ServiceProvider
$user = User::query()->where('username', $request->username)->first();
if ($user && !Hash::check($request->password, $user->password)) {
FailedLoginAttempt::create([
'user_id' => $user->id,
'username' => $request->username,
'ip_address' => $request->ip(),
]);
$user->notify(new FailedLogin(
$request->ip()
));
throw ValidationException::withMessages([
Fortify::username() => __('auth.failed'),
]);
}
if ($user && Hash::check($request->password, $user->password)) {
// Check if user is activated
$validatingGroup = cache()->rememberForever('validating_group', fn () => Group::query()->where('slug', '=', 'validating')->pluck('id'));
if ($user->active == 0 || $user->group_id == $validatingGroup[0]) {
if ($user->active == 0 || $user->group_id === $validatingGroup[0]) {
$request->session()->invalidate();
throw ValidationException::withMessages([
Fortify::username() => trans('auth.not-activated'),
Fortify::username() => __('auth.not-activated'),
]);
}
// Check if user is banned
$bannedGroup = cache()->rememberForever('banned_group', fn () => Group::query()->where('slug', '=', 'banned')->pluck('id'));
if ($user->group_id == $bannedGroup[0]) {
if ($user->group_id === $bannedGroup[0]) {
$request->session()->invalidate();
throw ValidationException::withMessages([
Fortify::username() => trans('auth.banned'),
Fortify::username() => __('auth.banned'),
]);
}
// Update Login Timestamp
$user->last_login = Carbon::now();
$user->save();
return $user;
}
return false;
});
RateLimiter::for('login', function (Request $request) {
$username = (string) $request->username;
return Limit::perMinute(5)->by($username.$request->ip());
});
RateLimiter::for('login', fn(Request $request) => Limit::perMinute(5)->by($request->ip()));
RateLimiter::for('two-factor', fn (Request $request) => Limit::perMinute(5)->by($request->session()->get('login.id')));
}
}