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.

ManagerTest.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Authentication\TwoFactorAuth;
  22. use Exception;
  23. use OC;
  24. use OC\Authentication\Token\IProvider as TokenProvider;
  25. use OC\Authentication\TwoFactorAuth\Manager;
  26. use OC\Authentication\TwoFactorAuth\ProviderLoader;
  27. use OCP\Activity\IEvent;
  28. use OCP\Activity\IManager;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\Authentication\TwoFactorAuth\IProvider;
  31. use OCP\Authentication\TwoFactorAuth\IRegistry;
  32. use OCP\IConfig;
  33. use OCP\ILogger;
  34. use OCP\ISession;
  35. use OCP\IUser;
  36. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  37. use Test\TestCase;
  38. class ManagerTest extends TestCase {
  39. /** @var IUser|\PHPUnit_Framework_MockObject_MockObject */
  40. private $user;
  41. /** @var ProviderLoader|\PHPUnit_Framework_MockObject_MockObject */
  42. private $providerLoader;
  43. /** @var IRegistry|\PHPUnit_Framework_MockObject_MockObject */
  44. private $providerRegistry;
  45. /** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
  46. private $session;
  47. /** @var Manager */
  48. private $manager;
  49. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  50. private $config;
  51. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  52. private $activityManager;
  53. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  54. private $logger;
  55. /** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */
  56. private $fakeProvider;
  57. /** @var IProvider|\PHPUnit_Framework_MockObject_MockObject */
  58. private $backupProvider;
  59. /** @var TokenProvider|\PHPUnit_Framework_MockObject_MockObject */
  60. private $tokenProvider;
  61. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  62. private $timeFactory;
  63. /** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
  64. private $eventDispatcher;
  65. protected function setUp() {
  66. parent::setUp();
  67. $this->user = $this->createMock(IUser::class);
  68. $this->providerLoader = $this->createMock(\OC\Authentication\TwoFactorAuth\ProviderLoader::class);
  69. $this->providerRegistry = $this->createMock(IRegistry::class);
  70. $this->session = $this->createMock(ISession::class);
  71. $this->config = $this->createMock(IConfig::class);
  72. $this->activityManager = $this->createMock(IManager::class);
  73. $this->logger = $this->createMock(ILogger::class);
  74. $this->tokenProvider = $this->createMock(TokenProvider::class);
  75. $this->timeFactory = $this->createMock(ITimeFactory::class);
  76. $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  77. $this->manager = new Manager(
  78. $this->providerLoader,
  79. $this->providerRegistry,
  80. $this->session,
  81. $this->config,
  82. $this->activityManager,
  83. $this->logger,
  84. $this->tokenProvider,
  85. $this->timeFactory,
  86. $this->eventDispatcher
  87. );
  88. $this->fakeProvider = $this->createMock(IProvider::class);
  89. $this->fakeProvider->method('getId')->willReturn('email');
  90. $this->fakeProvider->method('isTwoFactorAuthEnabledForUser')->willReturn(true);
  91. $this->backupProvider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider')->getMock();
  92. $this->backupProvider->method('getId')->willReturn('backup_codes');
  93. $this->backupProvider->method('isTwoFactorAuthEnabledForUser')->willReturn(true);
  94. }
  95. private function prepareNoProviders() {
  96. $this->providerLoader->method('getProviders')
  97. ->with($this->user)
  98. ->will($this->returnValue([]));
  99. }
  100. private function prepareProviders() {
  101. $this->providerRegistry->expects($this->once())
  102. ->method('getProviderStates')
  103. ->with($this->user)
  104. ->willReturn([
  105. $this->fakeProvider->getId() => true,
  106. ]);
  107. $this->providerLoader->expects($this->once())
  108. ->method('getProviders')
  109. ->with($this->user)
  110. ->willReturn([$this->fakeProvider]);
  111. }
  112. private function prepareProvidersWitBackupProvider() {
  113. $this->providerLoader->method('getProviders')
  114. ->with($this->user)
  115. ->willReturn([
  116. $this->fakeProvider,
  117. $this->backupProvider,
  118. ]);
  119. }
  120. public function testIsTwoFactorAuthenticated() {
  121. $this->user->expects($this->once())
  122. ->method('getUID')
  123. ->will($this->returnValue('user123'));
  124. $this->config->expects($this->once())
  125. ->method('getUserValue')
  126. ->with('user123', 'core', 'two_factor_auth_disabled', 0)
  127. ->willReturn(0);
  128. $this->providerRegistry->expects($this->once())
  129. ->method('getProviderStates')
  130. ->willReturn([
  131. 'twofactor_totp' => true,
  132. 'twofactor_u2f' => false,
  133. ]);
  134. $this->assertTrue($this->manager->isTwoFactorAuthenticated($this->user));
  135. }
  136. public function testGetProvider() {
  137. $this->providerRegistry->expects($this->once())
  138. ->method('getProviderStates')
  139. ->with($this->user)
  140. ->willReturn([
  141. $this->fakeProvider->getId() => true,
  142. ]);
  143. $this->providerLoader->expects($this->once())
  144. ->method('getProviders')
  145. ->with($this->user)
  146. ->willReturn([$this->fakeProvider]);
  147. $provider = $this->manager->getProvider($this->user, $this->fakeProvider->getId());
  148. $this->assertSame($this->fakeProvider, $provider);
  149. }
  150. public function testGetInvalidProvider() {
  151. $this->providerRegistry->expects($this->once())
  152. ->method('getProviderStates')
  153. ->with($this->user)
  154. ->willReturn([]);
  155. $this->providerLoader->expects($this->once())
  156. ->method('getProviders')
  157. ->with($this->user)
  158. ->willReturn([]);
  159. $provider = $this->manager->getProvider($this->user, 'nonexistent');
  160. $this->assertNull($provider);
  161. }
  162. public function testGetProviders() {
  163. $this->providerRegistry->expects($this->once())
  164. ->method('getProviderStates')
  165. ->with($this->user)
  166. ->willReturn([
  167. $this->fakeProvider->getId() => true,
  168. ]);
  169. $this->providerLoader->expects($this->once())
  170. ->method('getProviders')
  171. ->with($this->user)
  172. ->willReturn([$this->fakeProvider]);
  173. $expectedProviders = [
  174. 'email' => $this->fakeProvider,
  175. ];
  176. $providerSet = $this->manager->getProviderSet($this->user);
  177. $providers = $providerSet->getProviders();
  178. $this->assertEquals($expectedProviders, $providers);
  179. $this->assertFalse($providerSet->isProviderMissing());
  180. }
  181. public function testGetProvidersOneMissing() {
  182. $this->providerRegistry->expects($this->once())
  183. ->method('getProviderStates')
  184. ->with($this->user)
  185. ->willReturn([
  186. $this->fakeProvider->getId() => true,
  187. ]);
  188. $this->providerLoader->expects($this->once())
  189. ->method('getProviders')
  190. ->with($this->user)
  191. ->willReturn([]);
  192. $expectedProviders = [
  193. 'email' => $this->fakeProvider,
  194. ];
  195. $providerSet = $this->manager->getProviderSet($this->user);
  196. $this->assertTrue($providerSet->isProviderMissing());
  197. }
  198. public function testVerifyChallenge() {
  199. $this->prepareProviders();
  200. $challenge = 'passme';
  201. $event = $this->createMock(IEvent::class);
  202. $this->fakeProvider->expects($this->once())
  203. ->method('verifyChallenge')
  204. ->with($this->user, $challenge)
  205. ->will($this->returnValue(true));
  206. $this->session->expects($this->once())
  207. ->method('get')
  208. ->with('two_factor_remember_login')
  209. ->will($this->returnValue(false));
  210. $this->session->expects($this->at(1))
  211. ->method('remove')
  212. ->with('two_factor_auth_uid');
  213. $this->session->expects($this->at(2))
  214. ->method('remove')
  215. ->with('two_factor_remember_login');
  216. $this->session->expects($this->at(3))
  217. ->method('set')
  218. ->with(Manager::SESSION_UID_DONE, 'jos');
  219. $this->session->method('getId')
  220. ->willReturn('mysessionid');
  221. $this->activityManager->expects($this->once())
  222. ->method('generateEvent')
  223. ->willReturn($event);
  224. $this->user->expects($this->any())
  225. ->method('getUID')
  226. ->willReturn('jos');
  227. $event->expects($this->once())
  228. ->method('setApp')
  229. ->with($this->equalTo('core'))
  230. ->willReturnSelf();
  231. $event->expects($this->once())
  232. ->method('setType')
  233. ->with($this->equalTo('security'))
  234. ->willReturnSelf();
  235. $event->expects($this->once())
  236. ->method('setAuthor')
  237. ->with($this->equalTo('jos'))
  238. ->willReturnSelf();
  239. $event->expects($this->once())
  240. ->method('setAffectedUser')
  241. ->with($this->equalTo('jos'))
  242. ->willReturnSelf();
  243. $this->fakeProvider
  244. ->method('getDisplayName')
  245. ->willReturn('Fake 2FA');
  246. $event->expects($this->once())
  247. ->method('setSubject')
  248. ->with($this->equalTo('twofactor_success'), $this->equalTo([
  249. 'provider' => 'Fake 2FA',
  250. ]))
  251. ->willReturnSelf();
  252. $token = $this->createMock(OC\Authentication\Token\IToken::class);
  253. $this->tokenProvider->method('getToken')
  254. ->with('mysessionid')
  255. ->willReturn($token);
  256. $token->method('getId')
  257. ->willReturn(42);
  258. $this->config->expects($this->once())
  259. ->method('deleteUserValue')
  260. ->with('jos', 'login_token_2fa', 42);
  261. $result = $this->manager->verifyChallenge('email', $this->user, $challenge);
  262. $this->assertTrue($result);
  263. }
  264. public function testVerifyChallengeInvalidProviderId() {
  265. $this->prepareProviders();
  266. $challenge = 'passme';
  267. $this->fakeProvider->expects($this->never())
  268. ->method('verifyChallenge')
  269. ->with($this->user, $challenge);
  270. $this->session->expects($this->never())
  271. ->method('remove');
  272. $this->assertFalse($this->manager->verifyChallenge('dontexist', $this->user, $challenge));
  273. }
  274. public function testVerifyInvalidChallenge() {
  275. $this->prepareProviders();
  276. $challenge = 'dontpassme';
  277. $event = $this->createMock(IEvent::class);
  278. $this->fakeProvider->expects($this->once())
  279. ->method('verifyChallenge')
  280. ->with($this->user, $challenge)
  281. ->will($this->returnValue(false));
  282. $this->session->expects($this->never())
  283. ->method('remove');
  284. $this->activityManager->expects($this->once())
  285. ->method('generateEvent')
  286. ->willReturn($event);
  287. $this->user->expects($this->any())
  288. ->method('getUID')
  289. ->willReturn('jos');
  290. $event->expects($this->once())
  291. ->method('setApp')
  292. ->with($this->equalTo('core'))
  293. ->willReturnSelf();
  294. $event->expects($this->once())
  295. ->method('setType')
  296. ->with($this->equalTo('security'))
  297. ->willReturnSelf();
  298. $event->expects($this->once())
  299. ->method('setAuthor')
  300. ->with($this->equalTo('jos'))
  301. ->willReturnSelf();
  302. $event->expects($this->once())
  303. ->method('setAffectedUser')
  304. ->with($this->equalTo('jos'))
  305. ->willReturnSelf();
  306. $this->fakeProvider
  307. ->method('getDisplayName')
  308. ->willReturn('Fake 2FA');
  309. $event->expects($this->once())
  310. ->method('setSubject')
  311. ->with($this->equalTo('twofactor_failed'), $this->equalTo([
  312. 'provider' => 'Fake 2FA',
  313. ]))
  314. ->willReturnSelf();
  315. $this->assertFalse($this->manager->verifyChallenge('email', $this->user, $challenge));
  316. }
  317. public function testNeedsSecondFactor() {
  318. $user = $this->createMock(IUser::class);
  319. $this->session->expects($this->at(0))
  320. ->method('exists')
  321. ->with('app_password')
  322. ->willReturn(false);
  323. $this->session->expects($this->at(1))
  324. ->method('exists')
  325. ->with('two_factor_auth_uid')
  326. ->will($this->returnValue(false));
  327. $this->session->expects($this->at(2))
  328. ->method('exists')
  329. ->with(Manager::SESSION_UID_DONE)
  330. ->willReturn(false);
  331. $this->session->method('getId')
  332. ->willReturn('mysessionid');
  333. $token = $this->createMock(OC\Authentication\Token\IToken::class);
  334. $this->tokenProvider->method('getToken')
  335. ->with('mysessionid')
  336. ->willReturn($token);
  337. $token->method('getId')
  338. ->willReturn(42);
  339. $user->method('getUID')
  340. ->willReturn('user');
  341. $this->config->method('getUserKeys')
  342. ->with('user', 'login_token_2fa')
  343. ->willReturn([
  344. 42
  345. ]);
  346. $manager = $this->getMockBuilder(Manager::class)
  347. ->setConstructorArgs([
  348. $this->providerLoader,
  349. $this->providerRegistry,
  350. $this->session,
  351. $this->config,
  352. $this->activityManager,
  353. $this->logger,
  354. $this->tokenProvider,
  355. $this->timeFactory,
  356. $this->eventDispatcher
  357. ])
  358. ->setMethods(['loadTwoFactorApp', 'isTwoFactorAuthenticated'])// Do not actually load the apps
  359. ->getMock();
  360. $manager->method('isTwoFactorAuthenticated')
  361. ->with($user)
  362. ->willReturn(true);
  363. $this->assertTrue($manager->needsSecondFactor($user));
  364. }
  365. public function testNeedsSecondFactorUserIsNull() {
  366. $user = null;
  367. $this->session->expects($this->never())
  368. ->method('exists');
  369. $this->assertFalse($this->manager->needsSecondFactor($user));
  370. }
  371. public function testNeedsSecondFactorWithNoProviderAvailableAnymore() {
  372. $this->prepareNoProviders();
  373. $user = null;
  374. $this->session->expects($this->never())
  375. ->method('exists')
  376. ->with('two_factor_auth_uid')
  377. ->will($this->returnValue(true));
  378. $this->session->expects($this->never())
  379. ->method('remove')
  380. ->with('two_factor_auth_uid');
  381. $this->assertFalse($this->manager->needsSecondFactor($user));
  382. }
  383. public function testPrepareTwoFactorLogin() {
  384. $this->user->method('getUID')
  385. ->will($this->returnValue('ferdinand'));
  386. $this->session->expects($this->at(0))
  387. ->method('set')
  388. ->with('two_factor_auth_uid', 'ferdinand');
  389. $this->session->expects($this->at(1))
  390. ->method('set')
  391. ->with('two_factor_remember_login', true);
  392. $this->session->method('getId')
  393. ->willReturn('mysessionid');
  394. $token = $this->createMock(OC\Authentication\Token\IToken::class);
  395. $this->tokenProvider->method('getToken')
  396. ->with('mysessionid')
  397. ->willReturn($token);
  398. $token->method('getId')
  399. ->willReturn(42);
  400. $this->timeFactory->method('getTime')
  401. ->willReturn(1337);
  402. $this->config->method('setUserValue')
  403. ->with('ferdinand', 'login_token_2fa', 42, 1337);
  404. $this->manager->prepareTwoFactorLogin($this->user, true);
  405. }
  406. public function testPrepareTwoFactorLoginDontRemember() {
  407. $this->user->method('getUID')
  408. ->will($this->returnValue('ferdinand'));
  409. $this->session->expects($this->at(0))
  410. ->method('set')
  411. ->with('two_factor_auth_uid', 'ferdinand');
  412. $this->session->expects($this->at(1))
  413. ->method('set')
  414. ->with('two_factor_remember_login', false);
  415. $this->session->method('getId')
  416. ->willReturn('mysessionid');
  417. $token = $this->createMock(OC\Authentication\Token\IToken::class);
  418. $this->tokenProvider->method('getToken')
  419. ->with('mysessionid')
  420. ->willReturn($token);
  421. $token->method('getId')
  422. ->willReturn(42);
  423. $this->timeFactory->method('getTime')
  424. ->willReturn(1337);
  425. $this->config->method('setUserValue')
  426. ->with('ferdinand', 'login_token_2fa', 42, 1337);
  427. $this->manager->prepareTwoFactorLogin($this->user, false);
  428. }
  429. public function testNeedsSecondFactorSessionAuth() {
  430. $user = $this->createMock(IUser::class);
  431. $user->method('getUID')
  432. ->willReturn('user');
  433. $this->session->method('exists')
  434. ->will($this->returnCallback(function ($var) {
  435. if ($var === Manager::SESSION_UID_KEY) {
  436. return false;
  437. } else if ($var === 'app_password') {
  438. return false;
  439. }
  440. return true;
  441. }));
  442. $this->session->expects($this->once())
  443. ->method('get')
  444. ->with(Manager::SESSION_UID_DONE)
  445. ->willReturn('user');
  446. $this->assertFalse($this->manager->needsSecondFactor($user));
  447. }
  448. public function testNeedsSecondFactorSessionAuthFailDBPass() {
  449. $user = $this->createMock(IUser::class);
  450. $user->method('getUID')
  451. ->willReturn('user');
  452. $this->session->method('exists')
  453. ->willReturn(false);
  454. $this->session->method('getId')
  455. ->willReturn('mysessionid');
  456. $token = $this->createMock(OC\Authentication\Token\IToken::class);
  457. $token->method('getId')
  458. ->willReturn(40);
  459. $this->tokenProvider->method('getToken')
  460. ->with('mysessionid')
  461. ->willReturn($token);
  462. $this->config->method('getUserKeys')
  463. ->with('user', 'login_token_2fa')
  464. ->willReturn([
  465. 42, 43, 44
  466. ]);
  467. $this->session->expects($this->once())
  468. ->method('set')
  469. ->with(Manager::SESSION_UID_DONE, 'user');
  470. $this->assertFalse($this->manager->needsSecondFactor($user));
  471. }
  472. public function testNeedsSecondFactorInvalidToken() {
  473. $this->prepareNoProviders();
  474. $user = $this->createMock(IUser::class);
  475. $user->method('getUID')
  476. ->willReturn('user');
  477. $this->session->method('exists')
  478. ->willReturn(false);
  479. $this->session->method('getId')
  480. ->willReturn('mysessionid');
  481. $this->tokenProvider->method('getToken')
  482. ->with('mysessionid')
  483. ->willThrowException(new OC\Authentication\Exceptions\InvalidTokenException());
  484. $this->config->method('getUserKeys')->willReturn([]);
  485. $this->assertFalse($this->manager->needsSecondFactor($user));
  486. }
  487. public function testNeedsSecondFactorAppPassword() {
  488. $user = $this->createMock(IUser::class);
  489. $this->session->method('exists')
  490. ->with('app_password')
  491. ->willReturn(true);
  492. $this->assertFalse($this->manager->needsSecondFactor($user));
  493. }
  494. }