1
0

AddMissingSecretJob.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2022 Carl Schwan <carl@carlschwan.eu>
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  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
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Repair\NC25;
  22. use OCP\HintException;
  23. use OCP\IConfig;
  24. use OCP\Migration\IOutput;
  25. use OCP\Migration\IRepairStep;
  26. use OCP\Security\ISecureRandom;
  27. class AddMissingSecretJob implements IRepairStep {
  28. private IConfig $config;
  29. private ISecureRandom $random;
  30. public function __construct(IConfig $config, ISecureRandom $random) {
  31. $this->config = $config;
  32. $this->random = $random;
  33. }
  34. public function getName(): string {
  35. return 'Add possibly missing system config';
  36. }
  37. public function run(IOutput $output): void {
  38. $passwordSalt = $this->config->getSystemValueString('passwordsalt', '');
  39. if ($passwordSalt === '') {
  40. try {
  41. $this->config->setSystemValue('passwordsalt', $this->random->generate(30));
  42. } catch (HintException $e) {
  43. $output->warning("passwordsalt is missing from your config.php and your config.php is read only. Please fix it manually.");
  44. }
  45. }
  46. $secret = $this->config->getSystemValueString('secret', '');
  47. if ($secret === '') {
  48. try {
  49. $this->config->setSystemValue('secret', $this->random->generate(48));
  50. } catch (HintException $e) {
  51. $output->warning("secret is missing from your config.php and your config.php is read only. Please fix it manually.");
  52. }
  53. }
  54. }
  55. }