Handle http auth with bearer token

This commit is contained in:
Jérémy Munsch 2021-08-16 18:47:31 +02:00
parent 1aeae30c96
commit 2e2cad554c
5 changed files with 31 additions and 6 deletions

View file

@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api\V2;
use App\Enums\ApiError;
use App\Exceptions\Api\V2\ApiErrorException;
use App\Http\Controllers\Controller;
use App\Lib\Helper;
use App\Models\Service;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\Request;
@ -20,11 +21,12 @@ class ApiController extends Controller
public function authenticate(Request $request)
{
try {
if (!$request->bearerToken()) {
$bearerToken = Helper::getBearerTokenFromRequest($request);
if (!$bearerToken) {
throw new ApiErrorException(ApiError::missingParameter());
}
/** @var \App\Models\Service */
$service = Service::getFromBearerToken($request->bearerToken());
$service = Service::getFromBearerToken($bearerToken);
} catch (ModelNotFoundException $e) {
throw new ApiErrorException(ApiError::authFailed());
}

View file

@ -31,13 +31,13 @@ class ApiResponse
try {
if ($isApiV1Request) {
// Use patched v1 Auth mecanism (without bearer reimplementation)
$bearerToken = $request->bearerToken() ?? $request->api_token;
$bearerToken = Helper::getBearerTokenFromRequest($request) ?? $request->api_token;
$service = Service::getFromBearerToken($bearerToken);
$service->incrementStat();
}
if ($isApiV2Request) {
$bearerToken = $request->bearerToken();
$bearerToken = Helper::getBearerTokenFromRequest($request);
if ($bearerToken) {
$service = Service::getFromBearerToken($bearerToken);
}

View file

@ -27,7 +27,7 @@ class Authenticate extends Middleware
// TODO: check this works
if (Helper::isRequestForApiV1($request)) {
// Use patched v1 Auth mecanism (without bearer reimplementation)
$bearerToken = $request->bearerToken() ?? $request->api_token;
$bearerToken = Helper::getBearerTokenFromRequest($request) ?? $request->api_token;
$brand = Service::getFromBearerToken($bearerToken)->brand;
$request->request->add(['brand_id' => $brand->id]);
$response = $next($request);

View file

@ -2,6 +2,7 @@
namespace App\Http\Middleware;
use App\Lib\Helper;
use App\Models\Service;
use Closure;
use Illuminate\Database\Eloquent\ModelNotFoundException;
@ -18,7 +19,7 @@ class CheckBrand
*/
public function handle(Request $request, Closure $next)
{
$apiToken = $request->bearerToken() ?? $request->api_token;
$apiToken = Helper::getBearerTokenFromRequest($request) ?? $request->api_token;
if ($apiToken === null) {
abort(401, 'Le token d\'authentification est incorrect dfd' . \json_encode($request->all()));

View file

@ -4,6 +4,7 @@ namespace App\Lib;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
/**
* Helper Class
@ -92,4 +93,25 @@ class Helper
}
return static::$packageInfos;
}
/**
* Undocumented function
*
* @param Request $request
* @return string|null
*/
public static function getBearerTokenFromRequest(Request $request): ?string
{
$header = $request->header('Authorization', '');
if (Str::contains($header, 'Bearer ')) {
$auths = \explode(',', $header);
foreach ($auths as $auth) {
if (Str::contains($auth, 'Bearer ')) {
return Str::substr($auth, 7);
}
}
}
return null;
}
}