2014-03-06 15:00:25 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-07-21 14:49:16 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 14:49:16 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-03-26 10:44:34 +00: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 15:00:25 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2015-08-30 17:13:01 +00:00
|
|
|
namespace OCA\DAV\Connector;
|
2014-03-06 15:00:25 +00:00
|
|
|
|
2016-03-24 15:02:36 +00:00
|
|
|
use OCP\IRequest;
|
2016-04-01 15:04:10 +00:00
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\Share\Exceptions\ShareNotFound;
|
|
|
|
use OCP\Share\IManager;
|
2016-06-09 11:53:32 +00:00
|
|
|
use Sabre\DAV\Auth\Backend\AbstractBasic;
|
2016-03-24 15:02:36 +00:00
|
|
|
|
2016-04-01 15:04:10 +00:00
|
|
|
/**
|
|
|
|
* Class PublicAuth
|
|
|
|
*
|
|
|
|
* @package OCA\DAV\Connector
|
|
|
|
*/
|
2016-06-09 11:53:32 +00:00
|
|
|
class PublicAuth extends AbstractBasic {
|
2014-03-06 15:00:25 +00:00
|
|
|
|
2016-04-01 15:04:10 +00:00
|
|
|
/** @var \OCP\Share\IShare */
|
2014-03-06 15:00:25 +00:00
|
|
|
private $share;
|
|
|
|
|
2016-04-01 15:04:10 +00:00
|
|
|
/** @var IManager */
|
|
|
|
private $shareManager;
|
|
|
|
|
|
|
|
/** @var ISession */
|
|
|
|
private $session;
|
|
|
|
|
|
|
|
/** @var IRequest */
|
2016-03-24 15:02:36 +00:00
|
|
|
private $request;
|
|
|
|
|
2014-03-06 15:00:25 +00:00
|
|
|
/**
|
2016-03-24 15:02:36 +00:00
|
|
|
* @param IRequest $request
|
2016-04-01 15:04:10 +00:00
|
|
|
* @param IManager $shareManager
|
|
|
|
* @param ISession $session
|
2014-03-06 15:00:25 +00:00
|
|
|
*/
|
2016-04-01 15:04:10 +00:00
|
|
|
public function __construct(IRequest $request,
|
|
|
|
IManager $shareManager,
|
|
|
|
ISession $session) {
|
2016-03-24 15:02:36 +00:00
|
|
|
$this->request = $request;
|
2016-04-01 15:04:10 +00:00
|
|
|
$this->shareManager = $shareManager;
|
|
|
|
$this->session = $session;
|
2016-06-09 11:53:32 +00:00
|
|
|
|
|
|
|
// setup realm
|
2016-07-15 06:46:31 +00:00
|
|
|
$defaults = new \OCP\Defaults();
|
2016-06-09 11:53:32 +00:00
|
|
|
$this->realm = $defaults->getName();
|
2014-03-06 15:00:25 +00: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 17:32:30 +00:00
|
|
|
* @throws \Sabre\DAV\Exception\NotAuthenticated
|
2014-03-06 15:00:25 +00:00
|
|
|
*/
|
|
|
|
protected function validateUserPass($username, $password) {
|
2016-04-01 15:04:10 +00:00
|
|
|
try {
|
|
|
|
$share = $this->shareManager->getShareByToken($username);
|
|
|
|
} catch (ShareNotFound $e) {
|
2014-03-06 15:00:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-01 15:04:10 +00:00
|
|
|
$this->share = $share;
|
2016-03-17 10:35:31 +00:00
|
|
|
|
2016-04-01 15:04:10 +00:00
|
|
|
\OC_User::setIncognitoMode(true);
|
2014-11-17 12:10:15 +00:00
|
|
|
|
2016-04-01 15:04:10 +00:00
|
|
|
// check if the share is password protected
|
|
|
|
if ($share->getPassword() !== null) {
|
|
|
|
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
|
|
|
|
if ($this->shareManager->checkPassword($share, $password)) {
|
2014-03-06 15:00:25 +00:00
|
|
|
return true;
|
2016-04-01 15:04:10 +00:00
|
|
|
} else if ($this->session->exists('public_link_authenticated')
|
2016-07-11 08:27:47 +00:00
|
|
|
&& $this->session->get('public_link_authenticated') === (string)$share->getId()) {
|
2015-07-13 15:39:07 +00:00
|
|
|
return true;
|
2014-11-17 12:10:15 +00:00
|
|
|
} else {
|
2016-03-24 15:02:36 +00: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 17:32:30 +00:00
|
|
|
http_response_code(401);
|
2016-06-09 11:53:32 +00:00
|
|
|
header('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"');
|
2016-03-24 15:02:36 +00:00
|
|
|
throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
|
|
|
|
}
|
2014-11-17 12:10:15 +00:00
|
|
|
return false;
|
2014-03-06 15:00:25 +00:00
|
|
|
}
|
2016-04-01 15:04:10 +00:00
|
|
|
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
|
2014-12-04 18:51:04 +00:00
|
|
|
return true;
|
2014-03-06 15:00:25 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-01 15:04:10 +00:00
|
|
|
* @return \OCP\Share\IShare
|
2014-03-06 15:00:25 +00:00
|
|
|
*/
|
|
|
|
public function getShare() {
|
|
|
|
return $this->share;
|
|
|
|
}
|
|
|
|
}
|