Replace Rappasoft\LaravelAuthenticationLog by forked package

This commit is contained in:
Bubka 2024-04-20 18:10:30 +02:00
parent 7322184016
commit 11ceb52286
10 changed files with 403 additions and 393 deletions

View File

@ -1,30 +0,0 @@
<?php
namespace App\Models\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog;
use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable as TraitsAuthenticationLoggable;
trait AuthenticationLoggable
{
use TraitsAuthenticationLoggable;
public function authentications()
{
return $this->morphMany(AuthenticationLog::class, 'authenticatable')->latest('id');
}
/**
* Get authentications for the provided timespan (in month)
*/
public function authenticationsByPeriod(int $period = 1)
{
$from = Carbon::now()->subMonths($period);
return $this->authentications->filter(function (AuthenticationLog $authentication) use ($from) {
return $authentication->login_at >= $from || $authentication->logout_at >= $from;
});
}
}

View File

@ -2,8 +2,8 @@
namespace App\Models; namespace App\Models;
use App\Models\Traits\AuthenticationLoggable;
use App\Models\Traits\WebAuthnManageCredentials; use App\Models\Traits\WebAuthnManageCredentials;
use Bubka\LaravelAuthenticationLog\Traits\AuthenticationLoggable;
use Illuminate\Auth\Events\PasswordReset; use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Auth\Notifications\ResetPassword; use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Contracts\Translation\HasLocalePreference; use Illuminate\Contracts\Translation\HasLocalePreference;
@ -41,6 +41,21 @@ use Laravel\Passport\HasApiTokens;
* @property-read int|null $twofaccounts_count * @property-read int|null $twofaccounts_count
* @property-read \Illuminate\Database\Eloquent\Collection|\Laragear\WebAuthn\Models\WebAuthnCredential[] $webAuthnCredentials * @property-read \Illuminate\Database\Eloquent\Collection|\Laragear\WebAuthn\Models\WebAuthnCredential[] $webAuthnCredentials
* @property-read int|null $web_authn_credentials_count * @property-read int|null $web_authn_credentials_count
* @property string|null $oauth_id
* @property string|null $oauth_provider
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Bubka\LaravelAuthenticationLog\Models\AuthenticationLog> $authentications
* @property-read int|null $authentications_count
* @property-read \Bubka\LaravelAuthenticationLog\Models\AuthenticationLog|null $latestAuthentication
* @method static \Illuminate\Database\Eloquent\Builder|User admins()
* @method \Illuminate\Support\Carbon|null latestAuthentication()
* @method \Illuminate\Support\Carbon|null lastLoginAt()
* @method \Illuminate\Support\Carbon|null lastSuccessfulLoginAt()
* @method \Illuminate\Support\Carbon|null lastLoginIp()
* @method \Illuminate\Support\Carbon|null lastSuccessfulLoginIp()
* @method \Illuminate\Support\Carbon|null previousLoginAt()
* @method \Illuminate\Support\Carbon|null previousLoginIp()
* @method \Illuminate\Support\Collection<int, \Bubka\LaravelAuthenticationLog\Models\AuthenticationLog> authenticationsByPeriod()
* @mixin \Eloquent
*/ */
class User extends Authenticatable implements HasLocalePreference, WebAuthnAuthenticatable class User extends Authenticatable implements HasLocalePreference, WebAuthnAuthenticatable
{ {

View File

@ -7,7 +7,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
use Jenssegers\Agent\Agent; use Jenssegers\Agent\Agent;
use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog; use Bubka\LaravelAuthenticationLog\Models\AuthenticationLog;
class SignedInWithNewDevice extends Notification implements ShouldQueue class SignedInWithNewDevice extends Notification implements ShouldQueue
{ {
@ -22,6 +22,9 @@ class SignedInWithNewDevice extends Notification implements ShouldQueue
*/ */
protected $agent; protected $agent;
/**
* Create a new SignedInWithNewDevice instance
*/
public function __construct(AuthenticationLog $authenticationLog) public function __construct(AuthenticationLog $authenticationLog)
{ {
$this->authenticationLog = $authenticationLog; $this->authenticationLog = $authenticationLog;
@ -29,11 +32,17 @@ class SignedInWithNewDevice extends Notification implements ShouldQueue
$this->agent->setUserAgent($authenticationLog->user_agent); $this->agent->setUserAgent($authenticationLog->user_agent);
} }
/**
*
*/
public function via($notifiable) public function via($notifiable)
{ {
return $notifiable->notifyAuthenticationLogVia(); return $notifiable->notifyAuthenticationLogVia();
} }
/**
* Wrap the notification to a mail envelop
*/
public function toMail($notifiable) public function toMail($notifiable)
{ {
return (new MailMessage()) return (new MailMessage())

View File

@ -35,11 +35,20 @@
"laravel/tinker": "^2.8", "laravel/tinker": "^2.8",
"laravel/ui": "^4.2", "laravel/ui": "^4.2",
"paragonie/constant_time_encoding": "^2.6", "paragonie/constant_time_encoding": "^2.6",
"rappasoft/laravel-authentication-log": "^4.0",
"socialiteproviders/manager": "^4.4", "socialiteproviders/manager": "^4.4",
"spatie/eloquent-sortable": "^4.0.1", "spatie/eloquent-sortable": "^4.0.1",
"spomky-labs/otphp": "^11.0" "spomky-labs/otphp": "^11.0",
"bubka/laravel-authentication-log": "@dev"
}, },
"repositories": [
{
"type": "path",
"url": "../packages/bubka/laravel-authentication-log",
"options": {
"symlink": true
}
}
],
"require-dev": { "require-dev": {
"barryvdh/laravel-ide-helper": "^2.13", "barryvdh/laravel-ide-helper": "^2.13",
"brianium/paratest": "^7.3", "brianium/paratest": "^7.3",

687
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -13,14 +13,16 @@ return [
'login' => \Illuminate\Auth\Events\Login::class, 'login' => \Illuminate\Auth\Events\Login::class,
'failed' => \Illuminate\Auth\Events\Failed::class, 'failed' => \Illuminate\Auth\Events\Failed::class,
'logout' => \Illuminate\Auth\Events\Logout::class, 'logout' => \Illuminate\Auth\Events\Logout::class,
'logout-other-devices' => \Illuminate\Auth\Events\OtherDeviceLogout::class, // 'logout-other-devices' => \Illuminate\Auth\Events\OtherDeviceLogout::class,
// 'proxyUserAccess' => \App\Events\VisitedByProxyUser::class,
], ],
'listeners' => [ 'listeners' => [
'login' => \Rappasoft\LaravelAuthenticationLog\Listeners\LoginListener::class, 'login' => \Bubka\LaravelAuthenticationLog\Listeners\LoginListener::class,
'failed' => \Rappasoft\LaravelAuthenticationLog\Listeners\FailedLoginListener::class, 'failed' => \Bubka\LaravelAuthenticationLog\Listeners\FailedLoginListener::class,
'logout' => \Rappasoft\LaravelAuthenticationLog\Listeners\LogoutListener::class, 'logout' => \Bubka\LaravelAuthenticationLog\Listeners\LogoutListener::class,
'logout-other-devices' => \Rappasoft\LaravelAuthenticationLog\Listeners\OtherDeviceLogoutListener::class, // 'logout-other-devices' => \Bubka\LaravelAuthenticationLog\Listeners\OtherDeviceLogoutListener::class,
// 'proxyUserAccess' => \App\Listeners\VisitedByProxyUserListener::class,
], ],
'notifications' => [ 'notifications' => [
@ -42,7 +44,7 @@ return [
'location' => false, 'location' => false,
// The Notification class to send // The Notification class to send
'template' => \Rappasoft\LaravelAuthenticationLog\Notifications\FailedLogin::class, 'template' => \Bubka\LaravelAuthenticationLog\Notifications\FailedLogin::class,
], ],
], ],

View File

@ -18,6 +18,7 @@ return new class extends Migration
$table->timestamp('logout_at')->nullable(); $table->timestamp('logout_at')->nullable();
$table->boolean('cleared_by_user')->default(false); $table->boolean('cleared_by_user')->default(false);
$table->json('location')->nullable(); $table->json('location')->nullable();
$table->string('auth_method', 40)->nullable();
}); });
} }

View File

@ -11,4 +11,6 @@ parameters:
analyse: analyse:
- app/Protobuf/* - app/Protobuf/*
ignoreErrors: ignoreErrors:
-
message: '#.*geoip.*#'
checkMissingIterableValueType: false checkMissingIterableValueType: false

View File

@ -19,7 +19,7 @@ use Laravel\Passport\Http\Controllers\PersonalAccessTokenController;
// use App\Models\User; // use App\Models\User;
// use App\Notifications\SignedInWithNewDevice; // use App\Notifications\SignedInWithNewDevice;
// use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog; // use Bubka\LaravelAuthenticationLog\Models\AuthenticationLog;
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View File

@ -12,6 +12,7 @@ use Illuminate\Http\Request;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Password; use Illuminate\Support\Facades\Password;
@ -522,6 +523,24 @@ class UserManagerControllerTest extends FeatureTestCase
->assertForbidden(); ->assertForbidden();
} }
/**
* @test
*/
public function test_authLog_events_are_listened_by_authLog_listeners()
{
Event::fake();
foreach (config('authentication-log.listeners') as $type => $listenerClass) {
Event::assertListening(
config('authentication-log.events.' . $type),
$listenerClass
);
}
}
/**
* Local feeder because Factory cannot be used here
*/
protected function feedAuthenticationLog() : int protected function feedAuthenticationLog() : int
{ {
// Do not change creation order // Do not change creation order