|
11 | 11 | use OC\AppFramework\Http\Request; |
12 | 12 | use OC\Authentication\Events\LoginFailed; |
13 | 13 | use OC\Authentication\Exceptions\InvalidTokenException; |
| 14 | +use OC\Authentication\Exceptions\PasswordlessTokenException; |
14 | 15 | use OC\Authentication\Exceptions\PasswordLoginForbiddenException; |
15 | 16 | use OC\Authentication\Token\IProvider; |
16 | 17 | use OC\Authentication\Token\IToken; |
| 18 | +use OC\Authentication\Token\PublicKeyToken; |
17 | 19 | use OC\Security\CSRF\CsrfTokenManager; |
18 | 20 | use OC\Session\Memory; |
19 | 21 | use OC\User\LoginException; |
|
35 | 37 | use OCP\User\Events\PostLoginEvent; |
36 | 38 | use PHPUnit\Framework\MockObject\MockObject; |
37 | 39 | use Psr\Log\LoggerInterface; |
| 40 | +use function array_diff; |
| 41 | +use function get_class_methods; |
38 | 42 |
|
39 | 43 | /** |
40 | 44 | * @group DB |
@@ -309,6 +313,80 @@ public function testLoginInvalidPassword() { |
309 | 313 | $userSession->login('foo', 'bar'); |
310 | 314 | } |
311 | 315 |
|
| 316 | + public function testPasswordlessLoginNoLastCheckUpdate(): void { |
| 317 | + $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); |
| 318 | + $managerMethods = get_class_methods(Manager::class); |
| 319 | + // Keep following methods intact in order to ensure hooks are working |
| 320 | + $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); |
| 321 | + $manager = $this->getMockBuilder(Manager::class) |
| 322 | + ->setMethods($mockedManagerMethods) |
| 323 | + ->setConstructorArgs([ |
| 324 | + $this->config, |
| 325 | + $this->createMock(ICacheFactory::class), |
| 326 | + $this->createMock(IEventDispatcher::class), |
| 327 | + ]) |
| 328 | + ->getMock(); |
| 329 | + $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); |
| 330 | + |
| 331 | + $session->expects($this->never()) |
| 332 | + ->method('set'); |
| 333 | + $session->expects($this->once()) |
| 334 | + ->method('regenerateId'); |
| 335 | + $token = new PublicKeyToken(); |
| 336 | + $token->setLoginName('foo'); |
| 337 | + $token->setLastCheck(0); // Never |
| 338 | + $token->setUid('foo'); |
| 339 | + $this->tokenProvider |
| 340 | + ->method('getPassword') |
| 341 | + ->with($token) |
| 342 | + ->willThrowException(new PasswordlessTokenException()); |
| 343 | + $this->tokenProvider |
| 344 | + ->method('getToken') |
| 345 | + ->with('app-password') |
| 346 | + ->willReturn($token); |
| 347 | + $this->tokenProvider->expects(self::never()) |
| 348 | + ->method('updateToken'); |
| 349 | + |
| 350 | + $userSession->login('foo', 'app-password'); |
| 351 | + } |
| 352 | + |
| 353 | + public function testLoginLastCheckUpdate(): void { |
| 354 | + $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); |
| 355 | + $managerMethods = get_class_methods(Manager::class); |
| 356 | + // Keep following methods intact in order to ensure hooks are working |
| 357 | + $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); |
| 358 | + $manager = $this->getMockBuilder(Manager::class) |
| 359 | + ->setMethods($mockedManagerMethods) |
| 360 | + ->setConstructorArgs([ |
| 361 | + $this->config, |
| 362 | + $this->createMock(ICacheFactory::class), |
| 363 | + $this->createMock(IEventDispatcher::class), |
| 364 | + ]) |
| 365 | + ->getMock(); |
| 366 | + $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); |
| 367 | + |
| 368 | + $session->expects($this->never()) |
| 369 | + ->method('set'); |
| 370 | + $session->expects($this->once()) |
| 371 | + ->method('regenerateId'); |
| 372 | + $token = new PublicKeyToken(); |
| 373 | + $token->setLoginName('foo'); |
| 374 | + $token->setLastCheck(0); // Never |
| 375 | + $token->setUid('foo'); |
| 376 | + $this->tokenProvider |
| 377 | + ->method('getPassword') |
| 378 | + ->with($token) |
| 379 | + ->willReturn('secret'); |
| 380 | + $this->tokenProvider |
| 381 | + ->method('getToken') |
| 382 | + ->with('app-password') |
| 383 | + ->willReturn($token); |
| 384 | + $this->tokenProvider->expects(self::once()) |
| 385 | + ->method('updateToken'); |
| 386 | + |
| 387 | + $userSession->login('foo', 'app-password'); |
| 388 | + } |
| 389 | + |
312 | 390 | public function testLoginNonExisting() { |
313 | 391 | $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); |
314 | 392 | $manager = $this->createMock(Manager::class); |
|
0 commit comments