61 lines
1.6 KiB
PHP
61 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
|
|
class ApiTokenLifetime
|
|
{
|
|
public static function minutesForPortal(string $portal): int
|
|
{
|
|
$key = $portal . '_lifetime';
|
|
|
|
return (int) config('api_tokens.' . $key, config('session.lifetime', 120));
|
|
}
|
|
|
|
public static function expiresAtForPortal(string $portal): Carbon
|
|
{
|
|
return Carbon::now()->addMinutes(self::minutesForPortal($portal));
|
|
}
|
|
|
|
public static function loginMeta(string $portal): array
|
|
{
|
|
$expiresAt = self::expiresAtForPortal($portal);
|
|
|
|
return [
|
|
'token_expires_at' => $expiresAt->toIso8601String(),
|
|
'token_lifetime_minutes' => self::minutesForPortal($portal),
|
|
];
|
|
}
|
|
|
|
public static function isExpired(Authenticatable $user, string $portal): bool
|
|
{
|
|
if (! method_exists($user, 'token')) {
|
|
return false;
|
|
}
|
|
|
|
$token = $user->token();
|
|
if (! $token || ! $token->created_at) {
|
|
return false;
|
|
}
|
|
|
|
return Carbon::now()->greaterThanOrEqualTo(
|
|
Carbon::parse($token->created_at)->addMinutes(self::minutesForPortal($portal))
|
|
);
|
|
}
|
|
|
|
public static function revokeActiveTokens(Authenticatable $user): void
|
|
{
|
|
if (! method_exists($user, 'tokens')) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$user->tokens()->where('revoked', false)->update(['revoked' => true]);
|
|
} catch (\Throwable $e) {
|
|
// Login should not fail if revocation fails.
|
|
}
|
|
}
|
|
}
|