48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Services\HxbOtpRelayService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
|
|
class HxbOtpRelayController extends Controller
|
|
{
|
|
public function create(Request $request, HxbOtpRelayService $service): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'client_run_id' => ['required', 'string', 'regex:/^[A-Za-z0-9-]{8,64}$/'],
|
|
]);
|
|
|
|
return response()->json(
|
|
$service->create((string) $data['client_run_id']),
|
|
201,
|
|
['Cache-Control' => 'private, no-store'],
|
|
);
|
|
}
|
|
|
|
public function consume(
|
|
Request $request,
|
|
string $clientRunId,
|
|
HxbOtpRelayService $service,
|
|
): JsonResponse {
|
|
$bearer = trim((string) $request->bearerToken());
|
|
$result = $service->consume($clientRunId, $bearer);
|
|
$status = (string) ($result['status'] ?? 'not_found');
|
|
if ($status === HxbOtpRelayService::STATUS_CONSUMED && isset($result['code'])) {
|
|
return response()->json($result, 200, ['Cache-Control' => 'private, no-store']);
|
|
}
|
|
if ($status === HxbOtpRelayService::STATUS_PENDING) {
|
|
return response()->json($result, 202, ['Cache-Control' => 'private, no-store']);
|
|
}
|
|
if ($status === HxbOtpRelayService::STATUS_EXPIRED) {
|
|
return response()->json($result, 410, ['Cache-Control' => 'private, no-store']);
|
|
}
|
|
|
|
return response()->json(['status' => $status], $status === 'not_found' ? 404 : 410, [
|
|
'Cache-Control' => 'private, no-store',
|
|
]);
|
|
}
|
|
}
|