1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ShiftOneLabs\LaravelCors; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Http\Response; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request as SymfonyRequest; |
8
|
|
|
|
9
|
|
|
class CorsPolicy extends CorsService |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Check the request origin and the request method to determine if the |
13
|
|
|
* request is allowed for this CORS policy. |
14
|
|
|
* |
15
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
16
|
|
|
* |
17
|
|
|
* @return bool |
18
|
|
|
*/ |
19
|
46 |
|
public function isActualRequestAllowed(SymfonyRequest $request) |
20
|
|
|
{ |
21
|
46 |
|
return $this->isOriginAllowed($request) && $this->isMethodAllowed($request); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create the appropriate forbidden response based on the request. |
26
|
|
|
* |
27
|
|
|
* @param \Illuminate\Http\Request $request |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Http\Response |
30
|
|
|
*/ |
31
|
46 |
|
public function createNotAllowedResponse(Request $request) |
32
|
|
|
{ |
33
|
46 |
|
if (!$this->isOriginAllowed($request)) { |
34
|
46 |
|
return $this->createOriginNotAllowedResponse(); |
35
|
|
|
} |
36
|
|
|
|
37
|
46 |
|
if (!$this->isMethodAllowed($request)) { |
38
|
46 |
|
return $this->createMethodNotAllowedResponse(); |
39
|
|
|
} |
40
|
|
|
|
41
|
46 |
|
return $this->createResponse('Forbidden (cors).', 403); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create the response for an invalid request origin. |
46
|
|
|
* |
47
|
|
|
* @return \Illuminate\Http\Response |
48
|
|
|
*/ |
49
|
92 |
|
public function createOriginNotAllowedResponse() |
50
|
|
|
{ |
51
|
92 |
|
return $this->createResponse('Origin not allowed.', 403); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create the response for an invalid request method. |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Http\Response |
58
|
|
|
*/ |
59
|
92 |
|
public function createMethodNotAllowedResponse() |
60
|
|
|
{ |
61
|
92 |
|
return $this->createResponse('Method not allowed.', 405); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a response object. |
66
|
|
|
* |
67
|
|
|
* @param string|null $content |
68
|
|
|
* @param int $status |
69
|
|
|
* @param array $headers |
70
|
|
|
* |
71
|
|
|
* @return \Illuminate\Http\Response |
72
|
|
|
*/ |
73
|
138 |
|
protected function createResponse($content = '', $status = 200, $headers = []) |
74
|
|
|
{ |
75
|
138 |
|
return new Response($content, $status, $headers); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|