AuthorizeApiKeyMiddleware::handle()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 14
rs 9.9666
cc 3
nc 2
nop 2
1
<?php
2
3
namespace ArcherZdip\LaravelApiAuth\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Support\Facades\Log;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Log was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use ArcherZdip\LaravelApiAuth\ApiAuth;
9
use ArcherZdip\LaravelApiAuth\Models\ApiAuthAccessEvent;
10
use ArcherZdip\LaravelApiAuth\Exceptions\UnauthorizedException;
11
12
class AuthorizeApiKeyMiddleware
13
{
14
    const AUTH_HEADER = 'Authorization';
15
16
    /**
17
     * Handle an incoming request.
18
     *
19
     * @param \Illuminate\Http\Request $request
20
     * @param \Closure $next
21
     * @return mixed
22
     */
23
    public function handle($request, Closure $next)
24
    {
25
        $start = microtime(true);
26
        $token = $request->header(self::AUTH_HEADER) ?? '';
27
        $urlToken = $request->input('token');
28
        $token = $urlToken ?: $token;
29
30
        if (ApiAuth::isValid($token)) {
31
            $response = $next($request);
32
            $this->logAccessEvent($request, $token, $start);
33
34
            return $response;
35
        }
36
        throw new UnauthorizedException();
37
    }
38
39
    /**
40
     * Log an API KEY access event
41
     *
42
     * @param Request $request
43
     * @param string $token
44
     * @param $startTime
45
     */
46
    protected function logAccessEvent(Request $request, string $token, $startTime)
47
    {
48
        // Log access event
49
        if (config('apikey.logger.is_taken')) {
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        if (/** @scrutinizer ignore-call */ config('apikey.logger.is_taken')) {
Loading history...
50
            $appid = ApiAuth::getAppId($token);
51
52
            $attributes = [
53
                'appid'         => $appid,
54
                'ip_address'    => $request->ip(),
55
                'url'           => $request->fullUrl(),
56
                'params'        => $request->all(),
57
                'response_time' => (microtime(true) - $startTime) * 1000,
58
                'type'          => $request->method(),
59
            ];
60
61
            // database
62
            if (config('apikey.logger.driver') === 'database') {
63
                $this->logAccessEventForDB($attributes);
64
            } elseif (config('apikey.logger.driver') === 'file') {
65
                $this->logAccessEventForFile($attributes);
66
            }
67
        }
68
69
    }
70
71
    /**
72
     * Log an access event for DB
73
     *
74
     * @param array $attributes
75
     */
76
    protected function logAccessEventForDB(array $attributes)
77
    {
78
        ApiAuthAccessEvent::forceCreate($attributes);
79
    }
80
81
    /**
82
     * Log an access event for file
83
     *
84
     * @param array $attributes
85
     */
86
    protected function logAccessEventForFile(array $attributes)
87
    {
88
        $message = '[ApiKey Log] Params: ';
89
        Log::info($message, $attributes);
90
    }
91
}
92