LoginController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Core\Controller;
  26. use OC\AppFramework\Utility\TimeFactory;
  27. use OC\Authentication\TwoFactorAuth\Manager;
  28. use OC\Security\Bruteforce\Throttler;
  29. use OC\User\Session;
  30. use OC_App;
  31. use OC_Util;
  32. use OCP\AppFramework\Controller;
  33. use OCP\AppFramework\Http\RedirectResponse;
  34. use OCP\AppFramework\Http\TemplateResponse;
  35. use OCP\IConfig;
  36. use OCP\IRequest;
  37. use OCP\ISession;
  38. use OCP\IURLGenerator;
  39. use OCP\IUser;
  40. use OCP\IUserManager;
  41. class LoginController extends Controller {
  42. /** @var IUserManager */
  43. private $userManager;
  44. /** @var IConfig */
  45. private $config;
  46. /** @var ISession */
  47. private $session;
  48. /** @var Session */
  49. private $userSession;
  50. /** @var IURLGenerator */
  51. private $urlGenerator;
  52. /** @var Manager */
  53. private $twoFactorManager;
  54. /** @var Throttler */
  55. private $throttler;
  56. /**
  57. * @param string $appName
  58. * @param IRequest $request
  59. * @param IUserManager $userManager
  60. * @param IConfig $config
  61. * @param ISession $session
  62. * @param Session $userSession
  63. * @param IURLGenerator $urlGenerator
  64. * @param Manager $twoFactorManager
  65. * @param Throttler $throttler
  66. */
  67. function __construct($appName,
  68. IRequest $request,
  69. IUserManager $userManager,
  70. IConfig $config,
  71. ISession $session,
  72. Session $userSession,
  73. IURLGenerator $urlGenerator,
  74. Manager $twoFactorManager,
  75. Throttler $throttler) {
  76. parent::__construct($appName, $request);
  77. $this->userManager = $userManager;
  78. $this->config = $config;
  79. $this->session = $session;
  80. $this->userSession = $userSession;
  81. $this->urlGenerator = $urlGenerator;
  82. $this->twoFactorManager = $twoFactorManager;
  83. $this->throttler = $throttler;
  84. }
  85. /**
  86. * @NoAdminRequired
  87. * @UseSession
  88. *
  89. * @return RedirectResponse
  90. */
  91. public function logout() {
  92. $loginToken = $this->request->getCookie('oc_token');
  93. if (!is_null($loginToken)) {
  94. $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken);
  95. }
  96. $this->userSession->logout();
  97. return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm'));
  98. }
  99. /**
  100. * @PublicPage
  101. * @NoCSRFRequired
  102. * @UseSession
  103. *
  104. * @param string $user
  105. * @param string $redirect_url
  106. * @param string $remember_login
  107. *
  108. * @return TemplateResponse|RedirectResponse
  109. */
  110. public function showLoginForm($user, $redirect_url, $remember_login) {
  111. if ($this->userSession->isLoggedIn()) {
  112. return new RedirectResponse(OC_Util::getDefaultPageUrl());
  113. }
  114. $parameters = array();
  115. $loginMessages = $this->session->get('loginMessages');
  116. $errors = [];
  117. $messages = [];
  118. if (is_array($loginMessages)) {
  119. list($errors, $messages) = $loginMessages;
  120. }
  121. $this->session->remove('loginMessages');
  122. foreach ($errors as $value) {
  123. $parameters[$value] = true;
  124. }
  125. $parameters['messages'] = $messages;
  126. if (!is_null($user) && $user !== '') {
  127. $parameters['loginName'] = $user;
  128. $parameters['user_autofocus'] = false;
  129. } else {
  130. $parameters['loginName'] = '';
  131. $parameters['user_autofocus'] = true;
  132. }
  133. if (!empty($redirect_url)) {
  134. $parameters['redirect_url'] = $redirect_url;
  135. }
  136. $parameters['canResetPassword'] = true;
  137. $parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', '');
  138. if (!$parameters['resetPasswordLink']) {
  139. if (!is_null($user) && $user !== '') {
  140. $userObj = $this->userManager->get($user);
  141. if ($userObj instanceof IUser) {
  142. $parameters['canResetPassword'] = $userObj->canChangePassword();
  143. }
  144. }
  145. }
  146. $parameters['alt_login'] = OC_App::getAlternativeLogIns();
  147. $parameters['rememberLoginAllowed'] = OC_Util::rememberLoginAllowed();
  148. $parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0;
  149. if (!is_null($user) && $user !== '') {
  150. $parameters['loginName'] = $user;
  151. $parameters['user_autofocus'] = false;
  152. } else {
  153. $parameters['loginName'] = '';
  154. $parameters['user_autofocus'] = true;
  155. }
  156. return new TemplateResponse(
  157. $this->appName, 'login', $parameters, 'guest'
  158. );
  159. }
  160. /**
  161. * @PublicPage
  162. * @UseSession
  163. *
  164. * @param string $user
  165. * @param string $password
  166. * @param string $redirect_url
  167. * @return RedirectResponse
  168. */
  169. public function tryLogin($user, $password, $redirect_url) {
  170. $currentDelay = $this->throttler->getDelay($this->request->getRemoteAddress());
  171. $this->throttler->sleepDelay($this->request->getRemoteAddress());
  172. $originalUser = $user;
  173. // TODO: Add all the insane error handling
  174. /* @var $loginResult IUser */
  175. $loginResult = $this->userManager->checkPassword($user, $password);
  176. if ($loginResult === false) {
  177. $users = $this->userManager->getByEmail($user);
  178. // we only allow login by email if unique
  179. if (count($users) === 1) {
  180. $user = $users[0]->getUID();
  181. $loginResult = $this->userManager->checkPassword($user, $password);
  182. }
  183. }
  184. if ($loginResult === false) {
  185. $this->throttler->registerAttempt('login', $this->request->getRemoteAddress(), ['user' => $originalUser]);
  186. if($currentDelay === 0) {
  187. $this->throttler->sleepDelay($this->request->getRemoteAddress());
  188. }
  189. $this->session->set('loginMessages', [
  190. ['invalidpassword']
  191. ]);
  192. // Read current user and append if possible - we need to return the unmodified user otherwise we will leak the login name
  193. $args = !is_null($user) ? ['user' => $originalUser] : [];
  194. return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args));
  195. }
  196. // TODO: remove password checks from above and let the user session handle failures
  197. // requires https://github.com/owncloud/core/pull/24616
  198. $this->userSession->login($user, $password);
  199. $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password);
  200. if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) {
  201. $this->twoFactorManager->prepareTwoFactorLogin($loginResult);
  202. if (!is_null($redirect_url)) {
  203. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge', [
  204. 'redirect_url' => $redirect_url
  205. ]));
  206. }
  207. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
  208. }
  209. if (!is_null($redirect_url) && $this->userSession->isLoggedIn()) {
  210. $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirect_url));
  211. // Deny the redirect if the URL contains a @
  212. // This prevents unvalidated redirects like ?redirect_url=:user@domain.com
  213. if (strpos($location, '@') === false) {
  214. return new RedirectResponse($location);
  215. }
  216. }
  217. return new RedirectResponse(OC_Util::getDefaultPageUrl());
  218. }
  219. }