ApiAuthenticate::getUser()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\Http\Middleware;
10
11
use App\Models\User;
12
use App\Services\TokenAuth;
13
use Illuminate\Http\Request;
14
use Illuminate\Http\Response;
15
use Illuminate\Contracts\Auth\Guard;
16
use Illuminate\Contracts\Container\Container;
17
use Illuminate\Contracts\Auth\Authenticatable;
18
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
19
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
20
21
/**
22
 * Class ApiAuthenticate.
23
 */
24
class ApiAuthenticate
25
{
26
    /**
27
     * @var Guard
28
     */
29
    private $auth;
30
31
    /**
32
     * @var TokenAuth
33
     */
34
    private $tokenAuth;
35
36
    /**
37
     * @var Container
38
     */
39
    private $app;
40
41
    /**
42
     * ApiAuthenticate constructor.
43
     * @param Guard $auth
44
     * @param TokenAuth $tokenAuth
45
     * @param Container $app
46
     */
47
    public function __construct(Guard $auth, TokenAuth $tokenAuth, Container $app)
48
    {
49
        $this->auth = $auth;
50
        $this->app = $app;
51
        $this->tokenAuth = $tokenAuth;
52
    }
53
54
    /**
55
     * @param  Request $request
56
     * @param  \Closure $next
57
     * @return mixed
58
     * @throws BadRequestHttpException
59
     * @throws UnprocessableEntityHttpException
60
     */
61
    public function handle(Request $request, \Closure $next)
62
    {
63
        $user = $this->getUser($request);
64
65
        $this->app->instance(Authenticatable::class, $user);
66
67
        /** @var Response $response */
68
        return $next($request);
69
    }
70
71
    /**
72
     * @param  Request $request
73
     * @return Authenticatable|User
74
     * @throws BadRequestHttpException
75
     * @throws UnprocessableEntityHttpException
76
     */
77
    private function getUser(Request $request): Authenticatable
78
    {
79
        switch (true) {
80
            case $request->has('_token'):
81
                return $this->authByToken($request->get('_token', ''));
82
83
            case $request->headers->has('X-Api-Token'):
84
                return $this->authByToken($request->headers->get('X-Api-Token', ''));
85
86
            case $this->auth->check():
87
                return $this->auth->user();
88
        }
89
90
        return $this->tokenAuth->guest();
91
    }
92
93
    /**
94
     * @param  string $token
95
     * @return Authenticatable
96
     * @throws BadRequestHttpException
97
     * @throws UnprocessableEntityHttpException
98
     */
99
    private function authByToken(string $token): Authenticatable
100
    {
101
        return $this->tokenAuth->fromToken($token);
102
    }
103
}
104