Fix Auth service provider to return apiAuthFailed exception on api auth fail

This commit is contained in:
Jérémy Munsch 2021-09-21 12:08:28 +02:00
parent a3b919b1c5
commit 6edb0e34db
No known key found for this signature in database
GPG key ID: 55196BEE445F6049

View file

@ -2,6 +2,8 @@
namespace App\Providers;
use App\Enums\ApiError;
use App\Exceptions\Api\V2\ApiErrorException;
use App\Lib\Helper;
use App\Models\Service;
use Illuminate\Database\Eloquent\ModelNotFoundException;
@ -37,7 +39,7 @@ class AuthServiceProvider extends ServiceProvider
$apiToken = $request->api_token;
if (is_null($apiToken)) {
return null;
throw new ApiErrorException(ApiError::apiAuthFailed(), 401);
}
/**
@ -47,20 +49,22 @@ class AuthServiceProvider extends ServiceProvider
** this to route as a parameter
*/
if (!($route = Helper::getRoute($request))) {
return null;
throw new ApiErrorException(ApiError::apiAuthFailed(), 401);
}
try {
$service = Service::getFromApiToken($apiToken);
$service = $service->load('brand');
app()->bind(Service::class, function () use ($service) {
return $service;
});
$test = $service->authenticateApi($apiToken);
return $test ? $service : null;
} catch (ModelNotFoundException $e) {
return null;
throw new ApiErrorException(ApiError::apiAuthFailed(), 401);
}
$service = $service->load('brand');
app()->bind(Service::class, function () use ($service) {
return $service;
});
if ($service->authenticateApi($apiToken)) {
return $service;
}
throw new ApiErrorException(ApiError::apiAuthFailed(), 401);
});
}
}