Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>pull/15365/head
parent
5893f218c2
commit
170582d4f5
@ -0,0 +1,47 @@ |
||||
<?php |
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
abstract class ALoginCommand { |
||||
|
||||
/** @var ALoginCommand */ |
||||
protected $next; |
||||
|
||||
public function setNext(ALoginCommand $next): ALoginCommand { |
||||
$this->next = $next; |
||||
return $next; |
||||
} |
||||
|
||||
protected function processNextOrFinishSuccessfully(LoginData $loginData): LoginResult { |
||||
if ($this->next !== null) { |
||||
return $this->next->process($loginData); |
||||
} else { |
||||
return LoginResult::success($loginData); |
||||
} |
||||
} |
||||
|
||||
public abstract function process(LoginData $loginData): LoginResult; |
||||
|
||||
} |
||||
@ -0,0 +1,111 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
class Chain { |
||||
|
||||
/** @var PreLoginHookCommand */ |
||||
private $preLoginHookCommand; |
||||
|
||||
/** @var UserDisabledCheckCommand */ |
||||
private $userDisabledCheckCommand; |
||||
|
||||
/** @var UidLoginCommand */ |
||||
private $uidLoginCommand; |
||||
|
||||
/** @var EmailLoginCommand */ |
||||
private $emailLoginCommand; |
||||
|
||||
/** @var LoggedInCheckCommand */ |
||||
private $loggedInCheckCommand; |
||||
|
||||
/** @var CompleteLoginCommand */ |
||||
private $completeLoginCommand; |
||||
|
||||
/** @var CreateSessionTokenCommand */ |
||||
private $createSessionTokenCommand; |
||||
|
||||
/** @var ClearLostPasswordTokensCommand */ |
||||
private $clearLostPasswordTokensCommand; |
||||
|
||||
/** @var UpdateLastPasswordConfirmCommand */ |
||||
private $updateLastPasswordConfirmCommand; |
||||
|
||||
/** @var SetUserTimezoneCommand */ |
||||
private $setUserTimezoneCommand; |
||||
|
||||
/** @var TwoFactorCommand */ |
||||
private $twoFactorCommand; |
||||
|
||||
/** @var FinishRememberedLoginCommand */ |
||||
private $finishRememberedLoginCommand; |
||||
|
||||
public function __construct(PreLoginHookCommand $preLoginHookCommand, |
||||
UserDisabledCheckCommand $userDisabledCheckCommand, |
||||
UidLoginCommand $uidLoginCommand, |
||||
EmailLoginCommand $emailLoginCommand, |
||||
LoggedInCheckCommand $loggedInCheckCommand, |
||||
CompleteLoginCommand $completeLoginCommand, |
||||
CreateSessionTokenCommand $createSessionTokenCommand, |
||||
ClearLostPasswordTokensCommand $clearLostPasswordTokensCommand, |
||||
UpdateLastPasswordConfirmCommand $updateLastPasswordConfirmCommand, |
||||
SetUserTimezoneCommand $setUserTimezoneCommand, |
||||
TwoFactorCommand $twoFactorCommand, |
||||
FinishRememberedLoginCommand $finishRememberedLoginCommand |
||||
) { |
||||
$this->preLoginHookCommand = $preLoginHookCommand; |
||||
$this->userDisabledCheckCommand = $userDisabledCheckCommand; |
||||
$this->uidLoginCommand = $uidLoginCommand; |
||||
$this->emailLoginCommand = $emailLoginCommand; |
||||
$this->loggedInCheckCommand = $loggedInCheckCommand; |
||||
$this->completeLoginCommand = $completeLoginCommand; |
||||
$this->createSessionTokenCommand = $createSessionTokenCommand; |
||||
$this->clearLostPasswordTokensCommand = $clearLostPasswordTokensCommand; |
||||
$this->updateLastPasswordConfirmCommand = $updateLastPasswordConfirmCommand; |
||||
$this->setUserTimezoneCommand = $setUserTimezoneCommand; |
||||
$this->twoFactorCommand = $twoFactorCommand; |
||||
$this->finishRememberedLoginCommand = $finishRememberedLoginCommand; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
$chain = $this->preLoginHookCommand; |
||||
$chain |
||||
->setNext($this->userDisabledCheckCommand) |
||||
->setNext($this->uidLoginCommand) |
||||
->setNext($this->emailLoginCommand) |
||||
->setNext($this->loggedInCheckCommand) |
||||
->setNext($this->completeLoginCommand) |
||||
->setNext($this->createSessionTokenCommand) |
||||
->setNext($this->clearLostPasswordTokensCommand) |
||||
->setNext($this->updateLastPasswordConfirmCommand) |
||||
->setNext($this->setUserTimezoneCommand) |
||||
->setNext($this->twoFactorCommand) |
||||
->setNext($this->finishRememberedLoginCommand); |
||||
|
||||
return $chain->process($loginData); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,52 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OCP\IConfig; |
||||
|
||||
class ClearLostPasswordTokensCommand extends ALoginCommand { |
||||
|
||||
/** @var IConfig */ |
||||
private $config; |
||||
|
||||
public function __construct(IConfig $config) { |
||||
$this->config = $config; |
||||
} |
||||
|
||||
/** |
||||
* User has successfully logged in, now remove the password reset link, when it is available |
||||
*/ |
||||
public function process(LoginData $loginData): LoginResult { |
||||
$this->config->deleteUserValue( |
||||
$loginData->getUser()->getUID(), |
||||
'core', |
||||
'lostpassword' |
||||
); |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,50 @@ |
||||
<?php |
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OC\User\Session; |
||||
|
||||
class CompleteLoginCommand extends ALoginCommand { |
||||
|
||||
/** @var Session */ |
||||
private $userSession; |
||||
|
||||
public function __construct(Session $userSession) { |
||||
$this->userSession = $userSession; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
$this->userSession->completeLogin( |
||||
$loginData->getUser(), |
||||
[ |
||||
'loginName' => $loginData->getUsername(), |
||||
'password' => $loginData->getPassword(), |
||||
] |
||||
); |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,67 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OC\Authentication\Token\IToken; |
||||
use OC\User\Session; |
||||
use OCP\IConfig; |
||||
|
||||
class CreateSessionTokenCommand extends ALoginCommand { |
||||
|
||||
/** @var IConfig */ |
||||
private $config; |
||||
|
||||
/** @var Session */ |
||||
private $userSession; |
||||
|
||||
public function __construct(IConfig $config, |
||||
Session $userSession) { |
||||
$this->config = $config; |
||||
$this->userSession = $userSession; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
$tokenType = IToken::REMEMBER; |
||||
if ((int)$this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15) === 0) { |
||||
$loginData->setRememberLogin(false); |
||||
$tokenType = IToken::DO_NOT_REMEMBER; |
||||
} |
||||
|
||||
$this->userSession->createSessionToken( |
||||
$loginData->getRequest(), |
||||
$loginData->getUser()->getUID(), |
||||
$loginData->getUsername(), |
||||
$loginData->getPassword(), |
||||
$tokenType |
||||
); |
||||
$this->userSession->updateTokens( |
||||
$loginData->getUser()->getUID(), |
||||
$loginData->getUsername() |
||||
); |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
} |
||||
@ -0,0 +1,61 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OCP\IUserManager; |
||||
|
||||
class EmailLoginCommand extends ALoginCommand { |
||||
|
||||
/** @var IUserManager */ |
||||
private $userManager; |
||||
|
||||
public function __construct(IUserManager $userManager) { |
||||
$this->userManager = $userManager; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
if ($loginData->getUser() === false) { |
||||
$users = $this->userManager->getByEmail($loginData->getUsername()); |
||||
// we only allow login by email if unique |
||||
if (count($users) === 1) { |
||||
$username = $users[0]->getUID(); |
||||
if ($username !== $loginData->getUsername()) { |
||||
$user = $this->userManager->checkPassword( |
||||
$username, |
||||
$loginData->getPassword() |
||||
); |
||||
if ($user !== false) { |
||||
$loginData->setUser($user); |
||||
$loginData->setUsername($username); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,46 @@ |
||||
<?php |
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OC\User\Session; |
||||
|
||||
class FinishRememberedLoginCommand extends ALoginCommand { |
||||
|
||||
/** @var Session */ |
||||
private $userSession; |
||||
|
||||
public function __construct(Session $userSession) { |
||||
$this->userSession = $userSession; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
if ($loginData->isRememberLogin()) { |
||||
$this->userSession->createRememberMeToken($loginData->getUser()); |
||||
} |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,53 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OC\Core\Controller\LoginController; |
||||
use OCP\ILogger; |
||||
|
||||
class LoggedInCheckCommand extends ALoginCommand { |
||||
|
||||
/** @var ILogger */ |
||||
private $logger; |
||||
|
||||
public function __construct(ILogger $logger) { |
||||
$this->logger = $logger; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
if ($loginData->getUser() === false) { |
||||
$username = $loginData->getUsername(); |
||||
$ip = $loginData->getRequest()->getRemoteAddress(); |
||||
|
||||
$this->logger->warning("Login failed: $username (Remote IP: $ip)"); |
||||
|
||||
return LoginResult::failure($loginData, LoginController::LOGIN_MSG_INVALIDPASSWORD); |
||||
} |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,121 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OCP\IRequest; |
||||
use OCP\IUser; |
||||
|
||||
class LoginData { |
||||
|
||||
/** @var IRequest */ |
||||
private $request; |
||||
|
||||
/** @var string */ |
||||
private $username; |
||||
|
||||
/** @var string */ |
||||
private $password; |
||||
|
||||
/** @var string */ |
||||
private $redirectUrl; |
||||
|
||||
/** @var string */ |
||||
private $timeZone; |
||||
|
||||
/** @var string */ |
||||
private $timeZoneOffset; |
||||
|
||||
/** @var IUser|false|null */ |
||||
private $user = null; |
||||
|
||||
/** @var bool */ |
||||
private $rememberLogin = true; |
||||
|
||||
public function __construct(IRequest $request, |
||||
string $username, |
||||
string $password, |
||||
string $redirectUrl = null, |
||||
string $timeZone = '', |
||||
string $timeZoneOffset = '') { |
||||
$this->request = $request; |
||||
$this->username = $username; |
||||
$this->password = $password; |
||||
$this->redirectUrl = $redirectUrl; |
||||
$this->timeZone = $timeZone; |
||||
$this->timeZoneOffset = $timeZoneOffset; |
||||
} |
||||
|
||||
public function getRequest(): IRequest { |
||||
return $this->request; |
||||
} |
||||
|
||||
public function setUsername(string $username): void { |
||||
$this->username = $username; |
||||
} |
||||
|
||||
public function getUsername(): string { |
||||
return $this->username; |
||||
} |
||||
|
||||
public function getPassword(): string { |
||||
return $this->password; |
||||
} |
||||
|
||||
public function getRedirectUrl(): ?string { |
||||
return $this->redirectUrl; |
||||
} |
||||
|
||||
public function getTimeZone(): string { |
||||
return $this->timeZone; |
||||
} |
||||
|
||||
public function getTimeZoneOffset(): string { |
||||
return $this->timeZoneOffset; |
||||
} |
||||
|
||||
/** |
||||
* @param IUser|false|null $user |
||||
*/ |
||||
public function setUser($user) { |
||||
$this->user = $user; |
||||
} |
||||
|
||||
/** |
||||
* @return false|IUser|null |
||||
*/ |
||||
public function getUser() { |
||||
return $this->user; |
||||
} |
||||
|
||||
public function setRememberLogin(bool $rememberLogin): void { |
||||
$this->rememberLogin = $rememberLogin; |
||||
} |
||||
|
||||
public function isRememberLogin(): bool { |
||||
return $this->rememberLogin; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,83 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
class LoginResult { |
||||
|
||||
/** @var bool */ |
||||
private $success; |
||||
|
||||
/** @var LoginData */ |
||||
private $loginData; |
||||
|
||||
/** @var string|null */ |
||||
private $redirectUrl; |
||||
|
||||
/** @var string|null */ |
||||
private $errorMessage; |
||||
|
||||
private function __construct(bool $success, LoginData $loginData) { |
||||
$this->success = $success; |
||||
$this->loginData = $loginData; |
||||
} |
||||
|
||||
private function setRedirectUrl(string $url) { |
||||
$this->redirectUrl = $url; |
||||
} |
||||
|
||||
private function setErrorMessage(string $msg) { |
||||
$this->errorMessage = $msg; |
||||
} |
||||
|
||||
public static function success(LoginData $data, ?string $redirectUrl = null) { |
||||
$result = new static(true, $data); |
||||
if ($redirectUrl !== null) { |
||||
$result->setRedirectUrl($redirectUrl); |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
public static function failure(LoginData $data, string $msg = null): LoginResult { |
||||
$result = new static(false, $data); |
||||
if ($msg !== null) { |
||||
$result->setErrorMessage($msg); |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
public function isSuccess(): bool { |
||||
return $this->success; |
||||
} |
||||
|
||||
public function getRedirectUrl(): ?string { |
||||
return $this->redirectUrl; |
||||
} |
||||
|
||||
public function getErrorMessage(): ?string { |
||||
return $this->errorMessage; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,54 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OC\Hooks\PublicEmitter; |
||||
use OCP\IUserManager; |
||||
|
||||
class PreLoginHookCommand extends ALoginCommand { |
||||
|
||||
/** @var IUserManager */ |
||||
private $userManager; |
||||
|
||||
public function __construct(IUserManager $userManager) { |
||||
$this->userManager = $userManager; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
if ($this->userManager instanceof PublicEmitter) { |
||||
$this->userManager->emit( |
||||
'\OC\User', |
||||
'preLogin', |
||||
[ |
||||
$loginData->getUsername(), |
||||
$loginData->getPassword(), |
||||
] |
||||
); |
||||
} |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
} |
||||
@ -0,0 +1,62 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OCP\IConfig; |
||||
use OCP\ISession; |
||||
|
||||
class SetUserTimezoneCommand extends ALoginCommand { |
||||
|
||||
/** @var IConfig */ |
||||
private $config; |
||||
|
||||
/** @var ISession */ |
||||
private $session; |
||||
|
||||
public function __construct(IConfig $config, |
||||
ISession $session) { |
||||
$this->config = $config; |
||||
$this->session = $session; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
if ($loginData->getTimeZoneOffset() !== '') { |
||||
$this->config->setUserValue( |
||||
$loginData->getUser()->getUID(), |
||||
'core', |
||||
'timezone', |
||||
$loginData->getTimeZone() |
||||
); |
||||
$this->session->set( |
||||
'timezone', |
||||
$loginData->getTimeZoneOffset() |
||||
); |
||||
} |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,79 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use function array_pop; |
||||
use function count; |
||||
use OC\Authentication\TwoFactorAuth\Manager; |
||||
use OCP\Authentication\TwoFactorAuth\IProvider; |
||||
use OCP\IURLGenerator; |
||||
|
||||
class TwoFactorCommand extends ALoginCommand { |
||||
|
||||
/** @var Manager */ |
||||
private $twoFactorManager; |
||||
|
||||
/** @var IURLGenerator */ |
||||
private $urlGenerator; |
||||
|
||||
public function __construct(Manager $twoFactorManager, |
||||
IURLGenerator $urlGenerator) { |
||||
$this->twoFactorManager = $twoFactorManager; |
||||
$this->urlGenerator = $urlGenerator; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
if (!$this->twoFactorManager->isTwoFactorAuthenticated($loginData->getUser())) { |
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
|
||||
$this->twoFactorManager->prepareTwoFactorLogin($loginData->getUser(), $loginData->isRememberLogin()); |
||||
|
||||
$providers = $this->twoFactorManager->getProviderSet($loginData->getUser())->getPrimaryProviders(); |
||||
if (count($providers) === 1) { |
||||
// Single provider, hence we can redirect to that provider's challenge page directly |
||||
/* @var $provider IProvider */ |
||||
$provider = array_pop($providers); |
||||
$url = 'core.TwoFactorChallenge.showChallenge'; |
||||
$urlParams = [ |
||||
'challengeProviderId' => $provider->getId(), |
||||
]; |
||||
} else { |
||||
$url = 'core.TwoFactorChallenge.selectChallenge'; |
||||
$urlParams = []; |
||||
} |
||||
|
||||
if ($loginData->getRedirectUrl() !== null) { |
||||
$urlParams['redirect_url'] = $loginData->getRedirectUrl(); |
||||
} |
||||
|
||||
return LoginResult::success( |
||||
$loginData, |
||||
$this->urlGenerator->linkToRoute($url, $urlParams) |
||||
); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,57 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OC\User\Manager; |
||||
use OCP\IUser; |
||||
|
||||
class UidLoginCommand extends ALoginCommand { |
||||
|
||||
/** @var Manager */ |
||||
private $userManager; |
||||
|
||||
public function __construct(Manager $userManager) { |
||||
$this->userManager = $userManager; |
||||
} |
||||
|
||||
/** |
||||
* @param LoginData $loginData |
||||
* |
||||
* @return LoginResult |
||||
*/ |
||||
public function process(LoginData $loginData): LoginResult { |
||||
/* @var $loginResult IUser */ |
||||
$user = $this->userManager->checkPasswordNoLogging( |
||||
$loginData->getUsername(), |
||||
$loginData->getPassword() |
||||
); |
||||
|
||||
$loginData->setUser($user); |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,48 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OCP\ISession; |
||||
|
||||
class UpdateLastPasswordConfirmCommand extends ALoginCommand { |
||||
|
||||
/** @var ISession */ |
||||
private $session; |
||||
|
||||
public function __construct(ISession $session) { |
||||
$this->session = $session; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
$this->session->set( |
||||
'last-password-confirm', |
||||
$loginData->getUser()->getLastLogin() |
||||
); |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,60 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace OC\Authentication\Login; |
||||
|
||||
use OC\Core\Controller\LoginController; |
||||
use OCP\ILogger; |
||||
use OCP\IUserManager; |
||||
|
||||
class UserDisabledCheckCommand extends ALoginCommand { |
||||
|
||||
/** @var IUserManager */ |
||||
private $userManager; |
||||
|
||||
/** @var ILogger */ |
||||
private $logger; |
||||
|
||||
public function __construct(IUserManager $userManager, |
||||
ILogger $logger) { |
||||
$this->userManager = $userManager; |
||||
$this->logger = $logger; |
||||
} |
||||
|
||||
public function process(LoginData $loginData): LoginResult { |
||||
$user = $this->userManager->get($loginData->getUsername()); |
||||
if ($user !== null && $user->isEnabled() === false) { |
||||
$username = $loginData->getUsername(); |
||||
$ip = $loginData->getRequest()->getRemoteAddress(); |
||||
|
||||
$this->logger->warning("Login failed: $username disabled (Remote IP: $ip)"); |
||||
|
||||
return LoginResult::failure($loginData, LoginController::LOGIN_MSG_USERDISABLED); |
||||
} |
||||
|
||||
return $this->processNextOrFinishSuccessfully($loginData); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,122 @@ |
||||
<?php |
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\ALoginCommand; |
||||
use OC\Authentication\Login\LoginData; |
||||
use OCP\IRequest; |
||||
use OCP\IUser; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
use Test\TestCase; |
||||
|
||||
abstract class ALoginCommandTest extends TestCase { |
||||
|
||||
/** @var IRequest|MockObject */ |
||||
protected $request; |
||||
|
||||
/** @var string */ |
||||
protected $username = 'user123'; |
||||
|
||||
/** @var string */ |
||||
protected $password = '123456'; |
||||
|
||||
/** @var string */ |
||||
protected $redirectUrl = '/apps/contacts'; |
||||
|
||||
/** @var string */ |
||||
protected $timezone = 'Europe/Vienna'; |
||||
|
||||
protected $timeZoneOffset = '2'; |
||||
|
||||
/** @var IUser|MockObject */ |
||||
protected $user; |
||||
|
||||
/** @var ALoginCommand */ |
||||
protected $cmd; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->request = $this->createMock(IRequest::class); |
||||
$this->user = $this->createMock(IUser::class); |
||||
} |
||||
|
||||
protected function getBasicLoginData(): LoginData { |
||||
return new LoginData( |
||||
$this->request, |
||||
$this->username, |
||||
$this->password |
||||
); |
||||
} |
||||
|
||||
protected function getInvalidLoginData(): LoginData { |
||||
return new LoginData( |
||||
$this->request, |
||||
$this->username, |
||||
$this->password |
||||
); |
||||
} |
||||
|
||||
protected function getFailedLoginData(): LoginData { |
||||
$data = new LoginData( |
||||
$this->request, |
||||
$this->username, |
||||
$this->password |
||||
); |
||||
$data->setUser(false); |
||||
return $data; |
||||
} |
||||
|
||||
protected function getLoggedInLoginData(): LoginData { |
||||
$basic = $this->getBasicLoginData(); |
||||
$basic->setUser($this->user); |
||||
return $basic; |
||||
} |
||||
|
||||
protected function getLoggedInLoginDataWithRedirectUrl(): LoginData { |
||||
$data = new LoginData( |
||||
$this->request, |
||||
$this->username, |
||||
$this->password, |
||||
$this->redirectUrl |
||||
); |
||||
$data->setUser($this->user); |
||||
return $data; |
||||
} |
||||
|
||||
protected function getLoggedInLoginDataWithTimezone(): LoginData { |
||||
$data = new LoginData( |
||||
$this->request, |
||||
$this->username, |
||||
$this->password, |
||||
null, |
||||
$this->timezone, |
||||
$this->timeZoneOffset |
||||
); |
||||
$data->setUser($this->user); |
||||
return $data; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,67 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Test\Authentication\Login; |
||||
|
||||
use lib\Authentication\Login\ALoginCommandTest; |
||||
use OC\Authentication\Login\ClearLostPasswordTokensCommand; |
||||
use OC\Authentication\Login\LoginData; |
||||
use OCP\IConfig; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class ClearLostPasswordTokensCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var IConfig|MockObject */ |
||||
private $config; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->config = $this->createMock(IConfig::class); |
||||
|
||||
$this->cmd = new ClearLostPasswordTokensCommand( |
||||
$this->config |
||||
); |
||||
} |
||||
|
||||
public function testProcess() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$this->user->expects($this->once()) |
||||
->method('getUID') |
||||
->willReturn($this->username); |
||||
$this->config->expects($this->once()) |
||||
->method('deleteUserValue') |
||||
->with( |
||||
$this->username, |
||||
'core', |
||||
'lostpassword' |
||||
); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,67 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\CompleteLoginCommand; |
||||
use OC\User\Session; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class CompleteLoginCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var Session|MockObject */ |
||||
private $session; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->session = $this->createMock(Session::class); |
||||
|
||||
$this->cmd = new CompleteLoginCommand( |
||||
$this->session |
||||
); |
||||
} |
||||
|
||||
public function testProcess() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$this->session->expects($this->once()) |
||||
->method('completeLogin') |
||||
->with( |
||||
$this->user, |
||||
$this->equalTo( |
||||
[ |
||||
'loginName' => $this->username, |
||||
'password' => $this->password, |
||||
] |
||||
) |
||||
); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,121 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\CreateSessionTokenCommand; |
||||
use OC\Authentication\Token\IToken; |
||||
use OC\User\Session; |
||||
use OCP\IConfig; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class CreateSessionTokenCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var IConfig|MockObject */ |
||||
private $config; |
||||
|
||||
/** @var Session|MockObject */ |
||||
private $userSession; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->config = $this->createMock(IConfig::class); |
||||
$this->userSession = $this->createMock(Session::class); |
||||
|
||||
$this->cmd = new CreateSessionTokenCommand( |
||||
$this->config, |
||||
$this->userSession |
||||
); |
||||
} |
||||
|
||||
public function testProcess() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$this->config->expects($this->once()) |
||||
->method('getSystemValue') |
||||
->with( |
||||
'remember_login_cookie_lifetime', |
||||
60 * 60 * 24 * 15 |
||||
) |
||||
->willReturn(100); |
||||
$this->user->expects($this->any()) |
||||
->method('getUID') |
||||
->willReturn($this->username); |
||||
$this->userSession->expects($this->once()) |
||||
->method('createSessionToken') |
||||
->with( |
||||
$this->request, |
||||
$this->username, |
||||
$this->username, |
||||
$this->password, |
||||
IToken::REMEMBER |
||||
); |
||||
$this->userSession->expects($this->once()) |
||||
->method('updateTokens') |
||||
->with( |
||||
$this->username, |
||||
$this->username |
||||
); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
public function testProcessDoNotRemember() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$this->config->expects($this->once()) |
||||
->method('getSystemValue') |
||||
->with( |
||||
'remember_login_cookie_lifetime', |
||||
60 * 60 * 24 * 15 |
||||
) |
||||
->willReturn(0); |
||||
$this->user->expects($this->any()) |
||||
->method('getUID') |
||||
->willReturn($this->username); |
||||
$this->userSession->expects($this->once()) |
||||
->method('createSessionToken') |
||||
->with( |
||||
$this->request, |
||||
$this->username, |
||||
$this->username, |
||||
$this->password, |
||||
IToken::DO_NOT_REMEMBER |
||||
); |
||||
$this->userSession->expects($this->once()) |
||||
->method('updateTokens') |
||||
->with( |
||||
$this->username, |
||||
$this->username |
||||
); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
$this->assertFalse($data->isRememberLogin()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,165 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\EmailLoginCommand; |
||||
use OCP\IUser; |
||||
use OCP\IUserManager; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class EmailLoginCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var IUserManager|MockObject */ |
||||
private $userManager; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->userManager = $this->createMock(IUserManager::class); |
||||
|
||||
$this->cmd = new EmailLoginCommand( |
||||
$this->userManager |
||||
); |
||||
} |
||||
|
||||
public function testProcessAlreadyLoggedIn() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
public function testProcessNotAnEmailLogin() { |
||||
$data = $this->getFailedLoginData(); |
||||
$this->userManager->expects($this->once()) |
||||
->method('getByEmail') |
||||
->with($this->username) |
||||
->willReturn([]); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
public function testProcessDuplicateEmailLogin() { |
||||
$data = $this->getFailedLoginData(); |
||||
$this->userManager->expects($this->once()) |
||||
->method('getByEmail') |
||||
->with($this->username) |
||||
->willReturn([ |
||||
$this->createMock(IUser::class), |
||||
$this->createMock(IUser::class), |
||||
]); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
public function testProcessUidIsEmail() { |
||||
$email = 'user@domain.com'; |
||||
$data = $this->getFailedLoginData(); |
||||
$data->setUsername($email); |
||||
$emailUser = $this->createMock(IUser::class); |
||||
$emailUser->expects($this->any()) |
||||
->method('getUID') |
||||
->willReturn($email); |
||||
$this->userManager->expects($this->once()) |
||||
->method('getByEmail') |
||||
->with($email) |
||||
->willReturn([ |
||||
$emailUser, |
||||
]); |
||||
$this->userManager->expects($this->never()) |
||||
->method('checkPassword'); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
$this->assertFalse($data->getUser()); |
||||
$this->assertEquals($email, $data->getUsername()); |
||||
} |
||||
|
||||
public function testProcessWrongPassword() { |
||||
$email = 'user@domain.com'; |
||||
$data = $this->getFailedLoginData(); |
||||
$data->setUsername($email); |
||||
$emailUser = $this->createMock(IUser::class); |
||||
$emailUser->expects($this->any()) |
||||
->method('getUID') |
||||
->willReturn('user2'); |
||||
$this->userManager->expects($this->once()) |
||||
->method('getByEmail') |
||||
->with($email) |
||||
->willReturn([ |
||||
$emailUser, |
||||
]); |
||||
$this->userManager->expects($this->once()) |
||||
->method('checkPassword') |
||||
->with( |
||||
'user2', |
||||
$this->password |
||||
) |
||||
->willReturn(false); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
$this->assertFalse($data->getUser()); |
||||
$this->assertEquals($email, $data->getUsername()); |
||||
} |
||||
|
||||
public function testProcess() { |
||||
$email = 'user@domain.com'; |
||||
$data = $this->getFailedLoginData(); |
||||
$data->setUsername($email); |
||||
$emailUser = $this->createMock(IUser::class); |
||||
$emailUser->expects($this->any()) |
||||
->method('getUID') |
||||
->willReturn('user2'); |
||||
$this->userManager->expects($this->once()) |
||||
->method('getByEmail') |
||||
->with($email) |
||||
->willReturn([ |
||||
$emailUser, |
||||
]); |
||||
$this->userManager->expects($this->once()) |
||||
->method('checkPassword') |
||||
->with( |
||||
'user2', |
||||
$this->password |
||||
) |
||||
->willReturn($emailUser); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
$this->assertEquals($emailUser, $data->getUser()); |
||||
$this->assertEquals('user2', $data->getUsername()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,69 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\FinishRememberedLoginCommand; |
||||
use OC\User\Session; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class FinishRememberedLoginCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var Session|MockObject */ |
||||
private $userSession; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->userSession = $this->createMock(Session::class); |
||||
|
||||
$this->cmd = new FinishRememberedLoginCommand( |
||||
$this->userSession |
||||
); |
||||
} |
||||
|
||||
public function testProcessNotRememberedLogin() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$data->setRememberLogin(false); |
||||
$this->userSession->expects($this->never()) |
||||
->method('createRememberMeToken'); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
public function testProcess() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$this->userSession->expects($this->once()) |
||||
->method('createRememberMeToken') |
||||
->with($this->user); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,67 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\LoggedInCheckCommand; |
||||
use OC\Core\Controller\LoginController; |
||||
use OCP\ILogger; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class LoggedInCheckCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var ILogger|MockObject */ |
||||
private $logger; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->logger = $this->createMock(ILogger::class); |
||||
|
||||
$this->cmd = new LoggedInCheckCommand( |
||||
$this->logger |
||||
); |
||||
} |
||||
|
||||
public function testProcessSuccessfulLogin() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
public function testProcessFailedLogin() { |
||||
$data = $this->getFailedLoginData(); |
||||
$this->logger->expects($this->once()) |
||||
->method('warning'); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertFalse($result->isSuccess()); |
||||
$this->assertSame(LoginController::LOGIN_MSG_INVALIDPASSWORD, $result->getErrorMessage()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,66 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\PreLoginHookCommand; |
||||
use OC\User\Manager; |
||||
use OCP\IUserManager; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class PreLoginHookCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var IUserManager|MockObject */ |
||||
private $userManager; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->userManager = $this->createMock(Manager::class); |
||||
|
||||
$this->cmd = new PreLoginHookCommand( |
||||
$this->userManager |
||||
); |
||||
} |
||||
|
||||
public function testProcess() { |
||||
$data = $this->getBasicLoginData(); |
||||
$this->userManager->expects($this->once()) |
||||
->method('emit') |
||||
->with( |
||||
'\OC\User', |
||||
'preLogin', |
||||
[ |
||||
$this->username, |
||||
$this->password, |
||||
] |
||||
); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,90 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\SetUserTimezoneCommand; |
||||
use OCP\IConfig; |
||||
use OCP\ISession; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class SetUserTimezoneCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var IConfig|MockObject */ |
||||
private $config; |
||||
|
||||
/** @var ISession|MockObject */ |
||||
private $session; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->config = $this->createMock(IConfig::class); |
||||
$this->session = $this->createMock(ISession::class); |
||||
|
||||
$this->cmd = new SetUserTimezoneCommand( |
||||
$this->config, |
||||
$this->session |
||||
); |
||||
} |
||||
|
||||
public function testProcessNoTimezoneSet() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$this->config->expects($this->never()) |
||||
->method('setUserValue'); |
||||
$this->session->expects($this->never()) |
||||
->method('set'); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
public function testProcess() { |
||||
$data = $this->getLoggedInLoginDataWithTimezone(); |
||||
$this->user->expects($this->once()) |
||||
->method('getUID') |
||||
->willReturn($this->username); |
||||
$this->config->expects($this->once()) |
||||
->method('setUserValue') |
||||
->with( |
||||
$this->username, |
||||
'core', |
||||
'timezone', |
||||
$this->timezone |
||||
); |
||||
$this->session->expects($this->once()) |
||||
->method('set') |
||||
->with( |
||||
'timezone', |
||||
$this->timeZoneOffset |
||||
); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,179 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\TwoFactorCommand; |
||||
use OC\Authentication\TwoFactorAuth\Manager; |
||||
use OC\Authentication\TwoFactorAuth\ProviderSet; |
||||
use OCP\Authentication\TwoFactorAuth\IProvider as ITwoFactorAuthProvider; |
||||
use OCP\IURLGenerator; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class TwoFactorCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var Manager|MockObject */ |
||||
private $twoFactorManager; |
||||
|
||||
/** @var IURLGenerator|MockObject */ |
||||
private $urlGenerator; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->twoFactorManager = $this->createMock(Manager::class); |
||||
$this->urlGenerator = $this->createMock(IURLGenerator::class); |
||||
|
||||
$this->cmd = new TwoFactorCommand( |
||||
$this->twoFactorManager, |
||||
$this->urlGenerator |
||||
); |
||||
} |
||||
|
||||
public function testNotTwoFactorAuthenticated() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$this->twoFactorManager->expects($this->once()) |
||||
->method('isTwoFactorAuthenticated') |
||||
->willReturn(false); |
||||
$this->twoFactorManager->expects($this->never()) |
||||
->method('prepareTwoFactorLogin'); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
public function testProcessOneActiveProvider() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$this->twoFactorManager->expects($this->once()) |
||||
->method('isTwoFactorAuthenticated') |
||||
->willReturn(true); |
||||
$this->twoFactorManager->expects($this->once()) |
||||
->method('prepareTwoFactorLogin') |
||||
->with( |
||||
$this->user, |
||||
$data->isRememberLogin() |
||||
); |
||||
$provider = $this->createMock(ITwoFactorAuthProvider::class); |
||||
$this->twoFactorManager->expects($this->once()) |
||||
->method('getProviderSet') |
||||
->willReturn(new ProviderSet([ |
||||
$provider, |
||||
], false)); |
||||
$provider->expects($this->once()) |
||||
->method('getId') |
||||
->willReturn('test'); |
||||
$this->urlGenerator->expects($this->once()) |
||||
->method('linkToRoute') |
||||
->with( |
||||
'core.TwoFactorChallenge.showChallenge', |
||||
[ |
||||
'challengeProviderId' => 'test' |
||||
] |
||||
) |
||||
->willReturn('two/factor/url'); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
$this->assertEquals('two/factor/url', $result->getRedirectUrl()); |
||||
} |
||||
|
||||
public function testProcessTwoActiveProviders() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$this->twoFactorManager->expects($this->once()) |
||||
->method('isTwoFactorAuthenticated') |
||||
->willReturn(true); |
||||
$this->twoFactorManager->expects($this->once()) |
||||
->method('prepareTwoFactorLogin') |
||||
->with( |
||||
$this->user, |
||||
$data->isRememberLogin() |
||||
); |
||||
$provider1 = $this->createMock(ITwoFactorAuthProvider::class); |
||||
$provider2 = $this->createMock(ITwoFactorAuthProvider::class); |
||||
$provider1->expects($this->once()) |
||||
->method('getId') |
||||
->willReturn('test1'); |
||||
$provider2->expects($this->once()) |
||||
->method('getId') |
||||
->willReturn('test2'); |
||||
$this->twoFactorManager->expects($this->once()) |
||||
->method('getProviderSet') |
||||
->willReturn(new ProviderSet([ |
||||
$provider1, |
||||
$provider2, |
||||
], false)); |
||||
$this->urlGenerator->expects($this->once()) |
||||
->method('linkToRoute') |
||||
->with( |
||||
'core.TwoFactorChallenge.selectChallenge' |
||||
) |
||||
->willReturn('two/factor/url'); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
$this->assertEquals('two/factor/url', $result->getRedirectUrl()); |
||||
} |
||||
|
||||
public function testProcessWithRedirectUrl() { |
||||
$data = $this->getLoggedInLoginDataWithRedirectUrl(); |
||||
$this->twoFactorManager->expects($this->once()) |
||||
->method('isTwoFactorAuthenticated') |
||||
->willReturn(true); |
||||
$this->twoFactorManager->expects($this->once()) |
||||
->method('prepareTwoFactorLogin') |
||||
->with( |
||||
$this->user, |
||||
$data->isRememberLogin() |
||||
); |
||||
$provider = $this->createMock(ITwoFactorAuthProvider::class); |
||||
$this->twoFactorManager->expects($this->once()) |
||||
->method('getProviderSet') |
||||
->willReturn(new ProviderSet([ |
||||
$provider, |
||||
], false)); |
||||
$provider->expects($this->once()) |
||||
->method('getId') |
||||
->willReturn('test'); |
||||
$this->urlGenerator->expects($this->once()) |
||||
->method('linkToRoute') |
||||
->with( |
||||
'core.TwoFactorChallenge.showChallenge', |
||||
[ |
||||
'challengeProviderId' => 'test', |
||||
'redirect_url' => $this->redirectUrl, |
||||
] |
||||
) |
||||
->willReturn('two/factor/url'); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
$this->assertEquals('two/factor/url', $result->getRedirectUrl()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,80 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\UidCheckCommand; |
||||
use OC\Authentication\Login\UidLoginCommand; |
||||
use OC\User\Manager; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class UidLoginCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var Manager|MockObject */ |
||||
private $userManager; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->userManager = $this->createMock(Manager::class); |
||||
|
||||
$this->cmd = new UidLoginCommand( |
||||
$this->userManager |
||||
); |
||||
} |
||||
|
||||
public function testProcessFailingLogin() { |
||||
$data = $this->getBasicLoginData(); |
||||
$this->userManager->expects($this->once()) |
||||
->method('checkPasswordNoLogging') |
||||
->with( |
||||
$this->username, |
||||
$this->password |
||||
) |
||||
->willReturn(false); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
$this->assertFalse($data->getUser()); |
||||
} |
||||
|
||||
public function testProcess() { |
||||
$data = $this->getBasicLoginData(); |
||||
$this->userManager->expects($this->once()) |
||||
->method('checkPasswordNoLogging') |
||||
->with( |
||||
$this->username, |
||||
$this->password |
||||
) |
||||
->willReturn($this->user); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
$this->assertEquals($this->user, $data->getUser()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,64 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\UpdateLastPasswordConfirmCommand; |
||||
use OCP\ISession; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class UpdateLastPasswordConfirmCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var ISession|MockObject */ |
||||
private $session; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->session = $this->createMock(ISession::class); |
||||
|
||||
$this->cmd = new UpdateLastPasswordConfirmCommand( |
||||
$this->session |
||||
); |
||||
} |
||||
|
||||
public function testProcess() { |
||||
$data = $this->getLoggedInLoginData(); |
||||
$this->user->expects($this->once()) |
||||
->method('getLastLogin') |
||||
->willReturn(1234); |
||||
$this->session->expects($this->once()) |
||||
->method('set') |
||||
->with( |
||||
'last-password-confirm', |
||||
1234 |
||||
); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,97 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at> |
||||
* |
||||
* @license GNU AGPL version 3 or any later version |
||||
* |
||||
* This program is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU Affero General Public License as |
||||
* published by the Free Software Foundation, either version 3 of the |
||||
* License, or (at your option) any later version. |
||||
* |
||||
* 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 |
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
||||
*/ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace lib\Authentication\Login; |
||||
|
||||
use OC\Authentication\Login\UserDisabledCheckCommand; |
||||
use OC\Core\Controller\LoginController; |
||||
use OCP\ILogger; |
||||
use OCP\IUserManager; |
||||
use PHPUnit\Framework\MockObject\MockObject; |
||||
|
||||
class UserDisabledCheckCommandTest extends ALoginCommandTest { |
||||
|
||||
/** @var IUserManager|MockObject */ |
||||
private $userManager; |
||||
|
||||
/** @var ILogger|MockObject */ |
||||
private $logger; |
||||
|
||||
protected function setUp() { |
||||
parent::setUp(); |
||||
|
||||
$this->userManager = $this->createMock(IUserManager::class); |
||||
$this->logger = $this->createMock(ILogger::class); |
||||
|
||||
$this->cmd = new UserDisabledCheckCommand( |
||||
$this->userManager, |
||||
$this->logger |
||||
); |
||||
} |
||||
|
||||
public function testProcessNonExistingUser() { |
||||
$data = $this->getBasicLoginData(); |
||||
$this->userManager->expects($this->once()) |
||||
->method('get') |
||||
->with($this->username) |
||||
->willReturn(null); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
public function testProcessDisabledUser() { |
||||
$data = $this->getBasicLoginData(); |
||||
$this->userManager->expects($this->once()) |
||||
->method('get') |
||||
->with($this->username) |
||||
->willReturn($this->user); |
||||
$this->user->expects($this->once()) |
||||
->method('isEnabled') |
||||
->willReturn(false); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertFalse($result->isSuccess()); |
||||
$this->assertSame(LoginController::LOGIN_MSG_USERDISABLED, $result->getErrorMessage()); |
||||
} |
||||
|
||||
public function testProcess() { |
||||
$data = $this->getBasicLoginData(); |
||||
$this->userManager->expects($this->once()) |
||||
->method('get') |
||||
->with($this->username) |
||||
->willReturn($this->user); |
||||
$this->user->expects($this->once()) |
||||
->method('isEnabled') |
||||
->willReturn(true); |
||||
|
||||
$result = $this->cmd->process($data); |
||||
|
||||
$this->assertTrue($result->isSuccess()); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue