registerPolicies(); Auth::viaRequest('api-token', function (Request $request) { // https://stackoverflow.com/questions/25838183/what-is-the-oauth-2-0-bearer-token-exactly/25843058 /** * Un token devrais ĂȘtre fourni pour chaque requete apres authentification * api_token param est requis par Auth */ $apiToken = $request->api_token; if (is_null($apiToken)) { throw new ApiErrorException(ApiError::apiAuthFailed(), 401); } /** ** All ApiV2 routes must pass Brand model to controller action since a Brand owns ** a service token, otherwise the api cannot be called. ** This hack is a custom binder instead of passing ** this to route as a parameter */ if (!($route = Helper::getRoute($request))) { throw new ApiErrorException(ApiError::apiAuthFailed(), 401); } try { $service = Service::getFromApiToken($apiToken); } catch (ModelNotFoundException $e) { 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); }); } }