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

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