Skip to content
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
17 changes: 16 additions & 1 deletion core/Sharing/Property/PasswordSharePropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
21 changes: 21 additions & 0 deletions tests/Core/Sharing/Property/PasswordSharePropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))));
Expand Down
Loading