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.

TwoFactorMiddlewareTest.php 8.7KB

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