You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoginControllerTest.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Tests\Core\Controller;
  22. use OC\Authentication\Login\Chain as LoginChain;
  23. use OC\Authentication\Login\LoginData;
  24. use OC\Authentication\Login\LoginResult;
  25. use OC\Authentication\TwoFactorAuth\Manager;
  26. use OC\Core\Controller\LoginController;
  27. use OC\Security\Bruteforce\Throttler;
  28. use OC\User\Session;
  29. use OCP\AppFramework\Http\RedirectResponse;
  30. use OCP\AppFramework\Http\TemplateResponse;
  31. use OCP\Defaults;
  32. use OCP\IConfig;
  33. use OCP\IInitialStateService;
  34. use OCP\IL10N;
  35. use OCP\IRequest;
  36. use OCP\ISession;
  37. use OCP\IURLGenerator;
  38. use OCP\IUser;
  39. use OCP\IUserManager;
  40. use OCP\Notification\IManager;
  41. use PHPUnit\Framework\MockObject\MockObject;
  42. use Test\TestCase;
  43. class LoginControllerTest extends TestCase {
  44. /** @var LoginController */
  45. private $loginController;
  46. /** @var IRequest|MockObject */
  47. private $request;
  48. /** @var IUserManager|MockObject */
  49. private $userManager;
  50. /** @var IConfig|MockObject */
  51. private $config;
  52. /** @var ISession|MockObject */
  53. private $session;
  54. /** @var Session|MockObject */
  55. private $userSession;
  56. /** @var IURLGenerator|MockObject */
  57. private $urlGenerator;
  58. /** @var Manager|MockObject */
  59. private $twoFactorManager;
  60. /** @var Defaults|MockObject */
  61. private $defaults;
  62. /** @var Throttler|MockObject */
  63. private $throttler;
  64. /** @var LoginChain|MockObject */
  65. private $chain;
  66. /** @var IInitialStateService|MockObject */
  67. private $initialStateService;
  68. /** @var \OC\Authentication\WebAuthn\Manager|MockObject */
  69. private $webAuthnManager;
  70. /** @var IManager|MockObject */
  71. private $notificationManager;
  72. /** @var IL10N|MockObject */
  73. private $l;
  74. protected function setUp(): void {
  75. parent::setUp();
  76. $this->request = $this->createMock(IRequest::class);
  77. $this->userManager = $this->createMock(\OC\User\Manager::class);
  78. $this->config = $this->createMock(IConfig::class);
  79. $this->session = $this->createMock(ISession::class);
  80. $this->userSession = $this->createMock(Session::class);
  81. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  82. $this->twoFactorManager = $this->createMock(Manager::class);
  83. $this->defaults = $this->createMock(Defaults::class);
  84. $this->throttler = $this->createMock(Throttler::class);
  85. $this->chain = $this->createMock(LoginChain::class);
  86. $this->initialStateService = $this->createMock(IInitialStateService::class);
  87. $this->webAuthnManager = $this->createMock(\OC\Authentication\WebAuthn\Manager::class);
  88. $this->notificationManager = $this->createMock(IManager::class);
  89. $this->l = $this->createMock(IL10N::class);
  90. $this->l->expects($this->any())
  91. ->method('t')
  92. ->willReturnCallback(function ($text, $parameters = []) {
  93. return vsprintf($text, $parameters);
  94. });
  95. $this->request->method('getRemoteAddress')
  96. ->willReturn('1.2.3.4');
  97. $this->throttler->method('getDelay')
  98. ->with(
  99. $this->equalTo('1.2.3.4'),
  100. $this->equalTo('')
  101. )->willReturn(1000);
  102. $this->loginController = new LoginController(
  103. 'core',
  104. $this->request,
  105. $this->userManager,
  106. $this->config,
  107. $this->session,
  108. $this->userSession,
  109. $this->urlGenerator,
  110. $this->defaults,
  111. $this->throttler,
  112. $this->chain,
  113. $this->initialStateService,
  114. $this->webAuthnManager,
  115. $this->notificationManager,
  116. $this->l
  117. );
  118. }
  119. public function testLogoutWithoutToken() {
  120. $this->request
  121. ->expects($this->once())
  122. ->method('getCookie')
  123. ->with('nc_token')
  124. ->willReturn(null);
  125. $this->request
  126. ->expects($this->once())
  127. ->method('isUserAgent')
  128. ->willReturn(false);
  129. $this->config
  130. ->expects($this->never())
  131. ->method('deleteUserValue');
  132. $this->urlGenerator
  133. ->expects($this->once())
  134. ->method('linkToRouteAbsolute')
  135. ->with('core.login.showLoginForm')
  136. ->willReturn('/login');
  137. $expected = new RedirectResponse('/login');
  138. $expected->addHeader('Clear-Site-Data', '"cache", "storage"');
  139. $this->assertEquals($expected, $this->loginController->logout());
  140. }
  141. public function testLogoutNoClearSiteData() {
  142. $this->request
  143. ->expects($this->once())
  144. ->method('getCookie')
  145. ->with('nc_token')
  146. ->willReturn(null);
  147. $this->request
  148. ->expects($this->once())
  149. ->method('isUserAgent')
  150. ->willReturn(true);
  151. $this->urlGenerator
  152. ->expects($this->once())
  153. ->method('linkToRouteAbsolute')
  154. ->with('core.login.showLoginForm')
  155. ->willReturn('/login');
  156. $expected = new RedirectResponse('/login');
  157. $this->assertEquals($expected, $this->loginController->logout());
  158. }
  159. public function testLogoutWithToken() {
  160. $this->request
  161. ->expects($this->once())
  162. ->method('getCookie')
  163. ->with('nc_token')
  164. ->willReturn('MyLoginToken');
  165. $this->request
  166. ->expects($this->once())
  167. ->method('isUserAgent')
  168. ->willReturn(false);
  169. $user = $this->createMock(IUser::class);
  170. $user
  171. ->expects($this->once())
  172. ->method('getUID')
  173. ->willReturn('JohnDoe');
  174. $this->userSession
  175. ->expects($this->once())
  176. ->method('getUser')
  177. ->willReturn($user);
  178. $this->config
  179. ->expects($this->once())
  180. ->method('deleteUserValue')
  181. ->with('JohnDoe', 'login_token', 'MyLoginToken');
  182. $this->urlGenerator
  183. ->expects($this->once())
  184. ->method('linkToRouteAbsolute')
  185. ->with('core.login.showLoginForm')
  186. ->willReturn('/login');
  187. $expected = new RedirectResponse('/login');
  188. $expected->addHeader('Clear-Site-Data', '"cache", "storage"');
  189. $this->assertEquals($expected, $this->loginController->logout());
  190. }
  191. public function testShowLoginFormForLoggedInUsers() {
  192. $this->userSession
  193. ->expects($this->once())
  194. ->method('isLoggedIn')
  195. ->willReturn(true);
  196. $this->urlGenerator
  197. ->expects($this->once())
  198. ->method('linkToDefaultPageUrl')
  199. ->willReturn('/default/foo');
  200. $expectedResponse = new RedirectResponse('/default/foo');
  201. $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', ''));
  202. }
  203. public function testShowLoginFormWithErrorsInSession() {
  204. $this->userSession
  205. ->expects($this->once())
  206. ->method('isLoggedIn')
  207. ->willReturn(false);
  208. $this->session
  209. ->expects($this->once())
  210. ->method('get')
  211. ->with('loginMessages')
  212. ->willReturn(
  213. [
  214. [
  215. 'ErrorArray1',
  216. 'ErrorArray2',
  217. ],
  218. [
  219. 'MessageArray1',
  220. 'MessageArray2',
  221. ],
  222. ]
  223. );
  224. $this->initialStateService->expects($this->at(0))
  225. ->method('provideInitialState')
  226. ->with(
  227. 'core',
  228. 'loginMessages',
  229. [
  230. 'MessageArray1',
  231. 'MessageArray2',
  232. 'This community release of Nextcloud is unsupported and push notifications are limited.',
  233. ]
  234. );
  235. $this->initialStateService->expects($this->at(1))
  236. ->method('provideInitialState')
  237. ->with(
  238. 'core',
  239. 'loginErrors',
  240. [
  241. 'ErrorArray1',
  242. 'ErrorArray2',
  243. ]
  244. );
  245. $expectedResponse = new TemplateResponse(
  246. 'core',
  247. 'login',
  248. [
  249. 'alt_login' => [],
  250. 'pageTitle' => 'Login'
  251. ],
  252. 'guest'
  253. );
  254. $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', ''));
  255. }
  256. public function testShowLoginFormForFlowAuth() {
  257. $this->userSession
  258. ->expects($this->once())
  259. ->method('isLoggedIn')
  260. ->willReturn(false);
  261. $this->initialStateService->expects($this->at(4))
  262. ->method('provideInitialState')
  263. ->with(
  264. 'core',
  265. 'loginRedirectUrl',
  266. 'login/flow'
  267. );
  268. $expectedResponse = new TemplateResponse(
  269. 'core',
  270. 'login',
  271. [
  272. 'alt_login' => [],
  273. 'pageTitle' => 'Login'
  274. ],
  275. 'guest'
  276. );
  277. $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', 'login/flow'));
  278. }
  279. /**
  280. * @return array
  281. */
  282. public function passwordResetDataProvider(): array {
  283. return [
  284. [
  285. true,
  286. true,
  287. ],
  288. [
  289. false,
  290. false,
  291. ],
  292. ];
  293. }
  294. /**
  295. * @dataProvider passwordResetDataProvider
  296. */
  297. public function testShowLoginFormWithPasswordResetOption($canChangePassword,
  298. $expectedResult) {
  299. $this->userSession
  300. ->expects($this->once())
  301. ->method('isLoggedIn')
  302. ->willReturn(false);
  303. $this->config
  304. ->expects($this->exactly(2))
  305. ->method('getSystemValue')
  306. ->willReturnMap([
  307. ['login_form_autocomplete', true, true],
  308. ['lost_password_link', '', false],
  309. ]);
  310. $user = $this->createMock(IUser::class);
  311. $user
  312. ->expects($this->once())
  313. ->method('canChangePassword')
  314. ->willReturn($canChangePassword);
  315. $this->userManager
  316. ->expects($this->once())
  317. ->method('get')
  318. ->with('LdapUser')
  319. ->willReturn($user);
  320. $this->initialStateService->expects($this->at(2))
  321. ->method('provideInitialState')
  322. ->with(
  323. 'core',
  324. 'loginUsername',
  325. 'LdapUser'
  326. );
  327. $this->initialStateService->expects($this->at(6))
  328. ->method('provideInitialState')
  329. ->with(
  330. 'core',
  331. 'loginCanResetPassword',
  332. $expectedResult
  333. );
  334. $expectedResponse = new TemplateResponse(
  335. 'core',
  336. 'login',
  337. [
  338. 'alt_login' => [],
  339. 'pageTitle' => 'Login'
  340. ],
  341. 'guest'
  342. );
  343. $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('LdapUser', ''));
  344. }
  345. public function testShowLoginFormForUserNamed0() {
  346. $this->userSession
  347. ->expects($this->once())
  348. ->method('isLoggedIn')
  349. ->willReturn(false);
  350. $this->config
  351. ->expects($this->exactly(2))
  352. ->method('getSystemValue')
  353. ->willReturnMap([
  354. ['login_form_autocomplete', true, true],
  355. ['lost_password_link', '', false],
  356. ]);
  357. $user = $this->createMock(IUser::class);
  358. $user->expects($this->once())
  359. ->method('canChangePassword')
  360. ->willReturn(false);
  361. $this->userManager
  362. ->expects($this->once())
  363. ->method('get')
  364. ->with('0')
  365. ->willReturn($user);
  366. $this->initialStateService->expects($this->at(3))
  367. ->method('provideInitialState')
  368. ->with(
  369. 'core',
  370. 'loginAutocomplete',
  371. true
  372. );
  373. $this->initialStateService->expects($this->at(5))
  374. ->method('provideInitialState')
  375. ->with(
  376. 'core',
  377. 'loginResetPasswordLink',
  378. false
  379. );
  380. $this->initialStateService->expects($this->at(6))
  381. ->method('provideInitialState')
  382. ->with(
  383. 'core',
  384. 'loginCanResetPassword',
  385. false
  386. );
  387. $expectedResponse = new TemplateResponse(
  388. 'core',
  389. 'login',
  390. [
  391. 'alt_login' => [],
  392. 'pageTitle' => 'Login'
  393. ],
  394. 'guest'
  395. );
  396. $this->assertEquals($expectedResponse, $this->loginController->showLoginForm('0', ''));
  397. }
  398. public function testLoginWithInvalidCredentials() {
  399. $user = 'MyUserName';
  400. $password = 'secret';
  401. $loginPageUrl = '/login?redirect_url=/apps/files';
  402. $this->request
  403. ->expects($this->once())
  404. ->method('passesCSRFCheck')
  405. ->willReturn(true);
  406. $loginData = new LoginData(
  407. $this->request,
  408. $user,
  409. $password,
  410. '/apps/files'
  411. );
  412. $loginResult = LoginResult::failure($loginData, LoginController::LOGIN_MSG_INVALIDPASSWORD);
  413. $this->chain->expects($this->once())
  414. ->method('process')
  415. ->with($this->equalTo($loginData))
  416. ->willReturn($loginResult);
  417. $this->urlGenerator->expects($this->once())
  418. ->method('linkToRoute')
  419. ->with('core.login.showLoginForm', [
  420. 'user' => $user,
  421. 'redirect_url' => '/apps/files',
  422. 'direct' => 1,
  423. ])
  424. ->willReturn($loginPageUrl);
  425. $expected = new RedirectResponse($loginPageUrl);
  426. $expected->throttle(['user' => 'MyUserName']);
  427. $response = $this->loginController->tryLogin($user, $password, '/apps/files');
  428. $this->assertEquals($expected, $response);
  429. }
  430. public function testLoginWithValidCredentials() {
  431. $user = 'MyUserName';
  432. $password = 'secret';
  433. $this->request
  434. ->expects($this->once())
  435. ->method('passesCSRFCheck')
  436. ->willReturn(true);
  437. $loginData = new LoginData(
  438. $this->request,
  439. $user,
  440. $password
  441. );
  442. $loginResult = LoginResult::success($loginData);
  443. $this->chain->expects($this->once())
  444. ->method('process')
  445. ->with($this->equalTo($loginData))
  446. ->willReturn($loginResult);
  447. $this->urlGenerator
  448. ->expects($this->once())
  449. ->method('linkToDefaultPageUrl')
  450. ->willReturn('/default/foo');
  451. $expected = new RedirectResponse('/default/foo');
  452. $this->assertEquals($expected, $this->loginController->tryLogin($user, $password));
  453. }
  454. public function testLoginWithoutPassedCsrfCheckAndNotLoggedIn() {
  455. /** @var IUser|MockObject $user */
  456. $user = $this->createMock(IUser::class);
  457. $user->expects($this->any())
  458. ->method('getUID')
  459. ->willReturn('jane');
  460. $password = 'secret';
  461. $originalUrl = 'another%20url';
  462. $this->request
  463. ->expects($this->once())
  464. ->method('passesCSRFCheck')
  465. ->willReturn(false);
  466. $this->userSession->expects($this->once())
  467. ->method('isLoggedIn')
  468. ->with()
  469. ->willReturn(false);
  470. $this->config->expects($this->never())
  471. ->method('deleteUserValue');
  472. $this->userSession->expects($this->never())
  473. ->method('createRememberMeToken');
  474. $this->urlGenerator
  475. ->expects($this->once())
  476. ->method('linkToDefaultPageUrl')
  477. ->willReturn('/default/foo');
  478. $expected = new RedirectResponse('/default/foo');
  479. $this->assertEquals($expected, $this->loginController->tryLogin('Jane', $password, $originalUrl));
  480. }
  481. public function testLoginWithoutPassedCsrfCheckAndLoggedIn() {
  482. /** @var IUser|MockObject $user */
  483. $user = $this->createMock(IUser::class);
  484. $user->expects($this->any())
  485. ->method('getUID')
  486. ->willReturn('jane');
  487. $password = 'secret';
  488. $originalUrl = 'another url';
  489. $redirectUrl = 'http://localhost/another url';
  490. $this->request
  491. ->expects($this->once())
  492. ->method('passesCSRFCheck')
  493. ->willReturn(false);
  494. $this->userSession->expects($this->once())
  495. ->method('isLoggedIn')
  496. ->with()
  497. ->willReturn(true);
  498. $this->urlGenerator->expects($this->once())
  499. ->method('getAbsoluteURL')
  500. ->with(urldecode($originalUrl))
  501. ->willReturn($redirectUrl);
  502. $this->config->expects($this->never())
  503. ->method('deleteUserValue');
  504. $this->userSession->expects($this->never())
  505. ->method('createRememberMeToken');
  506. $this->config
  507. ->method('getSystemValue')
  508. ->with('remember_login_cookie_lifetime')
  509. ->willReturn(1234);
  510. $expected = new RedirectResponse($redirectUrl);
  511. $this->assertEquals($expected, $this->loginController->tryLogin('Jane', $password, $originalUrl));
  512. }
  513. public function testLoginWithValidCredentialsAndRedirectUrl() {
  514. $user = 'MyUserName';
  515. $password = 'secret';
  516. $redirectUrl = 'https://next.cloud/apps/mail';
  517. $this->request
  518. ->expects($this->once())
  519. ->method('passesCSRFCheck')
  520. ->willReturn(true);
  521. $loginData = new LoginData(
  522. $this->request,
  523. $user,
  524. $password,
  525. '/apps/mail'
  526. );
  527. $loginResult = LoginResult::success($loginData);
  528. $this->chain->expects($this->once())
  529. ->method('process')
  530. ->with($this->equalTo($loginData))
  531. ->willReturn($loginResult);
  532. $this->userSession->expects($this->once())
  533. ->method('isLoggedIn')
  534. ->willReturn(true);
  535. $this->urlGenerator->expects($this->once())
  536. ->method('getAbsoluteURL')
  537. ->with('/apps/mail')
  538. ->willReturn($redirectUrl);
  539. $expected = new RedirectResponse($redirectUrl);
  540. $response = $this->loginController->tryLogin($user, $password, '/apps/mail');
  541. $this->assertEquals($expected, $response);
  542. }
  543. public function testToNotLeakLoginName() {
  544. $this->request
  545. ->expects($this->once())
  546. ->method('passesCSRFCheck')
  547. ->willReturn(true);
  548. $loginPageUrl = '/login?redirect_url=/apps/files';
  549. $loginData = new LoginData(
  550. $this->request,
  551. 'john@doe.com',
  552. 'just wrong',
  553. '/apps/files'
  554. );
  555. $loginResult = LoginResult::failure($loginData, LoginController::LOGIN_MSG_INVALIDPASSWORD);
  556. $this->chain->expects($this->once())
  557. ->method('process')
  558. ->with($this->equalTo($loginData))
  559. ->willReturnCallback(function (LoginData $data) use ($loginResult) {
  560. $data->setUsername('john');
  561. return $loginResult;
  562. });
  563. $this->urlGenerator->expects($this->once())
  564. ->method('linkToRoute')
  565. ->with('core.login.showLoginForm', [
  566. 'user' => 'john@doe.com',
  567. 'redirect_url' => '/apps/files',
  568. 'direct' => 1,
  569. ])
  570. ->willReturn($loginPageUrl);
  571. $expected = new RedirectResponse($loginPageUrl);
  572. $expected->throttle(['user' => 'john']);
  573. $response = $this->loginController->tryLogin(
  574. 'john@doe.com',
  575. 'just wrong',
  576. '/apps/files'
  577. );
  578. $this->assertEquals($expected, $response);
  579. }
  580. }