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

Add typed events for password_policy #18019

Merged
merged 1 commit into from
Nov 27, 2019
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: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@
'OCP\\Search\\Provider' => $baseDir . '/lib/public/Search/Provider.php',
'OCP\\Search\\Result' => $baseDir . '/lib/public/Search/Result.php',
'OCP\\Security\\CSP\\AddContentSecurityPolicyEvent' => $baseDir . '/lib/public/Security/CSP/AddContentSecurityPolicyEvent.php',
'OCP\\Security\\Events\\GenerateSecurePasswordEvent' => $baseDir . '/lib/public/Security/Events/GenerateSecurePasswordEvent.php',
'OCP\\Security\\Events\\ValidatePasswordPolicyEvent' => $baseDir . '/lib/public/Security/Events/ValidatePasswordPolicyEvent.php',
'OCP\\Security\\FeaturePolicy\\AddFeaturePolicyEvent' => $baseDir . '/lib/public/Security/FeaturePolicy/AddFeaturePolicyEvent.php',
'OCP\\Security\\IContentSecurityPolicyManager' => $baseDir . '/lib/public/Security/IContentSecurityPolicyManager.php',
'OCP\\Security\\ICredentialsManager' => $baseDir . '/lib/public/Security/ICredentialsManager.php',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Search\\Provider' => __DIR__ . '/../../..' . '/lib/public/Search/Provider.php',
'OCP\\Search\\Result' => __DIR__ . '/../../..' . '/lib/public/Search/Result.php',
'OCP\\Security\\CSP\\AddContentSecurityPolicyEvent' => __DIR__ . '/../../..' . '/lib/public/Security/CSP/AddContentSecurityPolicyEvent.php',
'OCP\\Security\\Events\\GenerateSecurePasswordEvent' => __DIR__ . '/../../..' . '/lib/public/Security/Events/GenerateSecurePasswordEvent.php',
'OCP\\Security\\Events\\ValidatePasswordPolicyEvent' => __DIR__ . '/../../..' . '/lib/public/Security/Events/ValidatePasswordPolicyEvent.php',
'OCP\\Security\\FeaturePolicy\\AddFeaturePolicyEvent' => __DIR__ . '/../../..' . '/lib/public/Security/FeaturePolicy/AddFeaturePolicyEvent.php',
'OCP\\Security\\IContentSecurityPolicyManager' => __DIR__ . '/../../..' . '/lib/public/Security/IContentSecurityPolicyManager.php',
'OCP\\Security\\ICredentialsManager' => __DIR__ . '/../../..' . '/lib/public/Security/ICredentialsManager.php',
Expand Down
4 changes: 2 additions & 2 deletions lib/private/Share20/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\Security\Events\ValidatePasswordPolicyEvent;
use OCP\Security\IHasher;
use OCP\Security\ISecureRandom;
use OCP\Share;
Expand Down Expand Up @@ -191,8 +192,7 @@ protected function verifyPassword($password) {

// Let others verify the password
try {
$event = new GenericEvent($password);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
$this->eventDispatcher->dispatch(new ValidatePasswordPolicyEvent($password));
} catch (HintException $e) {
throw new \Exception($e->getHint());
}
Expand Down
15 changes: 7 additions & 8 deletions lib/private/User/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
namespace OC\User;

use OC\Cache\CappedMemoryCache;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\Security\Events\ValidatePasswordPolicyEvent;
use OCP\User\Backend\ABackend;
use OCP\User\Backend\ICheckPasswordBackend;
use OCP\User\Backend\ICountUsersBackend;
Expand All @@ -68,7 +70,6 @@
use OCP\User\Backend\IGetRealUIDBackend;
use OCP\User\Backend\ISetDisplayNameBackend;
use OCP\User\Backend\ISetPasswordBackend;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
Expand All @@ -86,7 +87,7 @@ class Database extends ABackend
/** @var CappedMemoryCache */
private $cache;

/** @var EventDispatcherInterface */
/** @var IEventDispatcher */
private $eventDispatcher;

/** @var IDBConnection */
Expand All @@ -98,13 +99,13 @@ class Database extends ABackend
/**
* \OC\User\Database constructor.
*
* @param EventDispatcherInterface $eventDispatcher
* @param IEventDispatcher $eventDispatcher
* @param string $table
*/
public function __construct($eventDispatcher = null, $table = 'users') {
$this->cache = new CappedMemoryCache();
$this->table = $table;
$this->eventDispatcher = $eventDispatcher ? $eventDispatcher : \OC::$server->getEventDispatcher();
$this->eventDispatcher = $eventDispatcher ? $eventDispatcher : \OC::$server->query(IEventDispatcher::class);
}

/**
Expand All @@ -130,8 +131,7 @@ public function createUser(string $uid, string $password): bool {
$this->fixDI();

if (!$this->userExists($uid)) {
$event = new GenericEvent($password);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
$this->eventDispatcher->dispatchTyped(new ValidatePasswordPolicyEvent($password));

$qb = $this->dbConn->getQueryBuilder();
$qb->insert($this->table)
Expand Down Expand Up @@ -199,8 +199,7 @@ public function setPassword(string $uid, string $password): bool {
$this->fixDI();

if ($this->userExists($uid)) {
$event = new GenericEvent($password);
$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
$this->eventDispatcher->dispatchTyped(new ValidatePasswordPolicyEvent($password));

$hasher = \OC::$server->getHasher();
$hashedPassword = $hasher->hash($password);
Expand Down
50 changes: 50 additions & 0 deletions lib/public/Security/Events/GenerateSecurePasswordEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCP\Security\Events;

use OCP\EventDispatcher\Event;

/**
* @since 18.0.0
*/
class GenerateSecurePasswordEvent extends Event {

/** @var null|string */
private $password;

/**
* @since 18.0.0
*/
public function getPassword(): ?string {
return $this->password;
}

/**
* @since 18.0.0
*/
public function setPassword(string $password): void {
$this->password = $password;
}

}
51 changes: 51 additions & 0 deletions lib/public/Security/Events/ValidatePasswordPolicyEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

/**
* @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCP\Security\Events;

use OCP\EventDispatcher\Event;

/**
* @since 18.0.0
*/
class ValidatePasswordPolicyEvent extends Event {
ChristophWurst marked this conversation as resolved.
Show resolved Hide resolved

/** @var string */
private $password;

/**
* @since 18.0.0
*/
public function __construct(string $password) {
parent::__construct();
$this->password = $password;
}

/**
* @since 18.0.0
*/
public function getPassword(): string {
return $this->password;
}

}
55 changes: 30 additions & 25 deletions tests/lib/Share20/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OC\Share20\Exception;
use OC\Share20\Manager;
use OC\Share20\Share;
use OCP\EventDispatcher\Event;
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
Expand All @@ -42,16 +43,18 @@
use OCP\IServerContainer;
use OCP\IURLGenerator;
use OCP\IUser;

use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\Security\Events\ValidatePasswordPolicyEvent;
use OCP\Security\IHasher;
use OCP\Security\ISecureRandom;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
use OCP\Share\IShareProvider;
use PHPUnit\Framework\MockObject\MockBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

Expand All @@ -65,37 +68,37 @@ class ManagerTest extends \Test\TestCase {

/** @var Manager */
protected $manager;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
/** @var ILogger|MockObject */
protected $logger;
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
/** @var IConfig|MockObject */
protected $config;
/** @var ISecureRandom|\PHPUnit_Framework_MockObject_MockObject */
/** @var ISecureRandom|MockObject */
protected $secureRandom;
/** @var IHasher|\PHPUnit_Framework_MockObject_MockObject */
/** @var IHasher|MockObject */
protected $hasher;
/** @var IShareProvider|\PHPUnit_Framework_MockObject_MockObject */
/** @var IShareProvider|MockObject */
protected $defaultProvider;
/** @var IMountManager|\PHPUnit_Framework_MockObject_MockObject */
/** @var IMountManager|MockObject */
protected $mountManager;
/** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
/** @var IGroupManager|MockObject */
protected $groupManager;
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
/** @var IL10N|MockObject */
protected $l;
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
/** @var IFactory|MockObject */
protected $l10nFactory;
/** @var DummyFactory */
protected $factory;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
/** @var IUserManager|MockObject */
protected $userManager;
/** @var IRootFolder | \PHPUnit_Framework_MockObject_MockObject */
/** @var IRootFolder | MockObject */
protected $rootFolder;
/** @var EventDispatcherInterface | \PHPUnit_Framework_MockObject_MockObject */
/** @var EventDispatcherInterface | MockObject */
protected $eventDispatcher;
/** @var IMailer|\PHPUnit_Framework_MockObject_MockObject */
/** @var IMailer|MockObject */
protected $mailer;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
/** @var IURLGenerator|MockObject */
protected $urlGenerator;
/** @var \OC_Defaults|\PHPUnit_Framework_MockObject_MockObject */
/** @var \OC_Defaults|MockObject */
protected $defaults;

public function setUp() {
Expand Down Expand Up @@ -146,7 +149,7 @@ public function setUp() {
}

/**
* @return \PHPUnit_Framework_MockObject_MockBuilder
* @return MockBuilder
*/
private function createManagerMock() {
return $this->getMockBuilder('\OC\Share20\Manager')
Expand Down Expand Up @@ -496,11 +499,12 @@ public function testVerifyPasswordHook() {
]));

$this->eventDispatcher->expects($this->once())->method('dispatch')
->willReturnCallback(function($eventName, GenericEvent $event) {
$this->assertSame('OCP\PasswordPolicy::validate', $eventName);
$this->assertSame('password', $event->getSubject());
->willReturnCallback(function(Event $event) {
$this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
/** @var ValidatePasswordPolicyEvent $event */
$this->assertSame('password', $event->getPassword());
}
);
);

$result = self::invokePrivate($this->manager, 'verifyPassword', ['password']);
$this->assertNull($result);
Expand All @@ -516,12 +520,13 @@ public function testVerifyPasswordHookFails() {
]));

$this->eventDispatcher->expects($this->once())->method('dispatch')
->willReturnCallback(function($eventName, GenericEvent $event) {
$this->assertSame('OCP\PasswordPolicy::validate', $eventName);
$this->assertSame('password', $event->getSubject());
->willReturnCallback(function(Event $event) {
$this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
/** @var ValidatePasswordPolicyEvent $event */
$this->assertSame('password', $event->getPassword());
throw new HintException('message', 'password not accepted');
}
);
);

self::invokePrivate($this->manager, 'verifyPassword', ['password']);
}
Expand Down
Loading