LoginControllerTest.php 16 KB

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