TwoFactorCommandTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. declare(strict_types=1);
  7. namespace Test\Authentication\Login;
  8. use OC\Authentication\Login\TwoFactorCommand;
  9. use OC\Authentication\TwoFactorAuth\Manager;
  10. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  11. use OC\Authentication\TwoFactorAuth\ProviderSet;
  12. use OCP\Authentication\TwoFactorAuth\IActivatableAtLogin;
  13. use OCP\Authentication\TwoFactorAuth\IProvider as ITwoFactorAuthProvider;
  14. use OCP\IURLGenerator;
  15. use PHPUnit\Framework\MockObject\MockObject;
  16. class TwoFactorCommandTest extends ALoginCommandTest {
  17. /** @var Manager|MockObject */
  18. private $twoFactorManager;
  19. /** @var MandatoryTwoFactor|MockObject */
  20. private $mandatoryTwoFactor;
  21. /** @var IURLGenerator|MockObject */
  22. private $urlGenerator;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->twoFactorManager = $this->createMock(Manager::class);
  26. $this->mandatoryTwoFactor = $this->createMock(MandatoryTwoFactor::class);
  27. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  28. $this->cmd = new TwoFactorCommand(
  29. $this->twoFactorManager,
  30. $this->mandatoryTwoFactor,
  31. $this->urlGenerator
  32. );
  33. }
  34. public function testNotTwoFactorAuthenticated(): void {
  35. $data = $this->getLoggedInLoginData();
  36. $this->twoFactorManager->expects($this->once())
  37. ->method('isTwoFactorAuthenticated')
  38. ->willReturn(false);
  39. $this->twoFactorManager->expects($this->never())
  40. ->method('prepareTwoFactorLogin');
  41. $result = $this->cmd->process($data);
  42. $this->assertTrue($result->isSuccess());
  43. }
  44. public function testProcessOneActiveProvider(): void {
  45. $data = $this->getLoggedInLoginData();
  46. $this->twoFactorManager->expects($this->once())
  47. ->method('isTwoFactorAuthenticated')
  48. ->willReturn(true);
  49. $this->twoFactorManager->expects($this->once())
  50. ->method('prepareTwoFactorLogin')
  51. ->with(
  52. $this->user,
  53. $data->isRememberLogin()
  54. );
  55. $provider = $this->createMock(ITwoFactorAuthProvider::class);
  56. $this->twoFactorManager->expects($this->once())
  57. ->method('getProviderSet')
  58. ->willReturn(new ProviderSet([
  59. $provider,
  60. ], false));
  61. $this->twoFactorManager->expects($this->once())
  62. ->method('getLoginSetupProviders')
  63. ->with($this->user)
  64. ->willReturn([]);
  65. $this->mandatoryTwoFactor->expects($this->any())
  66. ->method('isEnforcedFor')
  67. ->with($this->user)
  68. ->willReturn(false);
  69. $provider->expects($this->once())
  70. ->method('getId')
  71. ->willReturn('test');
  72. $this->urlGenerator->expects($this->once())
  73. ->method('linkToRoute')
  74. ->with(
  75. 'core.TwoFactorChallenge.showChallenge',
  76. [
  77. 'challengeProviderId' => 'test'
  78. ]
  79. )
  80. ->willReturn('two/factor/url');
  81. $result = $this->cmd->process($data);
  82. $this->assertTrue($result->isSuccess());
  83. $this->assertEquals('two/factor/url', $result->getRedirectUrl());
  84. }
  85. public function testProcessMissingProviders(): void {
  86. $data = $this->getLoggedInLoginData();
  87. $this->twoFactorManager->expects($this->once())
  88. ->method('isTwoFactorAuthenticated')
  89. ->willReturn(true);
  90. $this->twoFactorManager->expects($this->once())
  91. ->method('prepareTwoFactorLogin')
  92. ->with(
  93. $this->user,
  94. $data->isRememberLogin()
  95. );
  96. $provider = $this->createMock(ITwoFactorAuthProvider::class);
  97. $provider->expects($this->once())
  98. ->method('getId')
  99. ->willReturn('test1');
  100. $this->twoFactorManager->expects($this->once())
  101. ->method('getProviderSet')
  102. ->willReturn(new ProviderSet([
  103. $provider,
  104. ], true));
  105. $this->twoFactorManager->expects($this->once())
  106. ->method('getLoginSetupProviders')
  107. ->with($this->user)
  108. ->willReturn([]);
  109. $this->mandatoryTwoFactor->expects($this->any())
  110. ->method('isEnforcedFor')
  111. ->with($this->user)
  112. ->willReturn(false);
  113. $this->urlGenerator->expects($this->once())
  114. ->method('linkToRoute')
  115. ->with(
  116. 'core.TwoFactorChallenge.selectChallenge'
  117. )
  118. ->willReturn('two/factor/url');
  119. $result = $this->cmd->process($data);
  120. $this->assertTrue($result->isSuccess());
  121. $this->assertEquals('two/factor/url', $result->getRedirectUrl());
  122. }
  123. public function testProcessTwoActiveProviders(): void {
  124. $data = $this->getLoggedInLoginData();
  125. $this->twoFactorManager->expects($this->once())
  126. ->method('isTwoFactorAuthenticated')
  127. ->willReturn(true);
  128. $this->twoFactorManager->expects($this->once())
  129. ->method('prepareTwoFactorLogin')
  130. ->with(
  131. $this->user,
  132. $data->isRememberLogin()
  133. );
  134. $provider1 = $this->createMock(ITwoFactorAuthProvider::class);
  135. $provider2 = $this->createMock(ITwoFactorAuthProvider::class);
  136. $provider1->expects($this->once())
  137. ->method('getId')
  138. ->willReturn('test1');
  139. $provider2->expects($this->once())
  140. ->method('getId')
  141. ->willReturn('test2');
  142. $this->twoFactorManager->expects($this->once())
  143. ->method('getProviderSet')
  144. ->willReturn(new ProviderSet([
  145. $provider1,
  146. $provider2,
  147. ], false));
  148. $this->twoFactorManager->expects($this->once())
  149. ->method('getLoginSetupProviders')
  150. ->with($this->user)
  151. ->willReturn([]);
  152. $this->mandatoryTwoFactor->expects($this->any())
  153. ->method('isEnforcedFor')
  154. ->with($this->user)
  155. ->willReturn(false);
  156. $this->urlGenerator->expects($this->once())
  157. ->method('linkToRoute')
  158. ->with(
  159. 'core.TwoFactorChallenge.selectChallenge'
  160. )
  161. ->willReturn('two/factor/url');
  162. $result = $this->cmd->process($data);
  163. $this->assertTrue($result->isSuccess());
  164. $this->assertEquals('two/factor/url', $result->getRedirectUrl());
  165. }
  166. public function testProcessFailingProviderAndEnforcedButNoSetupProviders(): void {
  167. $data = $this->getLoggedInLoginData();
  168. $this->twoFactorManager->expects($this->once())
  169. ->method('isTwoFactorAuthenticated')
  170. ->willReturn(true);
  171. $this->twoFactorManager->expects($this->once())
  172. ->method('prepareTwoFactorLogin')
  173. ->with(
  174. $this->user,
  175. $data->isRememberLogin()
  176. );
  177. $this->twoFactorManager->expects($this->once())
  178. ->method('getProviderSet')
  179. ->willReturn(new ProviderSet([], true));
  180. $this->twoFactorManager->expects($this->once())
  181. ->method('getLoginSetupProviders')
  182. ->with($this->user)
  183. ->willReturn([]);
  184. $this->mandatoryTwoFactor->expects($this->any())
  185. ->method('isEnforcedFor')
  186. ->with($this->user)
  187. ->willReturn(true);
  188. $this->urlGenerator->expects($this->once())
  189. ->method('linkToRoute')
  190. ->with(
  191. 'core.TwoFactorChallenge.selectChallenge'
  192. )
  193. ->willReturn('two/factor/url');
  194. $result = $this->cmd->process($data);
  195. $this->assertTrue($result->isSuccess());
  196. $this->assertEquals('two/factor/url', $result->getRedirectUrl());
  197. }
  198. public function testProcessFailingProviderAndEnforced(): void {
  199. $data = $this->getLoggedInLoginData();
  200. $this->twoFactorManager->expects($this->once())
  201. ->method('isTwoFactorAuthenticated')
  202. ->willReturn(true);
  203. $this->twoFactorManager->expects($this->once())
  204. ->method('prepareTwoFactorLogin')
  205. ->with(
  206. $this->user,
  207. $data->isRememberLogin()
  208. );
  209. $provider = $this->createMock(IActivatableAtLogin::class);
  210. $this->twoFactorManager->expects($this->once())
  211. ->method('getProviderSet')
  212. ->willReturn(new ProviderSet([
  213. $provider,
  214. ], true));
  215. $this->twoFactorManager->expects($this->once())
  216. ->method('getLoginSetupProviders')
  217. ->with($this->user)
  218. ->willReturn([]);
  219. $this->mandatoryTwoFactor->expects($this->any())
  220. ->method('isEnforcedFor')
  221. ->with($this->user)
  222. ->willReturn(true);
  223. $this->urlGenerator->expects($this->once())
  224. ->method('linkToRoute')
  225. ->with(
  226. 'core.TwoFactorChallenge.selectChallenge'
  227. )
  228. ->willReturn('two/factor/url');
  229. $result = $this->cmd->process($data);
  230. $this->assertTrue($result->isSuccess());
  231. $this->assertEquals('two/factor/url', $result->getRedirectUrl());
  232. }
  233. public function testProcessNoProvidersButEnforced(): void {
  234. $data = $this->getLoggedInLoginData();
  235. $this->twoFactorManager->expects($this->once())
  236. ->method('isTwoFactorAuthenticated')
  237. ->willReturn(true);
  238. $this->twoFactorManager->expects($this->once())
  239. ->method('prepareTwoFactorLogin')
  240. ->with(
  241. $this->user,
  242. $data->isRememberLogin()
  243. );
  244. $this->twoFactorManager->expects($this->once())
  245. ->method('getProviderSet')
  246. ->willReturn(new ProviderSet([], false));
  247. $this->twoFactorManager->expects($this->once())
  248. ->method('getLoginSetupProviders')
  249. ->with($this->user)
  250. ->willReturn([]);
  251. $this->mandatoryTwoFactor->expects($this->any())
  252. ->method('isEnforcedFor')
  253. ->with($this->user)
  254. ->willReturn(true);
  255. $this->urlGenerator->expects($this->once())
  256. ->method('linkToRoute')
  257. ->with(
  258. 'core.TwoFactorChallenge.selectChallenge'
  259. )
  260. ->willReturn('two/factor/url');
  261. $result = $this->cmd->process($data);
  262. $this->assertTrue($result->isSuccess());
  263. $this->assertEquals('two/factor/url', $result->getRedirectUrl());
  264. }
  265. public function testProcessWithRedirectUrl(): void {
  266. $data = $this->getLoggedInLoginDataWithRedirectUrl();
  267. $this->twoFactorManager->expects($this->once())
  268. ->method('isTwoFactorAuthenticated')
  269. ->willReturn(true);
  270. $this->twoFactorManager->expects($this->once())
  271. ->method('prepareTwoFactorLogin')
  272. ->with(
  273. $this->user,
  274. $data->isRememberLogin()
  275. );
  276. $provider = $this->createMock(ITwoFactorAuthProvider::class);
  277. $this->twoFactorManager->expects($this->once())
  278. ->method('getProviderSet')
  279. ->willReturn(new ProviderSet([
  280. $provider,
  281. ], false));
  282. $this->twoFactorManager->expects($this->once())
  283. ->method('getLoginSetupProviders')
  284. ->with($this->user)
  285. ->willReturn([]);
  286. $this->mandatoryTwoFactor->expects($this->any())
  287. ->method('isEnforcedFor')
  288. ->with($this->user)
  289. ->willReturn(false);
  290. $provider->expects($this->once())
  291. ->method('getId')
  292. ->willReturn('test');
  293. $this->urlGenerator->expects($this->once())
  294. ->method('linkToRoute')
  295. ->with(
  296. 'core.TwoFactorChallenge.showChallenge',
  297. [
  298. 'challengeProviderId' => 'test',
  299. 'redirect_url' => $this->redirectUrl,
  300. ]
  301. )
  302. ->willReturn('two/factor/url');
  303. $result = $this->cmd->process($data);
  304. $this->assertTrue($result->isSuccess());
  305. $this->assertEquals('two/factor/url', $result->getRedirectUrl());
  306. }
  307. }