2016-04-11 13:08:00 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @author Lukas Reschke <lukas@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-19 11:43:49 +03:00
|
|
|
namespace Tests\Core\Controller;
|
2016-04-11 13:08:00 +03:00
|
|
|
|
2018-04-06 20:58:37 +03:00
|
|
|
use OC\Authentication\Token\IToken;
|
2016-05-11 12:23:25 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\Manager;
|
2018-05-22 09:52:16 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\ProviderSet;
|
2016-05-19 11:43:49 +03:00
|
|
|
use OC\Core\Controller\LoginController;
|
2018-01-13 23:18:05 +03:00
|
|
|
use OC\Security\Bruteforce\Throttler;
|
2017-01-12 14:38:45 +03:00
|
|
|
use OC\User\Session;
|
2018-08-08 16:25:59 +03:00
|
|
|
use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2018-05-22 09:52:16 +03:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvider;
|
2017-09-04 15:17:03 +03:00
|
|
|
use OCP\Defaults;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\IConfig;
|
2017-01-12 14:38:45 +03:00
|
|
|
use OCP\ILogger;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISession;
|
2016-04-18 13:14:07 +03:00
|
|
|
use OCP\IURLGenerator;
|
2016-06-09 17:44:31 +03:00
|
|
|
use OCP\IUser;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class LoginControllerTest extends TestCase {
|
|
|
|
/** @var LoginController */
|
|
|
|
private $loginController;
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $request;
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $userManager;
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $config;
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var ISession|\PHPUnit_Framework_MockObject_MockObject */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $session;
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var Session|\PHPUnit_Framework_MockObject_MockObject */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $userSession;
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
|
2016-04-18 13:14:07 +03:00
|
|
|
private $urlGenerator;
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $logger;
|
|
|
|
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $twoFactorManager;
|
2017-09-04 15:17:03 +03:00
|
|
|
/** @var Defaults|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $defaults;
|
2018-01-13 23:18:05 +03:00
|
|
|
/** @var Throttler|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $throttler;
|
2016-04-11 13:08:00 +03:00
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2017-01-12 14:38:45 +03:00
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->userManager = $this->createMock(\OC\User\Manager::class);
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
|
|
|
$this->session = $this->createMock(ISession::class);
|
|
|
|
$this->userSession = $this->createMock(Session::class);
|
|
|
|
$this->urlGenerator = $this->createMock(IURLGenerator::class);
|
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->twoFactorManager = $this->createMock(Manager::class);
|
2017-09-04 15:17:03 +03:00
|
|
|
$this->defaults = $this->createMock(Defaults::class);
|
2018-01-13 23:18:05 +03:00
|
|
|
$this->throttler = $this->createMock(Throttler::class);
|
|
|
|
|
|
|
|
$this->request->method('getRemoteAddress')
|
|
|
|
->willReturn('1.2.3.4');
|
|
|
|
$this->throttler->method('getDelay')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('1.2.3.4'),
|
|
|
|
$this->equalTo('')
|
|
|
|
)->willReturn(1000);
|
2016-04-11 13:08:00 +03:00
|
|
|
|
|
|
|
$this->loginController = new LoginController(
|
|
|
|
'core',
|
|
|
|
$this->request,
|
|
|
|
$this->userManager,
|
|
|
|
$this->config,
|
|
|
|
$this->session,
|
2016-04-18 13:14:07 +03:00
|
|
|
$this->userSession,
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->urlGenerator,
|
2017-01-12 14:38:45 +03:00
|
|
|
$this->logger,
|
2017-09-04 15:17:03 +03:00
|
|
|
$this->twoFactorManager,
|
2018-01-13 23:18:05 +03:00
|
|
|
$this->defaults,
|
|
|
|
$this->throttler
|
2016-04-11 13:08:00 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:14:07 +03:00
|
|
|
public function testLogoutWithoutToken() {
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getCookie')
|
2017-02-02 23:56:44 +03:00
|
|
|
->with('nc_token')
|
2016-04-18 13:14:07 +03:00
|
|
|
->willReturn(null);
|
|
|
|
$this->config
|
|
|
|
->expects($this->never())
|
|
|
|
->method('deleteUserValue');
|
|
|
|
$this->urlGenerator
|
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
|
|
|
->with('core.login.showLoginForm')
|
|
|
|
->willReturn('/login');
|
|
|
|
|
|
|
|
$expected = new RedirectResponse('/login');
|
2018-10-15 15:25:08 +03:00
|
|
|
$expected->addHeader('Clear-Site-Data', '"cache", "storage", "executionContexts"');
|
2016-04-18 13:14:07 +03:00
|
|
|
$this->assertEquals($expected, $this->loginController->logout());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogoutWithToken() {
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getCookie')
|
2017-02-02 23:56:44 +03:00
|
|
|
->with('nc_token')
|
2016-04-18 13:14:07 +03:00
|
|
|
->willReturn('MyLoginToken');
|
2017-01-12 14:38:45 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2016-04-18 13:14:07 +03:00
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('JohnDoe');
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('deleteUserValue')
|
|
|
|
->with('JohnDoe', 'login_token', 'MyLoginToken');
|
|
|
|
$this->urlGenerator
|
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
|
|
|
->with('core.login.showLoginForm')
|
|
|
|
->willReturn('/login');
|
|
|
|
|
|
|
|
$expected = new RedirectResponse('/login');
|
2018-10-15 15:25:08 +03:00
|
|
|
$expected->addHeader('Clear-Site-Data', '"cache", "storage", "executionContexts"');
|
2016-04-18 13:14:07 +03:00
|
|
|
$this->assertEquals($expected, $this->loginController->logout());
|
|
|
|
}
|
|
|
|
|
2016-04-11 13:08:00 +03:00
|
|
|
public function testShowLoginFormForLoggedInUsers() {
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$expectedResponse = new RedirectResponse(\OC_Util::getDefaultPageUrl());
|
|
|
|
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '', ''));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowLoginFormWithErrorsInSession() {
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('loginMessages')
|
|
|
|
->willReturn(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'ErrorArray1',
|
|
|
|
'ErrorArray2',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'MessageArray1',
|
|
|
|
'MessageArray2',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'ErrorArray1' => true,
|
|
|
|
'ErrorArray2' => true,
|
|
|
|
'messages' => [
|
|
|
|
'MessageArray1',
|
|
|
|
'MessageArray2',
|
|
|
|
],
|
2016-04-15 20:02:19 +03:00
|
|
|
'loginName' => '',
|
2016-04-11 13:08:00 +03:00
|
|
|
'user_autofocus' => true,
|
|
|
|
'canResetPassword' => true,
|
|
|
|
'alt_login' => [],
|
2016-08-15 18:19:32 +03:00
|
|
|
'resetPasswordLink' => null,
|
2018-01-13 23:18:05 +03:00
|
|
|
'throttle_delay' => 1000,
|
2016-04-11 13:08:00 +03:00
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', '', ''));
|
|
|
|
}
|
|
|
|
|
2017-12-21 02:25:30 +03:00
|
|
|
public function testShowLoginFormForFlowAuth() {
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'messages' => [],
|
|
|
|
'redirect_url' => 'login/flow',
|
|
|
|
'loginName' => '',
|
|
|
|
'user_autofocus' => true,
|
|
|
|
'canResetPassword' => true,
|
|
|
|
'alt_login' => [],
|
|
|
|
'resetPasswordLink' => null,
|
2018-01-13 23:18:05 +03:00
|
|
|
'throttle_delay' => 1000,
|
2017-12-21 02:25:30 +03:00
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('', 'login/flow', ''));
|
|
|
|
}
|
|
|
|
|
2016-04-11 13:08:00 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function passwordResetDataProvider() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider passwordResetDataProvider
|
|
|
|
*/
|
|
|
|
public function testShowLoginFormWithPasswordResetOption($canChangePassword,
|
|
|
|
$expectedResult) {
|
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('lost_password_link')
|
|
|
|
->willReturn(false);
|
2017-01-12 14:38:45 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2016-04-11 13:08:00 +03:00
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('canChangePassword')
|
|
|
|
->willReturn($canChangePassword);
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('LdapUser')
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'messages' => [],
|
2016-04-15 20:02:19 +03:00
|
|
|
'loginName' => 'LdapUser',
|
2016-04-11 13:08:00 +03:00
|
|
|
'user_autofocus' => false,
|
|
|
|
'canResetPassword' => $expectedResult,
|
|
|
|
'alt_login' => [],
|
2016-08-15 18:19:32 +03:00
|
|
|
'resetPasswordLink' => false,
|
2018-01-13 23:18:05 +03:00
|
|
|
'throttle_delay' => 1000,
|
2016-04-11 13:08:00 +03:00
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('LdapUser', '', ''));
|
|
|
|
}
|
2016-04-15 19:57:11 +03:00
|
|
|
|
2018-07-21 14:05:25 +03:00
|
|
|
/**
|
|
|
|
* Asserts that a disabled user can't login and gets the expected response.
|
|
|
|
*/
|
|
|
|
public function testLoginForDisabledUser() {
|
|
|
|
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user->method('getUID')
|
|
|
|
->willReturn('uid');
|
|
|
|
$user->method('isEnabled')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->userSession
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$loginName = 'iMDisabled';
|
|
|
|
$password = 'secret';
|
|
|
|
|
|
|
|
$this->session
|
|
|
|
->expects($this->once())
|
|
|
|
->method('set')
|
|
|
|
->with('loginMessages', [
|
|
|
|
[LoginController::LOGIN_MSG_USERDISABLED], []
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with($loginName)
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$expected = new RedirectResponse('');
|
|
|
|
$expected->throttle(['user' => $loginName]);
|
|
|
|
|
|
|
|
$response = $this->loginController->tryLogin(
|
|
|
|
$loginName, $password, null, false, 'Europe/Berlin', '1'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expected, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowLoginFormForUserNamed0() {
|
2016-04-15 19:57:11 +03:00
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->config
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('lost_password_link')
|
|
|
|
->willReturn(false);
|
2017-01-12 14:38:45 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2018-07-21 14:05:25 +03:00
|
|
|
$user->expects($this->once())
|
2016-04-15 19:57:11 +03:00
|
|
|
->method('canChangePassword')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('0')
|
|
|
|
->willReturn($user);
|
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'messages' => [],
|
2016-04-15 20:02:19 +03:00
|
|
|
'loginName' => '0',
|
2016-04-15 19:57:11 +03:00
|
|
|
'user_autofocus' => false,
|
|
|
|
'canResetPassword' => false,
|
|
|
|
'alt_login' => [],
|
2016-08-15 18:19:32 +03:00
|
|
|
'resetPasswordLink' => false,
|
2018-01-13 23:18:05 +03:00
|
|
|
'throttle_delay' => 1000,
|
2016-04-15 19:57:11 +03:00
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('0', '', ''));
|
|
|
|
}
|
2016-04-27 17:44:51 +03:00
|
|
|
|
|
|
|
public function testLoginWithInvalidCredentials() {
|
2016-07-20 19:36:15 +03:00
|
|
|
$user = 'MyUserName';
|
2016-04-27 17:44:51 +03:00
|
|
|
$password = 'secret';
|
2017-04-25 10:38:19 +03:00
|
|
|
$loginPageUrl = '/login?redirect_url=/apps/files';
|
2016-04-27 17:44:51 +03:00
|
|
|
|
2016-08-09 20:01:50 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(true);
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->userManager->expects($this->once())
|
2017-01-12 14:38:45 +03:00
|
|
|
->method('checkPasswordNoLogging')
|
2016-04-27 17:44:51 +03:00
|
|
|
->will($this->returnValue(false));
|
2017-08-06 22:27:28 +03:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('getByEmail')
|
|
|
|
->with($user)
|
|
|
|
->willReturn([]);
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
2017-04-25 10:38:19 +03:00
|
|
|
->with('core.login.showLoginForm', [
|
|
|
|
'user' => 'MyUserName',
|
|
|
|
'redirect_url' => '/apps/files',
|
|
|
|
])
|
2016-04-27 17:44:51 +03:00
|
|
|
->will($this->returnValue($loginPageUrl));
|
|
|
|
|
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('createSessionToken');
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('createRememberMeToken');
|
2016-08-23 13:54:45 +03:00
|
|
|
$this->config->expects($this->never())
|
|
|
|
->method('deleteUserValue');
|
2016-04-27 17:44:51 +03:00
|
|
|
|
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse($loginPageUrl);
|
2017-07-27 15:14:20 +03:00
|
|
|
$expected->throttle(['user' => 'MyUserName']);
|
2017-04-25 10:38:19 +03:00
|
|
|
$this->assertEquals($expected, $this->loginController->tryLogin($user, $password, '/apps/files'));
|
2016-04-27 17:44:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoginWithValidCredentials() {
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
|
|
|
|
$user = $this->createMock(IUser::class);
|
2016-08-23 13:54:45 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('uid'));
|
2016-11-30 16:59:59 +03:00
|
|
|
$loginName = 'loginli';
|
2016-12-08 12:45:24 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getLastLogin')
|
|
|
|
->willReturn(123456);
|
2016-04-27 17:44:51 +03:00
|
|
|
$password = 'secret';
|
2016-08-15 18:37:55 +03:00
|
|
|
$indexPageUrl = \OC_Util::getDefaultPageUrl();
|
2016-04-27 17:44:51 +03:00
|
|
|
|
2016-08-09 20:01:50 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(true);
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->userManager->expects($this->once())
|
2017-01-12 14:38:45 +03:00
|
|
|
->method('checkPasswordNoLogging')
|
2016-05-09 16:33:56 +03:00
|
|
|
->will($this->returnValue($user));
|
2016-05-24 11:50:18 +03:00
|
|
|
$this->userSession->expects($this->once())
|
2016-11-30 15:28:36 +03:00
|
|
|
->method('completeLogin')
|
|
|
|
->with($user, ['loginName' => $loginName, 'password' => $password]);
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('createSessionToken')
|
2018-04-06 20:58:37 +03:00
|
|
|
->with($this->request, $user->getUID(), $loginName, $password, IToken::REMEMBER);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('isTwoFactorAuthenticated')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(false));
|
2016-08-23 13:54:45 +03:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('deleteUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('uid', 'core', 'lostpassword');
|
2016-12-08 12:45:24 +03:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('setUserValue')
|
|
|
|
->with('uid', 'core', 'timezone', 'Europe/Berlin');
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('createRememberMeToken');
|
2016-04-27 17:44:51 +03:00
|
|
|
|
2016-12-08 12:45:24 +03:00
|
|
|
$this->session->expects($this->exactly(2))
|
|
|
|
->method('set')
|
|
|
|
->withConsecutive(
|
|
|
|
['last-password-confirm', 123456],
|
|
|
|
['timezone', '1']
|
|
|
|
);
|
|
|
|
|
2016-04-27 17:44:51 +03:00
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse($indexPageUrl);
|
2016-11-30 16:59:59 +03:00
|
|
|
$this->assertEquals($expected, $this->loginController->tryLogin($loginName, $password, null, false, 'Europe/Berlin', '1'));
|
2016-04-27 17:44:51 +03:00
|
|
|
}
|
|
|
|
|
2016-09-06 22:41:15 +03:00
|
|
|
public function testLoginWithValidCredentialsAndRememberMe() {
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
|
|
|
|
$user = $this->createMock(IUser::class);
|
2016-09-06 22:41:15 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('uid'));
|
2018-05-22 09:52:16 +03:00
|
|
|
$loginName = 'loginli';
|
2016-09-06 22:41:15 +03:00
|
|
|
$password = 'secret';
|
|
|
|
$indexPageUrl = \OC_Util::getDefaultPageUrl();
|
|
|
|
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->userManager->expects($this->once())
|
2017-01-12 14:38:45 +03:00
|
|
|
->method('checkPasswordNoLogging')
|
2016-09-06 22:41:15 +03:00
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->userSession->expects($this->once())
|
2016-11-30 15:28:36 +03:00
|
|
|
->method('completeLogin')
|
|
|
|
->with($user, ['loginName' => $loginName, 'password' => $password]);
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('createSessionToken')
|
2016-11-30 16:59:59 +03:00
|
|
|
->with($this->request, $user->getUID(), $loginName, $password, true);
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('isTwoFactorAuthenticated')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('deleteUserValue')
|
|
|
|
->with('uid', 'core', 'lostpassword');
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('createRememberMeToken')
|
|
|
|
->with($user);
|
|
|
|
|
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse($indexPageUrl);
|
2016-11-30 16:59:59 +03:00
|
|
|
$this->assertEquals($expected, $this->loginController->tryLogin($loginName, $password, null, true));
|
2016-09-06 22:41:15 +03:00
|
|
|
}
|
|
|
|
|
2016-08-09 20:01:50 +03:00
|
|
|
public function testLoginWithoutPassedCsrfCheckAndNotLoggedIn() {
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
|
|
|
|
$user = $this->createMock(IUser::class);
|
2016-08-09 20:01:50 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('jane'));
|
|
|
|
$password = 'secret';
|
|
|
|
$originalUrl = 'another%20url';
|
|
|
|
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->with()
|
|
|
|
->will($this->returnValue(false));
|
2016-08-23 13:54:45 +03:00
|
|
|
$this->config->expects($this->never())
|
|
|
|
->method('deleteUserValue');
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('createRememberMeToken');
|
2016-08-09 20:01:50 +03:00
|
|
|
|
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse(\OC_Util::getDefaultPageUrl());
|
|
|
|
$this->assertEquals($expected, $this->loginController->tryLogin('Jane', $password, $originalUrl));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoginWithoutPassedCsrfCheckAndLoggedIn() {
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
|
|
|
|
$user = $this->createMock(IUser::class);
|
2016-08-09 20:01:50 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('jane'));
|
|
|
|
$password = 'secret';
|
|
|
|
$originalUrl = 'another%20url';
|
|
|
|
$redirectUrl = 'http://localhost/another url';
|
|
|
|
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->with()
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('getAbsoluteURL')
|
|
|
|
->with(urldecode($originalUrl))
|
|
|
|
->will($this->returnValue($redirectUrl));
|
2016-08-23 13:54:45 +03:00
|
|
|
$this->config->expects($this->never())
|
|
|
|
->method('deleteUserValue');
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('createRememberMeToken');
|
2016-08-09 20:01:50 +03:00
|
|
|
|
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse($redirectUrl);
|
|
|
|
$this->assertEquals($expected, $this->loginController->tryLogin('Jane', $password, $originalUrl));
|
|
|
|
}
|
|
|
|
|
2016-04-27 17:44:51 +03:00
|
|
|
public function testLoginWithValidCredentialsAndRedirectUrl() {
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
|
|
|
|
$user = $this->createMock(IUser::class);
|
2016-05-09 16:33:56 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('jane'));
|
2016-04-27 17:44:51 +03:00
|
|
|
$password = 'secret';
|
|
|
|
$originalUrl = 'another%20url';
|
|
|
|
$redirectUrl = 'http://localhost/another url';
|
|
|
|
|
2016-08-09 20:01:50 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(true);
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->userManager->expects($this->once())
|
2017-01-12 14:38:45 +03:00
|
|
|
->method('checkPasswordNoLogging')
|
2016-05-24 11:50:18 +03:00
|
|
|
->with('Jane', $password)
|
2016-05-09 16:33:56 +03:00
|
|
|
->will($this->returnValue($user));
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('createSessionToken')
|
2018-04-06 20:58:37 +03:00
|
|
|
->with($this->request, $user->getUID(), 'Jane', $password, IToken::REMEMBER);
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->with()
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('getAbsoluteURL')
|
|
|
|
->with(urldecode($originalUrl))
|
|
|
|
->will($this->returnValue($redirectUrl));
|
2016-08-23 13:54:45 +03:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('deleteUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('jane', 'core', 'lostpassword');
|
2016-04-27 17:44:51 +03:00
|
|
|
|
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse(urldecode($redirectUrl));
|
2016-05-24 11:50:18 +03:00
|
|
|
$this->assertEquals($expected, $this->loginController->tryLogin('Jane', $password, $originalUrl));
|
2016-04-27 17:44:51 +03:00
|
|
|
}
|
2018-05-22 09:52:16 +03:00
|
|
|
|
2016-08-29 19:36:39 +03:00
|
|
|
public function testLoginWithOneTwoFactorProvider() {
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
|
|
|
|
$user = $this->createMock(IUser::class);
|
2016-05-24 11:50:18 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('john'));
|
2016-05-11 12:23:25 +03:00
|
|
|
$password = 'secret';
|
|
|
|
$challengeUrl = 'challenge/url';
|
2018-08-08 16:25:59 +03:00
|
|
|
$provider1 = $this->createMock(IProvider::class);
|
|
|
|
$provider1->method('getId')->willReturn('u2f');
|
|
|
|
$provider2 = $this->createMock(BackupCodesProvider::class);
|
|
|
|
$provider2->method('getId')->willReturn('backup');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-08-09 20:01:50 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(true);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->userManager->expects($this->once())
|
2017-01-12 14:38:45 +03:00
|
|
|
->method('checkPasswordNoLogging')
|
2016-05-11 12:23:25 +03:00
|
|
|
->will($this->returnValue($user));
|
2016-05-24 11:50:18 +03:00
|
|
|
$this->userSession->expects($this->once())
|
2016-11-30 15:28:36 +03:00
|
|
|
->method('completeLogin')
|
|
|
|
->with($user, ['loginName' => 'john@doe.com', 'password' => $password]);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('createSessionToken')
|
2018-04-06 20:58:37 +03:00
|
|
|
->with($this->request, $user->getUID(), 'john@doe.com', $password, IToken::REMEMBER);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('isTwoFactorAuthenticated')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('prepareTwoFactorLogin')
|
|
|
|
->with($user);
|
2018-08-08 16:25:59 +03:00
|
|
|
$providerSet = new ProviderSet([$provider1, $provider2], false);
|
2016-08-29 19:36:39 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
2018-05-22 09:52:16 +03:00
|
|
|
->method('getProviderSet')
|
2016-08-29 19:36:39 +03:00
|
|
|
->with($user)
|
2018-05-22 09:52:16 +03:00
|
|
|
->willReturn($providerSet);
|
2016-08-29 19:36:39 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.showChallenge', [
|
|
|
|
'challengeProviderId' => 'u2f',
|
|
|
|
])
|
|
|
|
->will($this->returnValue($challengeUrl));
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('deleteUserValue')
|
|
|
|
->with('john', 'core', 'lostpassword');
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('createRememberMeToken');
|
2016-08-29 19:36:39 +03:00
|
|
|
|
|
|
|
$expected = new RedirectResponse($challengeUrl);
|
|
|
|
$this->assertEquals($expected, $this->loginController->tryLogin('john@doe.com', $password, null));
|
|
|
|
}
|
|
|
|
|
2018-05-22 09:52:16 +03:00
|
|
|
public function testLoginWithMultipleTwoFactorProviders() {
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
|
|
|
|
$user = $this->createMock(IUser::class);
|
2016-08-29 19:36:39 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('john'));
|
|
|
|
$password = 'secret';
|
|
|
|
$challengeUrl = 'challenge/url';
|
2018-05-22 09:52:16 +03:00
|
|
|
$provider1 = $this->createMock(IProvider::class);
|
|
|
|
$provider2 = $this->createMock(IProvider::class);
|
|
|
|
$provider1->method('getId')->willReturn('prov1');
|
|
|
|
$provider2->method('getId')->willReturn('prov2');
|
2016-08-29 19:36:39 +03:00
|
|
|
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->userManager->expects($this->once())
|
2017-01-12 14:38:45 +03:00
|
|
|
->method('checkPasswordNoLogging')
|
2016-08-29 19:36:39 +03:00
|
|
|
->will($this->returnValue($user));
|
|
|
|
$this->userSession->expects($this->once())
|
2016-11-30 15:28:36 +03:00
|
|
|
->method('completeLogin')
|
|
|
|
->with($user, ['loginName' => 'john@doe.com', 'password' => $password]);
|
2016-08-29 19:36:39 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('createSessionToken')
|
2018-04-06 20:58:37 +03:00
|
|
|
->with($this->request, $user->getUID(), 'john@doe.com', $password, IToken::REMEMBER);
|
2016-08-29 19:36:39 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('isTwoFactorAuthenticated')
|
|
|
|
->with($user)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->twoFactorManager->expects($this->once())
|
|
|
|
->method('prepareTwoFactorLogin')
|
|
|
|
->with($user);
|
2018-05-22 09:52:16 +03:00
|
|
|
$providerSet = new ProviderSet([$provider1, $provider2], false);
|
2016-08-29 19:36:39 +03:00
|
|
|
$this->twoFactorManager->expects($this->once())
|
2018-05-22 09:52:16 +03:00
|
|
|
->method('getProviderSet')
|
2016-08-29 19:36:39 +03:00
|
|
|
->with($user)
|
2018-05-22 09:52:16 +03:00
|
|
|
->willReturn($providerSet);
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.TwoFactorChallenge.selectChallenge')
|
|
|
|
->will($this->returnValue($challengeUrl));
|
2016-08-23 13:54:45 +03:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('deleteUserValue')
|
2016-08-23 16:01:38 +03:00
|
|
|
->with('john', 'core', 'lostpassword');
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('createRememberMeToken');
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2016-06-09 17:44:31 +03:00
|
|
|
$expected = new RedirectResponse($challengeUrl);
|
2016-05-24 11:50:18 +03:00
|
|
|
$this->assertEquals($expected, $this->loginController->tryLogin('john@doe.com', $password, null));
|
2016-05-11 12:23:25 +03:00
|
|
|
}
|
2016-04-27 17:44:51 +03:00
|
|
|
|
2016-06-09 17:44:31 +03:00
|
|
|
public function testToNotLeakLoginName() {
|
2017-01-12 14:38:45 +03:00
|
|
|
/** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */
|
|
|
|
$user = $this->createMock(IUser::class);
|
2016-06-09 17:44:31 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('john'));
|
|
|
|
|
2017-01-12 14:38:45 +03:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('checkPasswordNoLogging')
|
|
|
|
->with('john@doe.com', 'just wrong')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userManager->expects($this->once())
|
2016-06-09 17:44:31 +03:00
|
|
|
->method('checkPassword')
|
2017-01-12 14:38:45 +03:00
|
|
|
->with('john', 'just wrong')
|
2016-06-09 17:44:31 +03:00
|
|
|
->willReturn(false);
|
2018-05-22 09:52:16 +03:00
|
|
|
|
2016-06-09 17:44:31 +03:00
|
|
|
$this->userManager->expects($this->once())
|
|
|
|
->method('getByEmail')
|
|
|
|
->with('john@doe.com')
|
|
|
|
->willReturn([$user]);
|
|
|
|
|
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
|
|
|
->with('core.login.showLoginForm', ['user' => 'john@doe.com'])
|
|
|
|
->will($this->returnValue(''));
|
2016-08-09 20:01:50 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(true);
|
2016-08-23 13:54:45 +03:00
|
|
|
$this->config->expects($this->never())
|
|
|
|
->method('deleteUserValue');
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->userSession->expects($this->never())
|
|
|
|
->method('createRememberMeToken');
|
2016-06-09 17:44:31 +03:00
|
|
|
|
|
|
|
$expected = new RedirectResponse('');
|
2017-07-27 15:14:20 +03:00
|
|
|
$expected->throttle(['user' => 'john']);
|
2016-06-09 17:44:31 +03:00
|
|
|
$this->assertEquals($expected, $this->loginController->tryLogin('john@doe.com', 'just wrong', null));
|
|
|
|
}
|
2016-04-11 13:08:00 +03:00
|
|
|
}
|