2016-05-11 12:23:25 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-25 13:02:05 +03:00
|
|
|
namespace Test\Core\Middleware;
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2019-10-25 15:42:00 +03:00
|
|
|
use OC\Authentication\Exceptions\TwoFactorAuthRequiredException;
|
|
|
|
use OC\Authentication\Exceptions\UserAlreadyLoggedInException;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\Manager;
|
2019-10-25 15:42:00 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\ProviderSet;
|
|
|
|
use OC\Core\Controller\TwoFactorChallengeController;
|
2016-05-25 13:02:05 +03:00
|
|
|
use OC\Core\Middleware\TwoFactorMiddleware;
|
2016-06-01 14:54:08 +03:00
|
|
|
use OC\AppFramework\Http\Request;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OC\User\Session;
|
2017-07-26 11:50:39 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
2016-09-02 12:04:29 +03:00
|
|
|
use OCP\AppFramework\Utility\IControllerMethodReflector;
|
2019-04-05 19:21:08 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\ALoginSetupController;
|
2019-10-25 15:42:00 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvider;
|
2016-09-02 12:04:29 +03:00
|
|
|
use OCP\IConfig;
|
2019-04-05 19:21:08 +03:00
|
|
|
use OCP\IRequest;
|
2016-09-02 12:04:29 +03:00
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUser;
|
2019-04-05 19:21:08 +03:00
|
|
|
use OCP\IUserSession;
|
2016-09-02 12:04:29 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2019-04-05 19:21:08 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2016-05-11 12:23:25 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class TwoFactorMiddlewareTest extends TestCase {
|
|
|
|
|
2019-04-05 19:21:08 +03:00
|
|
|
/** @var Manager|MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $twoFactorManager;
|
2019-04-05 19:21:08 +03:00
|
|
|
|
|
|
|
/** @var IUserSession|MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $userSession;
|
2019-04-05 19:21:08 +03:00
|
|
|
|
|
|
|
/** @var ISession|MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $session;
|
2019-04-05 19:21:08 +03:00
|
|
|
|
|
|
|
/** @var IURLGenerator|MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $urlGenerator;
|
2019-04-05 19:21:08 +03:00
|
|
|
|
|
|
|
/** @var IControllerMethodReflector|MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $reflector;
|
2019-04-05 19:21:08 +03:00
|
|
|
|
|
|
|
/** @var IRequest|MockObject */
|
2016-06-01 14:54:08 +03:00
|
|
|
private $request;
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
/** @var TwoFactorMiddleware */
|
|
|
|
private $middleware;
|
|
|
|
|
2017-07-26 11:50:39 +03:00
|
|
|
/** @var Controller */
|
|
|
|
private $controller;
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->twoFactorManager = $this->getMockBuilder(Manager::class)
|
2016-05-11 12:23:25 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->userSession = $this->getMockBuilder(Session::class)
|
2016-05-11 12:23:25 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-02 12:04:29 +03:00
|
|
|
$this->session = $this->createMock(ISession::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
$this->reflector = $this->createMock(IControllerMethodReflector::class);
|
2016-06-01 14:54:08 +03:00
|
|
|
$this->request = new Request(
|
|
|
|
[
|
|
|
|
'server' => [
|
|
|
|
'REQUEST_URI' => 'test/url'
|
|
|
|
]
|
|
|
|
],
|
2016-09-02 12:04:29 +03:00
|
|
|
$this->createMock(ISecureRandom::class),
|
|
|
|
$this->createMock(IConfig::class)
|
2016-06-01 14:54:08 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->middleware = new TwoFactorMiddleware($this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator, $this->reflector, $this->request);
|
2017-07-26 11:50:39 +03:00
|
|
|
$this->controller = $this->createMock(Controller::class);
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBeforeControllerNotLoggedIn() {
|
|
|
|
$this->reflector->expects($this->once())
|
|
|
|
->method('hasAnnotation')
|
|
|
|
->with('PublicPage')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('getUser');
|
|
|
|
|
2017-07-26 11:50:39 +03:00
|
|
|
$this->middleware->beforeController($this->controller, 'index');
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBeforeControllerPublicPage() {
|
|
|
|
$this->reflector->expects($this->once())
|
|
|
|
->method('hasAnnotation')
|
|
|
|
->with('PublicPage')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('isLoggedIn');
|
|
|
|
|
2017-07-26 11:50:39 +03:00
|
|
|
$this->middleware->beforeController($this->controller, 'create');
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2019-04-05 19:21:08 +03:00
|
|
|
public function testBeforeSetupController() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$controller = $this->createMock(ALoginSetupController::class);
|
|
|
|
$this->reflector->expects($this->once())
|
|
|
|
->method('hasAnnotation')
|
|
|
|
->with('PublicPage')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userSession->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('needsSecondFactor')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('isLoggedIn');
|
|
|
|
|
|
|
|
$this->middleware->beforeController($controller, 'create');
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
public function testBeforeControllerNoTwoFactorCheckNeeded() {
|
2016-09-02 12:04:29 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->reflector->expects($this->once())
|
|
|
|
->method('hasAnnotation')
|
|
|
|
->with('PublicPage')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('isTwoFactorAuthenticated')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2017-07-26 11:50:39 +03:00
|
|
|
$this->middleware->beforeController($this->controller, 'index');
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OC\Authentication\Exceptions\TwoFactorAuthRequiredException
|
|
|
|
*/
|
|
|
|
public function testBeforeControllerTwoFactorAuthRequired() {
|
2016-09-02 12:04:29 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
|
|
|
$this->reflector->expects($this->once())
|
|
|
|
->method('hasAnnotation')
|
|
|
|
->with('PublicPage')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('isTwoFactorAuthenticated')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('needsSecondFactor')
|
2016-08-24 11:42:07 +03:00
|
|
|
->with($user)
|
2016-05-11 12:23:25 +03:00
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2017-07-26 11:50:39 +03:00
|
|
|
$this->middleware->beforeController($this->controller, 'index');
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OC\Authentication\Exceptions\UserAlreadyLoggedInException
|
|
|
|
*/
|
|
|
|
public function testBeforeControllerUserAlreadyLoggedIn() {
|
2016-09-02 12:04:29 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2019-10-25 15:42:00 +03:00
|
|
|
$this->reflector
|
2016-05-11 12:23:25 +03:00
|
|
|
->method('hasAnnotation')
|
2019-10-25 15:42:00 +03:00
|
|
|
->willReturn(false);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->will($this->returnValue(true));
|
2019-10-25 15:42:00 +03:00
|
|
|
$this->userSession
|
2016-05-11 12:23:25 +03:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('isTwoFactorAuthenticated')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('needsSecondFactor')
|
2016-08-24 11:42:07 +03:00
|
|
|
->with($user)
|
2016-05-11 12:23:25 +03:00
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$twoFactorChallengeController = $this->getMockBuilder('\OC\Core\Controller\TwoFactorChallengeController')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$this->middleware->beforeController($twoFactorChallengeController, 'index');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAfterExceptionTwoFactorAuthRequired() {
|
|
|
|
$ex = new \OC\Authentication\Exceptions\TwoFactorAuthRequiredException();
|
|
|
|
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.selectChallenge')
|
2016-06-01 14:54:08 +03:00
|
|
|
->will($this->returnValue('test/url'));
|
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse('test/url');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2017-07-26 11:50:39 +03:00
|
|
|
$this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAfterException() {
|
|
|
|
$ex = new \OC\Authentication\Exceptions\UserAlreadyLoggedInException();
|
|
|
|
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('files.view.index')
|
|
|
|
->will($this->returnValue('redirect/url'));
|
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url');
|
|
|
|
|
2017-07-26 11:50:39 +03:00
|
|
|
$this->assertEquals($expected, $this->middleware->afterException($this->controller, 'index', $ex));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
|
|
|
|
2019-10-25 15:42:00 +03:00
|
|
|
public function testRequires2FASetupDoneAnnotated() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
|
|
|
$this->reflector
|
|
|
|
->method('hasAnnotation')
|
|
|
|
->will($this->returnCallback(function (string $annotation) {
|
|
|
|
return $annotation === 'TwoFactorSetUpDoneRequired';
|
|
|
|
}));
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->userSession
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('isTwoFactorAuthenticated')
|
|
|
|
->with($user)
|
|
|
|
->willReturn(true);
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('needsSecondFactor')
|
|
|
|
->with($user)
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->expectException(UserAlreadyLoggedInException::class);
|
|
|
|
|
|
|
|
$twoFactorChallengeController = $this->getMockBuilder(TwoFactorChallengeController::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$this->middleware->beforeController($twoFactorChallengeController, 'index');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataRequires2FASetupDone() {
|
|
|
|
$provider = $this->createMock(IProvider::class);
|
|
|
|
$provider->method('getId')
|
|
|
|
->willReturn('2FAftw');
|
|
|
|
|
|
|
|
return [
|
|
|
|
[[], false, false],
|
|
|
|
[[], true, true],
|
|
|
|
[[$provider], false, true],
|
|
|
|
[[$provider], true, true],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataRequires2FASetupDone
|
|
|
|
*/
|
|
|
|
public function testRequires2FASetupDone(array $providers, bool $missingProviders, bool $expectEception) {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
|
|
|
|
$this->reflector
|
|
|
|
->method('hasAnnotation')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userSession
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
|
|
|
$providerSet = new ProviderSet($providers, $missingProviders);
|
|
|
|
$this->twoFactorManager->method('getProviderSet')
|
|
|
|
->with($user)
|
|
|
|
->willReturn($providerSet);
|
|
|
|
$this->userSession
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
if ($expectEception) {
|
|
|
|
$this->expectException(TwoFactorAuthRequiredException::class);
|
|
|
|
} else {
|
|
|
|
// hack to make phpunit shut up. Since we don't expect an exception here...
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$twoFactorChallengeController = $this->getMockBuilder(TwoFactorChallengeController::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$this->middleware->beforeController($twoFactorChallengeController, 'index');
|
|
|
|
}
|
|
|
|
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|