]> source.dussan.org Git - nextcloud-server.git/commitdiff
Only save login credentials in database once there is an external storage that needs it
authorRobin Appelman <robin@icewind.nl>
Fri, 8 May 2020 14:38:13 +0000 (16:38 +0200)
committerMorris Jobke <hey@morrisjobke.de>
Thu, 30 Jul 2020 09:43:15 +0000 (11:43 +0200)
Signed-off-by: Robin Appelman <robin@icewind.nl>
apps/files_external/lib/Lib/Auth/Password/LoginCredentials.php

index 9f41697452f7b603f92be0c4fff56790b01eda86..b8dace8bf53361f67cfb9b40575826714e5f9ed3 100644 (file)
@@ -27,6 +27,8 @@ namespace OCA\Files_External\Lib\Auth\Password;
 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;
@@ -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']);