sessioncredentials.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @author Robin McCorkell <robin@mccorkell.me.uk>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Files_External\Lib\Auth\Password;
  22. use \OCP\IUser;
  23. use \OCP\IL10N;
  24. use \OCA\Files_External\Lib\DefinitionParameter;
  25. use \OCA\Files_External\Lib\Auth\AuthMechanism;
  26. use \OCA\Files_External\Lib\StorageConfig;
  27. use \OCP\ISession;
  28. use \OCP\Security\ICrypto;
  29. use \OCP\Files\Storage;
  30. use \OCA\Files_External\Lib\SessionStorageWrapper;
  31. use \OCA\Files_External\Lib\InsufficientDataForMeaningfulAnswerException;
  32. /**
  33. * Username and password from login credentials, saved in session
  34. */
  35. class SessionCredentials extends AuthMechanism {
  36. /** @var ISession */
  37. protected $session;
  38. /** @var ICrypto */
  39. protected $crypto;
  40. public function __construct(IL10N $l, ISession $session, ICrypto $crypto) {
  41. $this->session = $session;
  42. $this->crypto = $crypto;
  43. $this
  44. ->setIdentifier('password::sessioncredentials')
  45. ->setScheme(self::SCHEME_PASSWORD)
  46. ->setText($l->t('Log-in credentials, save in session'))
  47. ->addParameters([
  48. ])
  49. ;
  50. \OCP\Util::connectHook('OC_User', 'post_login', $this, 'authenticate');
  51. }
  52. /**
  53. * Hook listener on post login
  54. *
  55. * @param array $params
  56. */
  57. public function authenticate(array $params) {
  58. $this->session->set('password::sessioncredentials/credentials', $this->crypto->encrypt(json_encode($params)));
  59. }
  60. public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) {
  61. $encrypted = $this->session->get('password::sessioncredentials/credentials');
  62. if (!isset($encrypted)) {
  63. throw new InsufficientDataForMeaningfulAnswerException('No session credentials saved');
  64. }
  65. $credentials = json_decode($this->crypto->decrypt($encrypted), true);
  66. $storage->setBackendOption('user', $this->session->get('loginname'));
  67. $storage->setBackendOption('password', $credentials['password']);
  68. }
  69. public function wrapStorage(Storage $storage) {
  70. return new SessionStorageWrapper(['storage' => $storage]);
  71. }
  72. }