EncryptionTrait.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Traits;
  8. use OC\Encryption\EncryptionWrapper;
  9. use OC\Files\SetupManager;
  10. use OC\Memcache\ArrayCache;
  11. use OCA\Encryption\AppInfo\Application;
  12. use OCA\Encryption\KeyManager;
  13. use OCA\Encryption\Users\Setup;
  14. use OCP\Encryption\IManager;
  15. use OCP\IUserManager;
  16. use Psr\Log\LoggerInterface;
  17. /**
  18. * Enables encryption
  19. */
  20. trait EncryptionTrait {
  21. // from MountProviderTrait
  22. abstract protected function registerStorageWrapper($name, $wrapper);
  23. // from phpunit
  24. abstract protected static function markTestSkipped(string $message = ''): void;
  25. abstract protected static function assertTrue($condition, string $message = ''): void;
  26. private $encryptionWasEnabled;
  27. private $originalEncryptionModule;
  28. /** @var IUserManager */
  29. private $userManager;
  30. /** @var SetupManager */
  31. private $setupManager;
  32. /**
  33. * @var \OCP\IConfig
  34. */
  35. private $config;
  36. /**
  37. * @var \OCA\Encryption\AppInfo\Application
  38. */
  39. private $encryptionApp;
  40. protected function loginWithEncryption($user = '') {
  41. \OC_Util::tearDownFS();
  42. \OC_User::setUserId('');
  43. // needed for fully logout
  44. \OC::$server->getUserSession()->setUser(null);
  45. $this->setupManager->tearDown();
  46. \OC_User::setUserId($user);
  47. $this->postLogin();
  48. \OC_Util::setupFS($user);
  49. if ($this->userManager->userExists($user)) {
  50. \OC::$server->getUserFolder($user);
  51. }
  52. }
  53. protected function setupForUser($name, $password) {
  54. $this->setupManager->tearDown();
  55. $this->setupManager->setupForUser($this->userManager->get($name));
  56. $container = $this->encryptionApp->getContainer();
  57. /** @var KeyManager $keyManager */
  58. $keyManager = $container->query(KeyManager::class);
  59. /** @var Setup $userSetup */
  60. $userSetup = $container->query(Setup::class);
  61. $userSetup->setupUser($name, $password);
  62. $encryptionManager = $container->query(IManager::class);
  63. $this->encryptionApp->setUp($encryptionManager);
  64. $keyManager->init($name, $password);
  65. }
  66. protected function postLogin() {
  67. $encryptionWrapper = new EncryptionWrapper(
  68. new ArrayCache(),
  69. \OC::$server->getEncryptionManager(),
  70. \OC::$server->get(LoggerInterface::class)
  71. );
  72. $this->registerStorageWrapper('oc_encryption', [$encryptionWrapper, 'wrapStorage']);
  73. }
  74. protected function setUpEncryptionTrait() {
  75. $isReady = \OC::$server->getEncryptionManager()->isReady();
  76. if (!$isReady) {
  77. $this->markTestSkipped('Encryption not ready');
  78. }
  79. $this->userManager = \OC::$server->get(IUserManager::class);
  80. $this->setupManager = \OC::$server->get(SetupManager::class);
  81. \OC_App::loadApp('encryption');
  82. $this->encryptionApp = new Application([], $isReady);
  83. $this->config = \OC::$server->getConfig();
  84. $this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no');
  85. $this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module');
  86. $this->config->setAppValue('core', 'default_encryption_module', \OCA\Encryption\Crypto\Encryption::ID);
  87. $this->config->setAppValue('core', 'encryption_enabled', 'yes');
  88. $this->assertTrue(\OC::$server->getEncryptionManager()->isEnabled());
  89. }
  90. protected function tearDownEncryptionTrait() {
  91. if ($this->config) {
  92. $this->config->setAppValue('core', 'encryption_enabled', $this->encryptionWasEnabled);
  93. $this->config->setAppValue('core', 'default_encryption_module', $this->originalEncryptionModule);
  94. $this->config->deleteAppValue('encryption', 'useMasterKey');
  95. }
  96. }
  97. }