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

131 lines
3.5 KiB
PHP
Raw Normal View History

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
*/
namespace OCA\DAV\Connector;
2014-03-06 15:00:25 +00: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 15:00:25 +00:00
/** @var \OCP\Share\IShare */
2014-03-06 15:00:25 +00:00
private $share;
/** @var IManager */
private $shareManager;
/** @var ISession */
private $session;
/** @var IRequest */
private $request;
2014-03-06 15:00:25 +00:00
/**
* @param IRequest $request
* @param IManager $shareManager
* @param ISession $session
2014-03-06 15:00:25 +00:00
*/
public function __construct(IRequest $request,
IManager $shareManager,
ISession $session) {
$this->request = $request;
$this->shareManager = $shareManager;
$this->session = $session;
// setup realm
2016-07-15 06:46:31 +00:00
$defaults = new \OCP\Defaults();
$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) {
try {
$share = $this->shareManager->getShareByToken($username);
} catch (ShareNotFound $e) {
2014-03-06 15:00:25 +00: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 15:00:25 +00:00
return true;
} 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()) {
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 17:32:30 +00: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 15:00:25 +00:00
}
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
return true;
2014-03-06 15:00:25 +00:00
} else {
return false;
}
} else {
return true;
}
}
/**
* @return \OCP\Share\IShare
2014-03-06 15:00:25 +00:00
*/
public function getShare() {
return $this->share;
}
}