TwoFactorMiddlewareTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\Authentication\TwoFactorAuth\Manager;
  23. use OC\Core\Middleware\TwoFactorMiddleware;
  24. use OC\AppFramework\Http\Request;
  25. use OC\User\Session;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Utility\IControllerMethodReflector;
  28. use OCP\IConfig;
  29. use OCP\ISession;
  30. use OCP\IURLGenerator;
  31. use OCP\IUser;
  32. use OCP\Security\ISecureRandom;
  33. use Test\TestCase;
  34. class TwoFactorMiddlewareTest extends TestCase {
  35. private $twoFactorManager;
  36. private $userSession;
  37. private $session;
  38. private $urlGenerator;
  39. private $reflector;
  40. private $request;
  41. /** @var TwoFactorMiddleware */
  42. private $middleware;
  43. /** @var Controller */
  44. private $controller;
  45. protected function setUp() {
  46. parent::setUp();
  47. $this->twoFactorManager = $this->getMockBuilder(Manager::class)
  48. ->disableOriginalConstructor()
  49. ->getMock();
  50. $this->userSession = $this->getMockBuilder(Session::class)
  51. ->disableOriginalConstructor()
  52. ->getMock();
  53. $this->session = $this->createMock(ISession::class);
  54. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  55. $this->reflector = $this->createMock(IControllerMethodReflector::class);
  56. $this->request = new Request(
  57. [
  58. 'server' => [
  59. 'REQUEST_URI' => 'test/url'
  60. ]
  61. ],
  62. $this->createMock(ISecureRandom::class),
  63. $this->createMock(IConfig::class)
  64. );
  65. $this->middleware = new TwoFactorMiddleware($this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator, $this->reflector, $this->request);
  66. $this->controller = $this->createMock(Controller::class);
  67. }
  68. public function testBeforeControllerNotLoggedIn() {
  69. $this->reflector->expects($this->once())
  70. ->method('hasAnnotation')
  71. ->with('PublicPage')
  72. ->will($this->returnValue(false));
  73. $this->userSession->expects($this->once())
  74. ->method('isLoggedIn')
  75. ->will($this->returnValue(false));
  76. $this->userSession->expects($this->never())
  77. ->method('getUser');
  78. $this->middleware->beforeController($this->controller, 'index');
  79. }
  80. public function testBeforeControllerPublicPage() {
  81. $this->reflector->expects($this->once())
  82. ->method('hasAnnotation')
  83. ->with('PublicPage')
  84. ->will($this->returnValue(true));
  85. $this->userSession->expects($this->never())
  86. ->method('isLoggedIn');
  87. $this->middleware->beforeController($this->controller, 'create');
  88. }
  89. public function testBeforeControllerNoTwoFactorCheckNeeded() {
  90. $user = $this->createMock(IUser::class);
  91. $this->reflector->expects($this->once())
  92. ->method('hasAnnotation')
  93. ->with('PublicPage')
  94. ->will($this->returnValue(false));
  95. $this->userSession->expects($this->once())
  96. ->method('isLoggedIn')
  97. ->will($this->returnValue(true));
  98. $this->userSession->expects($this->once())
  99. ->method('getUser')
  100. ->will($this->returnValue($user));
  101. $this->twoFactorManager->expects($this->once())
  102. ->method('isTwoFactorAuthenticated')
  103. ->with($user)
  104. ->will($this->returnValue(false));
  105. $this->middleware->beforeController($this->controller, 'index');
  106. }
  107. /**
  108. * @expectedException \OC\Authentication\Exceptions\TwoFactorAuthRequiredException
  109. */
  110. public function testBeforeControllerTwoFactorAuthRequired() {
  111. $user = $this->createMock(IUser::class);
  112. $this->reflector->expects($this->once())
  113. ->method('hasAnnotation')
  114. ->with('PublicPage')
  115. ->will($this->returnValue(false));
  116. $this->userSession->expects($this->once())
  117. ->method('isLoggedIn')
  118. ->will($this->returnValue(true));
  119. $this->userSession->expects($this->once())
  120. ->method('getUser')
  121. ->will($this->returnValue($user));
  122. $this->twoFactorManager->expects($this->once())
  123. ->method('isTwoFactorAuthenticated')
  124. ->with($user)
  125. ->will($this->returnValue(true));
  126. $this->twoFactorManager->expects($this->once())
  127. ->method('needsSecondFactor')
  128. ->with($user)
  129. ->will($this->returnValue(true));
  130. $this->middleware->beforeController($this->controller, 'index');
  131. }
  132. /**
  133. * @expectedException \OC\Authentication\Exceptions\UserAlreadyLoggedInException
  134. */
  135. public function testBeforeControllerUserAlreadyLoggedIn() {
  136. $user = $this->createMock(IUser::class);
  137. $this->reflector->expects($this->once())
  138. ->method('hasAnnotation')
  139. ->with('PublicPage')
  140. ->will($this->returnValue(false));
  141. $this->userSession->expects($this->once())
  142. ->method('isLoggedIn')
  143. ->will($this->returnValue(true));
  144. $this->userSession->expects($this->once())
  145. ->method('getUser')
  146. ->will($this->returnValue($user));
  147. $this->twoFactorManager->expects($this->once())
  148. ->method('isTwoFactorAuthenticated')
  149. ->with($user)
  150. ->will($this->returnValue(true));
  151. $this->twoFactorManager->expects($this->once())
  152. ->method('needsSecondFactor')
  153. ->with($user)
  154. ->will($this->returnValue(false));
  155. $twoFactorChallengeController = $this->getMockBuilder('\OC\Core\Controller\TwoFactorChallengeController')
  156. ->disableOriginalConstructor()
  157. ->getMock();
  158. $this->middleware->beforeController($twoFactorChallengeController, 'index');
  159. }
  160. public function testAfterExceptionTwoFactorAuthRequired() {
  161. $ex = new \OC\Authentication\Exceptions\TwoFactorAuthRequiredException();
  162. $this->urlGenerator->expects($this->once())
  163. ->method('linkToRoute')
  164. ->with('core.TwoFactorChallenge.selectChallenge')
  165. ->will($this->returnValue('test/url'));
  166. $expected = new \OCP\AppFramework\Http\RedirectResponse('test/url');
  167. $this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex));
  168. }
  169. public function testAfterException() {
  170. $ex = new \OC\Authentication\Exceptions\UserAlreadyLoggedInException();
  171. $this->urlGenerator->expects($this->once())
  172. ->method('linkToRoute')
  173. ->with('files.view.index')
  174. ->will($this->returnValue('redirect/url'));
  175. $expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url');
  176. $this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex));
  177. }
  178. }