This commit is contained in:
kesselb 2021-05-31 19:02:35 +02:00 committed by GitHub
commit e6ae4d653b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 168 additions and 9 deletions

View File

@ -50,18 +50,22 @@ class StorePasswordListener implements IEventListener {
return;
}
$stored = $this->credentialsManager->retrieve($event->getUser()->getUID(), LoginCredentials::CREDENTIALS_IDENTIFIER);
$update = isset($stored['password']) && $stored['password'] !== $event->getPassword();
if (!$update && $event instanceof UserLoggedInEvent) {
$update = isset($stored['user']) && $stored['user'] !== $event->getLoginName();
$stored = (array)$this->credentialsManager->retrieve($event->getUser()->getUID(), LoginCredentials::CREDENTIALS_IDENTIFIER);
$credentials = $stored;
/** Case 1: PasswordUpdateEvent with saved credentials => Update password */
if ($event instanceof PasswordUpdatedEvent
&& isset($credentials['user'], $credentials['password'])) {
$credentials['password'] = $event->getPassword();
}
if ($stored && $update) {
$credentials = [
'user' => $event->getLoginName(),
'password' => $event->getPassword()
];
/** Case 2: UserLoggedInEvent => Update user or password */
if ($event instanceof UserLoggedInEvent) {
$credentials['user'] = $event->getLoginName();
$credentials['password'] = $event->getPassword();
}
if ($stored !== $credentials) {
$this->credentialsManager->store($event->getUser()->getUID(), LoginCredentials::CREDENTIALS_IDENTIFIER, $credentials);
}
}

View File

@ -0,0 +1,155 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Daniel Kesselberg <mail@danielkesselberg.de>
*
* @author Daniel Kesselberg <mail@danielkesselberg.de>
*
* @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/>.
*
*/
namespace OCA\Files_External\Tests\Listener;
use OCA\Files_External\Lib\Auth\Password\LoginCredentials;
use OCA\Files_External\Listener\StorePasswordListener;
use OCP\IUser;
use OCP\Security\ICredentialsManager;
use OCP\User\Events\PasswordUpdatedEvent;
use OCP\User\Events\UserLoggedInEvent;
use OCP\User\Events\UserLoggedOutEvent;
use Test\TestCase;
class StorePasswordListenerTest extends TestCase {
/** @var ICredentialsManager */
private $credentialsManager;
/** @var IUser */
private $user;
/** @var StorePasswordListener */
private $storePasswordListener;
protected function setUp(): void {
parent::setUp();
$this->credentialsManager = $this->createMock(ICredentialsManager::class);
$this->storePasswordListener = new StorePasswordListener($this->credentialsManager);
$this->user = $this->createMock(IUser::class);
$this->user->method('getUID')
->willReturn('admin');
}
public function testIgnoreOtherEvent(): void {
$this->credentialsManager->expects($this->never())
->method('retrieve');
$this->credentialsManager->expects($this->never())
->method('store');
$this->storePasswordListener->handle(new UserLoggedOutEvent());
}
public function testIgnoreTokenLogin(): void {
$this->credentialsManager->expects($this->never())
->method('retrieve');
$this->credentialsManager->expects($this->never())
->method('store');
$event = new UserLoggedInEvent($this->user, 'admin', 'password', true);
$this->storePasswordListener->handle($event);
}
public function testUserLoggedInNoCredentials(): void {
$this->credentialsManager->expects($this->once())
->method('retrieve')
->willReturn(null);
$this->credentialsManager->expects($this->once())
->method('store')
->with('admin', LoginCredentials::CREDENTIALS_IDENTIFIER, ['user' => 'admin', 'password' => 'password']);
$event = new UserLoggedInEvent($this->user, 'admin', 'password', false);
$this->storePasswordListener->handle($event);
}
public function testUserLoggedInUpdateCredentials(): void {
$this->credentialsManager->expects($this->once())
->method('retrieve')
->willReturn(['user' => 'admin2', 'password' => 'password2']);
$this->credentialsManager->expects($this->once())
->method('store')
->with('admin', LoginCredentials::CREDENTIALS_IDENTIFIER, ['user' => 'admin', 'password' => 'password']);
$event = new UserLoggedInEvent($this->user, 'admin', 'password', false);
$this->storePasswordListener->handle($event);
}
public function testUserLoggedInSameCredentials(): void {
$this->credentialsManager->expects($this->once())
->method('retrieve')
->willReturn(['user' => 'admin', 'password' => 'password']);
$this->credentialsManager->expects($this->never())
->method('store');
$event = new UserLoggedInEvent($this->user, 'admin', 'password', false);
$this->storePasswordListener->handle($event);
}
public function testPasswordUpdatedNoCredentials(): void {
$this->credentialsManager->expects($this->once())
->method('retrieve')
->willReturn(null);
$this->credentialsManager->expects($this->never())
->method('store');
$event = new PasswordUpdatedEvent($this->user, 'password', 'recoveryPassword');
$this->storePasswordListener->handle($event);
}
public function testPasswordUpdatedUpdatePassword(): void {
$this->credentialsManager->expects($this->once())
->method('retrieve')
->willReturn(['user' => 'admin', 'password' => 'password2']);
$this->credentialsManager->expects($this->once())
->method('store')
->with('admin', LoginCredentials::CREDENTIALS_IDENTIFIER, ['user' => 'admin', 'password' => 'password']);
$event = new PasswordUpdatedEvent($this->user, 'password', 'recoveryPassword');
$this->storePasswordListener->handle($event);
}
public function testPasswordUpdatedSamePassword(): void {
$this->credentialsManager->expects($this->once())
->method('retrieve')
->willReturn(['user' => 'admin', 'password' => 'password']);
$this->credentialsManager->expects($this->never())
->method('store');
$event = new PasswordUpdatedEvent($this->user, 'password', 'recoveryPassword');
$this->storePasswordListener->handle($event);
}
}