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

136 lines
3.6 KiB
PHP
Raw Normal View History

2014-03-06 19:00:25 +04:00
<?php
/**
2016-07-21 17:49:16 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @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/>
2015-03-26 13:44:34 +03:00
*
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 OCP\Share\IShare;
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
2016-07-15 09:46:31 +03:00
$defaults = new \OCP\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() === IShare::TYPE_LINK
|| $share->getShareType() === IShare::TYPE_EMAIL
|| $share->getShareType() === IShare::TYPE_CIRCLE) {
if ($this->shareManager->checkPassword($share, $password)) {
2014-03-06 19:00:25 +04:00
return true;
} 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()) {
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() === IShare::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;
}
}