2016-04-11 13:08:00 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2017-02-07 03:25:39 +03:00
|
|
|
* @copyright Copyright (c) 2017, Sandro Lutz <sandro.lutz@temparus.ch>
|
2016-09-19 16:33:30 +03:00
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-02-07 03:25:39 +03:00
|
|
|
* @author Sandro Lutz <sandro.lutz@temparus.ch>
|
2016-04-25 15:10:55 +03:00
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2016-04-11 13:08:00 +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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\Manager;
|
2016-04-25 15:10:55 +03:00
|
|
|
use OC\User\Session;
|
|
|
|
use OC_App;
|
|
|
|
use OC_Util;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
2016-09-19 16:33:30 +03:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2016-09-29 17:38:29 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvider;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\IConfig;
|
2017-01-12 14:38:45 +03:00
|
|
|
use OCP\ILogger;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
2016-04-18 13:14:07 +03:00
|
|
|
use OCP\IURLGenerator;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2016-09-29 17:38:29 +03:00
|
|
|
use OCP\IUserSession;
|
2017-02-07 02:12:19 +03:00
|
|
|
use OC\Hooks\PublicEmitter;
|
2016-04-11 13:08:00 +03:00
|
|
|
|
|
|
|
class LoginController extends Controller {
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
/** @var ISession */
|
|
|
|
private $session;
|
2016-09-29 17:38:29 +03:00
|
|
|
/** @var IUserSession|Session */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $userSession;
|
2016-04-18 13:14:07 +03:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2016-05-11 12:23:25 +03:00
|
|
|
/** @var Manager */
|
|
|
|
private $twoFactorManager;
|
|
|
|
|
2016-04-11 13:08:00 +03:00
|
|
|
/**
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param ISession $session
|
2016-09-29 17:38:29 +03:00
|
|
|
* @param IUserSession $userSession
|
2016-04-18 13:14:07 +03:00
|
|
|
* @param IURLGenerator $urlGenerator
|
2017-01-12 14:38:45 +03:00
|
|
|
* @param ILogger $logger
|
2016-05-11 12:23:25 +03:00
|
|
|
* @param Manager $twoFactorManager
|
2016-04-11 13:08:00 +03:00
|
|
|
*/
|
2017-01-12 14:38:45 +03:00
|
|
|
public function __construct($appName,
|
2016-07-20 19:36:15 +03:00
|
|
|
IRequest $request,
|
|
|
|
IUserManager $userManager,
|
|
|
|
IConfig $config,
|
|
|
|
ISession $session,
|
2016-09-29 17:38:29 +03:00
|
|
|
IUserSession $userSession,
|
2016-07-20 19:36:15 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
2017-01-12 14:38:45 +03:00
|
|
|
ILogger $logger,
|
2017-04-13 23:50:44 +03:00
|
|
|
Manager $twoFactorManager) {
|
2016-04-11 13:08:00 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->session = $session;
|
|
|
|
$this->userSession = $userSession;
|
2016-04-18 13:14:07 +03:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2017-01-12 14:38:45 +03:00
|
|
|
$this->logger = $logger;
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->twoFactorManager = $twoFactorManager;
|
2016-04-18 13:14:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @UseSession
|
|
|
|
*
|
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
|
|
|
public function logout() {
|
2017-02-02 23:56:44 +03:00
|
|
|
$loginToken = $this->request->getCookie('nc_token');
|
2016-04-18 13:14:07 +03:00
|
|
|
if (!is_null($loginToken)) {
|
|
|
|
$this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken);
|
|
|
|
}
|
|
|
|
$this->userSession->logout();
|
|
|
|
|
|
|
|
return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm'));
|
2016-04-11 13:08:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @UseSession
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @param string $redirect_url
|
|
|
|
* @param string $remember_login
|
|
|
|
*
|
2016-05-06 17:31:40 +03:00
|
|
|
* @return TemplateResponse|RedirectResponse
|
2016-04-11 13:08:00 +03:00
|
|
|
*/
|
2016-04-25 15:10:55 +03:00
|
|
|
public function showLoginForm($user, $redirect_url, $remember_login) {
|
|
|
|
if ($this->userSession->isLoggedIn()) {
|
|
|
|
return new RedirectResponse(OC_Util::getDefaultPageUrl());
|
2016-04-11 13:08:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$parameters = array();
|
|
|
|
$loginMessages = $this->session->get('loginMessages');
|
|
|
|
$errors = [];
|
|
|
|
$messages = [];
|
2016-04-25 15:10:55 +03:00
|
|
|
if (is_array($loginMessages)) {
|
2016-04-11 13:08:00 +03:00
|
|
|
list($errors, $messages) = $loginMessages;
|
|
|
|
}
|
|
|
|
$this->session->remove('loginMessages');
|
|
|
|
foreach ($errors as $value) {
|
|
|
|
$parameters[$value] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameters['messages'] = $messages;
|
2016-04-15 20:51:00 +03:00
|
|
|
if (!is_null($user) && $user !== '') {
|
2016-04-15 20:02:19 +03:00
|
|
|
$parameters['loginName'] = $user;
|
2016-04-11 13:08:00 +03:00
|
|
|
$parameters['user_autofocus'] = false;
|
|
|
|
} else {
|
2016-04-15 20:02:19 +03:00
|
|
|
$parameters['loginName'] = '';
|
2016-04-11 13:08:00 +03:00
|
|
|
$parameters['user_autofocus'] = true;
|
|
|
|
}
|
|
|
|
if (!empty($redirect_url)) {
|
|
|
|
$parameters['redirect_url'] = $redirect_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameters['canResetPassword'] = true;
|
2016-05-24 10:23:25 +03:00
|
|
|
$parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', '');
|
|
|
|
if (!$parameters['resetPasswordLink']) {
|
2016-04-15 20:51:00 +03:00
|
|
|
if (!is_null($user) && $user !== '') {
|
2016-04-11 13:08:00 +03:00
|
|
|
$userObj = $this->userManager->get($user);
|
|
|
|
if ($userObj instanceof IUser) {
|
|
|
|
$parameters['canResetPassword'] = $userObj->canChangePassword();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 15:10:55 +03:00
|
|
|
$parameters['alt_login'] = OC_App::getAlternativeLogIns();
|
2016-04-11 13:08:00 +03:00
|
|
|
$parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0;
|
|
|
|
|
2016-04-15 20:51:00 +03:00
|
|
|
if (!is_null($user) && $user !== '') {
|
2016-04-15 20:02:19 +03:00
|
|
|
$parameters['loginName'] = $user;
|
2016-04-11 13:08:00 +03:00
|
|
|
$parameters['user_autofocus'] = false;
|
|
|
|
} else {
|
2016-04-15 20:02:19 +03:00
|
|
|
$parameters['loginName'] = '';
|
2016-04-11 13:08:00 +03:00
|
|
|
$parameters['user_autofocus'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new TemplateResponse(
|
2016-04-25 15:10:55 +03:00
|
|
|
$this->appName, 'login', $parameters, 'guest'
|
2016-04-11 13:08:00 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-09 20:01:50 +03:00
|
|
|
/**
|
|
|
|
* @param string $redirectUrl
|
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
|
|
|
private function generateRedirect($redirectUrl) {
|
|
|
|
if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) {
|
|
|
|
$location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl));
|
|
|
|
// Deny the redirect if the URL contains a @
|
|
|
|
// This prevents unvalidated redirects like ?redirect_url=:user@domain.com
|
|
|
|
if (strpos($location, '@') === false) {
|
|
|
|
return new RedirectResponse($location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new RedirectResponse(OC_Util::getDefaultPageUrl());
|
|
|
|
}
|
|
|
|
|
2016-04-25 15:10:55 +03:00
|
|
|
/**
|
|
|
|
* @PublicPage
|
|
|
|
* @UseSession
|
2016-08-09 20:01:50 +03:00
|
|
|
* @NoCSRFRequired
|
2017-04-13 23:50:44 +03:00
|
|
|
* @BruteForceProtection(action=login)
|
2016-04-25 15:10:55 +03:00
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @param string $password
|
|
|
|
* @param string $redirect_url
|
2016-09-06 22:41:15 +03:00
|
|
|
* @param boolean $remember_login
|
2016-12-08 12:45:24 +03:00
|
|
|
* @param string $timezone
|
|
|
|
* @param string $timezone_offset
|
2016-04-25 15:10:55 +03:00
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
2016-12-08 12:45:24 +03:00
|
|
|
public function tryLogin($user, $password, $redirect_url, $remember_login = false, $timezone = '', $timezone_offset = '') {
|
2016-11-30 16:59:59 +03:00
|
|
|
if(!is_string($user)) {
|
|
|
|
throw new \InvalidArgumentException('Username must be string');
|
|
|
|
}
|
2016-07-20 19:36:15 +03:00
|
|
|
|
2016-08-09 20:01:50 +03:00
|
|
|
// If the user is already logged in and the CSRF check does not pass then
|
|
|
|
// simply redirect the user to the correct page as required. This is the
|
|
|
|
// case when an user has already logged-in, in another tab.
|
|
|
|
if(!$this->request->passesCSRFCheck()) {
|
|
|
|
return $this->generateRedirect($redirect_url);
|
|
|
|
}
|
|
|
|
|
2017-02-01 22:16:51 +03:00
|
|
|
if ($this->userManager instanceof PublicEmitter) {
|
2017-02-01 20:13:41 +03:00
|
|
|
$this->userManager->emit('\OC\User', 'preLogin', array($user, $password));
|
|
|
|
}
|
2017-01-31 22:21:58 +03:00
|
|
|
|
2016-06-09 17:44:31 +03:00
|
|
|
$originalUser = $user;
|
2016-04-25 15:10:55 +03:00
|
|
|
// TODO: Add all the insane error handling
|
2016-05-11 12:23:25 +03:00
|
|
|
/* @var $loginResult IUser */
|
2017-01-12 14:38:45 +03:00
|
|
|
$loginResult = $this->userManager->checkPasswordNoLogging($user, $password);
|
2016-05-09 16:33:56 +03:00
|
|
|
if ($loginResult === false) {
|
2016-04-27 13:01:13 +03:00
|
|
|
$users = $this->userManager->getByEmail($user);
|
|
|
|
// we only allow login by email if unique
|
|
|
|
if (count($users) === 1) {
|
2016-05-09 12:04:18 +03:00
|
|
|
$user = $users[0]->getUID();
|
|
|
|
$loginResult = $this->userManager->checkPassword($user, $password);
|
2017-01-12 14:38:45 +03:00
|
|
|
} else {
|
|
|
|
$this->logger->warning('Login failed: \''. $user .'\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', ['app' => 'core']);
|
2016-04-27 13:01:13 +03:00
|
|
|
}
|
|
|
|
}
|
2016-05-09 16:33:56 +03:00
|
|
|
if ($loginResult === false) {
|
2017-04-13 23:50:44 +03:00
|
|
|
// Read current user and append if possible - we need to return the unmodified user otherwise we will leak the login name
|
|
|
|
$args = !is_null($user) ? ['user' => $originalUser] : [];
|
|
|
|
$response = new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args));
|
|
|
|
$response->throttle();
|
2016-05-04 10:05:03 +03:00
|
|
|
$this->session->set('loginMessages', [
|
2016-08-08 09:08:29 +03:00
|
|
|
['invalidpassword'], []
|
2016-05-04 10:05:03 +03:00
|
|
|
]);
|
2017-04-13 23:50:44 +03:00
|
|
|
return $response;
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
2016-05-20 18:54:46 +03:00
|
|
|
// TODO: remove password checks from above and let the user session handle failures
|
|
|
|
// requires https://github.com/owncloud/core/pull/24616
|
2016-11-30 15:28:36 +03:00
|
|
|
$this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]);
|
2016-12-02 23:07:45 +03:00
|
|
|
$this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, (int)$remember_login);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-08-23 13:54:45 +03:00
|
|
|
// User has successfully logged in, now remove the password reset link, when it is available
|
2016-08-23 16:01:38 +03:00
|
|
|
$this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword');
|
2016-08-23 13:54:45 +03:00
|
|
|
|
2016-09-19 16:33:30 +03:00
|
|
|
$this->session->set('last-password-confirm', $loginResult->getLastLogin());
|
|
|
|
|
2016-12-08 12:45:24 +03:00
|
|
|
if ($timezone_offset !== '') {
|
|
|
|
$this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone);
|
|
|
|
$this->session->set('timezone', $timezone_offset);
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) {
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login);
|
2016-08-29 19:36:39 +03:00
|
|
|
|
|
|
|
$providers = $this->twoFactorManager->getProviders($loginResult);
|
|
|
|
if (count($providers) === 1) {
|
|
|
|
// Single provider, hence we can redirect to that provider's challenge page directly
|
|
|
|
/* @var $provider IProvider */
|
|
|
|
$provider = array_pop($providers);
|
|
|
|
$url = 'core.TwoFactorChallenge.showChallenge';
|
|
|
|
$urlParams = [
|
|
|
|
'challengeProviderId' => $provider->getId(),
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$url = 'core.TwoFactorChallenge.selectChallenge';
|
|
|
|
$urlParams = [];
|
|
|
|
}
|
|
|
|
|
2016-06-01 14:54:08 +03:00
|
|
|
if (!is_null($redirect_url)) {
|
2016-08-29 19:36:39 +03:00
|
|
|
$urlParams['redirect_url'] = $redirect_url;
|
2016-06-01 14:54:08 +03:00
|
|
|
}
|
2016-08-29 19:36:39 +03:00
|
|
|
|
|
|
|
return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2016-09-06 22:41:15 +03:00
|
|
|
if ($remember_login) {
|
|
|
|
$this->userSession->createRememberMeToken($loginResult);
|
|
|
|
}
|
|
|
|
|
2016-08-09 20:01:50 +03:00
|
|
|
return $this->generateRedirect($redirect_url);
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
|
|
|
|
2016-09-19 16:33:30 +03:00
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @UseSession
|
2017-04-13 23:50:44 +03:00
|
|
|
* @BruteForceProtection(action=sudo)
|
2016-09-19 16:33:30 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* @param string $password
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function confirmPassword($password) {
|
2016-12-21 11:51:21 +03:00
|
|
|
$loginName = $this->userSession->getLoginName();
|
|
|
|
$loginResult = $this->userManager->checkPassword($loginName, $password);
|
2016-09-19 16:33:30 +03:00
|
|
|
if ($loginResult === false) {
|
2017-04-13 23:50:44 +03:00
|
|
|
$response = new DataResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
$response->throttle();
|
|
|
|
return $response;
|
2016-09-19 16:33:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$confirmTimestamp = time();
|
|
|
|
$this->session->set('last-password-confirm', $confirmTimestamp);
|
|
|
|
return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK);
|
|
|
|
}
|
2016-04-11 13:08:00 +03:00
|
|
|
}
|