1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Laravel Lodash package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Longman\LaravelLodash\Middlewares; |
13
|
|
|
|
14
|
|
|
use Closure; |
15
|
|
|
use Illuminate\Http\Request; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
|
19
|
|
|
class AllowCorsRequests |
20
|
|
|
{ |
21
|
|
|
protected $logger; |
22
|
|
|
|
23
|
|
|
public function __construct(LoggerInterface $logger) |
24
|
|
|
{ |
25
|
|
|
$this->logger = $logger; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function handle(Request $request, Closure $next) |
29
|
|
|
{ |
30
|
|
|
if (! $request->headers->has('Origin')) { |
31
|
|
|
return $next($request); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$origin = $request->headers->get('Origin', ''); |
35
|
|
|
$host = $this->parseUrl($origin); |
|
|
|
|
36
|
|
View Code Duplication |
if (empty($host)) { |
|
|
|
|
37
|
|
|
$this->logRequest('Origin is invalid', [ |
38
|
|
|
'origin' => $origin, |
39
|
|
|
'parsed' => $host, |
40
|
|
|
]); |
41
|
|
|
|
42
|
|
|
return $this->response($request, 'Origin is invalid', Response::HTTP_BAD_REQUEST); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$allowed_origins = config('lodash.cors.allow_origins', []); |
46
|
|
|
$current_app = $this->parseUrl(config('app.url', '')); |
47
|
|
|
if (! empty($host)) { |
48
|
|
|
$allowed_origins[] = $current_app; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$found = false; |
52
|
|
|
foreach ($allowed_origins as $allowed_origin) { |
53
|
|
|
if ($host === $allowed_origin || ends_with($host, '.' . $allowed_origin)) { |
|
|
|
|
54
|
|
|
$found = true; |
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
if (! $found) { |
|
|
|
|
60
|
|
|
$this->logRequest('Origin is not allowed', [ |
61
|
|
|
'origin' => $origin, |
62
|
|
|
'parsed' => $host, |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
return $this->response($request, 'Origin is not allowed', Response::HTTP_METHOD_NOT_ALLOWED); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($request->method() === Request::METHOD_OPTIONS) { |
69
|
|
|
$allowed_headers = config('lodash.cors.allow_headers', []); |
70
|
|
|
$allowed_methods = config('lodash.cors.allow_methods', []); |
71
|
|
|
|
72
|
|
|
$response = $this->response($request, 'Allowed', Response::HTTP_OK); |
73
|
|
|
|
74
|
|
|
$response->headers->set('Access-Control-Allow-Origin', $origin); |
75
|
|
|
$response->headers->set('Access-Control-Allow-Credentials', 'true'); |
76
|
|
|
|
77
|
|
|
$response->headers->set('Access-Control-Allow-Methods', implode(',', $allowed_methods)); |
78
|
|
|
$response->headers->set('Access-Control-Allow-Headers', implode(',', $allowed_headers)); |
79
|
|
|
$response->headers->set('Access-Control-Max-Age', '1728000'); |
80
|
|
|
|
81
|
|
|
return $response; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** @var \Illuminate\Http\Response $response */ |
85
|
|
|
$response = $next($request); |
86
|
|
|
|
87
|
|
|
$response->headers->set('Access-Control-Allow-Origin', $origin); |
88
|
|
|
$response->headers->set('Access-Control-Allow-Credentials', 'true'); |
89
|
|
|
|
90
|
|
|
return $response; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function logRequest(string $message, array $context = []): void |
94
|
|
|
{ |
95
|
|
|
$this->logger->warning($message, $context); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function parseUrl(string $url): string |
99
|
|
|
{ |
100
|
|
|
$host = (string) parse_url($url, PHP_URL_HOST); |
101
|
|
|
|
102
|
|
|
if (starts_with($host, 'www.')) { |
|
|
|
|
103
|
|
|
$host = str_replace('www.', '', $host); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (strpos($host, ':') !== false) { |
107
|
|
|
$host = strtok($host, ':'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $host; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function response(Request $request, string $message, int $code): Response |
114
|
|
|
{ |
115
|
|
|
if ($request->wantsJson()) { |
116
|
|
|
return response()->json(['message' => $message], $code); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return response($message, $code); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.