Changeset View
Changeset View
Standalone View
Standalone View
src/app/AuthAttempt.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
use Iatstuti\Database\Support\NullableFields; | use Dyrynda\Database\Support\NullableFields; | ||||
use App\Traits\UuidStrKeyTrait; | use App\Traits\UuidStrKeyTrait; | ||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
/** | /** | ||||
* The eloquent definition of an AuthAttempt. | * The eloquent definition of an AuthAttempt. | ||||
* | * | ||||
* An AuthAttempt represents an authenticaton attempt from an application/client. | * An AuthAttempt represents an authenticaton attempt from an application/client. | ||||
*/ | */ | ||||
class AuthAttempt extends Model | class AuthAttempt extends Model | ||||
{ | { | ||||
use NullableFields; | use NullableFields; | ||||
use UuidStrKeyTrait; | use UuidStrKeyTrait; | ||||
// No specific reason | // No specific reason | ||||
public const REASON_NONE = ''; | public const REASON_NONE = ''; | ||||
// Password mismatch | // Password mismatch | ||||
public const REASON_PASSWORD = 'password'; | public const REASON_PASSWORD = 'password'; | ||||
// Geolocation whitelist mismatch | // Geolocation whitelist mismatch | ||||
public const REASON_GEOLOCATION = 'geolocation'; | public const REASON_GEOLOCATION = 'geolocation'; | ||||
private const STATUS_ACCEPTED = 'ACCEPTED'; | private const STATUS_ACCEPTED = 'ACCEPTED'; | ||||
private const STATUS_DENIED = 'DENIED'; | private const STATUS_DENIED = 'DENIED'; | ||||
protected $nullable = [ | protected $nullable = ['reason']; | ||||
'reason', | |||||
]; | |||||
/** @var array<int, string> The attributes that are mass assignable */ | |||||
protected $fillable = [ | protected $fillable = [ | ||||
'ip', | 'ip', | ||||
'user_id', | 'user_id', | ||||
'status', | 'status', | ||||
'reason', | 'reason', | ||||
'expires_at', | 'expires_at', | ||||
'last_seen', | 'last_seen', | ||||
]; | ]; | ||||
/** @var array<string, string> The attributes that should be cast */ | |||||
protected $casts = [ | protected $casts = [ | ||||
'expires_at' => 'datetime', | 'expires_at' => 'datetime', | ||||
'last_seen' => 'datetime' | 'last_seen' => 'datetime' | ||||
]; | ]; | ||||
/** | /** | ||||
* Prepare a date for array / JSON serialization. | * Prepare a date for array / JSON serialization. | ||||
* | * | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | class AuthAttempt extends Model | ||||
/** | /** | ||||
* Notify the user of this authentication attempt. | * Notify the user of this authentication attempt. | ||||
* | * | ||||
* @return bool false if there was no means to notify | * @return bool false if there was no means to notify | ||||
*/ | */ | ||||
public function notify(): bool | public function notify(): bool | ||||
{ | { | ||||
return \App\CompanionApp::notifyUser($this->user_id, ['token' => $this->id]); | return CompanionApp::notifyUser($this->user_id, ['token' => $this->id]); | ||||
} | } | ||||
/** | /** | ||||
* Notify the user and wait for a confirmation. | * Notify the user and wait for a confirmation. | ||||
*/ | */ | ||||
private function notifyAndWait() | private function notifyAndWait() | ||||
{ | { | ||||
if (!$this->notify()) { | if (!$this->notify()) { | ||||
Show All 31 Lines | class AuthAttempt extends Model | ||||
/** | /** | ||||
* Record a new authentication attempt or update an existing one. | * Record a new authentication attempt or update an existing one. | ||||
* | * | ||||
* @param \App\User $user The user attempting to authenticate. | * @param \App\User $user The user attempting to authenticate. | ||||
* @param string $clientIP The ip the authentication attempt is coming from. | * @param string $clientIP The ip the authentication attempt is coming from. | ||||
* | * | ||||
* @return \App\AuthAttempt | * @return \App\AuthAttempt | ||||
*/ | */ | ||||
public static function recordAuthAttempt(\App\User $user, $clientIP) | public static function recordAuthAttempt(User $user, $clientIP) | ||||
{ | { | ||||
$authAttempt = \App\AuthAttempt::where('ip', $clientIP)->where('user_id', $user->id)->first(); | $authAttempt = AuthAttempt::where('ip', $clientIP)->where('user_id', $user->id)->first(); | ||||
if (!$authAttempt) { | if (!$authAttempt) { | ||||
$authAttempt = new \App\AuthAttempt(); | $authAttempt = new AuthAttempt(); | ||||
$authAttempt->ip = $clientIP; | $authAttempt->ip = $clientIP; | ||||
$authAttempt->user_id = $user->id; | $authAttempt->user_id = $user->id; | ||||
} | } | ||||
$authAttempt->last_seen = Carbon::now(); | $authAttempt->last_seen = Carbon::now(); | ||||
$authAttempt->save(); | $authAttempt->save(); | ||||
return $authAttempt; | return $authAttempt; | ||||
Show All 24 Lines |