diff --git a/core/Sharing/Property/PasswordSharePropertyType.php b/core/Sharing/Property/PasswordSharePropertyType.php index 6b491ac2d9704..4f9422327b7a9 100644 --- a/core/Sharing/Property/PasswordSharePropertyType.php +++ b/core/Sharing/Property/PasswordSharePropertyType.php @@ -10,20 +10,29 @@ namespace OC\Core\Sharing\Property; use OC\Core\AppInfo\Application; +use OCP\EventDispatcher\IEventDispatcher; use OCP\L10N\IFactory; +use OCP\Security\Events\GenerateSecurePasswordEvent; use OCP\Security\IHasher; +use OCP\Security\ISecureRandom; +use OCP\Security\PasswordContext; use OCP\Share\IManager; use OCP\Sharing\Property\APasswordSharePropertyType; use OCP\Sharing\Property\ISharePropertyTypeFilter; use OCP\Sharing\Share; use OCP\Sharing\ShareAccessContext; +use Random\Randomizer; final class PasswordSharePropertyType extends APasswordSharePropertyType implements ISharePropertyTypeFilter { + private readonly Randomizer $randomizer; + public function __construct( private readonly IManager $legacyManager, private readonly IHasher $hasher, + private readonly IEventDispatcher $eventDispatcher, ) { + $this->randomizer = new Randomizer(); } #[\Override] @@ -54,7 +63,13 @@ public function isRequired(): bool { #[\Override] public function getDefaultValue(): ?string { - return null; + if (!$this->isRequired()) { + return null; + } + + $event = new GenerateSecurePasswordEvent(PasswordContext::SHARING); + $this->eventDispatcher->dispatchTyped($event); + return $event->getPassword() ?? $this->randomizer->getBytesFromString(ISecureRandom::CHAR_ALPHANUMERIC, 20); } #[\Override] diff --git a/tests/Core/Sharing/Property/PasswordSharePropertyTypeTest.php b/tests/Core/Sharing/Property/PasswordSharePropertyTypeTest.php index febd440646931..c558805c2473b 100644 --- a/tests/Core/Sharing/Property/PasswordSharePropertyTypeTest.php +++ b/tests/Core/Sharing/Property/PasswordSharePropertyTypeTest.php @@ -9,9 +9,13 @@ namespace Tests\Core\Sharing\Property; +use OC\Core\AppInfo\Application; +use OC\Core\AppInfo\ConfigLexicon; use OC\Core\Sharing\Property\PasswordSharePropertyType; +use OCP\IAppConfig; use OCP\IUser; use OCP\IUserManager; +use OCP\L10N\IFactory; use OCP\Security\IHasher; use OCP\Server; use OCP\Sharing\Property\ShareProperty; @@ -64,6 +68,23 @@ private function createDummyShare(?ShareProperty $property): Share { ); } + public function testGetDefaultValue(): void { + $appConfig = Server::get(IAppConfig::class); + $appConfig->deleteKey(Application::APP_ID, ConfigLexicon::SHARE_LINK_PASSWORD_ENFORCED); + + $this->assertNull($this->propertyType->getDefaultValue()); + + $appConfig->setValueBool(Application::APP_ID, ConfigLexicon::SHARE_LINK_PASSWORD_ENFORCED, true); + + $value = $this->propertyType->getDefaultValue(); + $this->assertNotNull($value); + /** @psalm-suppress RedundantCastGivenDocblockType psalm:strict and rector:strict fight over the cast -_- */ + $this->assertGreaterThan(1, strlen((string)$value)); + $this->assertTrue($this->propertyType->validateValue(Server::get(IFactory::class), $value)); + + $appConfig->deleteKey(Application::APP_ID, ConfigLexicon::SHARE_LINK_PASSWORD_ENFORCED); + } + public function testIsFiltered(): void { $this->assertFalse($this->propertyType->isFiltered(new ShareAccessContext(arguments: [$this->propertyType::class => '123']), $this->createDummyShare(new ShareProperty($this->propertyType::class, Server::get(IHasher::class)->hash('123'))))); $this->assertFalse($this->propertyType->isFiltered(new ShareAccessContext(arguments: [$this->propertyType::class => '123']), $this->createDummyShare(new ShareProperty($this->propertyType::class, null))));