feat: Implement refresh token system for client authentication, including token creation, rotation, and revocation.

This commit is contained in:
pbao9
2026-03-16 10:23:11 +07:00
parent 44f4f64b37
commit c2e90696b6
4 changed files with 232 additions and 41 deletions
+87 -7
View File
@@ -2,13 +2,18 @@
namespace App\Services\Api\Client;
use App\Models\RefreshToken;
use App\Repositories\Api\Client\ClientRepositoryInterface;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Tymon\JWTAuth\Facades\JWTAuth;
class ClientAuthService
{
// Refresh token lifetime in days
const REFRESH_TOKEN_TTL_DAYS = 30;
protected $clientRepository;
public function __construct(ClientRepositoryInterface $clientRepository)
@@ -17,7 +22,7 @@ class ClientAuthService
}
/**
* Xác thực khách hàng và tạo token
* Xác thực khách hàng và tạo access + refresh token
*/
public function authenticateClient(array $credentials): array
{
@@ -36,20 +41,23 @@ class ClientAuthService
3 => 'bị khoá',
default => 'vô hiệu hoá',
};
throw ValidationException::withMessages([
'email' => ["Tài khoản của bạn đã {$statusLabel}, vui lòng liên hệ hotline."],
]);
}
$refreshToken = $this->createRefreshToken($client->id);
return [
'client' => $client,
'token' => $token,
'client' => $client,
'token' => $token,
'refresh_token' => $refreshToken,
];
}
/**
* Đăng ký khách hàng mới và tạo token
* Đăng ký khách hàng mới và tạo access + refresh token
*/
public function registerClient(array $data): array
{
@@ -61,10 +69,82 @@ class ClientAuthService
]);
$token = JWTAuth::fromUser($client);
$refreshToken = $this->createRefreshToken($client->id);
return [
'client' => $client,
'token' => $token,
'client' => $client,
'token' => $token,
'refresh_token' => $refreshToken,
];
}
/**
* Tạo refresh token mới, lưu hash vào DB, trả về raw token.
*/
public function createRefreshToken(int $customerId): string
{
$raw = Str::random(64);
$hash = hash('sha256', $raw);
RefreshToken::create([
'customer_id' => $customerId,
'token' => $hash,
'expires_at' => now()->addDays(self::REFRESH_TOKEN_TTL_DAYS),
]);
return $raw;
}
/**
* Xác minh refresh token, xoay vòng (rotation), trả về access token mới + refresh token mới.
* Ném ValidationException nếu token không hợp lệ hoặc đã hết hạn.
*/
public function rotateRefreshToken(string $rawToken): array
{
$hash = hash('sha256', $rawToken);
$record = RefreshToken::where('token', $hash)->first();
if (!$record || $record->isExpired()) {
// Xoá token cũ nếu tồn tại nhưng hết hạn
$record?->delete();
throw ValidationException::withMessages([
'refresh_token' => ['Refresh token không hợp lệ hoặc đã hết hạn.'],
]);
}
$customer = $record->customer;
if (!$customer || (int)$customer->status !== 1) {
$record->delete();
throw ValidationException::withMessages([
'refresh_token' => ['Tài khoản không hợp lệ.'],
]);
}
// Rotation: xoá token cũ
$record->delete();
// Tạo access token mới
$newAccessToken = JWTAuth::fromUser($customer);
// Tạo refresh token mới
$newRefreshToken = $this->createRefreshToken($customer->id);
return [
'client' => $customer,
'token' => $newAccessToken,
'refresh_token' => $newRefreshToken,
];
}
/**
* Huỷ refresh token khi đăng xuất.
*/
public function revokeRefreshToken(string $rawToken): void
{
$hash = hash('sha256', $rawToken);
RefreshToken::where('token', $hash)->delete();
}
}