|
| 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 | +} |
0 commit comments