Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor OC\Server::getSecureRandom #40124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/private/Cache/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function set($key, $value, $ttl = 0) {
$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
);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\IRequestId;
use OCP\PreConditionNotMetException;
use OCP\Profiler\IProfiler;
use OCP\Security\ISecureRandom;
use OCP\Server;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -666,7 +667,7 @@ public function migrateToSchema(Schema $toSchema, bool $dryRun = false) {

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 = Server::get(\OCP\EventDispatcher\IEventDispatcher::class);
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Security/SecureRandom.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* use a fallback.
*
* Usage:
* \OC::$server->getSecureRandom()->generate(10);
* \OC::$server->get(ISecureRandom::class)->generate(10);
* @package OC\Security
*/
class SecureRandom implements ISecureRandom {
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Setup/PostgreSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function setupDatabase($username) {
//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);

Expand Down
7 changes: 4 additions & 3 deletions lib/private/Share20/ProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\Security\IHasher;
use OCP\Security\ISecureRandom;
use OCP\Share\IManager;
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
Expand Down Expand Up @@ -127,7 +128,7 @@ protected function federatedShareProvider() {
$this->serverContainer->get(LoggerInterface::class),
);
$tokenHandler = new TokenHandler(
$this->serverContainer->getSecureRandom()
$this->serverContainer->get(ISecureRandom::class)
);

$this->federatedProvider = new FederatedShareProvider(
Expand Down Expand Up @@ -169,7 +170,7 @@ protected function getShareByMailProvider() {
$this->shareByMailProvider = new ShareByMailProvider(
$this->serverContainer->getConfig(),
$this->serverContainer->getDatabaseConnection(),
$this->serverContainer->getSecureRandom(),
$this->serverContainer->get(ISecureRandom::class),
$this->serverContainer->getUserManager(),
$this->serverContainer->get(IRootFolder::class),
$this->serverContainer->getL10N('sharebymail'),
Expand Down Expand Up @@ -211,7 +212,7 @@ protected function getShareByCircleProvider() {
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->get(IRootFolder::class),
$this->serverContainer->getL10N('circles'),
Expand Down
3 changes: 2 additions & 1 deletion lib/private/legacy/OC_Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\L10N\IFactory;
use OCP\Security\ISecureRandom;
use OCP\Share\IManager;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -782,7 +783,7 @@ public static function getInstanceId() {
$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;
Expand Down
2 changes: 1 addition & 1 deletion lib/public/Security/ISecureRandom.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* use a fallback.
*
* Usage:
* \OC::$server->getSecureRandom()->generate(10);
* \OC::$server->get(ISecureRandom::class)->generate(10);
*
* @since 8.0.0
*/
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/DB/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OC\DB\SQLiteMigrator;
use OCP\DB\Types;
use OCP\IConfig;
use OCP\Security\ISecureRandom;

/**
* Class MigratorTest
Expand Down Expand Up @@ -56,7 +57,7 @@ protected function setUp(): void {

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);
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ protected static function invokePrivate($object, $methodName, array $parameters
* @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
Expand Down
Loading