aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Summers <18727110+summersab@users.noreply.github.com>2023-08-29 16:29:33 -0500
committerAndrew Summers <18727110+summersab@users.noreply.github.com>2023-08-29 21:32:40 -0500
commit1395a5360274a6c6c0b4084d22da53466998c954 (patch)
tree01f3f8d5b532b0ccff57b4f4a806f11d94e87ce6
parent9d1547f59d53b46ba401ddf0e7f4c6e4f067004e (diff)
downloadnextcloud-server-1395a5360274a6c6c0b4084d22da53466998c954.tar.gz
nextcloud-server-1395a5360274a6c6c0b4084d22da53466998c954.zip
Refactor `OC\Server::getSecureRandom`
Signed-off-by: Andrew Summers <18727110+summersab@users.noreply.github.com>
-rw-r--r--core/Command/Maintenance/Install.php3
-rw-r--r--lib/private/Cache/File.php2
-rw-r--r--lib/private/DB/Connection.php3
-rw-r--r--lib/private/Security/SecureRandom.php2
-rw-r--r--lib/private/Setup.php2
-rw-r--r--lib/private/Setup/PostgreSQL.php2
-rw-r--r--lib/private/Share20/ProviderFactory.php7
-rw-r--r--lib/private/legacy/OC_Util.php5
-rw-r--r--lib/public/Security/ISecureRandom.php4
-rw-r--r--tests/lib/DB/MigratorTest.php3
-rw-r--r--tests/lib/TestCase.php2
11 files changed, 21 insertions, 14 deletions
diff --git a/core/Command/Maintenance/Install.php b/core/Command/Maintenance/Install.php
index 643bb54c0d6..ac5b3ddfbe0 100644
--- a/core/Command/Maintenance/Install.php
+++ b/core/Command/Maintenance/Install.php
@@ -36,6 +36,7 @@ use OC\Installer;
use OC\Setup;
use OC\SystemConfig;
use OCP\Defaults;
+use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
@@ -80,7 +81,7 @@ class Install extends Command {
$server->getL10N('lib'),
$server->query(Defaults::class),
$server->get(LoggerInterface::class),
- $server->getSecureRandom(),
+ $server->get(ISecureRandom::class),
\OC::$server->query(Installer::class)
);
$sysInfo = $setupHelper->getSystemInfo(true);
diff --git a/lib/private/Cache/File.php b/lib/private/Cache/File.php
index 72fc95a802b..9b0493da38b 100644
--- a/lib/private/Cache/File.php
+++ b/lib/private/Cache/File.php
@@ -105,7 +105,7 @@ class File implements ICache {
$storage = $this->getStorage();
$result = false;
// unique id to avoid chunk collision, just in case
- $uniqueId = \OC::$server->getSecureRandom()->generate(
+ $uniqueId = \OC::$server->get(ISecureRandom::class)->generate(
16,
ISecureRandom::CHAR_ALPHANUMERIC
);
diff --git a/lib/private/DB/Connection.php b/lib/private/DB/Connection.php
index 85c6a72dfdb..6150e4b2045 100644
--- a/lib/private/DB/Connection.php
+++ b/lib/private/DB/Connection.php
@@ -52,6 +52,7 @@ use OCP\Diagnostics\IEventLogger;
use OCP\IRequestId;
use OCP\PreConditionNotMetException;
use OCP\Profiler\IProfiler;
+use OCP\Security\ISecureRandom;
use OC\DB\QueryBuilder\QueryBuilder;
use OC\SystemConfig;
use Psr\Log\LoggerInterface;
@@ -592,7 +593,7 @@ class Connection extends \Doctrine\DBAL\Connection {
private function getMigrator() {
// TODO properly inject those dependencies
- $random = \OC::$server->getSecureRandom();
+ $random = \OC::$server->get(ISecureRandom::class);
$platform = $this->getDatabasePlatform();
$config = \OC::$server->getConfig();
$dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class);
diff --git a/lib/private/Security/SecureRandom.php b/lib/private/Security/SecureRandom.php
index cbd1dc8db6d..dcf50f00629 100644
--- a/lib/private/Security/SecureRandom.php
+++ b/lib/private/Security/SecureRandom.php
@@ -35,7 +35,7 @@ use OCP\Security\ISecureRandom;
* use a fallback.
*
* Usage:
- * \OC::$server->getSecureRandom()->generate(10);
+ * \OC::$server->get(ISecureRandom::class)->generate(10);
* @package OC\Security
*/
class SecureRandom implements ISecureRandom {
diff --git a/lib/private/Setup.php b/lib/private/Setup.php
index 0993fe54f47..5c7cd10a2a7 100644
--- a/lib/private/Setup.php
+++ b/lib/private/Setup.php
@@ -510,7 +510,7 @@ class Setup {
\OC::$server->getL10N('lib'),
\OCP\Server::get(Defaults::class),
\OC::$server->get(LoggerInterface::class),
- \OC::$server->getSecureRandom(),
+ \OC::$server->get(ISecureRandom::class),
\OCP\Server::get(Installer::class)
);
diff --git a/lib/private/Setup/PostgreSQL.php b/lib/private/Setup/PostgreSQL.php
index 490cbba69a9..d400bbf7e8b 100644
--- a/lib/private/Setup/PostgreSQL.php
+++ b/lib/private/Setup/PostgreSQL.php
@@ -69,7 +69,7 @@ class PostgreSQL extends AbstractDatabase {
//add prefix to the postgresql user name to prevent collisions
$this->dbUser = 'oc_' . strtolower($username);
//create a new password so we don't need to store the admin config in the config file
- $this->dbPassword = \OC::$server->getSecureRandom()->generate(30, ISecureRandom::CHAR_ALPHANUMERIC);
+ $this->dbPassword = \OC::$server->get(ISecureRandom::class)->generate(30, ISecureRandom::CHAR_ALPHANUMERIC);
$this->createDBUser($connection);
diff --git a/lib/private/Share20/ProviderFactory.php b/lib/private/Share20/ProviderFactory.php
index 8c01d660915..03284b6d78d 100644
--- a/lib/private/Share20/ProviderFactory.php
+++ b/lib/private/Share20/ProviderFactory.php
@@ -44,6 +44,7 @@ use OCA\Talk\Share\RoomShareProvider;
use OCP\Defaults;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IServerContainer;
+use OCP\Security\ISecureRandom;
use OCP\Share\IManager;
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
@@ -146,7 +147,7 @@ class ProviderFactory implements IProviderFactory {
$this->serverContainer->get(LoggerInterface::class),
);
$tokenHandler = new TokenHandler(
- $this->serverContainer->getSecureRandom()
+ $this->serverContainer->get(ISecureRandom::class)
);
$this->federatedProvider = new FederatedShareProvider(
@@ -188,7 +189,7 @@ class ProviderFactory implements IProviderFactory {
$this->shareByMailProvider = new ShareByMailProvider(
$this->serverContainer->getConfig(),
$this->serverContainer->getDatabaseConnection(),
- $this->serverContainer->getSecureRandom(),
+ $this->serverContainer->get(ISecureRandom::class),
$this->serverContainer->getUserManager(),
$this->serverContainer->getLazyRootFolder(),
$this->serverContainer->getL10N('sharebymail'),
@@ -230,7 +231,7 @@ class ProviderFactory implements IProviderFactory {
if ($this->shareByCircleProvider === null) {
$this->shareByCircleProvider = new \OCA\Circles\ShareByCircleProvider(
$this->serverContainer->getDatabaseConnection(),
- $this->serverContainer->getSecureRandom(),
+ $this->serverContainer->get(ISecureRandom::class),
$this->serverContainer->getUserManager(),
$this->serverContainer->getLazyRootFolder(),
$this->serverContainer->getL10N('circles'),
diff --git a/lib/private/legacy/OC_Util.php b/lib/private/legacy/OC_Util.php
index 9d62c46137e..a0575c4bef3 100644
--- a/lib/private/legacy/OC_Util.php
+++ b/lib/private/legacy/OC_Util.php
@@ -71,6 +71,7 @@ use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IURLGenerator;
use OCP\IUser;
+use OCP\Security\ISecureRandom;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;
@@ -518,7 +519,7 @@ class OC_Util {
\OC::$server->getL10N('lib'),
\OC::$server->get(\OCP\Defaults::class),
\OC::$server->get(LoggerInterface::class),
- \OC::$server->getSecureRandom(),
+ \OC::$server->get(ISecureRandom::class),
\OC::$server->get(\OC\Installer::class)
);
@@ -845,7 +846,7 @@ class OC_Util {
$id = \OC::$server->getSystemConfig()->getValue('instanceid', null);
if (is_null($id)) {
// We need to guarantee at least one letter in instanceid so it can be used as the session_name
- $id = 'oc' . \OC::$server->getSecureRandom()->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
+ $id = 'oc' . \OC::$server->get(ISecureRandom::class)->generate(10, \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_DIGITS);
\OC::$server->getSystemConfig()->setValue('instanceid', $id);
}
return $id;
diff --git a/lib/public/Security/ISecureRandom.php b/lib/public/Security/ISecureRandom.php
index 3634ebf99f7..6cb6854a033 100644
--- a/lib/public/Security/ISecureRandom.php
+++ b/lib/public/Security/ISecureRandom.php
@@ -28,13 +28,15 @@ declare(strict_types=1);
*/
namespace OCP\Security;
+use OCP\Security\ISecureRandom;
+
/**
* Class SecureRandom provides a wrapper around the random_int function to generate
* secure random strings. For PHP 7 the native CSPRNG is used, older versions do
* use a fallback.
*
* Usage:
- * \OC::$server->getSecureRandom()->generate(10);
+ * \OC::$server->get(ISecureRandom::class)->generate(10);
*
* @since 8.0.0
*/
diff --git a/tests/lib/DB/MigratorTest.php b/tests/lib/DB/MigratorTest.php
index 4d7d9cab19f..06bb005cc44 100644
--- a/tests/lib/DB/MigratorTest.php
+++ b/tests/lib/DB/MigratorTest.php
@@ -24,6 +24,7 @@ use OC\DB\PostgreSqlMigrator;
use OC\DB\SQLiteMigrator;
use OCP\DB\Types;
use OCP\IConfig;
+use OCP\Security\ISecureRandom;
/**
* Class MigratorTest
@@ -61,7 +62,7 @@ class MigratorTest extends \Test\TestCase {
private function getMigrator(): Migrator {
$platform = $this->connection->getDatabasePlatform();
- $random = \OC::$server->getSecureRandom();
+ $random = \OC::$server->get(ISecureRandom::class);
$dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class);
if ($platform instanceof SqlitePlatform) {
return new SQLiteMigrator($this->connection, $this->config, $dispatcher);
diff --git a/tests/lib/TestCase.php b/tests/lib/TestCase.php
index f5fc9a6e8f2..130deaf11a7 100644
--- a/tests/lib/TestCase.php
+++ b/tests/lib/TestCase.php
@@ -255,7 +255,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
* @return string
*/
protected static function getUniqueID($prefix = '', $length = 13) {
- return $prefix . \OC::$server->getSecureRandom()->generate(
+ return $prefix . \OC::$server->get(ISecureRandom::class)->generate(
$length,
// Do not use dots and slashes as we use the value for file names
ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER