|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Noveen Sachdeva <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2017, ownCloud GmbH |
|
6
|
|
|
* @license AGPL-3.0 |
|
7
|
|
|
* |
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
10
|
|
|
* as published by the Free Software Foundation. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OCA\DAV\Connector\Sabre; |
|
23
|
|
|
|
|
24
|
|
|
use Sabre\HTTP\RequestInterface; |
|
25
|
|
|
use Sabre\HTTP\ResponseInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class CorsPlugin is a plugin which adds CORS headers to the responses |
|
29
|
|
|
*/ |
|
30
|
|
|
class CorsPlugin extends \Sabre\DAV\ServerPlugin { |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Reference to main server object |
|
34
|
|
|
* |
|
35
|
|
|
* @var \Sabre\DAV\Server |
|
36
|
|
|
*/ |
|
37
|
|
|
private $server; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Reference to logged in user's session |
|
41
|
|
|
* |
|
42
|
|
|
* @var \OCP\IUserSession |
|
43
|
|
|
*/ |
|
44
|
|
|
private $userSession; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var string[] |
|
48
|
|
|
*/ |
|
49
|
|
|
private $extraHeaders = null; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \OCP\IUserSession $userSession |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(\OCP\IUserSession $userSession) { |
|
55
|
|
|
$this->userSession = $userSession; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function getExtraHeaders(RequestInterface $request) { |
|
59
|
|
|
if ($this->extraHeaders === null) { |
|
60
|
|
|
// TODO: design a way to have plugins provide these |
|
61
|
|
|
$this->extraHeaders['Access-Control-Allow-Headers'] = ["X-OC-Mtime", "OC-Checksum", "OC-Total-Length", "Depth", "Destination", "Overwrite"]; |
|
62
|
|
|
$this->extraHeaders['Access-Control-Allow-Methods'] = $this->server->getAllowedMethods($request->getPath()); |
|
63
|
|
|
} |
|
64
|
|
|
return $this->extraHeaders; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* This initializes the plugin. |
|
69
|
|
|
* |
|
70
|
|
|
* This function is called by \Sabre\DAV\Server, after |
|
71
|
|
|
* addPlugin is called. |
|
72
|
|
|
* |
|
73
|
|
|
* This method should set up the required event subscriptions. |
|
74
|
|
|
* |
|
75
|
|
|
* @param \Sabre\DAV\Server $server |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
public function initialize(\Sabre\DAV\Server $server) { |
|
79
|
|
|
$this->server = $server; |
|
80
|
|
|
|
|
81
|
|
|
$request = $this->server->httpRequest; |
|
82
|
|
|
if (!$request->hasHeader('Origin') || \OCP\Util::isSameDomain($request->getHeader('Origin'), $request->getAbsoluteUrl())) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->server->on('beforeMethod', [$this, 'setCorsHeaders']); |
|
87
|
|
|
$this->server->on('beforeMethod:OPTIONS', [$this, 'setOptionsRequestHeaders']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* This method sets the cors headers for all requests |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function setCorsHeaders(RequestInterface $request, ResponseInterface $response) { |
|
96
|
|
|
if ($request->getHeader('origin') !== null && !is_null($this->userSession->getUser())) { |
|
97
|
|
|
$requesterDomain = $request->getHeader('origin'); |
|
98
|
|
|
$userId = $this->userSession->getUser()->getUID(); |
|
99
|
|
|
$response = \OC_Response::setCorsHeaders($userId, $requesterDomain, $response, null, $this->getExtraHeaders($request)); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Handles the OPTIONS request |
|
105
|
|
|
* |
|
106
|
|
|
* @param RequestInterface $request |
|
107
|
|
|
* @param ResponseInterface $response |
|
108
|
|
|
* |
|
109
|
|
|
* @return false |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setOptionsRequestHeaders(RequestInterface $request, ResponseInterface $response) { |
|
112
|
|
|
$authorization = $request->getHeader('Authorization'); |
|
113
|
|
|
if ($authorization === null || $authorization === '') { |
|
114
|
|
|
// Set the proper response |
|
115
|
|
|
$response->setStatus(200); |
|
116
|
|
|
$response = \OC_Response::setOptionsRequestHeaders($response, $this->getExtraHeaders($request)); |
|
117
|
|
|
|
|
118
|
|
|
// Since All OPTIONS requests are unauthorized, we will have to return false from here |
|
119
|
|
|
// If we don't return false, due to no authorization, a 401-Unauthorized will be thrown |
|
120
|
|
|
// Which we don't want here |
|
121
|
|
|
// Hence this sendResponse |
|
122
|
|
|
$this->server->sapi->sendResponse($response); |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|