TwoFactorMiddlewareTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Core\Middleware;
  8. use OC\AppFramework\Http\Request;
  9. use OC\Authentication\Exceptions\TwoFactorAuthRequiredException;
  10. use OC\Authentication\Exceptions\UserAlreadyLoggedInException;
  11. use OC\Authentication\TwoFactorAuth\Manager;
  12. use OC\Authentication\TwoFactorAuth\ProviderSet;
  13. use OC\Core\Controller\TwoFactorChallengeController;
  14. use OC\Core\Middleware\TwoFactorMiddleware;
  15. use OC\User\Session;
  16. use OCP\AppFramework\Controller;
  17. use OCP\AppFramework\Http\RedirectResponse;
  18. use OCP\AppFramework\Utility\IControllerMethodReflector;
  19. use OCP\Authentication\TwoFactorAuth\ALoginSetupController;
  20. use OCP\Authentication\TwoFactorAuth\IProvider;
  21. use OCP\IConfig;
  22. use OCP\IRequest;
  23. use OCP\IRequestId;
  24. use OCP\ISession;
  25. use OCP\IURLGenerator;
  26. use OCP\IUser;
  27. use OCP\IUserSession;
  28. use PHPUnit\Framework\MockObject\MockObject;
  29. use Test\TestCase;
  30. class TwoFactorMiddlewareTest extends TestCase {
  31. /** @var Manager|MockObject */
  32. private $twoFactorManager;
  33. /** @var IUserSession|MockObject */
  34. private $userSession;
  35. /** @var ISession|MockObject */
  36. private $session;
  37. /** @var IURLGenerator|MockObject */
  38. private $urlGenerator;
  39. /** @var IControllerMethodReflector|MockObject */
  40. private $reflector;
  41. /** @var IRequest|MockObject */
  42. private $request;
  43. /** @var TwoFactorMiddleware */
  44. private $middleware;
  45. /** @var Controller */
  46. private $controller;
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->twoFactorManager = $this->getMockBuilder(Manager::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $this->userSession = $this->getMockBuilder(Session::class)
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->session = $this->createMock(ISession::class);
  56. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  57. $this->reflector = $this->createMock(IControllerMethodReflector::class);
  58. $this->request = new Request(
  59. [
  60. 'server' => [
  61. 'REQUEST_URI' => 'test/url'
  62. ]
  63. ],
  64. $this->createMock(IRequestId::class),
  65. $this->createMock(IConfig::class)
  66. );
  67. $this->middleware = new TwoFactorMiddleware($this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator, $this->reflector, $this->request);
  68. $this->controller = $this->createMock(Controller::class);
  69. }
  70. public function testBeforeControllerNotLoggedIn() {
  71. $this->userSession->expects($this->once())
  72. ->method('isLoggedIn')
  73. ->willReturn(false);
  74. $this->userSession->expects($this->never())
  75. ->method('getUser');
  76. $this->middleware->beforeController($this->controller, 'index');
  77. }
  78. public function testBeforeSetupController() {
  79. $user = $this->createMock(IUser::class);
  80. $controller = $this->createMock(ALoginSetupController::class);
  81. $this->userSession->expects($this->any())
  82. ->method('getUser')
  83. ->willReturn($user);
  84. $this->twoFactorManager->expects($this->once())
  85. ->method('needsSecondFactor')
  86. ->willReturn(true);
  87. $this->userSession->expects($this->never())
  88. ->method('isLoggedIn');
  89. $this->middleware->beforeController($controller, 'create');
  90. }
  91. public function testBeforeControllerNoTwoFactorCheckNeeded() {
  92. $user = $this->createMock(IUser::class);
  93. $this->userSession->expects($this->once())
  94. ->method('isLoggedIn')
  95. ->willReturn(true);
  96. $this->userSession->expects($this->once())
  97. ->method('getUser')
  98. ->willReturn($user);
  99. $this->twoFactorManager->expects($this->once())
  100. ->method('isTwoFactorAuthenticated')
  101. ->with($user)
  102. ->willReturn(false);
  103. $this->middleware->beforeController($this->controller, 'index');
  104. }
  105. public function testBeforeControllerTwoFactorAuthRequired() {
  106. $this->expectException(TwoFactorAuthRequiredException::class);
  107. $user = $this->createMock(IUser::class);
  108. $this->userSession->expects($this->once())
  109. ->method('isLoggedIn')
  110. ->willReturn(true);
  111. $this->userSession->expects($this->once())
  112. ->method('getUser')
  113. ->willReturn($user);
  114. $this->twoFactorManager->expects($this->once())
  115. ->method('isTwoFactorAuthenticated')
  116. ->with($user)
  117. ->willReturn(true);
  118. $this->twoFactorManager->expects($this->once())
  119. ->method('needsSecondFactor')
  120. ->with($user)
  121. ->willReturn(true);
  122. $this->middleware->beforeController($this->controller, 'index');
  123. }
  124. public function testBeforeControllerUserAlreadyLoggedIn() {
  125. $this->expectException(UserAlreadyLoggedInException::class);
  126. $user = $this->createMock(IUser::class);
  127. $this->reflector
  128. ->method('hasAnnotation')
  129. ->willReturn(false);
  130. $this->userSession->expects($this->once())
  131. ->method('isLoggedIn')
  132. ->willReturn(true);
  133. $this->userSession
  134. ->method('getUser')
  135. ->willReturn($user);
  136. $this->twoFactorManager->expects($this->once())
  137. ->method('isTwoFactorAuthenticated')
  138. ->with($user)
  139. ->willReturn(true);
  140. $this->twoFactorManager->expects($this->once())
  141. ->method('needsSecondFactor')
  142. ->with($user)
  143. ->willReturn(false);
  144. $twoFactorChallengeController = $this->getMockBuilder(TwoFactorChallengeController::class)
  145. ->disableOriginalConstructor()
  146. ->getMock();
  147. $this->middleware->beforeController($twoFactorChallengeController, 'index');
  148. }
  149. public function testAfterExceptionTwoFactorAuthRequired() {
  150. $ex = new TwoFactorAuthRequiredException();
  151. $this->urlGenerator->expects($this->once())
  152. ->method('linkToRoute')
  153. ->with('core.TwoFactorChallenge.selectChallenge')
  154. ->willReturn('test/url');
  155. $expected = new RedirectResponse('test/url');
  156. $this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex));
  157. }
  158. public function testAfterException() {
  159. $ex = new UserAlreadyLoggedInException();
  160. $this->urlGenerator->expects($this->once())
  161. ->method('linkToRoute')
  162. ->with('files.view.index')
  163. ->willReturn('redirect/url');
  164. $expected = new RedirectResponse('redirect/url');
  165. $this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex));
  166. }
  167. public function testRequires2FASetupDoneAnnotated() {
  168. $user = $this->createMock(IUser::class);
  169. $this->reflector
  170. ->method('hasAnnotation')
  171. ->willReturnCallback(function (string $annotation) {
  172. return $annotation === 'TwoFactorSetUpDoneRequired';
  173. });
  174. $this->userSession->expects($this->once())
  175. ->method('isLoggedIn')
  176. ->willReturn(true);
  177. $this->userSession
  178. ->method('getUser')
  179. ->willReturn($user);
  180. $this->twoFactorManager->expects($this->once())
  181. ->method('isTwoFactorAuthenticated')
  182. ->with($user)
  183. ->willReturn(true);
  184. $this->twoFactorManager->expects($this->once())
  185. ->method('needsSecondFactor')
  186. ->with($user)
  187. ->willReturn(false);
  188. $this->expectException(UserAlreadyLoggedInException::class);
  189. $twoFactorChallengeController = $this->getMockBuilder(TwoFactorChallengeController::class)
  190. ->disableOriginalConstructor()
  191. ->getMock();
  192. $this->middleware->beforeController($twoFactorChallengeController, 'index');
  193. }
  194. public function dataRequires2FASetupDone() {
  195. $provider = $this->createMock(IProvider::class);
  196. $provider->method('getId')
  197. ->willReturn('2FAftw');
  198. return [
  199. [[], false, false],
  200. [[], true, true],
  201. [[$provider], false, true],
  202. [[$provider], true, true],
  203. ];
  204. }
  205. /**
  206. * @dataProvider dataRequires2FASetupDone
  207. */
  208. public function testRequires2FASetupDone(array $providers, bool $missingProviders, bool $expectEception) {
  209. $user = $this->createMock(IUser::class);
  210. $this->reflector
  211. ->method('hasAnnotation')
  212. ->willReturn(false);
  213. $this->userSession
  214. ->method('getUser')
  215. ->willReturn($user);
  216. $providerSet = new ProviderSet($providers, $missingProviders);
  217. $this->twoFactorManager->method('getProviderSet')
  218. ->with($user)
  219. ->willReturn($providerSet);
  220. $this->userSession
  221. ->method('isLoggedIn')
  222. ->willReturn(false);
  223. if ($expectEception) {
  224. $this->expectException(TwoFactorAuthRequiredException::class);
  225. } else {
  226. // hack to make phpunit shut up. Since we don't expect an exception here...
  227. $this->assertTrue(true);
  228. }
  229. $twoFactorChallengeController = $this->getMockBuilder(TwoFactorChallengeController::class)
  230. ->disableOriginalConstructor()
  231. ->getMock();
  232. $this->middleware->beforeController($twoFactorChallengeController, 'index');
  233. }
  234. }