Skip to content

Commit

Permalink
update saved credentials on password change
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed May 20, 2020
1 parent 4d98aac commit c89db6c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion apps/files_external/lib/Lib/Auth/Password/LoginCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
use OCA\Files_External\Lib\StorageConfig;
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
use OCP\Authentication\LoginCredentials\IStore as CredentialsStore;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IL10N;
use OCP\ISession;
use OCP\IUser;
use OCP\Security\ICredentialsManager;
use OCP\User\Events\PasswordUpdatedEvent;
use OCP\User\Events\UserLoggedInEvent;

/**
* Username and password from login credentials, saved in DB
Expand All @@ -49,7 +52,7 @@ class LoginCredentials extends AuthMechanism {
/** @var CredentialsStore */
private $credentialsStore;

public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore) {
public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore, IEventDispatcher $eventDispatcher) {
$this->session = $session;
$this->credentialsManager = $credentialsManager;
$this->credentialsStore = $credentialsStore;
Expand All @@ -60,6 +63,29 @@ public function __construct(IL10N $l, ISession $session, ICredentialsManager $cr
->setText($l->t('Log-in credentials, save in database'))
->addParameters([
]);

$eventDispatcher->addListener(UserLoggedInEvent::class, [$this, 'updateCredentials']);
$eventDispatcher->addListener(PasswordUpdatedEvent::class, [$this, 'updateCredentials']);
}

/**
* @param UserLoggedInEvent | PasswordUpdatedEvent $event
*/
public function updateCredentials($event) {
if ($event instanceof UserLoggedInEvent && $event->isTokenLogin()) {
return;
}

$stored = $this->credentialsManager->retrieve($event->getUser()->getUID(), self::CREDENTIALS_IDENTIFIER);

if ($stored && $stored['password'] != $event->getPassword()) {
$credentials = [
'user' => $stored['user'],
'password' => $event->getPassword()
];

$this->credentialsManager->store($event->getUser()->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials);
}
}

private function getCredentials(IUser $user): array {
Expand Down

0 comments on commit c89db6c

Please sign in to comment.