2014-03-06 19:00:25 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2014-03-06 19:00:25 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
namespace OCA\DAV\Connector;
|
2014-03-06 19:00:25 +04:00
|
|
|
|
2016-03-24 18:02:36 +03:00
|
|
|
use OCP\IRequest;
|
2016-04-01 18:04:10 +03:00
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
|
|
|
use OCP\Share\IManager;
|
2016-06-09 14:53:32 +03:00
|
|
|
use Sabre\DAV\Auth\Backend\AbstractBasic;
|
2016-03-24 18:02:36 +03:00
|
|
|
|
2016-04-01 18:04:10 +03:00
|
|
|
/**
|
|
|
|
* Class PublicAuth
|
|
|
|
*
|
|
|
|
* @package OCA\DAV\Connector
|
|
|
|
*/
|
2016-06-09 14:53:32 +03:00
|
|
|
class PublicAuth extends AbstractBasic {
|
2014-03-06 19:00:25 +04:00
|
|
|
|
2016-04-01 18:04:10 +03:00
|
|
|
/** @var \OCP\Share\IShare */
|
2014-03-06 19:00:25 +04:00
|
|
|
private $share;
|
|
|
|
|
2016-04-01 18:04:10 +03:00
|
|
|
/** @var IManager */
|
|
|
|
private $shareManager;
|
|
|
|
|
|
|
|
/** @var ISession */
|
|
|
|
private $session;
|
|
|
|
|
|
|
|
/** @var IRequest */
|
2016-03-24 18:02:36 +03:00
|
|
|
private $request;
|
|
|
|
|
2014-03-06 19:00:25 +04:00
|
|
|
/**
|
2016-03-24 18:02:36 +03:00
|
|
|
* @param IRequest $request
|
2016-04-01 18:04:10 +03:00
|
|
|
* @param IManager $shareManager
|
|
|
|
* @param ISession $session
|
2014-03-06 19:00:25 +04:00
|
|
|
*/
|
2016-04-01 18:04:10 +03:00
|
|
|
public function __construct(IRequest $request,
|
|
|
|
IManager $shareManager,
|
|
|
|
ISession $session) {
|
2016-03-24 18:02:36 +03:00
|
|
|
$this->request = $request;
|
2016-04-01 18:04:10 +03:00
|
|
|
$this->shareManager = $shareManager;
|
|
|
|
$this->session = $session;
|
2016-06-09 14:53:32 +03:00
|
|
|
|
|
|
|
// setup realm
|
2016-07-15 09:46:31 +03:00
|
|
|
$defaults = new \OCP\Defaults();
|
2016-06-09 14:53:32 +03:00
|
|
|
$this->realm = $defaults->getName();
|
2014-03-06 19:00:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates a username and password
|
|
|
|
*
|
|
|
|
* This method should return true or false depending on if login
|
|
|
|
* succeeded.
|
|
|
|
*
|
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
*
|
|
|
|
* @return bool
|
2016-03-31 20:32:30 +03:00
|
|
|
* @throws \Sabre\DAV\Exception\NotAuthenticated
|
2014-03-06 19:00:25 +04:00
|
|
|
*/
|
|
|
|
protected function validateUserPass($username, $password) {
|
2016-04-01 18:04:10 +03:00
|
|
|
try {
|
|
|
|
$share = $this->shareManager->getShareByToken($username);
|
|
|
|
} catch (ShareNotFound $e) {
|
2014-03-06 19:00:25 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-01 18:04:10 +03:00
|
|
|
$this->share = $share;
|
2016-03-17 13:35:31 +03:00
|
|
|
|
2016-04-01 18:04:10 +03:00
|
|
|
\OC_User::setIncognitoMode(true);
|
2014-11-17 15:10:15 +03:00
|
|
|
|
2016-04-01 18:04:10 +03:00
|
|
|
// check if the share is password protected
|
|
|
|
if ($share->getPassword() !== null) {
|
2017-05-04 12:20:20 +03:00
|
|
|
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
|
2016-04-01 18:04:10 +03:00
|
|
|
if ($this->shareManager->checkPassword($share, $password)) {
|
2014-03-06 19:00:25 +04:00
|
|
|
return true;
|
2016-04-01 18:04:10 +03:00
|
|
|
} else if ($this->session->exists('public_link_authenticated')
|
2016-07-11 11:27:47 +03:00
|
|
|
&& $this->session->get('public_link_authenticated') === (string)$share->getId()) {
|
2015-07-13 18:39:07 +03:00
|
|
|
return true;
|
2014-11-17 15:10:15 +03:00
|
|
|
} else {
|
2016-03-24 18:02:36 +03:00
|
|
|
if (in_array('XMLHttpRequest', explode(',', $this->request->getHeader('X-Requested-With')))) {
|
|
|
|
// do not re-authenticate over ajax, use dummy auth name to prevent browser popup
|
2016-03-31 20:32:30 +03:00
|
|
|
http_response_code(401);
|
2016-06-09 14:53:32 +03:00
|
|
|
header('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"');
|
2016-03-24 18:02:36 +03:00
|
|
|
throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
|
|
|
|
}
|
2014-11-17 15:10:15 +03:00
|
|
|
return false;
|
2014-03-06 19:00:25 +04:00
|
|
|
}
|
2016-04-01 18:04:10 +03:00
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
|
2014-12-04 21:51:04 +03:00
|
|
|
return true;
|
2014-03-06 19:00:25 +04:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-01 18:04:10 +03:00
|
|
|
* @return \OCP\Share\IShare
|
2014-03-06 19:00:25 +04:00
|
|
|
*/
|
|
|
|
public function getShare() {
|
|
|
|
return $this->share;
|
|
|
|
}
|
|
|
|
}
|