Skip to content

Commit

Permalink
emit events for before-after-failed link password checks
Browse files Browse the repository at this point in the history
  • Loading branch information
karakayasemi committed May 22, 2020
1 parent 702d4cb commit e1a5cca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions changelog/unreleased/37438
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Add 3 new events (before-fail-after) for link password validations.

'share.beforelinkpasswordcheck', 'share.afterlinkpasswordcheck' and 'share.failedlinkpasswordcheck'
events has been added for link password validations.

https://github.com/owncloud/core/pull/37438
8 changes: 6 additions & 2 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1488,12 +1488,16 @@ public function checkPassword(\OCP\Share\IShare $share, $password) {
if ($password === null || $share->getPassword() === null) {
return false;
}

$beforeEvent = new GenericEvent(null, ['shareObject' => $share]);
$this->eventDispatcher->dispatch('share.beforelinkpasswordcheck', $beforeEvent);
$newHash = '';
if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) {
$failEvent = new GenericEvent(null, ['shareObject' => $share]);
$this->eventDispatcher->dispatch('share.failedlinkpasswordcheck', $failEvent);
return false;
}

$afterEvent = new GenericEvent(null, ['shareObject' => $share]);
$this->eventDispatcher->dispatch('share.afterlinkpasswordcheck', $afterEvent);
if (!empty($newHash)) {
$share->setPassword($newHash);
$provider = $this->factory->getProviderForType($share->getShareType());
Expand Down
33 changes: 33 additions & 0 deletions tests/lib/Share20/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3151,17 +3151,50 @@ public function testCheckPasswordInvalidPassword() {

$this->hasher->method('verify')->with('invalidpassword', 'password', '')->willReturn(false);

$calledBeforeEvent = [];
$this->eventDispatcher->addListener('share.beforelinkpasswordcheck',
function (GenericEvent $event) use (&$calledBeforeEvent) {
$calledBeforeEvent[] = 'share.beforelinkpasswordcheck';
$calledBeforeEvent[] = $event;
});
$calledFailEvent = [];
$this->eventDispatcher->addListener('share.failedlinkpasswordcheck',
function (GenericEvent $event) use (&$calledFailEvent) {
$calledFailEvent[] = 'share.failedlinkpasswordcheck';
$calledFailEvent[] = $event;
});
$this->assertFalse($this->manager->checkPassword($share, 'invalidpassword'));
$this->assertEquals('share.beforelinkpasswordcheck', $calledBeforeEvent[0]);
$this->assertEquals('share.failedlinkpasswordcheck', $calledFailEvent[0]);
$this->assertInstanceOf(GenericEvent::class, $calledBeforeEvent[1]);
$this->assertInstanceOf(GenericEvent::class, $calledFailEvent[1]);
}

public function testCheckPasswordValidPassword() {
$share = $this->createMock('\OCP\Share\IShare');
$share->method('getShareType')->willReturn(\OCP\Share::SHARE_TYPE_LINK);
$share->method('getPassword')->willReturn('passwordHash');

$calledBeforeEvent = [];
$this->eventDispatcher->addListener('share.beforelinkpasswordcheck',
function (GenericEvent $event) use (&$calledBeforeEvent) {
$calledBeforeEvent[] = 'share.beforelinkpasswordcheck';
$calledBeforeEvent[] = $event;
});
$calledAfterEvent = [];
$this->eventDispatcher->addListener('share.afterlinkpasswordcheck',
function (GenericEvent $event) use (&$calledAfterEvent) {
$calledAfterEvent[] = 'share.afterlinkpasswordcheck';
$calledAfterEvent[] = $event;
});

$this->hasher->method('verify')->with('password', 'passwordHash', '')->willReturn(true);

$this->assertTrue($this->manager->checkPassword($share, 'password'));
$this->assertEquals('share.beforelinkpasswordcheck', $calledBeforeEvent[0]);
$this->assertEquals('share.afterlinkpasswordcheck', $calledAfterEvent[0]);
$this->assertInstanceOf(GenericEvent::class, $calledBeforeEvent[1]);
$this->assertInstanceOf(GenericEvent::class, $calledAfterEvent[1]);
}

public function testCheckPasswordUpdateShare() {
Expand Down

0 comments on commit e1a5cca

Please sign in to comment.