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
|
|
|
|
2019-05-03 16:09:19 +03:00
|
|
|
use OC\Authentication\Login\Chain as LoginChain;
|
|
|
|
use OC\Authentication\Login\LoginData;
|
|
|
|
use OC\Authentication\Login\LoginResult;
|
2016-05-11 12:23:25 +03:00
|
|
|
use OC\Authentication\TwoFactorAuth\Manager;
|
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;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2017-09-04 15:17:03 +03:00
|
|
|
use OCP\Defaults;
|
2016-04-11 13:08:00 +03:00
|
|
|
use OCP\IConfig;
|
2019-05-21 09:58:35 +03:00
|
|
|
use OCP\IInitialStateService;
|
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;
|
2019-05-03 16:09:19 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2016-04-11 13:08:00 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class LoginControllerTest extends TestCase {
|
2019-05-03 16:09:19 +03:00
|
|
|
|
2016-04-11 13:08:00 +03:00
|
|
|
/** @var LoginController */
|
|
|
|
private $loginController;
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
/** @var IRequest|MockObject */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $request;
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
/** @var IUserManager|MockObject */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $userManager;
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
/** @var IConfig|MockObject */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $config;
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
/** @var ISession|MockObject */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $session;
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
/** @var Session|MockObject */
|
2016-04-11 13:08:00 +03:00
|
|
|
private $userSession;
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
/** @var IURLGenerator|MockObject */
|
2016-04-18 13:14:07 +03:00
|
|
|
private $urlGenerator;
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
/** @var ILogger|MockObject */
|
2017-01-12 14:38:45 +03:00
|
|
|
private $logger;
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
/** @var Manager|MockObject */
|
2016-05-11 12:23:25 +03:00
|
|
|
private $twoFactorManager;
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
/** @var Defaults|MockObject */
|
2017-09-04 15:17:03 +03:00
|
|
|
private $defaults;
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
/** @var Throttler|MockObject */
|
2018-01-13 23:18:05 +03:00
|
|
|
private $throttler;
|
2016-04-11 13:08:00 +03:00
|
|
|
|
2019-05-03 16:09:19 +03:00
|
|
|
/** @var LoginChain|MockObject */
|
|
|
|
private $chain;
|
|
|
|
|
2019-05-21 09:58:35 +03:00
|
|
|
/** @var IInitialStateService|MockObject */
|
|
|
|
private $initialStateService;
|
|
|
|
|
2020-02-09 22:06:08 +03:00
|
|
|
/** @var \OC\Authentication\WebAuthn\Manager|MockObject */
|
|
|
|
private $webAuthnManager;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-04-11 13:08:00 +03:00
|
|
|
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);
|
2019-05-03 16:09:19 +03:00
|
|
|
$this->chain = $this->createMock(LoginChain::class);
|
2019-05-21 09:58:35 +03:00
|
|
|
$this->initialStateService = $this->createMock(IInitialStateService::class);
|
2020-02-09 22:06:08 +03:00
|
|
|
$this->webAuthnManager = $this->createMock(\OC\Authentication\WebAuthn\Manager::class);
|
|
|
|
|
2018-01-13 23:18:05 +03:00
|
|
|
|
|
|
|
$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,
|
2018-01-13 23:18:05 +03:00
|
|
|
$this->defaults,
|
2019-05-03 16:09:19 +03:00
|
|
|
$this->throttler,
|
2019-05-21 09:58:35 +03:00
|
|
|
$this->chain,
|
2020-02-09 22:06:08 +03:00
|
|
|
$this->initialStateService,
|
|
|
|
$this->webAuthnManager
|
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);
|
2019-11-12 23:48:56 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isUserAgent')
|
|
|
|
->willReturn(false);
|
2016-04-18 13:14:07 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->never())
|
|
|
|
->method('deleteUserValue');
|
|
|
|
$this->urlGenerator
|
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
|
|
|
->with('core.login.showLoginForm')
|
|
|
|
->willReturn('/login');
|
|
|
|
|
|
|
|
$expected = new RedirectResponse('/login');
|
2019-07-09 15:44:10 +03:00
|
|
|
$expected->addHeader('Clear-Site-Data', '"cache", "storage"');
|
2016-04-18 13:14:07 +03:00
|
|
|
$this->assertEquals($expected, $this->loginController->logout());
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:48:56 +03:00
|
|
|
public function testLogoutNoClearSiteData() {
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getCookie')
|
|
|
|
->with('nc_token')
|
|
|
|
->willReturn(null);
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isUserAgent')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->urlGenerator
|
|
|
|
->expects($this->once())
|
|
|
|
->method('linkToRouteAbsolute')
|
|
|
|
->with('core.login.showLoginForm')
|
|
|
|
->willReturn('/login');
|
|
|
|
|
|
|
|
$expected = new RedirectResponse('/login');
|
|
|
|
$this->assertEquals($expected, $this->loginController->logout());
|
|
|
|
}
|
|
|
|
|
2016-04-18 13:14:07 +03:00
|
|
|
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');
|
2019-11-12 23:48:56 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isUserAgent')
|
|
|
|
->willReturn(false);
|
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');
|
2019-07-09 15:44:10 +03:00
|
|
|
$expected->addHeader('Clear-Site-Data', '"cache", "storage"');
|
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',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
2019-05-21 09:58:35 +03:00
|
|
|
$this->initialStateService->expects($this->at(0))
|
|
|
|
->method('provideInitialState')
|
|
|
|
->with(
|
|
|
|
'core',
|
|
|
|
'loginMessages',
|
|
|
|
[
|
|
|
|
'MessageArray1',
|
|
|
|
'MessageArray2',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$this->initialStateService->expects($this->at(1))
|
|
|
|
->method('provideInitialState')
|
|
|
|
->with(
|
|
|
|
'core',
|
|
|
|
'loginErrors',
|
|
|
|
[
|
|
|
|
'ErrorArray1',
|
|
|
|
'ErrorArray2',
|
|
|
|
]
|
|
|
|
);
|
2016-04-11 13:08:00 +03:00
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'alt_login' => [],
|
|
|
|
],
|
|
|
|
'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);
|
2019-05-21 09:58:35 +03:00
|
|
|
$this->initialStateService->expects($this->at(2))
|
|
|
|
->method('provideInitialState')
|
|
|
|
->with(
|
|
|
|
'core',
|
|
|
|
'loginRedirectUrl',
|
|
|
|
'login/flow'
|
|
|
|
);
|
2017-12-21 02:25:30 +03:00
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'alt_login' => [],
|
|
|
|
],
|
|
|
|
'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
|
2018-10-29 10:09:19 +03:00
|
|
|
->expects($this->exactly(2))
|
2016-04-11 13:08:00 +03:00
|
|
|
->method('getSystemValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2020-04-09 10:22:29 +03:00
|
|
|
['login_form_autocomplete', true, true],
|
|
|
|
['lost_password_link', '', 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);
|
2019-05-21 09:58:35 +03:00
|
|
|
$this->initialStateService->expects($this->at(0))
|
|
|
|
->method('provideInitialState')
|
|
|
|
->with(
|
|
|
|
'core',
|
|
|
|
'loginUsername',
|
|
|
|
'LdapUser'
|
|
|
|
);
|
|
|
|
$this->initialStateService->expects($this->at(4))
|
|
|
|
->method('provideInitialState')
|
|
|
|
->with(
|
|
|
|
'core',
|
|
|
|
'loginCanResetPassword',
|
|
|
|
$expectedResult
|
|
|
|
);
|
2016-04-11 13:08:00 +03:00
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'alt_login' => [],
|
|
|
|
],
|
|
|
|
'guest'
|
|
|
|
);
|
|
|
|
$this->assertEquals($expectedResponse, $this->loginController->showLoginForm('LdapUser', '', ''));
|
|
|
|
}
|
2016-04-15 19:57:11 +03:00
|
|
|
|
2018-07-21 14:05:25 +03:00
|
|
|
public function testShowLoginFormForUserNamed0() {
|
2016-04-15 19:57:11 +03:00
|
|
|
$this->userSession
|
|
|
|
->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->config
|
2018-10-29 10:09:19 +03:00
|
|
|
->expects($this->exactly(2))
|
2016-04-15 19:57:11 +03:00
|
|
|
->method('getSystemValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2020-04-09 10:22:29 +03:00
|
|
|
['login_form_autocomplete', true, true],
|
|
|
|
['lost_password_link', '', 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);
|
2019-05-21 09:58:35 +03:00
|
|
|
$this->initialStateService->expects($this->at(1))
|
|
|
|
->method('provideInitialState')
|
|
|
|
->with(
|
|
|
|
'core',
|
|
|
|
'loginAutocomplete',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->initialStateService->expects($this->at(3))
|
|
|
|
->method('provideInitialState')
|
|
|
|
->with(
|
|
|
|
'core',
|
|
|
|
'loginResetPasswordLink',
|
|
|
|
false
|
|
|
|
);
|
|
|
|
$this->initialStateService->expects($this->at(4))
|
|
|
|
->method('provideInitialState')
|
|
|
|
->with(
|
|
|
|
'core',
|
|
|
|
'loginCanResetPassword',
|
|
|
|
false
|
|
|
|
);
|
2016-04-15 19:57:11 +03:00
|
|
|
|
|
|
|
$expectedResponse = new TemplateResponse(
|
|
|
|
'core',
|
|
|
|
'login',
|
|
|
|
[
|
|
|
|
'alt_login' => [],
|
|
|
|
],
|
|
|
|
'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);
|
2019-05-03 16:09:19 +03:00
|
|
|
$loginData = new LoginData(
|
|
|
|
$this->request,
|
|
|
|
$user,
|
|
|
|
$password,
|
|
|
|
'/apps/files'
|
|
|
|
);
|
|
|
|
$loginResult = LoginResult::failure($loginData, LoginController::LOGIN_MSG_INVALIDPASSWORD);
|
|
|
|
$this->chain->expects($this->once())
|
|
|
|
->method('process')
|
|
|
|
->with($this->equalTo($loginData))
|
|
|
|
->willReturn($loginResult);
|
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', [
|
2019-05-03 16:09:19 +03:00
|
|
|
'user' => $user,
|
2017-04-25 10:38:19 +03:00
|
|
|
'redirect_url' => '/apps/files',
|
|
|
|
])
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($loginPageUrl);
|
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']);
|
2019-05-03 16:09:19 +03:00
|
|
|
|
|
|
|
$response = $this->loginController->tryLogin($user, $password, '/apps/files');
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $response);
|
2016-04-27 17:44:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoginWithValidCredentials() {
|
2019-05-03 16:09:19 +03:00
|
|
|
$user = 'MyUserName';
|
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);
|
2019-05-03 16:09:19 +03:00
|
|
|
$loginData = new LoginData(
|
|
|
|
$this->request,
|
|
|
|
$user,
|
|
|
|
$password
|
|
|
|
);
|
|
|
|
$loginResult = LoginResult::success($loginData);
|
|
|
|
$this->chain->expects($this->once())
|
|
|
|
->method('process')
|
|
|
|
->with($this->equalTo($loginData))
|
|
|
|
->willReturn($loginResult);
|
2016-04-27 17:44:51 +03:00
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse($indexPageUrl);
|
2016-09-06 22:41:15 +03:00
|
|
|
|
2019-05-03 16:09:19 +03:00
|
|
|
$response = $this->loginController->tryLogin($user, $password);
|
2016-09-06 22:41:15 +03:00
|
|
|
|
2019-05-03 16:09:19 +03:00
|
|
|
$this->assertEquals($expected, $response);
|
2016-09-06 22:41:15 +03:00
|
|
|
}
|
|
|
|
|
2016-08-09 20:01:50 +03:00
|
|
|
public function testLoginWithoutPassedCsrfCheckAndNotLoggedIn() {
|
2019-05-03 16:09:19 +03:00
|
|
|
/** @var IUser|MockObject $user */
|
2017-01-12 14:38:45 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2016-08-09 20:01:50 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('jane');
|
2016-08-09 20:01:50 +03:00
|
|
|
$password = 'secret';
|
|
|
|
$originalUrl = 'another%20url';
|
|
|
|
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->with()
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(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() {
|
2019-05-03 16:09:19 +03:00
|
|
|
/** @var IUser|MockObject $user */
|
2017-01-12 14:38:45 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2016-08-09 20:01:50 +03:00
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('jane');
|
2016-08-09 20:01:50 +03:00
|
|
|
$password = 'secret';
|
2020-04-10 09:50:15 +03:00
|
|
|
$originalUrl = 'another url';
|
2016-08-09 20:01:50 +03:00
|
|
|
$redirectUrl = 'http://localhost/another url';
|
|
|
|
|
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(false);
|
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
|
|
|
->with()
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2016-08-09 20:01:50 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('getAbsoluteURL')
|
|
|
|
->with(urldecode($originalUrl))
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($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');
|
2019-01-22 18:16:55 +03:00
|
|
|
$this->config
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('remember_login_cookie_lifetime')
|
|
|
|
->willReturn(1234);
|
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() {
|
2019-05-03 16:09:19 +03:00
|
|
|
$user = 'MyUserName';
|
2016-04-27 17:44:51 +03:00
|
|
|
$password = 'secret';
|
2019-05-03 16:09:19 +03:00
|
|
|
$indexPageUrl = \OC_Util::getDefaultPageUrl();
|
|
|
|
$redirectUrl = 'https://next.cloud/apps/mail';
|
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);
|
2019-05-03 16:09:19 +03:00
|
|
|
$loginData = new LoginData(
|
|
|
|
$this->request,
|
|
|
|
$user,
|
|
|
|
$password,
|
2020-04-10 09:50:15 +03:00
|
|
|
'/apps/mail'
|
2019-05-03 16:09:19 +03:00
|
|
|
);
|
|
|
|
$loginResult = LoginResult::success($loginData);
|
|
|
|
$this->chain->expects($this->once())
|
|
|
|
->method('process')
|
|
|
|
->with($this->equalTo($loginData))
|
|
|
|
->willReturn($loginResult);
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->userSession->expects($this->once())
|
|
|
|
->method('isLoggedIn')
|
2019-05-03 16:09:19 +03:00
|
|
|
->willReturn(true);
|
2016-04-27 17:44:51 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('getAbsoluteURL')
|
2020-04-10 09:50:15 +03:00
|
|
|
->with('/apps/mail')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($redirectUrl);
|
2019-05-03 16:09:19 +03:00
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse($redirectUrl);
|
2016-05-11 12:23:25 +03:00
|
|
|
|
2020-04-10 09:50:15 +03:00
|
|
|
$response = $this->loginController->tryLogin($user, $password, '/apps/mail');
|
2016-08-29 19:36:39 +03:00
|
|
|
|
2019-05-03 16:09:19 +03:00
|
|
|
$this->assertEquals($expected, $response);
|
2016-08-29 19:36:39 +03:00
|
|
|
}
|
|
|
|
|
2019-05-03 16:09:19 +03:00
|
|
|
public function testToNotLeakLoginName() {
|
2016-08-29 19:36:39 +03:00
|
|
|
$this->request
|
|
|
|
->expects($this->once())
|
|
|
|
->method('passesCSRFCheck')
|
|
|
|
->willReturn(true);
|
2019-05-03 16:09:19 +03:00
|
|
|
$loginPageUrl = '/login?redirect_url=/apps/files';
|
|
|
|
$loginData = new LoginData(
|
|
|
|
$this->request,
|
|
|
|
'john@doe.com',
|
|
|
|
'just wrong',
|
|
|
|
'/apps/files'
|
|
|
|
);
|
|
|
|
$loginResult = LoginResult::failure($loginData, LoginController::LOGIN_MSG_INVALIDPASSWORD);
|
|
|
|
$this->chain->expects($this->once())
|
|
|
|
->method('process')
|
|
|
|
->with($this->equalTo($loginData))
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function (LoginData $data) use ($loginResult) {
|
2019-05-03 16:09:19 +03:00
|
|
|
$data->setUsername('john');
|
|
|
|
return $loginResult;
|
|
|
|
});
|
2016-05-11 12:23:25 +03:00
|
|
|
$this->urlGenerator->expects($this->once())
|
|
|
|
->method('linkToRoute')
|
2019-05-03 16:09:19 +03:00
|
|
|
->with('core.login.showLoginForm', [
|
|
|
|
'user' => 'john@doe.com',
|
|
|
|
'redirect_url' => '/apps/files',
|
|
|
|
])
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($loginPageUrl);
|
2019-05-03 16:09:19 +03:00
|
|
|
$expected = new \OCP\AppFramework\Http\RedirectResponse($loginPageUrl);
|
|
|
|
$expected->throttle(['user' => 'john']);
|
2016-06-09 17:44:31 +03:00
|
|
|
|
2019-05-03 16:09:19 +03:00
|
|
|
$response = $this->loginController->tryLogin(
|
|
|
|
'john@doe.com',
|
|
|
|
'just wrong',
|
|
|
|
'/apps/files'
|
|
|
|
);
|
2016-06-09 17:44:31 +03:00
|
|
|
|
2019-05-03 16:09:19 +03:00
|
|
|
$this->assertEquals($expected, $response);
|
2016-06-09 17:44:31 +03:00
|
|
|
}
|
2016-04-11 13:08:00 +03:00
|
|
|
}
|