Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| AuthServiceProvider | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |
100.00% |
1 / 1 |
| boot | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Providers; |
| 4 | |
| 5 | use App\Lib\Helper; |
| 6 | use App\Models\Service; |
| 7 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 8 | use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; |
| 9 | use Illuminate\Http\Request; |
| 10 | use Illuminate\Support\Facades\Auth; |
| 11 | |
| 12 | class AuthServiceProvider extends ServiceProvider |
| 13 | { |
| 14 | /** |
| 15 | * The policy mappings for the application. |
| 16 | * |
| 17 | * @var array |
| 18 | */ |
| 19 | protected $policies = []; |
| 20 | |
| 21 | /** |
| 22 | * Register any authentication / authorization services. |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | public function boot(): void |
| 27 | { |
| 28 | // Register policies. |
| 29 | $this->registerPolicies(); |
| 30 | |
| 31 | Auth::viaRequest('api-hmac', function (Request $request) { |
| 32 | |
| 33 | /** |
| 34 | * Un token devrais ĂȘtre fourni pour chaque requete |
| 35 | */ |
| 36 | if (is_null($request->bearer_token)) { |
| 37 | return null; |
| 38 | } |
| 39 | |
| 40 | // * Si la route actuelle n'as pas de nom alors on refuse de la traiter. |
| 41 | if (!(Helper::getRoute($request))) { |
| 42 | // @codeCoverageIgnoreStart |
| 43 | return null; |
| 44 | // @codeCoverageIgnoreEnd |
| 45 | } |
| 46 | |
| 47 | $service = null; |
| 48 | |
| 49 | try { |
| 50 | // * On récupÚre le service actuel correspondant au token fourni. |
| 51 | $service = Service::getFromBearerToken($request->bearer_token ?? ''); |
| 52 | } catch (ModelNotFoundException $e) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | // * Preload the brand as we will access it often later. |
| 57 | $service = $service->load('brand'); |
| 58 | |
| 59 | /** |
| 60 | ** All ApiV4 routes must pass Brand model to controller action since a Brand owns |
| 61 | ** a service token, otherwise the api cannot be called. |
| 62 | ** This hack is a custom binder instead of passing |
| 63 | ** this to route as a parameter |
| 64 | */ |
| 65 | app()->bind(Service::class, function () use ($service) { |
| 66 | return $service; |
| 67 | }); |
| 68 | |
| 69 | // * On authenticate la request en vérifiant sa signature. |
| 70 | if ($service->authenticateApi($request)) { |
| 71 | return $service; |
| 72 | } |
| 73 | |
| 74 | return null; |
| 75 | }); |
| 76 | } |
| 77 | } |