Skip to content

Commit 2fe41a5

Browse files
committed
Respect password policies (if any) when generating a temporary password.
This commit is part of #31005 Signed-off-by: Cyrille Bollu <cyrpub@bollu.be>
1 parent 871b76f commit 2fe41a5

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

apps/files_sharing/lib/BackgroundJob/ResetExpiredPasswordsJob.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,24 @@ protected function run($argument) {
5353
$qb = $this->connection->getQueryBuilder();
5454

5555
// QUESTION: DOES THE DATETIME COMPARAISON WORK WELL WHEN TIMEZONES ENTER THE GAME?
56+
// I THINK SO, BECAUSE EVERYTHING HAPPENS ON THE SERVER, HENCE ON THE SAME TZ
5657
$qb->select('id')
5758
->from('share')
5859
->where($qb->expr()->lte('password_expiration_time', $qb->createNamedParameter((new \DateTime())->format('Y-m-d H:i:s'))));
5960

6061
$result = $qb->execute();
6162
while ($row = $result->fetch()) {
62-
// QUESTION: SHALL I RESPECT PASSWORD POLICY HERE (WHEN USED)?
63+
64+
// Generates a random password respecting any password policy defined
65+
$eventDispatcher = \OC::$server->query(IEventDispatcher::class);
66+
$event = new \OCP\Security\Events\GenerateSecurePasswordEvent();
67+
$eventDispatcher->dispatchTyped($event);
68+
$password = $event->getPassword() ?? $this->hasher->hash($this->secureRandom->generate(20));
69+
70+
// Updates share password and expiration time
6371
$qb->update('share')
6472
->where($qb->expr()->eq('id', $qb->createNamedParameter($row['id'])))
65-
->set('password', $qb->createNamedParameter($this->hasher->hash($this->secureRandom->generate(20))))
73+
->set('password', $qb->createNamedParameter($password))
6674
->set('password_expiration_time', $qb->createNamedParameter((new \DateTime())->add(new \DateInterval('P1D'))->format('Y-m-d H:i:s')))
6775
->execute();
6876
}

apps/files_sharing/lib/Controller/ShareController.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,12 @@ protected function validateIdentity(string $identityToken): bool {
233233
}
234234

235235
protected function generatePassword() {
236-
// QUESTION: SHALL I RESPECT PASSWORD POLICY HERE (WHEN USED)?
237-
$password = \OC::$server->getSecureRandom()->generate(20);
236+
// Generates a password respecting any password policy defined
237+
$eventDispatcher = \OC::$server->query(IEventDispatcher::class);
238+
$event = new \OCP\Security\Events\GenerateSecurePasswordEvent();
239+
$eventDispatcher->dispatchTyped($event);
240+
$password = $event->getPassword() ?? $password = \OC::$server->getSecureRandom()->generate(20);
241+
238242
$this->share->setPassword($password);
239243
$this->shareManager->updateShare($this->share, true);
240244
return;

0 commit comments

Comments
 (0)