1
0

SessionTest.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\User;
  8. use OC\AppFramework\Http\Request;
  9. use OC\Authentication\Events\LoginFailed;
  10. use OC\Authentication\Exceptions\InvalidTokenException;
  11. use OC\Authentication\Exceptions\PasswordlessTokenException;
  12. use OC\Authentication\Exceptions\PasswordLoginForbiddenException;
  13. use OC\Authentication\Token\IProvider;
  14. use OC\Authentication\Token\IToken;
  15. use OC\Authentication\Token\PublicKeyToken;
  16. use OC\Security\CSRF\CsrfTokenManager;
  17. use OC\Session\Memory;
  18. use OC\User\LoginException;
  19. use OC\User\Manager;
  20. use OC\User\Session;
  21. use OC\User\User;
  22. use OCA\DAV\Connector\Sabre\Auth;
  23. use OCP\AppFramework\Utility\ITimeFactory;
  24. use OCP\EventDispatcher\IEventDispatcher;
  25. use OCP\ICacheFactory;
  26. use OCP\IConfig;
  27. use OCP\IRequest;
  28. use OCP\IRequestId;
  29. use OCP\ISession;
  30. use OCP\IUser;
  31. use OCP\Lockdown\ILockdownManager;
  32. use OCP\Security\Bruteforce\IThrottler;
  33. use OCP\Security\ISecureRandom;
  34. use OCP\User\Events\PostLoginEvent;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Psr\Log\LoggerInterface;
  37. use function array_diff;
  38. use function get_class_methods;
  39. /**
  40. * @group DB
  41. * @package Test\User
  42. */
  43. class SessionTest extends \Test\TestCase {
  44. /** @var ITimeFactory|MockObject */
  45. private $timeFactory;
  46. /** @var IProvider|MockObject */
  47. private $tokenProvider;
  48. /** @var IConfig|MockObject */
  49. private $config;
  50. /** @var IThrottler|MockObject */
  51. private $throttler;
  52. /** @var ISecureRandom|MockObject */
  53. private $random;
  54. /** @var Manager|MockObject */
  55. private $manager;
  56. /** @var ISession|MockObject */
  57. private $session;
  58. /** @var Session|MockObject */
  59. private $userSession;
  60. /** @var ILockdownManager|MockObject */
  61. private $lockdownManager;
  62. /** @var LoggerInterface|MockObject */
  63. private $logger;
  64. /** @var IEventDispatcher|MockObject */
  65. private $dispatcher;
  66. protected function setUp(): void {
  67. parent::setUp();
  68. $this->timeFactory = $this->createMock(ITimeFactory::class);
  69. $this->timeFactory->expects($this->any())
  70. ->method('getTime')
  71. ->willReturn(10000);
  72. $this->tokenProvider = $this->createMock(IProvider::class);
  73. $this->config = $this->createMock(IConfig::class);
  74. $this->throttler = $this->createMock(IThrottler::class);
  75. $this->random = $this->createMock(ISecureRandom::class);
  76. $this->manager = $this->createMock(Manager::class);
  77. $this->session = $this->createMock(ISession::class);
  78. $this->lockdownManager = $this->createMock(ILockdownManager::class);
  79. $this->logger = $this->createMock(LoggerInterface::class);
  80. $this->dispatcher = $this->createMock(IEventDispatcher::class);
  81. $this->userSession = $this->getMockBuilder(Session::class)
  82. ->setConstructorArgs([
  83. $this->manager,
  84. $this->session,
  85. $this->timeFactory,
  86. $this->tokenProvider,
  87. $this->config,
  88. $this->random,
  89. $this->lockdownManager,
  90. $this->logger,
  91. $this->dispatcher
  92. ])
  93. ->setMethods([
  94. 'setMagicInCookie',
  95. ])
  96. ->getMock();
  97. \OC_User::setIncognitoMode(false);
  98. }
  99. public function isLoggedInData() {
  100. return [
  101. [true],
  102. [false],
  103. ];
  104. }
  105. /**
  106. * @dataProvider isLoggedInData
  107. */
  108. public function testIsLoggedIn($isLoggedIn) {
  109. $session = $this->createMock(Memory::class);
  110. $manager = $this->createMock(Manager::class);
  111. $userSession = $this->getMockBuilder(Session::class)
  112. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  113. ->setMethods([
  114. 'getUser'
  115. ])
  116. ->getMock();
  117. $user = new User('sepp', null, $this->createMock(IEventDispatcher::class));
  118. $userSession->expects($this->once())
  119. ->method('getUser')
  120. ->willReturn($isLoggedIn ? $user : null);
  121. $this->assertEquals($isLoggedIn, $userSession->isLoggedIn());
  122. }
  123. public function testSetUser() {
  124. $session = $this->createMock(Memory::class);
  125. $session->expects($this->once())
  126. ->method('set')
  127. ->with('user_id', 'foo');
  128. $manager = $this->createMock(Manager::class);
  129. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  130. $user = $this->createMock(IUser::class);
  131. $user->expects($this->once())
  132. ->method('getUID')
  133. ->willReturn('foo');
  134. $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
  135. $userSession->setUser($user);
  136. }
  137. public function testLoginValidPasswordEnabled() {
  138. $session = $this->createMock(Memory::class);
  139. $session->expects($this->once())
  140. ->method('regenerateId');
  141. $this->tokenProvider->expects($this->once())
  142. ->method('getToken')
  143. ->with('bar')
  144. ->will($this->throwException(new InvalidTokenException()));
  145. $session->expects($this->exactly(2))
  146. ->method('set')
  147. ->with($this->callback(function ($key) {
  148. switch ($key) {
  149. case 'user_id':
  150. case 'loginname':
  151. return true;
  152. break;
  153. default:
  154. return false;
  155. break;
  156. }
  157. }, 'foo'));
  158. $managerMethods = get_class_methods(Manager::class);
  159. //keep following methods intact in order to ensure hooks are working
  160. $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
  161. $manager = $this->getMockBuilder(Manager::class)
  162. ->setMethods($mockedManagerMethods)
  163. ->setConstructorArgs([
  164. $this->config,
  165. $this->createMock(ICacheFactory::class),
  166. $this->createMock(IEventDispatcher::class),
  167. $this->createMock(LoggerInterface::class),
  168. ])
  169. ->getMock();
  170. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  171. $user = $this->createMock(IUser::class);
  172. $user->expects($this->any())
  173. ->method('isEnabled')
  174. ->willReturn(true);
  175. $user->expects($this->any())
  176. ->method('getUID')
  177. ->willReturn('foo');
  178. $user->expects($this->once())
  179. ->method('updateLastLoginTimestamp');
  180. $manager->expects($this->once())
  181. ->method('checkPasswordNoLogging')
  182. ->with('foo', 'bar')
  183. ->willReturn($user);
  184. $userSession = $this->getMockBuilder(Session::class)
  185. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  186. ->setMethods([
  187. 'prepareUserLogin'
  188. ])
  189. ->getMock();
  190. $userSession->expects($this->once())
  191. ->method('prepareUserLogin');
  192. $this->dispatcher->expects($this->once())
  193. ->method('dispatchTyped')
  194. ->with(
  195. $this->callback(function (PostLoginEvent $e) {
  196. return $e->getUser()->getUID() === 'foo' &&
  197. $e->getPassword() === 'bar' &&
  198. $e->isTokenLogin() === false;
  199. })
  200. );
  201. $userSession->login('foo', 'bar');
  202. $this->assertEquals($user, $userSession->getUser());
  203. }
  204. public function testLoginValidPasswordDisabled() {
  205. $this->expectException(LoginException::class);
  206. $session = $this->createMock(Memory::class);
  207. $session->expects($this->never())
  208. ->method('set');
  209. $session->expects($this->once())
  210. ->method('regenerateId');
  211. $this->tokenProvider->expects($this->once())
  212. ->method('getToken')
  213. ->with('bar')
  214. ->will($this->throwException(new InvalidTokenException()));
  215. $managerMethods = get_class_methods(Manager::class);
  216. //keep following methods intact in order to ensure hooks are working
  217. $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
  218. $manager = $this->getMockBuilder(Manager::class)
  219. ->setMethods($mockedManagerMethods)
  220. ->setConstructorArgs([
  221. $this->config,
  222. $this->createMock(ICacheFactory::class),
  223. $this->createMock(IEventDispatcher::class),
  224. $this->createMock(LoggerInterface::class),
  225. ])
  226. ->getMock();
  227. $user = $this->createMock(IUser::class);
  228. $user->expects($this->any())
  229. ->method('isEnabled')
  230. ->willReturn(false);
  231. $user->expects($this->never())
  232. ->method('updateLastLoginTimestamp');
  233. $manager->expects($this->once())
  234. ->method('checkPasswordNoLogging')
  235. ->with('foo', 'bar')
  236. ->willReturn($user);
  237. $this->dispatcher->expects($this->never())
  238. ->method('dispatch');
  239. $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
  240. $userSession->login('foo', 'bar');
  241. }
  242. public function testLoginInvalidPassword() {
  243. $session = $this->createMock(Memory::class);
  244. $managerMethods = get_class_methods(Manager::class);
  245. //keep following methods intact in order to ensure hooks are working
  246. $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
  247. $manager = $this->getMockBuilder(Manager::class)
  248. ->setMethods($mockedManagerMethods)
  249. ->setConstructorArgs([
  250. $this->config,
  251. $this->createMock(ICacheFactory::class),
  252. $this->createMock(IEventDispatcher::class),
  253. $this->createMock(LoggerInterface::class),
  254. ])
  255. ->getMock();
  256. $backend = $this->createMock(\Test\Util\User\Dummy::class);
  257. $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
  258. $user = $this->createMock(IUser::class);
  259. $session->expects($this->never())
  260. ->method('set');
  261. $session->expects($this->once())
  262. ->method('regenerateId');
  263. $this->tokenProvider->expects($this->once())
  264. ->method('getToken')
  265. ->with('bar')
  266. ->will($this->throwException(new InvalidTokenException()));
  267. $user->expects($this->never())
  268. ->method('isEnabled');
  269. $user->expects($this->never())
  270. ->method('updateLastLoginTimestamp');
  271. $manager->expects($this->once())
  272. ->method('checkPasswordNoLogging')
  273. ->with('foo', 'bar')
  274. ->willReturn(false);
  275. $this->dispatcher->expects($this->never())
  276. ->method('dispatch');
  277. $userSession->login('foo', 'bar');
  278. }
  279. public function testPasswordlessLoginNoLastCheckUpdate(): void {
  280. $session = $this->createMock(Memory::class);
  281. $managerMethods = get_class_methods(Manager::class);
  282. // Keep following methods intact in order to ensure hooks are working
  283. $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
  284. $manager = $this->getMockBuilder(Manager::class)
  285. ->setMethods($mockedManagerMethods)
  286. ->setConstructorArgs([
  287. $this->config,
  288. $this->createMock(ICacheFactory::class),
  289. $this->createMock(IEventDispatcher::class),
  290. $this->createMock(LoggerInterface::class),
  291. ])
  292. ->getMock();
  293. $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
  294. $session->expects($this->never())
  295. ->method('set');
  296. $session->expects($this->once())
  297. ->method('regenerateId');
  298. $token = new PublicKeyToken();
  299. $token->setLoginName('foo');
  300. $token->setLastCheck(0); // Never
  301. $token->setUid('foo');
  302. $this->tokenProvider
  303. ->method('getPassword')
  304. ->with($token)
  305. ->willThrowException(new PasswordlessTokenException());
  306. $this->tokenProvider
  307. ->method('getToken')
  308. ->with('app-password')
  309. ->willReturn($token);
  310. $this->tokenProvider->expects(self::never())
  311. ->method('updateToken');
  312. $userSession->login('foo', 'app-password');
  313. }
  314. public function testLoginLastCheckUpdate(): void {
  315. $session = $this->createMock(Memory::class);
  316. $managerMethods = get_class_methods(Manager::class);
  317. // Keep following methods intact in order to ensure hooks are working
  318. $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
  319. $manager = $this->getMockBuilder(Manager::class)
  320. ->setMethods($mockedManagerMethods)
  321. ->setConstructorArgs([
  322. $this->config,
  323. $this->createMock(ICacheFactory::class),
  324. $this->createMock(IEventDispatcher::class),
  325. $this->createMock(LoggerInterface::class),
  326. ])
  327. ->getMock();
  328. $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
  329. $session->expects($this->never())
  330. ->method('set');
  331. $session->expects($this->once())
  332. ->method('regenerateId');
  333. $token = new PublicKeyToken();
  334. $token->setLoginName('foo');
  335. $token->setLastCheck(0); // Never
  336. $token->setUid('foo');
  337. $this->tokenProvider
  338. ->method('getPassword')
  339. ->with($token)
  340. ->willReturn('secret');
  341. $this->tokenProvider
  342. ->method('getToken')
  343. ->with('app-password')
  344. ->willReturn($token);
  345. $this->tokenProvider->expects(self::once())
  346. ->method('updateToken');
  347. $userSession->login('foo', 'app-password');
  348. }
  349. public function testLoginNonExisting() {
  350. $session = $this->createMock(Memory::class);
  351. $manager = $this->createMock(Manager::class);
  352. $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
  353. $session->expects($this->never())
  354. ->method('set');
  355. $session->expects($this->once())
  356. ->method('regenerateId');
  357. $this->tokenProvider->expects($this->once())
  358. ->method('getToken')
  359. ->with('bar')
  360. ->will($this->throwException(new InvalidTokenException()));
  361. $manager->expects($this->once())
  362. ->method('checkPasswordNoLogging')
  363. ->with('foo', 'bar')
  364. ->willReturn(false);
  365. $userSession->login('foo', 'bar');
  366. }
  367. public function testLogClientInNoTokenPasswordWith2fa() {
  368. $this->expectException(PasswordLoginForbiddenException::class);
  369. $manager = $this->createMock(Manager::class);
  370. $session = $this->createMock(ISession::class);
  371. $request = $this->createMock(IRequest::class);
  372. /** @var Session $userSession */
  373. $userSession = $this->getMockBuilder(Session::class)
  374. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  375. ->setMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
  376. ->getMock();
  377. $this->tokenProvider->expects($this->once())
  378. ->method('getToken')
  379. ->with('doe')
  380. ->will($this->throwException(new InvalidTokenException()));
  381. $this->config->expects($this->once())
  382. ->method('getSystemValueBool')
  383. ->with('token_auth_enforced', false)
  384. ->willReturn(true);
  385. $request
  386. ->expects($this->any())
  387. ->method('getRemoteAddress')
  388. ->willReturn('192.168.0.1');
  389. $this->throttler
  390. ->expects($this->once())
  391. ->method('sleepDelayOrThrowOnMax')
  392. ->with('192.168.0.1');
  393. $this->throttler
  394. ->expects($this->any())
  395. ->method('getDelay')
  396. ->with('192.168.0.1')
  397. ->willReturn(0);
  398. $userSession->logClientIn('john', 'doe', $request, $this->throttler);
  399. }
  400. public function testLogClientInUnexist() {
  401. $manager = $this->createMock(Manager::class);
  402. $session = $this->createMock(ISession::class);
  403. $request = $this->createMock(IRequest::class);
  404. /** @var Session $userSession */
  405. $userSession = $this->getMockBuilder(Session::class)
  406. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  407. ->setMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
  408. ->getMock();
  409. $this->tokenProvider->expects($this->once())
  410. ->method('getToken')
  411. ->with('doe')
  412. ->will($this->throwException(new InvalidTokenException()));
  413. $this->config->expects($this->once())
  414. ->method('getSystemValueBool')
  415. ->with('token_auth_enforced', false)
  416. ->willReturn(false);
  417. $manager->method('getByEmail')
  418. ->with('unexist')
  419. ->willReturn([]);
  420. $this->assertFalse($userSession->logClientIn('unexist', 'doe', $request, $this->throttler));
  421. }
  422. public function testLogClientInWithTokenPassword() {
  423. $manager = $this->createMock(Manager::class);
  424. $session = $this->createMock(ISession::class);
  425. $request = $this->createMock(IRequest::class);
  426. /** @var Session $userSession */
  427. $userSession = $this->getMockBuilder(Session::class)
  428. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  429. ->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
  430. ->getMock();
  431. $userSession->expects($this->once())
  432. ->method('isTokenPassword')
  433. ->willReturn(true);
  434. $userSession->expects($this->once())
  435. ->method('login')
  436. ->with('john', 'I-AM-AN-APP-PASSWORD')
  437. ->willReturn(true);
  438. $session->expects($this->once())
  439. ->method('set')
  440. ->with('app_password', 'I-AM-AN-APP-PASSWORD');
  441. $request
  442. ->expects($this->any())
  443. ->method('getRemoteAddress')
  444. ->willReturn('192.168.0.1');
  445. $this->throttler
  446. ->expects($this->once())
  447. ->method('sleepDelayOrThrowOnMax')
  448. ->with('192.168.0.1');
  449. $this->throttler
  450. ->expects($this->any())
  451. ->method('getDelay')
  452. ->with('192.168.0.1')
  453. ->willReturn(0);
  454. $this->assertTrue($userSession->logClientIn('john', 'I-AM-AN-APP-PASSWORD', $request, $this->throttler));
  455. }
  456. public function testLogClientInNoTokenPasswordNo2fa() {
  457. $this->expectException(PasswordLoginForbiddenException::class);
  458. $manager = $this->createMock(Manager::class);
  459. $session = $this->createMock(ISession::class);
  460. $request = $this->createMock(IRequest::class);
  461. /** @var Session $userSession */
  462. $userSession = $this->getMockBuilder(Session::class)
  463. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  464. ->setMethods(['login', 'isTwoFactorEnforced'])
  465. ->getMock();
  466. $this->tokenProvider->expects($this->once())
  467. ->method('getToken')
  468. ->with('doe')
  469. ->will($this->throwException(new InvalidTokenException()));
  470. $this->config->expects($this->once())
  471. ->method('getSystemValueBool')
  472. ->with('token_auth_enforced', false)
  473. ->willReturn(false);
  474. $userSession->expects($this->once())
  475. ->method('isTwoFactorEnforced')
  476. ->with('john')
  477. ->willReturn(true);
  478. $request
  479. ->expects($this->any())
  480. ->method('getRemoteAddress')
  481. ->willReturn('192.168.0.1');
  482. $this->throttler
  483. ->expects($this->once())
  484. ->method('sleepDelayOrThrowOnMax')
  485. ->with('192.168.0.1');
  486. $this->throttler
  487. ->expects($this->any())
  488. ->method('getDelay')
  489. ->with('192.168.0.1')
  490. ->willReturn(0);
  491. $userSession->logClientIn('john', 'doe', $request, $this->throttler);
  492. }
  493. public function testTryTokenLoginNoHeaderNoSessionCookie(): void {
  494. $request = $this->createMock(IRequest::class);
  495. $this->config->expects(self::once())
  496. ->method('getSystemValueString')
  497. ->with('instanceid')
  498. ->willReturn('abc123');
  499. $request->method('getHeader')->with('Authorization')->willReturn('');
  500. $request->method('getCookie')->with('abc123')->willReturn(null);
  501. $this->tokenProvider->expects(self::never())
  502. ->method('getToken');
  503. $loginResult = $this->userSession->tryTokenLogin($request);
  504. self::assertFalse($loginResult);
  505. }
  506. public function testTryTokenLoginAuthorizationHeaderTokenNotFound(): void {
  507. $request = $this->createMock(IRequest::class);
  508. $request->method('getHeader')->with('Authorization')->willReturn('Bearer abcde-12345');
  509. $this->tokenProvider->expects(self::once())
  510. ->method('getToken')
  511. ->with('abcde-12345')
  512. ->willThrowException(new InvalidTokenException());
  513. $loginResult = $this->userSession->tryTokenLogin($request);
  514. self::assertFalse($loginResult);
  515. }
  516. public function testTryTokenLoginSessionIdTokenNotFound(): void {
  517. $request = $this->createMock(IRequest::class);
  518. $this->config->expects(self::once())
  519. ->method('getSystemValueString')
  520. ->with('instanceid')
  521. ->willReturn('abc123');
  522. $request->method('getHeader')->with('Authorization')->willReturn('');
  523. $request->method('getCookie')->with('abc123')->willReturn('abcde12345');
  524. $this->session->expects(self::once())
  525. ->method('getId')
  526. ->willReturn('abcde12345');
  527. $this->tokenProvider->expects(self::once())
  528. ->method('getToken')
  529. ->with('abcde12345')
  530. ->willThrowException(new InvalidTokenException());
  531. $loginResult = $this->userSession->tryTokenLogin($request);
  532. self::assertFalse($loginResult);
  533. }
  534. public function testRememberLoginValidToken() {
  535. $session = $this->createMock(Memory::class);
  536. $managerMethods = get_class_methods(Manager::class);
  537. //keep following methods intact in order to ensure hooks are working
  538. $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
  539. $manager = $this->getMockBuilder(Manager::class)
  540. ->setMethods($mockedManagerMethods)
  541. ->setConstructorArgs([
  542. $this->config,
  543. $this->createMock(ICacheFactory::class),
  544. $this->createMock(IEventDispatcher::class),
  545. $this->createMock(LoggerInterface::class),
  546. ])
  547. ->getMock();
  548. $userSession = $this->getMockBuilder(Session::class)
  549. //override, otherwise tests will fail because of setcookie()
  550. ->setMethods(['setMagicInCookie', 'setLoginName'])
  551. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  552. ->getMock();
  553. $user = $this->createMock(IUser::class);
  554. $token = 'goodToken';
  555. $oldSessionId = 'sess321';
  556. $sessionId = 'sess123';
  557. $session->expects($this->once())
  558. ->method('regenerateId');
  559. $manager->expects($this->once())
  560. ->method('get')
  561. ->with('foo')
  562. ->willReturn($user);
  563. $this->config->expects($this->once())
  564. ->method('getUserKeys')
  565. ->with('foo', 'login_token')
  566. ->willReturn([$token]);
  567. $this->config->expects($this->once())
  568. ->method('deleteUserValue')
  569. ->with('foo', 'login_token', $token);
  570. $this->random->expects($this->once())
  571. ->method('generate')
  572. ->with(32)
  573. ->willReturn('abcdefg123456');
  574. $this->config->expects($this->once())
  575. ->method('setUserValue')
  576. ->with('foo', 'login_token', 'abcdefg123456', 10000);
  577. $tokenObject = $this->createMock(IToken::class);
  578. $tokenObject->expects($this->once())
  579. ->method('getLoginName')
  580. ->willReturn('foobar');
  581. $tokenObject->method('getId')
  582. ->willReturn(42);
  583. $session->expects($this->once())
  584. ->method('getId')
  585. ->willReturn($sessionId);
  586. $this->tokenProvider->expects($this->once())
  587. ->method('renewSessionToken')
  588. ->with($oldSessionId, $sessionId)
  589. ->willReturn($tokenObject);
  590. $this->tokenProvider->expects($this->never())
  591. ->method('getToken');
  592. $user->expects($this->any())
  593. ->method('getUID')
  594. ->willReturn('foo');
  595. $userSession->expects($this->once())
  596. ->method('setMagicInCookie');
  597. $user->expects($this->once())
  598. ->method('updateLastLoginTimestamp');
  599. $setUID = false;
  600. $session
  601. ->method('set')
  602. ->willReturnCallback(function ($k, $v) use (&$setUID) {
  603. if ($k === 'user_id' && $v === 'foo') {
  604. $setUID = true;
  605. }
  606. });
  607. $userSession->expects($this->once())
  608. ->method('setLoginName')
  609. ->willReturn('foobar');
  610. $granted = $userSession->loginWithCookie('foo', $token, $oldSessionId);
  611. $this->assertTrue($setUID);
  612. $this->assertTrue($granted);
  613. }
  614. public function testRememberLoginInvalidSessionToken() {
  615. $session = $this->createMock(Memory::class);
  616. $managerMethods = get_class_methods(Manager::class);
  617. //keep following methods intact in order to ensure hooks are working
  618. $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
  619. $manager = $this->getMockBuilder(Manager::class)
  620. ->setMethods($mockedManagerMethods)
  621. ->setConstructorArgs([
  622. $this->config,
  623. $this->createMock(ICacheFactory::class),
  624. $this->createMock(IEventDispatcher::class),
  625. $this->createMock(LoggerInterface::class),
  626. ])
  627. ->getMock();
  628. $userSession = $this->getMockBuilder(Session::class)
  629. //override, otherwise tests will fail because of setcookie()
  630. ->setMethods(['setMagicInCookie'])
  631. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  632. ->getMock();
  633. $user = $this->createMock(IUser::class);
  634. $token = 'goodToken';
  635. $oldSessionId = 'sess321';
  636. $sessionId = 'sess123';
  637. $session->expects($this->once())
  638. ->method('regenerateId');
  639. $manager->expects($this->once())
  640. ->method('get')
  641. ->with('foo')
  642. ->willReturn($user);
  643. $this->config->expects($this->once())
  644. ->method('getUserKeys')
  645. ->with('foo', 'login_token')
  646. ->willReturn([$token]);
  647. $this->config->expects($this->once())
  648. ->method('deleteUserValue')
  649. ->with('foo', 'login_token', $token);
  650. $this->config->expects($this->once())
  651. ->method('setUserValue'); // TODO: mock new random value
  652. $session->expects($this->once())
  653. ->method('getId')
  654. ->willReturn($sessionId);
  655. $this->tokenProvider->expects($this->once())
  656. ->method('renewSessionToken')
  657. ->with($oldSessionId, $sessionId)
  658. ->will($this->throwException(new InvalidTokenException()));
  659. $user->expects($this->never())
  660. ->method('getUID')
  661. ->willReturn('foo');
  662. $userSession->expects($this->never())
  663. ->method('setMagicInCookie');
  664. $user->expects($this->never())
  665. ->method('updateLastLoginTimestamp');
  666. $session->expects($this->never())
  667. ->method('set')
  668. ->with('user_id', 'foo');
  669. $granted = $userSession->loginWithCookie('foo', $token, $oldSessionId);
  670. $this->assertFalse($granted);
  671. }
  672. public function testRememberLoginInvalidToken() {
  673. $session = $this->createMock(Memory::class);
  674. $managerMethods = get_class_methods(Manager::class);
  675. //keep following methods intact in order to ensure hooks are working
  676. $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
  677. $manager = $this->getMockBuilder(Manager::class)
  678. ->setMethods($mockedManagerMethods)
  679. ->setConstructorArgs([
  680. $this->config,
  681. $this->createMock(ICacheFactory::class),
  682. $this->createMock(IEventDispatcher::class),
  683. $this->createMock(LoggerInterface::class),
  684. ])
  685. ->getMock();
  686. $userSession = $this->getMockBuilder(Session::class)
  687. //override, otherwise tests will fail because of setcookie()
  688. ->setMethods(['setMagicInCookie'])
  689. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  690. ->getMock();
  691. $user = $this->createMock(IUser::class);
  692. $token = 'goodToken';
  693. $oldSessionId = 'sess321';
  694. $session->expects($this->once())
  695. ->method('regenerateId');
  696. $manager->expects($this->once())
  697. ->method('get')
  698. ->with('foo')
  699. ->willReturn($user);
  700. $this->config->expects($this->once())
  701. ->method('getUserKeys')
  702. ->with('foo', 'login_token')
  703. ->willReturn(['anothertoken']);
  704. $this->config->expects($this->never())
  705. ->method('deleteUserValue')
  706. ->with('foo', 'login_token', $token);
  707. $this->tokenProvider->expects($this->never())
  708. ->method('renewSessionToken');
  709. $userSession->expects($this->never())
  710. ->method('setMagicInCookie');
  711. $user->expects($this->never())
  712. ->method('updateLastLoginTimestamp');
  713. $session->expects($this->never())
  714. ->method('set')
  715. ->with('user_id', 'foo');
  716. $granted = $userSession->loginWithCookie('foo', $token, $oldSessionId);
  717. $this->assertFalse($granted);
  718. }
  719. public function testRememberLoginInvalidUser() {
  720. $session = $this->createMock(Memory::class);
  721. $managerMethods = get_class_methods(Manager::class);
  722. //keep following methods intact in order to ensure hooks are working
  723. $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
  724. $manager = $this->getMockBuilder(Manager::class)
  725. ->setMethods($mockedManagerMethods)
  726. ->setConstructorArgs([
  727. $this->config,
  728. $this->createMock(ICacheFactory::class),
  729. $this->createMock(IEventDispatcher::class),
  730. $this->createMock(LoggerInterface::class),
  731. ])
  732. ->getMock();
  733. $userSession = $this->getMockBuilder(Session::class)
  734. //override, otherwise tests will fail because of setcookie()
  735. ->setMethods(['setMagicInCookie'])
  736. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  737. ->getMock();
  738. $token = 'goodToken';
  739. $oldSessionId = 'sess321';
  740. $session->expects($this->once())
  741. ->method('regenerateId');
  742. $manager->expects($this->once())
  743. ->method('get')
  744. ->with('foo')
  745. ->willReturn(null);
  746. $this->config->expects($this->never())
  747. ->method('getUserKeys')
  748. ->with('foo', 'login_token')
  749. ->willReturn(['anothertoken']);
  750. $this->tokenProvider->expects($this->never())
  751. ->method('renewSessionToken');
  752. $userSession->expects($this->never())
  753. ->method('setMagicInCookie');
  754. $session->expects($this->never())
  755. ->method('set')
  756. ->with('user_id', 'foo');
  757. $granted = $userSession->loginWithCookie('foo', $token, $oldSessionId);
  758. $this->assertFalse($granted);
  759. }
  760. public function testActiveUserAfterSetSession() {
  761. $users = [
  762. 'foo' => new User('foo', null, $this->createMock(IEventDispatcher::class)),
  763. 'bar' => new User('bar', null, $this->createMock(IEventDispatcher::class))
  764. ];
  765. $manager = $this->getMockBuilder(Manager::class)
  766. ->disableOriginalConstructor()
  767. ->getMock();
  768. $manager->expects($this->any())
  769. ->method('get')
  770. ->willReturnCallback(function ($uid) use ($users) {
  771. return $users[$uid];
  772. });
  773. $session = new Memory();
  774. $session->set('user_id', 'foo');
  775. $userSession = $this->getMockBuilder(Session::class)
  776. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  777. ->setMethods([
  778. 'validateSession'
  779. ])
  780. ->getMock();
  781. $userSession->expects($this->any())
  782. ->method('validateSession');
  783. $this->assertEquals($users['foo'], $userSession->getUser());
  784. $session2 = new Memory();
  785. $session2->set('user_id', 'bar');
  786. $userSession->setSession($session2);
  787. $this->assertEquals($users['bar'], $userSession->getUser());
  788. }
  789. public function testCreateSessionToken() {
  790. $manager = $this->createMock(Manager::class);
  791. $session = $this->createMock(ISession::class);
  792. $user = $this->createMock(IUser::class);
  793. $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
  794. $requestId = $this->createMock(IRequestId::class);
  795. $config = $this->createMock(IConfig::class);
  796. $csrf = $this->getMockBuilder(CsrfTokenManager::class)
  797. ->disableOriginalConstructor()
  798. ->getMock();
  799. $request = new Request([
  800. 'server' => [
  801. 'HTTP_USER_AGENT' => 'Firefox',
  802. ]
  803. ], $requestId, $config, $csrf);
  804. $uid = 'user123';
  805. $loginName = 'User123';
  806. $password = 'passme';
  807. $sessionId = 'abcxyz';
  808. $manager->expects($this->once())
  809. ->method('get')
  810. ->with($uid)
  811. ->willReturn($user);
  812. $session->expects($this->once())
  813. ->method('getId')
  814. ->willReturn($sessionId);
  815. $this->tokenProvider->expects($this->once())
  816. ->method('getToken')
  817. ->with($password)
  818. ->will($this->throwException(new InvalidTokenException()));
  819. $this->tokenProvider->expects($this->once())
  820. ->method('generateToken')
  821. ->with($sessionId, $uid, $loginName, $password, 'Firefox', IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER);
  822. $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password));
  823. }
  824. public function testCreateRememberedSessionToken() {
  825. $manager = $this->createMock(Manager::class);
  826. $session = $this->createMock(ISession::class);
  827. $user = $this->createMock(IUser::class);
  828. $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
  829. $requestId = $this->createMock(IRequestId::class);
  830. $config = $this->createMock(IConfig::class);
  831. $csrf = $this->getMockBuilder(CsrfTokenManager::class)
  832. ->disableOriginalConstructor()
  833. ->getMock();
  834. $request = new Request([
  835. 'server' => [
  836. 'HTTP_USER_AGENT' => 'Firefox',
  837. ]
  838. ], $requestId, $config, $csrf);
  839. $uid = 'user123';
  840. $loginName = 'User123';
  841. $password = 'passme';
  842. $sessionId = 'abcxyz';
  843. $manager->expects($this->once())
  844. ->method('get')
  845. ->with($uid)
  846. ->willReturn($user);
  847. $session->expects($this->once())
  848. ->method('getId')
  849. ->willReturn($sessionId);
  850. $this->tokenProvider->expects($this->once())
  851. ->method('getToken')
  852. ->with($password)
  853. ->will($this->throwException(new InvalidTokenException()));
  854. $this->tokenProvider->expects($this->once())
  855. ->method('generateToken')
  856. ->with($sessionId, $uid, $loginName, $password, 'Firefox', IToken::TEMPORARY_TOKEN, IToken::REMEMBER);
  857. $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password, true));
  858. }
  859. public function testCreateSessionTokenWithTokenPassword() {
  860. $manager = $this->getMockBuilder(Manager::class)
  861. ->disableOriginalConstructor()
  862. ->getMock();
  863. $session = $this->createMock(ISession::class);
  864. $token = $this->createMock(IToken::class);
  865. $user = $this->createMock(IUser::class);
  866. $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
  867. $requestId = $this->createMock(IRequestId::class);
  868. $config = $this->createMock(IConfig::class);
  869. $csrf = $this->getMockBuilder(CsrfTokenManager::class)
  870. ->disableOriginalConstructor()
  871. ->getMock();
  872. $request = new Request([
  873. 'server' => [
  874. 'HTTP_USER_AGENT' => 'Firefox',
  875. ]
  876. ], $requestId, $config, $csrf);
  877. $uid = 'user123';
  878. $loginName = 'User123';
  879. $password = 'iamatoken';
  880. $realPassword = 'passme';
  881. $sessionId = 'abcxyz';
  882. $manager->expects($this->once())
  883. ->method('get')
  884. ->with($uid)
  885. ->willReturn($user);
  886. $session->expects($this->once())
  887. ->method('getId')
  888. ->willReturn($sessionId);
  889. $this->tokenProvider->expects($this->once())
  890. ->method('getToken')
  891. ->with($password)
  892. ->willReturn($token);
  893. $this->tokenProvider->expects($this->once())
  894. ->method('getPassword')
  895. ->with($token, $password)
  896. ->willReturn($realPassword);
  897. $this->tokenProvider->expects($this->once())
  898. ->method('generateToken')
  899. ->with($sessionId, $uid, $loginName, $realPassword, 'Firefox', IToken::TEMPORARY_TOKEN, IToken::DO_NOT_REMEMBER);
  900. $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password));
  901. }
  902. public function testCreateSessionTokenWithNonExistentUser() {
  903. $manager = $this->getMockBuilder(Manager::class)
  904. ->disableOriginalConstructor()
  905. ->getMock();
  906. $session = $this->createMock(ISession::class);
  907. $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
  908. $request = $this->createMock(IRequest::class);
  909. $uid = 'user123';
  910. $loginName = 'User123';
  911. $password = 'passme';
  912. $manager->expects($this->once())
  913. ->method('get')
  914. ->with($uid)
  915. ->willReturn(null);
  916. $this->assertFalse($userSession->createSessionToken($request, $uid, $loginName, $password));
  917. }
  918. public function testCreateRememberMeToken() {
  919. $user = $this->createMock(IUser::class);
  920. $user
  921. ->expects($this->exactly(2))
  922. ->method('getUID')
  923. ->willReturn('UserUid');
  924. $this->random
  925. ->expects($this->once())
  926. ->method('generate')
  927. ->with(32)
  928. ->willReturn('LongRandomToken');
  929. $this->config
  930. ->expects($this->once())
  931. ->method('setUserValue')
  932. ->with('UserUid', 'login_token', 'LongRandomToken', 10000);
  933. $this->userSession
  934. ->expects($this->once())
  935. ->method('setMagicInCookie')
  936. ->with('UserUid', 'LongRandomToken');
  937. $this->userSession->createRememberMeToken($user);
  938. }
  939. public function testTryBasicAuthLoginValid() {
  940. $request = $this->createMock(Request::class);
  941. $request->method('__get')
  942. ->willReturn([
  943. 'PHP_AUTH_USER' => 'username',
  944. 'PHP_AUTH_PW' => 'password',
  945. ]);
  946. $request->method('__isset')
  947. ->with('server')
  948. ->willReturn(true);
  949. $davAuthenticatedSet = false;
  950. $lastPasswordConfirmSet = false;
  951. $this->session
  952. ->method('set')
  953. ->willReturnCallback(function ($k, $v) use (&$davAuthenticatedSet, &$lastPasswordConfirmSet) {
  954. switch ($k) {
  955. case Auth::DAV_AUTHENTICATED:
  956. $davAuthenticatedSet = $v;
  957. return;
  958. case 'last-password-confirm':
  959. $lastPasswordConfirmSet = 1000;
  960. return;
  961. default:
  962. throw new \Exception();
  963. }
  964. });
  965. $userSession = $this->getMockBuilder(Session::class)
  966. ->setConstructorArgs([
  967. $this->manager,
  968. $this->session,
  969. $this->timeFactory,
  970. $this->tokenProvider,
  971. $this->config,
  972. $this->random,
  973. $this->lockdownManager,
  974. $this->logger,
  975. $this->dispatcher
  976. ])
  977. ->setMethods([
  978. 'logClientIn',
  979. 'getUser',
  980. ])
  981. ->getMock();
  982. /** @var Session|MockObject */
  983. $userSession->expects($this->once())
  984. ->method('logClientIn')
  985. ->with(
  986. $this->equalTo('username'),
  987. $this->equalTo('password'),
  988. $this->equalTo($request),
  989. $this->equalTo($this->throttler)
  990. )->willReturn(true);
  991. $user = $this->createMock(IUser::class);
  992. $user->method('getUID')->willReturn('username');
  993. $userSession->expects($this->once())
  994. ->method('getUser')
  995. ->willReturn($user);
  996. $this->assertTrue($userSession->tryBasicAuthLogin($request, $this->throttler));
  997. $this->assertSame('username', $davAuthenticatedSet);
  998. $this->assertSame(1000, $lastPasswordConfirmSet);
  999. }
  1000. public function testTryBasicAuthLoginNoLogin() {
  1001. $request = $this->createMock(Request::class);
  1002. $request->method('__get')
  1003. ->willReturn([]);
  1004. $request->method('__isset')
  1005. ->with('server')
  1006. ->willReturn(true);
  1007. $this->session->expects($this->never())
  1008. ->method($this->anything());
  1009. $userSession = $this->getMockBuilder(Session::class)
  1010. ->setConstructorArgs([
  1011. $this->manager,
  1012. $this->session,
  1013. $this->timeFactory,
  1014. $this->tokenProvider,
  1015. $this->config,
  1016. $this->random,
  1017. $this->lockdownManager,
  1018. $this->logger,
  1019. $this->dispatcher
  1020. ])
  1021. ->setMethods([
  1022. 'logClientIn',
  1023. ])
  1024. ->getMock();
  1025. /** @var Session|MockObject */
  1026. $userSession->expects($this->never())
  1027. ->method('logClientIn');
  1028. $this->assertFalse($userSession->tryBasicAuthLogin($request, $this->throttler));
  1029. }
  1030. public function testUpdateTokens() {
  1031. $this->tokenProvider->expects($this->once())
  1032. ->method('updatePasswords')
  1033. ->with('uid', 'pass');
  1034. $this->userSession->updateTokens('uid', 'pass');
  1035. }
  1036. public function testLogClientInThrottlerUsername() {
  1037. $manager = $this->createMock(Manager::class);
  1038. $session = $this->createMock(ISession::class);
  1039. $request = $this->createMock(IRequest::class);
  1040. /** @var Session $userSession */
  1041. $userSession = $this->getMockBuilder(Session::class)
  1042. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  1043. ->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
  1044. ->getMock();
  1045. $userSession->expects($this->once())
  1046. ->method('isTokenPassword')
  1047. ->willReturn(true);
  1048. $userSession->expects($this->once())
  1049. ->method('login')
  1050. ->with('john', 'I-AM-AN-PASSWORD')
  1051. ->willReturn(false);
  1052. $session->expects($this->never())
  1053. ->method('set');
  1054. $request
  1055. ->method('getRemoteAddress')
  1056. ->willReturn('192.168.0.1');
  1057. $this->throttler
  1058. ->expects($this->exactly(2))
  1059. ->method('sleepDelayOrThrowOnMax')
  1060. ->with('192.168.0.1');
  1061. $this->throttler
  1062. ->expects($this->any())
  1063. ->method('getDelay')
  1064. ->with('192.168.0.1')
  1065. ->willReturn(0);
  1066. $this->throttler
  1067. ->expects($this->once())
  1068. ->method('registerAttempt')
  1069. ->with('login', '192.168.0.1', ['user' => 'john']);
  1070. $this->dispatcher
  1071. ->expects($this->once())
  1072. ->method('dispatchTyped')
  1073. ->with(new LoginFailed('john', 'I-AM-AN-PASSWORD'));
  1074. $this->assertFalse($userSession->logClientIn('john', 'I-AM-AN-PASSWORD', $request, $this->throttler));
  1075. }
  1076. public function testLogClientInThrottlerEmail() {
  1077. $manager = $this->createMock(Manager::class);
  1078. $session = $this->createMock(ISession::class);
  1079. $request = $this->createMock(IRequest::class);
  1080. /** @var Session $userSession */
  1081. $userSession = $this->getMockBuilder(Session::class)
  1082. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
  1083. ->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
  1084. ->getMock();
  1085. $userSession->expects($this->once())
  1086. ->method('isTokenPassword')
  1087. ->willReturn(false);
  1088. $userSession->expects($this->once())
  1089. ->method('login')
  1090. ->with('john@foo.bar', 'I-AM-AN-PASSWORD')
  1091. ->willReturn(false);
  1092. $manager
  1093. ->method('getByEmail')
  1094. ->with('john@foo.bar')
  1095. ->willReturn([]);
  1096. $session->expects($this->never())
  1097. ->method('set');
  1098. $request
  1099. ->method('getRemoteAddress')
  1100. ->willReturn('192.168.0.1');
  1101. $this->throttler
  1102. ->expects($this->exactly(2))
  1103. ->method('sleepDelayOrThrowOnMax')
  1104. ->with('192.168.0.1');
  1105. $this->throttler
  1106. ->expects($this->any())
  1107. ->method('getDelay')
  1108. ->with('192.168.0.1')
  1109. ->willReturn(0);
  1110. $this->throttler
  1111. ->expects($this->once())
  1112. ->method('registerAttempt')
  1113. ->with('login', '192.168.0.1', ['user' => 'john@foo.bar']);
  1114. $this->dispatcher
  1115. ->expects($this->once())
  1116. ->method('dispatchTyped')
  1117. ->with(new LoginFailed('john@foo.bar', 'I-AM-AN-PASSWORD'));
  1118. $this->assertFalse($userSession->logClientIn('john@foo.bar', 'I-AM-AN-PASSWORD', $request, $this->throttler));
  1119. }
  1120. }