Skip to content

Commit fc5590b

Browse files
committed
Add password verification for public shares
1 parent 572be38 commit fc5590b

File tree

7 files changed

+131
-5
lines changed

7 files changed

+131
-5
lines changed

apps/dav/lib/DAV/PublicAuth.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,9 @@ class PublicAuth implements BackendInterface {
2929
/** @var string[] */
3030
private $publicURLs;
3131

32-
/**
33-
* @param string[] $publicURLs
34-
*/
3532
public function __construct() {
3633
$this->publicURLs = [
3734
'public-calendars',
38-
'public-files',
3935
'principals/system/public'
4036
];
4137
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* @author Thomas Müller <[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+
namespace OCA\DAV\Files\PublicFiles;
22+
23+
use OCP\Share\IManager;
24+
use OCP\Share\IShare;
25+
use Sabre\DAV\Auth\Backend\AbstractBasic;
26+
use Sabre\DAV\Server;
27+
use Sabre\HTTP\RequestInterface;
28+
use Sabre\HTTP\ResponseInterface;
29+
30+
class PublicSharingAuth extends AbstractBasic {
31+
32+
/** @var Server */
33+
private $server;
34+
/** @var IShare */
35+
private $share;
36+
/** @var IManager */
37+
private $shareManager;
38+
39+
/**
40+
* PublicSharingAuth constructor.
41+
*
42+
* @param Server $server
43+
*/
44+
public function __construct(Server $server, IManager $manager) {
45+
$this->server = $server;
46+
$this->shareManager = $manager;
47+
$this->principalPrefix = 'principals/system/';
48+
$this->setRealm('owncloud/share');
49+
}
50+
51+
/**
52+
* When this method is called, the backend must check if authentication was
53+
* successful.
54+
*
55+
* The returned value must be one of the following
56+
*
57+
* [true, "principals/username"]
58+
* [false, "reason for failure"]
59+
*
60+
* If authentication was successful, it's expected that the authentication
61+
* backend returns a so-called principal url.
62+
*
63+
* Examples of a principal url:
64+
*
65+
* principals/admin
66+
* principals/user1
67+
* principals/users/joe
68+
* principals/uid/123457
69+
*
70+
* If you don't use WebDAV ACL (RFC3744) we recommend that you simply
71+
* return a string such as:
72+
*
73+
* principals/users/[username]
74+
*
75+
* @param RequestInterface $request
76+
* @param ResponseInterface $response
77+
* @return array
78+
*/
79+
function check(RequestInterface $request, ResponseInterface $response) {
80+
81+
$node = $this->server->tree->getNodeForPath($request->getPath());
82+
if (!$node instanceof ShareNode && !$node instanceof SharedFile && !$node instanceof SharedFolder) {
83+
return [true, "principals/system/public"];
84+
}
85+
$this->share = $node->getShare();
86+
$password = $this->share->getPassword();
87+
if ($password === null) {
88+
return [true, "principals/system/public"];
89+
}
90+
91+
return parent::check($request, $response);
92+
}
93+
94+
/**
95+
* @inheritdoc
96+
*/
97+
function challenge(RequestInterface $request, ResponseInterface $response) {
98+
}
99+
100+
/**
101+
* Validates a username and password
102+
*
103+
* This method should return true or false depending on if login
104+
* succeeded.
105+
*
106+
* @param string $username
107+
* @param string $password
108+
* @return bool
109+
*/
110+
protected function validateUserPass($username, $password) {
111+
if ($username !== 'public') {
112+
return false;
113+
}
114+
return $this->shareManager->checkPassword($this->share, $password);
115+
}
116+
}

apps/dav/lib/Files/PublicFiles/RootCollection.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ function getChild($name) {
6565
try {
6666
$share = $this->shareManager->getShareByToken($name);
6767
$password = $share->getPassword();
68-
// TODO: check password
6968
return new ShareNode($share);
7069
} catch (ShareNotFound $ex) {
7170
throw new NotFound();

apps/dav/lib/Files/PublicFiles/ShareNode.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,8 @@ function getChildren() {
5252
function getName() {
5353
return $this->share->getToken();
5454
}
55+
56+
function getShare() {
57+
return $this->share;
58+
}
5559
}

apps/dav/lib/Files/PublicFiles/SharedFile.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,9 @@ function getACL() {
101101
]
102102
];
103103
}
104+
105+
function getShare() {
106+
return $this->share;
107+
}
108+
104109
}

apps/dav/lib/Files/PublicFiles/SharedFolder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,8 @@ function getACL() {
125125
return $acl;
126126
}
127127

128+
function getShare() {
129+
return $this->share;
130+
}
131+
128132
}

apps/dav/lib/Server.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use OCA\DAV\DAV\PublicAuth;
5151
use OCA\DAV\Files\BrowserErrorPagePlugin;
5252
use OCA\DAV\Files\PreviewPlugin;
53+
use OCA\DAV\Files\PublicFiles\PublicSharingAuth;
5354
use OCA\DAV\SystemTag\SystemTagPlugin;
5455
use OCA\DAV\Upload\ChunkingPlugin;
5556
use OCP\IRequest;
@@ -101,6 +102,7 @@ public function __construct(IRequest $request, $baseUri) {
101102
$this->server->addPlugin(new BlockLegacyClientPlugin($config));
102103
$this->server->addPlugin(new CorsPlugin(\OC::$server->getUserSession()));
103104
$authPlugin = new Plugin();
105+
$authPlugin->addBackend(new PublicSharingAuth($this->server, \OC::$server->getShareManager()));
104106
$authPlugin->addBackend(new PublicAuth());
105107
$this->server->addPlugin($authPlugin);
106108

0 commit comments

Comments
 (0)