Completed
Push — 2.0 ( 60f4f1...be6014 )
by Kirill
04:10
created

ApiAuthenticate::authByToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
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\Support\Arr;
14
use Illuminate\Http\Request;
15
use Illuminate\Http\Response;
16
use Illuminate\Contracts\Auth\Guard;
17
use Tymon\JWTAuth\Exceptions\JWTException;
18
use Illuminate\Contracts\Container\Container;
19
use Tymon\JWTAuth\Providers\JWT\JWTInterface;
20
use Illuminate\Contracts\Auth\Authenticatable;
21
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
22
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
23
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
24
25
/**
26
 * Class ApiAuthenticate.
27
 */
28
class ApiAuthenticate
29
{
30
    /**
31
     * @var Guard
32
     */
33
    private $auth;
34
35
    /**
36
     * @var TokenAuth
37
     */
38
    private $tokenAuth;
39
40
    /**
41
     * @var Container
42
     */
43
    private $app;
44
45
    /**
46
     * ApiAuthenticate constructor.
47
     * @param Guard $auth
48
     * @param TokenAuth $tokenAuth
49
     * @param Container $app
50
     */
51
    public function __construct(Guard $auth, TokenAuth $tokenAuth, Container $app)
52
    {
53
        $this->auth = $auth;
54
        $this->app = $app;
55
        $this->tokenAuth = $tokenAuth;
56
    }
57
58
    /**
59
     * @param  Request $request
60
     * @param  \Closure $next
61
     * @return mixed
62
     * @throws BadRequestHttpException
63
     * @throws UnprocessableEntityHttpException
64
     */
65
    public function handle(Request $request, \Closure $next)
66
    {
67
        $user = $this->getUser($request);
68
69
        $this->app->instance(Authenticatable::class, $user);
70
71
        /** @var Response $response */
72
        return $next($request);
73
    }
74
75
    /**
76
     * @param  Request $request
77
     * @return Authenticatable|User
78
     * @throws BadRequestHttpException
79
     * @throws UnprocessableEntityHttpException
80
     */
81
    private function getUser(Request $request): Authenticatable
82
    {
83
        switch (true) {
84
            case $request->has('_token'):
85
                return $this->authByToken($request->get('_token', ''));
86
87
            case $request->headers->has('X-Api-Token'):
88
                return $this->authByToken($request->headers->get('X-Api-Token', ''));
89
90
            case $this->auth->check():
91
                return $this->auth->user();
92
        }
93
94
        return $this->tokenAuth->guest();
95
    }
96
97
    /**
98
     * @param  string $token
99
     * @return Authenticatable
100
     * @throws BadRequestHttpException
101
     * @throws UnprocessableEntityHttpException
102
     */
103
    private function authByToken(string $token): Authenticatable
104
    {
105
        return $this->tokenAuth->fromToken($token);
106
    }
107
}
108