SessionTest.php 38 KB

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