ldc-crm/app/Providers/AuthServiceProvider.php
Jérémy Munsch 787ee5a4c5 - tag 2.0.1
- Add Questions segementation
- Fix Customer delete Tests
2021-08-11 17:37:59 +02:00

66 lines
2 KiB
PHP

<?php
namespace App\Providers;
use App\Lib\Helper;
use App\Models\Service;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->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)) {
return null;
}
/**
** 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))) {
return null;
}
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;
}
});
}
}