39 lines
1.1 KiB
PHP
39 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V2;
|
|
|
|
use App\Enums\ApiError;
|
|
use App\Exceptions\Api\V2\ApiErrorException;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Service;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class ApiController extends Controller
|
|
{
|
|
public function ping()
|
|
{
|
|
return response()->json();
|
|
}
|
|
|
|
public function authenticate(Request $request)
|
|
{
|
|
try {
|
|
if (!$request->bearerToken()) {
|
|
throw new ApiErrorException(ApiError::missingParameter());
|
|
}
|
|
/** @var \App\Models\Service */
|
|
$service = Service::getFromBearerToken($request->bearerToken());
|
|
} catch (ModelNotFoundException $e) {
|
|
throw new ApiErrorException(ApiError::authFailed());
|
|
}
|
|
if (!Hash::check($request->bearer_pwd, $service->bearer_pwd)) {
|
|
throw new ApiErrorException(ApiError::authFailed());
|
|
}
|
|
|
|
$apiToken = $service->generateApiToken();
|
|
$service->saveOrFail();
|
|
return response()->json(['api_token' => $apiToken]);
|
|
}
|
|
}
|