mirror of
https://github.com/nextcloud/server.git
synced 2026-03-07 16:01:08 -05:00
By default, the name of the Nextcloud instance is an empty string, until changed by the admin. This leads to an empty realm sent with the WWW-Authenticate header, while the realm is mandatory for Basic HTTP authentication. Some clients have issues with an empty realm, e.g. Thunderbird cannot store passwords in this case. This commit applies "Nextcloud" as fallback for the realm, in case the name of the Nextcloud instance is not set. Solves: https://help.nextcloud.com/t/thunderbird-dont-save-caldav-password-because-of-missing-httprealm-or-formsubmiturl/93233 Signed-off-by: MichaIng <micha@dietpi.com>
86 lines
2.5 KiB
PHP
86 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
|
|
*
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
*
|
|
* @license GNU AGPL version 3 or any later version
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* 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
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
namespace OCA\DAV\Connector\Sabre;
|
|
|
|
use OCP\IRequest;
|
|
use OCP\ISession;
|
|
use OCP\IUserSession;
|
|
use Sabre\DAV\Auth\Backend\AbstractBearer;
|
|
use Sabre\HTTP\RequestInterface;
|
|
use Sabre\HTTP\ResponseInterface;
|
|
|
|
class BearerAuth extends AbstractBearer {
|
|
private IUserSession $userSession;
|
|
private ISession $session;
|
|
private IRequest $request;
|
|
private string $principalPrefix;
|
|
|
|
public function __construct(IUserSession $userSession,
|
|
ISession $session,
|
|
IRequest $request,
|
|
$principalPrefix = 'principals/users/') {
|
|
$this->userSession = $userSession;
|
|
$this->session = $session;
|
|
$this->request = $request;
|
|
$this->principalPrefix = $principalPrefix;
|
|
|
|
// setup realm
|
|
$defaults = new \OCP\Defaults();
|
|
$this->realm = $defaults->getName() ?: 'Nextcloud';
|
|
}
|
|
|
|
private function setupUserFs($userId) {
|
|
\OC_Util::setupFS($userId);
|
|
$this->session->close();
|
|
return $this->principalPrefix . $userId;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function validateBearerToken($bearerToken) {
|
|
\OC_Util::setupFS();
|
|
|
|
if (!$this->userSession->isLoggedIn()) {
|
|
$this->userSession->tryTokenLogin($this->request);
|
|
}
|
|
if ($this->userSession->isLoggedIn()) {
|
|
return $this->setupUserFs($this->userSession->getUser()->getUID());
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* \Sabre\DAV\Auth\Backend\AbstractBearer::challenge sets an WWW-Authenticate
|
|
* header which some DAV clients can't handle. Thus we override this function
|
|
* and make it simply return a 401.
|
|
*
|
|
* @param RequestInterface $request
|
|
* @param ResponseInterface $response
|
|
*/
|
|
public function challenge(RequestInterface $request, ResponseInterface $response): void {
|
|
$response->setStatus(401);
|
|
}
|
|
}
|