nextcloud/apps/dav/lib/Connector/PublicAuth.php

129 lines
3.4 KiB
PHP
Raw Normal View History

2014-03-06 19:00:25 +04:00
<?php
/**
2016-05-26 20:56:05 +03:00
* @author Björn Schießle <bjoern@schiessle.org>
* @author Lukas Reschke <lukas@statuscode.ch>
2015-03-26 13:44:34 +03:00
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
2016-05-26 20:56:05 +03:00
* @author Roeland Jago Douma <rullzer@owncloud.com>
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
*
2016-01-12 17:02:16 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
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
*/
namespace OCA\DAV\Connector;
2014-03-06 19:00:25 +04:00
use OCP\IRequest;
use OCP\ISession;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use Sabre\DAV\Auth\Backend\AbstractBasic;
/**
* Class PublicAuth
*
* @package OCA\DAV\Connector
*/
class PublicAuth extends AbstractBasic {
2014-03-06 19:00:25 +04:00
/** @var \OCP\Share\IShare */
2014-03-06 19:00:25 +04:00
private $share;
/** @var IManager */
private $shareManager;
/** @var ISession */
private $session;
/** @var IRequest */
private $request;
2014-03-06 19:00:25 +04:00
/**
* @param IRequest $request
* @param IManager $shareManager
* @param ISession $session
2014-03-06 19:00:25 +04:00
*/
public function __construct(IRequest $request,
IManager $shareManager,
ISession $session) {
$this->request = $request;
$this->shareManager = $shareManager;
$this->session = $session;
// setup realm
$defaults = new \OC_Defaults();
$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) {
try {
$share = $this->shareManager->getShareByToken($username);
} catch (ShareNotFound $e) {
2014-03-06 19:00:25 +04:00
return false;
}
$this->share = $share;
\OC_User::setIncognitoMode(true);
// 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 19:00:25 +04:00
return true;
} else if ($this->session->exists('public_link_authenticated')
&& $this->session->get('public_link_authenticated') === $share->getId()) {
return true;
} else {
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);
header('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"');
throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
}
return false;
2014-03-06 19:00:25 +04:00
}
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
return true;
2014-03-06 19:00:25 +04:00
} else {
return false;
}
} else {
return true;
}
}
/**
* @return \OCP\Share\IShare
2014-03-06 19:00:25 +04:00
*/
public function getShare() {
return $this->share;
}
}