1
0

RandomnessSecure.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OCP\IConfig;
  9. use OCP\IL10N;
  10. use OCP\IURLGenerator;
  11. use OCP\Security\ISecureRandom;
  12. use OCP\SetupCheck\ISetupCheck;
  13. use OCP\SetupCheck\SetupResult;
  14. class RandomnessSecure implements ISetupCheck {
  15. public function __construct(
  16. private IL10N $l10n,
  17. private IConfig $config,
  18. private IURLGenerator $urlGenerator,
  19. private ISecureRandom $secureRandom,
  20. ) {
  21. }
  22. public function getName(): string {
  23. return $this->l10n->t('Random generator');
  24. }
  25. public function getCategory(): string {
  26. return 'security';
  27. }
  28. public function run(): SetupResult {
  29. try {
  30. $this->secureRandom->generate(1);
  31. } catch (\Exception $ex) {
  32. return SetupResult::error(
  33. $this->l10n->t('No suitable source for randomness found by PHP which is highly discouraged for security reasons.'),
  34. $this->urlGenerator->linkToDocs('admin-security')
  35. );
  36. }
  37. return SetupResult::success($this->l10n->t('Secure'));
  38. }
  39. }