Skip to content

Commit

Permalink
Only save login credentials in database once there is an external sto…
Browse files Browse the repository at this point in the history
…rage that needs it

Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed May 8, 2020
1 parent a636d99 commit 3a40d1e
Showing 1 changed file with 29 additions and 23 deletions.
52 changes: 29 additions & 23 deletions apps/files_external/lib/Lib/Auth/Password/LoginCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use OCA\Files_External\Lib\Auth\AuthMechanism;
use OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
use OCA\Files_External\Lib\StorageConfig;
use OCP\Authentication\Exceptions\CredentialsUnavailableException;
use OCP\Authentication\LoginCredentials\IStore as CredentialsStore;
use OCP\IL10N;
use OCP\ISession;
use OCP\IUser;
Expand All @@ -44,45 +46,49 @@ class LoginCredentials extends AuthMechanism {
/** @var ICredentialsManager */
protected $credentialsManager;

public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager) {
/** @var CredentialsStore */
private $credentialsStore;

public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore) {
$this->session = $session;
$this->credentialsManager = $credentialsManager;
$this->credentialsStore = $credentialsStore;

$this
->setIdentifier('password::logincredentials')
->setScheme(self::SCHEME_PASSWORD)
->setText($l->t('Log-in credentials, save in database'))
->addParameters([
])
;

\OCP\Util::connectHook('OC_User', 'post_login', $this, 'authenticate');
]);
}

/**
* Hook listener on post login
*
* @param array $params
*/
public function authenticate(array $params) {
$userId = $params['uid'];
$credentials = [
'user' => $this->session->get('loginname'),
'password' => $params['password']
];
$this->credentialsManager->store($userId, self::CREDENTIALS_IDENTIFIER, $credentials);
private function getCredentials(IUser $user): array {
$credentials = $this->credentialsManager->retrieve($user->getUID(), self::CREDENTIALS_IDENTIFIER);

if (is_null($credentials)) {
// nothing saved in db, try to get it from the session and save it
try {
$sessionCredentials = $this->credentialsStore->getLoginCredentials();

$credentials = [
'user' => $sessionCredentials->getLoginName(),
'password' => $sessionCredentials->getPassword()
];

$this->credentialsManager->store($user->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials);
} catch (CredentialsUnavailableException $e) {
throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
}
}

return $credentials;
}

public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
if (!isset($user)) {
throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
}
$uid = $user->getUID();
$credentials = $this->credentialsManager->retrieve($uid, self::CREDENTIALS_IDENTIFIER);

if (!isset($credentials)) {
throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved');
}
$credentials = $this->getCredentials($user);

$storage->setBackendOption('user', $credentials['user']);
$storage->setBackendOption('password', $credentials['password']);
Expand Down

0 comments on commit 3a40d1e

Please sign in to comment.