1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Cors\CorsProfile; |
4
|
|
|
|
5
|
|
|
class DefaultProfile implements CorsProfile |
6
|
|
|
{ |
7
|
|
|
/** @var \Illuminate\Http\Request */ |
8
|
|
|
protected $request; |
9
|
|
|
|
10
|
|
|
public function setRequest($request) |
11
|
|
|
{ |
12
|
|
|
$this->request = $request; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
public function allowCredentials(): bool |
16
|
|
|
{ |
17
|
|
|
return config('cors.default_profile.allow_credentials') ?? false; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function allowOrigins(): array |
21
|
|
|
{ |
22
|
|
|
return config('cors.default_profile.allow_origins'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function allowMethods(): array |
26
|
|
|
{ |
27
|
|
|
return config('cors.default_profile.allow_methods'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function allowHeaders(): array |
31
|
|
|
{ |
32
|
|
|
return config('cors.default_profile.allow_headers'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function exposeHeaders(): array |
36
|
|
|
{ |
37
|
|
|
return config('cors.default_profile.expose_headers') ?? []; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function maxAge(): int |
41
|
|
|
{ |
42
|
|
|
return config('cors.default_profile.max_age'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function addCorsHeaders($response) |
46
|
|
|
{ |
47
|
|
|
if ($this->allowCredentials()) { |
48
|
|
|
$response->headers->set('Access-Control-Allow-Credentials', 'true'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$response->headers->set('Access-Control-Allow-Origin', $this->allowedOriginsToString()); |
52
|
|
|
$response->headers->set('Access-Control-Expose-Headers', $this->toString($this->exposeHeaders())); |
53
|
|
|
|
54
|
|
|
return $response; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function addPreflightHeaders($response) |
58
|
|
|
{ |
59
|
|
|
if ($this->allowCredentials()) { |
60
|
|
|
$response->headers->set('Access-Control-Allow-Credentials', 'true'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$response->headers->set('Access-Control-Allow-Methods', $this->toString($this->allowMethods())); |
64
|
|
|
$response->headers->set('Access-Control-Allow-Headers', $this->toString($this->allowHeaders())); |
65
|
|
|
$response->headers->set('Access-Control-Allow-Origin', $this->allowedOriginsToString()); |
66
|
|
|
$response->headers->set('Access-Control-Max-Age', $this->maxAge()); |
67
|
|
|
|
68
|
|
|
return $response; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function isAllowed(): bool |
72
|
|
|
{ |
73
|
|
|
if (! in_array($this->request->method(), $this->allowMethods())) { |
74
|
|
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (in_array('*', $this->allowOrigins())) { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$matches = collect($this->allowOrigins())->filter(function ($allowedOrigin) { |
82
|
|
|
return fnmatch($allowedOrigin, $this->request->header('Origin')); |
83
|
|
|
}); |
84
|
|
|
|
85
|
|
|
return $matches->count() > 0; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function toString(array $array): string |
89
|
|
|
{ |
90
|
|
|
return implode(', ', $array); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function allowedOriginsToString(): string |
94
|
|
|
{ |
95
|
|
|
if (! $this->isAllowed()) { |
96
|
|
|
return ''; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (in_array('*', $this->allowOrigins()) && ! $this->allowCredentials()) { |
100
|
|
|
return '*'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->request->header('Origin'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|