llmbackend/app/Http/Controllers/Api/AuthController.php
2024-12-04 16:41:43 +08:00

89 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api;
use App\Constants\ErrorCode;
use App\Http\Controllers\Controller;
use App\Services\Auth\TokenService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
class AuthController extends Controller
{
/**
* @var TokenService
*/
private readonly TokenService $tokenService;
public function __construct(TokenService $tokenService)
{
$this->tokenService = $tokenService;
}
/**
* 獲取訪問令牌
*
* @param Request $request
* @return JsonResponse
* @throws ValidationException
*/
public function getAccessToken(Request $request): JsonResponse
{
try {
/** @var array{auth_token: string} $validated */
$validated = $request->validate([
'auth_token' => 'required|string|size:64',
]);
/** @var array{client_id: int, expires_at: string}|null $authTokenData */
$authTokenData = $this->tokenService->validateAuthToken($validated['auth_token']);
if (!$authTokenData || !isset($authTokenData['expires_at'])) {
return $this->error(
ErrorCode::TOKEN_INVALID,
'認證令牌無效。'
);
}
if (now()->isAfter($authTokenData['expires_at'])) {
return $this->error(
ErrorCode::TOKEN_EXPIRED,
'認證令牌已過期,請重新獲取。'
);
}
/** @var array{access_token: string, expires_in: int} $result */
$result = $this->tokenService->generateAccessToken($authTokenData);
if (!isset($result['access_token']) || !isset($result['expires_in'])) {
throw new \RuntimeException('生成訪問令牌失敗。');
}
return $this->success([
'access_token' => $result['access_token'],
'expires_in' => $result['expires_in'],
]);
} catch (ValidationException $e) {
return $this->error(
ErrorCode::VALIDATION_ERROR,
ErrorCode::getMessage(ErrorCode::VALIDATION_ERROR),
$e->errors()
);
} catch (\Exception $e) {
Log::error('Error generating access token', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return $this->error(
ErrorCode::SERVER_ERROR,
ErrorCode::getMessage(ErrorCode::SERVER_ERROR)
);
}
}
}