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