PasswordConfirmationMiddlewareTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\AppFramework\Middleware\Security;
  7. use OC\AppFramework\Middleware\Security\Exceptions\NotConfirmedException;
  8. use OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware;
  9. use OC\AppFramework\Utility\ControllerMethodReflector;
  10. use OC\Authentication\Token\IProvider;
  11. use OC\User\Manager;
  12. use OCP\AppFramework\Utility\ITimeFactory;
  13. use OCP\Authentication\Token\IToken;
  14. use OCP\IRequest;
  15. use OCP\ISession;
  16. use OCP\IUser;
  17. use OCP\IUserSession;
  18. use Psr\Log\LoggerInterface;
  19. use Test\AppFramework\Middleware\Security\Mock\PasswordConfirmationMiddlewareController;
  20. use Test\TestCase;
  21. class PasswordConfirmationMiddlewareTest extends TestCase {
  22. /** @var ControllerMethodReflector */
  23. private $reflector;
  24. /** @var ISession&\PHPUnit\Framework\MockObject\MockObject */
  25. private $session;
  26. /** @var IUserSession&\PHPUnit\Framework\MockObject\MockObject */
  27. private $userSession;
  28. /** @var IUser&\PHPUnit\Framework\MockObject\MockObject */
  29. private $user;
  30. /** @var PasswordConfirmationMiddleware */
  31. private $middleware;
  32. /** @var PasswordConfirmationMiddlewareController */
  33. private $controller;
  34. /** @var ITimeFactory&\PHPUnit\Framework\MockObject\MockObject */
  35. private $timeFactory;
  36. private IProvider&\PHPUnit\Framework\MockObject\MockObject $tokenProvider;
  37. private LoggerInterface $logger;
  38. /** @var IRequest&\PHPUnit\Framework\MockObject\MockObject */
  39. private IRequest $request;
  40. /** @var Manager&\PHPUnit\Framework\MockObject\MockObject */
  41. private Manager $userManager;
  42. protected function setUp(): void {
  43. $this->reflector = new ControllerMethodReflector();
  44. $this->session = $this->createMock(ISession::class);
  45. $this->userSession = $this->createMock(IUserSession::class);
  46. $this->user = $this->createMock(IUser::class);
  47. $this->timeFactory = $this->createMock(ITimeFactory::class);
  48. $this->tokenProvider = $this->createMock(IProvider::class);
  49. $this->logger = $this->createMock(LoggerInterface::class);
  50. $this->request = $this->createMock(IRequest::class);
  51. $this->userManager = $this->createMock(Manager::class);
  52. $this->controller = new PasswordConfirmationMiddlewareController(
  53. 'test',
  54. $this->createMock(IRequest::class)
  55. );
  56. $this->middleware = new PasswordConfirmationMiddleware(
  57. $this->reflector,
  58. $this->session,
  59. $this->userSession,
  60. $this->timeFactory,
  61. $this->tokenProvider,
  62. $this->logger,
  63. $this->request,
  64. $this->userManager,
  65. );
  66. }
  67. public function testNoAnnotationNorAttribute(): void {
  68. $this->reflector->reflect($this->controller, __FUNCTION__);
  69. $this->session->expects($this->never())
  70. ->method($this->anything());
  71. $this->userSession->expects($this->never())
  72. ->method($this->anything());
  73. $this->middleware->beforeController($this->controller, __FUNCTION__);
  74. }
  75. public function testDifferentAnnotation(): void {
  76. $this->reflector->reflect($this->controller, __FUNCTION__);
  77. $this->session->expects($this->never())
  78. ->method($this->anything());
  79. $this->userSession->expects($this->never())
  80. ->method($this->anything());
  81. $this->middleware->beforeController($this->controller, __FUNCTION__);
  82. }
  83. /**
  84. * @dataProvider dataProvider
  85. */
  86. public function testAnnotation($backend, $lastConfirm, $currentTime, $exception): void {
  87. $this->reflector->reflect($this->controller, __FUNCTION__);
  88. $this->user->method('getBackendClassName')
  89. ->willReturn($backend);
  90. $this->userSession->method('getUser')
  91. ->willReturn($this->user);
  92. $this->session->method('get')
  93. ->with('last-password-confirm')
  94. ->willReturn($lastConfirm);
  95. $this->timeFactory->method('getTime')
  96. ->willReturn($currentTime);
  97. $token = $this->createMock(IToken::class);
  98. $token->method('getScopeAsArray')
  99. ->willReturn([]);
  100. $this->tokenProvider->expects($this->once())
  101. ->method('getToken')
  102. ->willReturn($token);
  103. $thrown = false;
  104. try {
  105. $this->middleware->beforeController($this->controller, __FUNCTION__);
  106. } catch (NotConfirmedException $e) {
  107. $thrown = true;
  108. }
  109. $this->assertSame($exception, $thrown);
  110. }
  111. /**
  112. * @dataProvider dataProvider
  113. */
  114. public function testAttribute($backend, $lastConfirm, $currentTime, $exception): void {
  115. $this->reflector->reflect($this->controller, __FUNCTION__);
  116. $this->user->method('getBackendClassName')
  117. ->willReturn($backend);
  118. $this->userSession->method('getUser')
  119. ->willReturn($this->user);
  120. $this->session->method('get')
  121. ->with('last-password-confirm')
  122. ->willReturn($lastConfirm);
  123. $this->timeFactory->method('getTime')
  124. ->willReturn($currentTime);
  125. $token = $this->createMock(IToken::class);
  126. $token->method('getScopeAsArray')
  127. ->willReturn([]);
  128. $this->tokenProvider->expects($this->once())
  129. ->method('getToken')
  130. ->willReturn($token);
  131. $thrown = false;
  132. try {
  133. $this->middleware->beforeController($this->controller, __FUNCTION__);
  134. } catch (NotConfirmedException $e) {
  135. $thrown = true;
  136. }
  137. $this->assertSame($exception, $thrown);
  138. }
  139. public function dataProvider() {
  140. return [
  141. ['foo', 2000, 4000, true],
  142. ['foo', 2000, 3000, false],
  143. ['user_saml', 2000, 4000, false],
  144. ['user_saml', 2000, 3000, false],
  145. ['foo', 2000, 3815, false],
  146. ['foo', 2000, 3816, true],
  147. ];
  148. }
  149. public function testSSO(): void {
  150. static $sessionId = 'mySession1d';
  151. $this->reflector->reflect($this->controller, __FUNCTION__);
  152. $this->user->method('getBackendClassName')
  153. ->willReturn('fictional_backend');
  154. $this->userSession->method('getUser')
  155. ->willReturn($this->user);
  156. $this->session->method('get')
  157. ->with('last-password-confirm')
  158. ->willReturn(0);
  159. $this->session->method('getId')
  160. ->willReturn($sessionId);
  161. $this->timeFactory->method('getTime')
  162. ->willReturn(9876);
  163. $token = $this->createMock(IToken::class);
  164. $token->method('getScopeAsArray')
  165. ->willReturn([IToken::SCOPE_SKIP_PASSWORD_VALIDATION => true]);
  166. $this->tokenProvider->expects($this->once())
  167. ->method('getToken')
  168. ->with($sessionId)
  169. ->willReturn($token);
  170. $thrown = false;
  171. try {
  172. $this->middleware->beforeController($this->controller, __FUNCTION__);
  173. } catch (NotConfirmedException) {
  174. $thrown = true;
  175. }
  176. $this->assertSame(false, $thrown);
  177. }
  178. }