SessionTest.php 48 KB

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