You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SessionTest.php 30KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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\Session\Memory;
  10. use OC\User\User;
  11. /**
  12. * @group DB
  13. * @package Test\User
  14. */
  15. class SessionTest extends \Test\TestCase {
  16. /** @var \OCP\AppFramework\Utility\ITimeFactory */
  17. private $timeFactory;
  18. /** @var \OC\Authentication\Token\DefaultTokenProvider */
  19. protected $tokenProvider;
  20. /** @var \OCP\IConfig */
  21. private $config;
  22. protected function setUp() {
  23. parent::setUp();
  24. $this->timeFactory = $this->getMock('\OCP\AppFramework\Utility\ITimeFactory');
  25. $this->timeFactory->expects($this->any())
  26. ->method('getTime')
  27. ->will($this->returnValue(10000));
  28. $this->tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider');
  29. $this->config = $this->getMock('\OCP\IConfig');
  30. }
  31. public function testGetUser() {
  32. $token = new \OC\Authentication\Token\DefaultToken();
  33. $token->setLoginName('User123');
  34. $token->setLastCheck(200);
  35. $expectedUser = $this->getMock('\OCP\IUser');
  36. $expectedUser->expects($this->any())
  37. ->method('getUID')
  38. ->will($this->returnValue('user123'));
  39. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  40. $session->expects($this->at(0))
  41. ->method('get')
  42. ->with('user_id')
  43. ->will($this->returnValue($expectedUser->getUID()));
  44. $sessionId = 'abcdef12345';
  45. $manager = $this->getMockBuilder('\OC\User\Manager')
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $session->expects($this->at(1))
  49. ->method('get')
  50. ->with('app_password')
  51. ->will($this->returnValue(null)); // No password set -> browser session
  52. $session->expects($this->once())
  53. ->method('getId')
  54. ->will($this->returnValue($sessionId));
  55. $this->tokenProvider->expects($this->once())
  56. ->method('getToken')
  57. ->with($sessionId)
  58. ->will($this->returnValue($token));
  59. $this->tokenProvider->expects($this->once())
  60. ->method('getPassword')
  61. ->with($token, $sessionId)
  62. ->will($this->returnValue('passme'));
  63. $manager->expects($this->once())
  64. ->method('checkPassword')
  65. ->with('User123', 'passme')
  66. ->will($this->returnValue(true));
  67. $expectedUser->expects($this->once())
  68. ->method('isEnabled')
  69. ->will($this->returnValue(true));
  70. $this->tokenProvider->expects($this->once())
  71. ->method('updateTokenActivity')
  72. ->with($token);
  73. $manager->expects($this->any())
  74. ->method('get')
  75. ->with($expectedUser->getUID())
  76. ->will($this->returnValue($expectedUser));
  77. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  78. $user = $userSession->getUser();
  79. $this->assertSame($expectedUser, $user);
  80. $this->assertSame(10000, $token->getLastCheck());
  81. }
  82. public function isLoggedInData() {
  83. return [
  84. [true],
  85. [false],
  86. ];
  87. }
  88. /**
  89. * @dataProvider isLoggedInData
  90. */
  91. public function testIsLoggedIn($isLoggedIn) {
  92. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  93. $manager = $this->getMockBuilder('\OC\User\Manager')
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $userSession = $this->getMockBuilder('\OC\User\Session')
  97. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
  98. ->setMethods([
  99. 'getUser'
  100. ])
  101. ->getMock();
  102. $user = new User('sepp', null);
  103. $userSession->expects($this->once())
  104. ->method('getUser')
  105. ->will($this->returnValue($isLoggedIn ? $user : null));
  106. $this->assertEquals($isLoggedIn, $userSession->isLoggedIn());
  107. }
  108. public function testSetUser() {
  109. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  110. $session->expects($this->once())
  111. ->method('set')
  112. ->with('user_id', 'foo');
  113. $manager = $this->getMock('\OC\User\Manager');
  114. $backend = $this->getMock('\Test\Util\User\Dummy');
  115. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  116. $user->expects($this->once())
  117. ->method('getUID')
  118. ->will($this->returnValue('foo'));
  119. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  120. $userSession->setUser($user);
  121. }
  122. public function testLoginValidPasswordEnabled() {
  123. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  124. $session->expects($this->once())
  125. ->method('regenerateId');
  126. $this->tokenProvider->expects($this->once())
  127. ->method('getToken')
  128. ->with('bar')
  129. ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
  130. $session->expects($this->exactly(2))
  131. ->method('set')
  132. ->with($this->callback(function ($key) {
  133. switch ($key) {
  134. case 'user_id':
  135. case 'loginname':
  136. return true;
  137. break;
  138. default:
  139. return false;
  140. break;
  141. }
  142. }, 'foo'));
  143. $managerMethods = get_class_methods('\OC\User\Manager');
  144. //keep following methods intact in order to ensure hooks are
  145. //working
  146. $doNotMock = array('__construct', 'emit', 'listen');
  147. foreach ($doNotMock as $methodName) {
  148. $i = array_search($methodName, $managerMethods, true);
  149. if ($i !== false) {
  150. unset($managerMethods[$i]);
  151. }
  152. }
  153. $manager = $this->getMock('\OC\User\Manager', $managerMethods, array());
  154. $backend = $this->getMock('\Test\Util\User\Dummy');
  155. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  156. $user->expects($this->any())
  157. ->method('isEnabled')
  158. ->will($this->returnValue(true));
  159. $user->expects($this->any())
  160. ->method('getUID')
  161. ->will($this->returnValue('foo'));
  162. $user->expects($this->once())
  163. ->method('updateLastLoginTimestamp');
  164. $manager->expects($this->once())
  165. ->method('checkPassword')
  166. ->with('foo', 'bar')
  167. ->will($this->returnValue($user));
  168. $userSession = $this->getMockBuilder('\OC\User\Session')
  169. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
  170. ->setMethods([
  171. 'prepareUserLogin'
  172. ])
  173. ->getMock();
  174. $userSession->expects($this->once())
  175. ->method('prepareUserLogin');
  176. $userSession->login('foo', 'bar');
  177. $this->assertEquals($user, $userSession->getUser());
  178. }
  179. /**
  180. * @expectedException \OC\User\LoginException
  181. */
  182. public function testLoginValidPasswordDisabled() {
  183. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  184. $session->expects($this->never())
  185. ->method('set');
  186. $session->expects($this->once())
  187. ->method('regenerateId');
  188. $this->tokenProvider->expects($this->once())
  189. ->method('getToken')
  190. ->with('bar')
  191. ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
  192. $managerMethods = get_class_methods('\OC\User\Manager');
  193. //keep following methods intact in order to ensure hooks are
  194. //working
  195. $doNotMock = array('__construct', 'emit', 'listen');
  196. foreach ($doNotMock as $methodName) {
  197. $i = array_search($methodName, $managerMethods, true);
  198. if ($i !== false) {
  199. unset($managerMethods[$i]);
  200. }
  201. }
  202. $manager = $this->getMock('\OC\User\Manager', $managerMethods, array());
  203. $backend = $this->getMock('\Test\Util\User\Dummy');
  204. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  205. $user->expects($this->any())
  206. ->method('isEnabled')
  207. ->will($this->returnValue(false));
  208. $user->expects($this->never())
  209. ->method('updateLastLoginTimestamp');
  210. $manager->expects($this->once())
  211. ->method('checkPassword')
  212. ->with('foo', 'bar')
  213. ->will($this->returnValue($user));
  214. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  215. $userSession->login('foo', 'bar');
  216. }
  217. public function testLoginInvalidPassword() {
  218. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  219. $managerMethods = get_class_methods('\OC\User\Manager');
  220. //keep following methods intact in order to ensure hooks are
  221. //working
  222. $doNotMock = array('__construct', 'emit', 'listen');
  223. foreach ($doNotMock as $methodName) {
  224. $i = array_search($methodName, $managerMethods, true);
  225. if ($i !== false) {
  226. unset($managerMethods[$i]);
  227. }
  228. }
  229. $manager = $this->getMock('\OC\User\Manager', $managerMethods, array());
  230. $backend = $this->getMock('\Test\Util\User\Dummy');
  231. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  232. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  233. $session->expects($this->never())
  234. ->method('set');
  235. $session->expects($this->once())
  236. ->method('regenerateId');
  237. $this->tokenProvider->expects($this->once())
  238. ->method('getToken')
  239. ->with('bar')
  240. ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
  241. $user->expects($this->never())
  242. ->method('isEnabled');
  243. $user->expects($this->never())
  244. ->method('updateLastLoginTimestamp');
  245. $manager->expects($this->once())
  246. ->method('checkPassword')
  247. ->with('foo', 'bar')
  248. ->will($this->returnValue(false));
  249. $userSession->login('foo', 'bar');
  250. }
  251. public function testLoginNonExisting() {
  252. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  253. $manager = $this->getMock('\OC\User\Manager');
  254. $backend = $this->getMock('\Test\Util\User\Dummy');
  255. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  256. $session->expects($this->never())
  257. ->method('set');
  258. $session->expects($this->once())
  259. ->method('regenerateId');
  260. $this->tokenProvider->expects($this->once())
  261. ->method('getToken')
  262. ->with('bar')
  263. ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
  264. $manager->expects($this->once())
  265. ->method('checkPassword')
  266. ->with('foo', 'bar')
  267. ->will($this->returnValue(false));
  268. $userSession->login('foo', 'bar');
  269. }
  270. /**
  271. * When using a device token, the loginname must match the one that was used
  272. * when generating the token on the browser.
  273. */
  274. public function testLoginWithDifferentTokenLoginName() {
  275. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  276. $manager = $this->getMock('\OC\User\Manager');
  277. $backend = $this->getMock('\Test\Util\User\Dummy');
  278. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  279. $username = 'user123';
  280. $token = new \OC\Authentication\Token\DefaultToken();
  281. $token->setLoginName($username);
  282. $session->expects($this->never())
  283. ->method('set');
  284. $session->expects($this->once())
  285. ->method('regenerateId');
  286. $this->tokenProvider->expects($this->once())
  287. ->method('getToken')
  288. ->with('bar')
  289. ->will($this->returnValue($token));
  290. $manager->expects($this->once())
  291. ->method('checkPassword')
  292. ->with('foo', 'bar')
  293. ->will($this->returnValue(false));
  294. $userSession->login('foo', 'bar');
  295. }
  296. /**
  297. * @expectedException \OC\Authentication\Exceptions\PasswordLoginForbiddenException
  298. */
  299. public function testLogClientInNoTokenPasswordWith2fa() {
  300. $manager = $this->getMockBuilder('\OC\User\Manager')
  301. ->disableOriginalConstructor()
  302. ->getMock();
  303. $session = $this->getMock('\OCP\ISession');
  304. $request = $this->getMock('\OCP\IRequest');
  305. $user = $this->getMock('\OCP\IUser');
  306. /** @var \OC\User\Session $userSession */
  307. $userSession = $this->getMockBuilder('\OC\User\Session')
  308. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
  309. ->setMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
  310. ->getMock();
  311. $this->tokenProvider->expects($this->once())
  312. ->method('getToken')
  313. ->with('doe')
  314. ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
  315. $this->config->expects($this->once())
  316. ->method('getSystemValue')
  317. ->with('token_auth_enforced', false)
  318. ->will($this->returnValue(true));
  319. $userSession->logClientIn('john', 'doe', $request);
  320. }
  321. public function testLogClientInWithTokenPassword() {
  322. $manager = $this->getMockBuilder('\OC\User\Manager')
  323. ->disableOriginalConstructor()
  324. ->getMock();
  325. $session = $this->getMock('\OCP\ISession');
  326. $request = $this->getMock('\OCP\IRequest');
  327. $user = $this->getMock('\OCP\IUser');
  328. /** @var \OC\User\Session $userSession */
  329. $userSession = $this->getMockBuilder('\OC\User\Session')
  330. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
  331. ->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
  332. ->getMock();
  333. $userSession->expects($this->once())
  334. ->method('isTokenPassword')
  335. ->will($this->returnValue(true));
  336. $userSession->expects($this->once())
  337. ->method('login')
  338. ->with('john', 'I-AM-AN-APP-PASSWORD')
  339. ->will($this->returnValue(true));
  340. $session->expects($this->once())
  341. ->method('set')
  342. ->with('app_password', 'I-AM-AN-APP-PASSWORD');
  343. $this->assertTrue($userSession->logClientIn('john', 'I-AM-AN-APP-PASSWORD', $request));
  344. }
  345. /**
  346. * @expectedException \OC\Authentication\Exceptions\PasswordLoginForbiddenException
  347. */
  348. public function testLogClientInNoTokenPasswordNo2fa() {
  349. $manager = $this->getMockBuilder('\OC\User\Manager')
  350. ->disableOriginalConstructor()
  351. ->getMock();
  352. $session = $this->getMock('\OCP\ISession');
  353. $user = $this->getMock('\OCP\IUser');
  354. $request = $this->getMock('\OCP\IRequest');
  355. /** @var \OC\User\Session $userSession */
  356. $userSession = $this->getMockBuilder('\OC\User\Session')
  357. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
  358. ->setMethods(['login', 'isTwoFactorEnforced'])
  359. ->getMock();
  360. $this->tokenProvider->expects($this->once())
  361. ->method('getToken')
  362. ->with('doe')
  363. ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
  364. $this->config->expects($this->once())
  365. ->method('getSystemValue')
  366. ->with('token_auth_enforced', false)
  367. ->will($this->returnValue(false));
  368. $userSession->expects($this->once())
  369. ->method('isTwoFactorEnforced')
  370. ->with('john')
  371. ->will($this->returnValue(true));
  372. $userSession->logClientIn('john', 'doe', $request);
  373. }
  374. public function testRememberLoginValidToken() {
  375. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  376. $session->expects($this->exactly(1))
  377. ->method('set')
  378. ->with($this->callback(function ($key) {
  379. switch ($key) {
  380. case 'user_id':
  381. return true;
  382. default:
  383. return false;
  384. }
  385. }, 'foo'));
  386. $session->expects($this->once())
  387. ->method('regenerateId');
  388. $managerMethods = get_class_methods('\OC\User\Manager');
  389. //keep following methods intact in order to ensure hooks are
  390. //working
  391. $doNotMock = array('__construct', 'emit', 'listen');
  392. foreach ($doNotMock as $methodName) {
  393. $i = array_search($methodName, $managerMethods, true);
  394. if ($i !== false) {
  395. unset($managerMethods[$i]);
  396. }
  397. }
  398. $manager = $this->getMock('\OC\User\Manager', $managerMethods, array());
  399. $backend = $this->getMock('\Test\Util\User\Dummy');
  400. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  401. $user->expects($this->any())
  402. ->method('getUID')
  403. ->will($this->returnValue('foo'));
  404. $user->expects($this->once())
  405. ->method('updateLastLoginTimestamp');
  406. $manager->expects($this->once())
  407. ->method('get')
  408. ->with('foo')
  409. ->will($this->returnValue($user));
  410. //prepare login token
  411. $token = 'goodToken';
  412. \OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time());
  413. $userSession = $this->getMock(
  414. '\OC\User\Session',
  415. //override, otherwise tests will fail because of setcookie()
  416. array('setMagicInCookie'),
  417. //there are passed as parameters to the constructor
  418. array($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config));
  419. $granted = $userSession->loginWithCookie('foo', $token);
  420. $this->assertSame($granted, true);
  421. }
  422. public function testRememberLoginInvalidToken() {
  423. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  424. $session->expects($this->never())
  425. ->method('set');
  426. $session->expects($this->once())
  427. ->method('regenerateId');
  428. $managerMethods = get_class_methods('\OC\User\Manager');
  429. //keep following methods intact in order to ensure hooks are
  430. //working
  431. $doNotMock = array('__construct', 'emit', 'listen');
  432. foreach ($doNotMock as $methodName) {
  433. $i = array_search($methodName, $managerMethods, true);
  434. if ($i !== false) {
  435. unset($managerMethods[$i]);
  436. }
  437. }
  438. $manager = $this->getMock('\OC\User\Manager', $managerMethods, array());
  439. $backend = $this->getMock('\Test\Util\User\Dummy');
  440. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  441. $user->expects($this->any())
  442. ->method('getUID')
  443. ->will($this->returnValue('foo'));
  444. $user->expects($this->never())
  445. ->method('updateLastLoginTimestamp');
  446. $manager->expects($this->once())
  447. ->method('get')
  448. ->with('foo')
  449. ->will($this->returnValue($user));
  450. //prepare login token
  451. $token = 'goodToken';
  452. \OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time());
  453. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  454. $granted = $userSession->loginWithCookie('foo', 'badToken');
  455. $this->assertSame($granted, false);
  456. }
  457. public function testRememberLoginInvalidUser() {
  458. $session = $this->getMock('\OC\Session\Memory', array(), array(''));
  459. $session->expects($this->never())
  460. ->method('set');
  461. $session->expects($this->once())
  462. ->method('regenerateId');
  463. $managerMethods = get_class_methods('\OC\User\Manager');
  464. //keep following methods intact in order to ensure hooks are
  465. //working
  466. $doNotMock = array('__construct', 'emit', 'listen');
  467. foreach ($doNotMock as $methodName) {
  468. $i = array_search($methodName, $managerMethods, true);
  469. if ($i !== false) {
  470. unset($managerMethods[$i]);
  471. }
  472. }
  473. $manager = $this->getMock('\OC\User\Manager', $managerMethods, array());
  474. $backend = $this->getMock('\Test\Util\User\Dummy');
  475. $user = $this->getMock('\OC\User\User', array(), array('foo', $backend));
  476. $user->expects($this->never())
  477. ->method('getUID');
  478. $user->expects($this->never())
  479. ->method('updateLastLoginTimestamp');
  480. $manager->expects($this->once())
  481. ->method('get')
  482. ->with('foo')
  483. ->will($this->returnValue(null));
  484. //prepare login token
  485. $token = 'goodToken';
  486. \OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time());
  487. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  488. $granted = $userSession->loginWithCookie('foo', $token);
  489. $this->assertSame($granted, false);
  490. }
  491. public function testActiveUserAfterSetSession() {
  492. $users = array(
  493. 'foo' => new User('foo', null),
  494. 'bar' => new User('bar', null)
  495. );
  496. $manager = $this->getMockBuilder('\OC\User\Manager')
  497. ->disableOriginalConstructor()
  498. ->getMock();
  499. $manager->expects($this->any())
  500. ->method('get')
  501. ->will($this->returnCallback(function ($uid) use ($users) {
  502. return $users[$uid];
  503. }));
  504. $session = new Memory('');
  505. $session->set('user_id', 'foo');
  506. $userSession = $this->getMockBuilder('\OC\User\Session')
  507. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
  508. ->setMethods([
  509. 'validateSession'
  510. ])
  511. ->getMock();
  512. $userSession->expects($this->any())
  513. ->method('validateSession');
  514. $this->assertEquals($users['foo'], $userSession->getUser());
  515. $session2 = new Memory('');
  516. $session2->set('user_id', 'bar');
  517. $userSession->setSession($session2);
  518. $this->assertEquals($users['bar'], $userSession->getUser());
  519. }
  520. public function testCreateSessionToken() {
  521. $manager = $this->getMockBuilder('\OC\User\Manager')
  522. ->disableOriginalConstructor()
  523. ->getMock();
  524. $session = $this->getMock('\OCP\ISession');
  525. $token = $this->getMock('\OC\Authentication\Token\IToken');
  526. $user = $this->getMock('\OCP\IUser');
  527. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  528. $random = $this->getMock('\OCP\Security\ISecureRandom');
  529. $config = $this->getMock('\OCP\IConfig');
  530. $csrf = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenManager')
  531. ->disableOriginalConstructor()
  532. ->getMock();
  533. $request = new \OC\AppFramework\Http\Request([
  534. 'server' => [
  535. 'HTTP_USER_AGENT' => 'Firefox',
  536. ]
  537. ], $random, $config, $csrf);
  538. $uid = 'user123';
  539. $loginName = 'User123';
  540. $password = 'passme';
  541. $sessionId = 'abcxyz';
  542. $manager->expects($this->once())
  543. ->method('get')
  544. ->with($uid)
  545. ->will($this->returnValue($user));
  546. $session->expects($this->once())
  547. ->method('getId')
  548. ->will($this->returnValue($sessionId));
  549. $this->tokenProvider->expects($this->once())
  550. ->method('getToken')
  551. ->with($password)
  552. ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
  553. $this->tokenProvider->expects($this->once())
  554. ->method('generateToken')
  555. ->with($sessionId, $uid, $loginName, $password, 'Firefox');
  556. $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password));
  557. }
  558. public function testCreateSessionTokenWithTokenPassword() {
  559. $manager = $this->getMockBuilder('\OC\User\Manager')
  560. ->disableOriginalConstructor()
  561. ->getMock();
  562. $session = $this->getMock('\OCP\ISession');
  563. $token = $this->getMock('\OC\Authentication\Token\IToken');
  564. $user = $this->getMock('\OCP\IUser');
  565. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  566. $random = $this->getMock('\OCP\Security\ISecureRandom');
  567. $config = $this->getMock('\OCP\IConfig');
  568. $csrf = $this->getMockBuilder('\OC\Security\CSRF\CsrfTokenManager')
  569. ->disableOriginalConstructor()
  570. ->getMock();
  571. $request = new \OC\AppFramework\Http\Request([
  572. 'server' => [
  573. 'HTTP_USER_AGENT' => 'Firefox',
  574. ]
  575. ], $random, $config, $csrf);
  576. $uid = 'user123';
  577. $loginName = 'User123';
  578. $password = 'iamatoken';
  579. $realPassword = 'passme';
  580. $sessionId = 'abcxyz';
  581. $manager->expects($this->once())
  582. ->method('get')
  583. ->with($uid)
  584. ->will($this->returnValue($user));
  585. $session->expects($this->once())
  586. ->method('getId')
  587. ->will($this->returnValue($sessionId));
  588. $this->tokenProvider->expects($this->once())
  589. ->method('getToken')
  590. ->with($password)
  591. ->will($this->returnValue($token));
  592. $this->tokenProvider->expects($this->once())
  593. ->method('getPassword')
  594. ->with($token, $password)
  595. ->will($this->returnValue($realPassword));
  596. $this->tokenProvider->expects($this->once())
  597. ->method('generateToken')
  598. ->with($sessionId, $uid, $loginName, $realPassword, 'Firefox');
  599. $this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password));
  600. }
  601. public function testCreateSessionTokenWithNonExistentUser() {
  602. $manager = $this->getMockBuilder('\OC\User\Manager')
  603. ->disableOriginalConstructor()
  604. ->getMock();
  605. $session = $this->getMock('\OCP\ISession');
  606. $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config);
  607. $request = $this->getMock('\OCP\IRequest');
  608. $uid = 'user123';
  609. $loginName = 'User123';
  610. $password = 'passme';
  611. $manager->expects($this->once())
  612. ->method('get')
  613. ->with($uid)
  614. ->will($this->returnValue(null));
  615. $this->assertFalse($userSession->createSessionToken($request, $uid, $loginName, $password));
  616. }
  617. public function testTryTokenLoginWithDisabledUser() {
  618. $manager = $this->getMockBuilder('\OC\User\Manager')
  619. ->disableOriginalConstructor()
  620. ->getMock();
  621. $session = new Memory('');
  622. $token = new \OC\Authentication\Token\DefaultToken();
  623. $token->setLoginName('fritz');
  624. $token->setUid('fritz0');
  625. $token->setLastCheck(100); // Needs check
  626. $user = $this->getMock('\OCP\IUser');
  627. $userSession = $this->getMockBuilder('\OC\User\Session')
  628. ->setMethods(['logout'])
  629. ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config])
  630. ->getMock();
  631. $request = $this->getMock('\OCP\IRequest');
  632. $request->expects($this->once())
  633. ->method('getHeader')
  634. ->with('Authorization')
  635. ->will($this->returnValue('token xxxxx'));
  636. $this->tokenProvider->expects($this->once())
  637. ->method('getToken')
  638. ->with('xxxxx')
  639. ->will($this->returnValue($token));
  640. $manager->expects($this->once())
  641. ->method('get')
  642. ->with('fritz0')
  643. ->will($this->returnValue($user));
  644. $user->expects($this->once())
  645. ->method('isEnabled')
  646. ->will($this->returnValue(false));
  647. $this->assertFalse($userSession->tryTokenLogin($request));
  648. }
  649. public function testValidateSessionDisabledUser() {
  650. $userManager = $this->getMock('\OCP\IUserManager');
  651. $session = $this->getMock('\OCP\ISession');
  652. $timeFactory = $this->getMock('\OCP\AppFramework\Utility\ITimeFactory');
  653. $tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider');
  654. $userSession = $this->getMockBuilder('\OC\User\Session')
  655. ->setConstructorArgs([$userManager, $session, $timeFactory, $tokenProvider, $this->config])
  656. ->setMethods(['logout'])
  657. ->getMock();
  658. $user = $this->getMock('\OCP\IUser');
  659. $token = new \OC\Authentication\Token\DefaultToken();
  660. $token->setLoginName('susan');
  661. $token->setLastCheck(20);
  662. $session->expects($this->once())
  663. ->method('get')
  664. ->with('app_password')
  665. ->will($this->returnValue('APP-PASSWORD'));
  666. $tokenProvider->expects($this->once())
  667. ->method('getToken')
  668. ->with('APP-PASSWORD')
  669. ->will($this->returnValue($token));
  670. $timeFactory->expects($this->once())
  671. ->method('getTime')
  672. ->will($this->returnValue(1000)); // more than 5min since last check
  673. $tokenProvider->expects($this->once())
  674. ->method('getPassword')
  675. ->with($token, 'APP-PASSWORD')
  676. ->will($this->returnValue('123456'));
  677. $userManager->expects($this->once())
  678. ->method('checkPassword')
  679. ->with('susan', '123456')
  680. ->will($this->returnValue(true));
  681. $user->expects($this->once())
  682. ->method('isEnabled')
  683. ->will($this->returnValue(false));
  684. $tokenProvider->expects($this->once())
  685. ->method('invalidateToken')
  686. ->with('APP-PASSWORD');
  687. $userSession->expects($this->once())
  688. ->method('logout');
  689. $userSession->setUser($user);
  690. $this->invokePrivate($userSession, 'validateSession');
  691. }
  692. public function testValidateSessionNoPassword() {
  693. $userManager = $this->getMock('\OCP\IUserManager');
  694. $session = $this->getMock('\OCP\ISession');
  695. $timeFactory = $this->getMock('\OCP\AppFramework\Utility\ITimeFactory');
  696. $tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider');
  697. $userSession = $this->getMockBuilder('\OC\User\Session')
  698. ->setConstructorArgs([$userManager, $session, $timeFactory, $tokenProvider, $this->config])
  699. ->setMethods(['logout'])
  700. ->getMock();
  701. $user = $this->getMock('\OCP\IUser');
  702. $token = new \OC\Authentication\Token\DefaultToken();
  703. $token->setLastCheck(20);
  704. $session->expects($this->once())
  705. ->method('get')
  706. ->with('app_password')
  707. ->will($this->returnValue('APP-PASSWORD'));
  708. $tokenProvider->expects($this->once())
  709. ->method('getToken')
  710. ->with('APP-PASSWORD')
  711. ->will($this->returnValue($token));
  712. $timeFactory->expects($this->once())
  713. ->method('getTime')
  714. ->will($this->returnValue(1000)); // more than 5min since last check
  715. $tokenProvider->expects($this->once())
  716. ->method('getPassword')
  717. ->with($token, 'APP-PASSWORD')
  718. ->will($this->throwException(new \OC\Authentication\Exceptions\PasswordlessTokenException()));
  719. $tokenProvider->expects($this->once())
  720. ->method('updateToken')
  721. ->with($token);
  722. $this->invokePrivate($userSession, 'validateSession', [$user]);
  723. $this->assertEquals(1000, $token->getLastCheck());
  724. }
  725. public function testUpdateSessionTokenPassword() {
  726. $userManager = $this->getMock('\OCP\IUserManager');
  727. $session = $this->getMock('\OCP\ISession');
  728. $timeFactory = $this->getMock('\OCP\AppFramework\Utility\ITimeFactory');
  729. $tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider');
  730. $userSession = new \OC\User\Session($userManager, $session, $timeFactory, $tokenProvider, $this->config);
  731. $password = '123456';
  732. $sessionId ='session1234';
  733. $token = new \OC\Authentication\Token\DefaultToken();
  734. $session->expects($this->once())
  735. ->method('getId')
  736. ->will($this->returnValue($sessionId));
  737. $tokenProvider->expects($this->once())
  738. ->method('getToken')
  739. ->with($sessionId)
  740. ->will($this->returnValue($token));
  741. $tokenProvider->expects($this->once())
  742. ->method('setPassword')
  743. ->with($token, $sessionId, $password);
  744. $userSession->updateSessionTokenPassword($password);
  745. }
  746. public function testUpdateSessionTokenPasswordNoSessionAvailable() {
  747. $userManager = $this->getMock('\OCP\IUserManager');
  748. $session = $this->getMock('\OCP\ISession');
  749. $timeFactory = $this->getMock('\OCP\AppFramework\Utility\ITimeFactory');
  750. $tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider');
  751. $userSession = new \OC\User\Session($userManager, $session, $timeFactory, $tokenProvider, $this->config);
  752. $session->expects($this->once())
  753. ->method('getId')
  754. ->will($this->throwException(new \OCP\Session\Exceptions\SessionNotAvailableException()));
  755. $userSession->updateSessionTokenPassword('1234');
  756. }
  757. public function testUpdateSessionTokenPasswordInvalidTokenException() {
  758. $userManager = $this->getMock('\OCP\IUserManager');
  759. $session = $this->getMock('\OCP\ISession');
  760. $timeFactory = $this->getMock('\OCP\AppFramework\Utility\ITimeFactory');
  761. $tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider');
  762. $userSession = new \OC\User\Session($userManager, $session, $timeFactory, $tokenProvider, $this->config);
  763. $password = '123456';
  764. $sessionId ='session1234';
  765. $token = new \OC\Authentication\Token\DefaultToken();
  766. $session->expects($this->once())
  767. ->method('getId')
  768. ->will($this->returnValue($sessionId));
  769. $tokenProvider->expects($this->once())
  770. ->method('getToken')
  771. ->with($sessionId)
  772. ->will($this->returnValue($token));
  773. $tokenProvider->expects($this->once())
  774. ->method('setPassword')
  775. ->with($token, $sessionId, $password)
  776. ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
  777. $userSession->updateSessionTokenPassword($password);
  778. }
  779. }