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