1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ArcherZdip\LaravelApiAuth\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Http\Request; |
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
|
|
|
|
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')) { |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths