PasswordConfirmationMiddlewareTest.php 5.6 KB

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