From c7885fb30dea0f60c6fd12e940841e8b0698aade Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Tue, 19 Nov 2019 19:16:56 +0100 Subject: [PATCH 1/2] Use typed events Signed-off-by: Christoph Wurst --- README.md | 7 ++++--- lib/AppInfo/Application.php | 28 +++++++++++++++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 888a2ca5..79a609f5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,8 @@ Currently the app checks passwords for public link shares and for user passwords You can easily check passwords for your own app by adding following code to your app: ```` -$eventDispatcher = \OC::$server->getEventDispatcher(); -$event = new Symfony\Component\EventDispatcher\GenericEvent($password); -$eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); +$eventDispatcher = \OC::$server->query(IEventDispatcher::class); +$event = new \OCP\Security\Events\GenerateSecurePasswordEvent(); +$eventDispatcher->dispatchTyped($event); +$password = $event->getPassword() ?? 'fallback when this app is not enabled'; ```` diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index b56cf873..f4033a40 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -28,6 +28,10 @@ use OCA\Password_Policy\Generator; use OCA\Password_Policy\PasswordValidator; use OCP\AppFramework\App; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Security\Events\GenerateSecurePasswordEvent; +use OCP\Security\Events\ValidatePasswordPolicyEvent; use Symfony\Component\EventDispatcher\GenericEvent; class Application extends App { @@ -36,24 +40,34 @@ public function __construct() { $container = $this->getContainer(); $server = $container->getServer(); - $eventDispatcher = $server->getEventDispatcher(); + /** @var IEventDispatcher $eventDispatcher */ + $eventDispatcher = $server->query(IEventDispatcher::class); /** register capabilities */ $container->registerCapability(Capabilities::class); + $eventDispatcher->addListener( + ValidatePasswordPolicyEvent::class, + function (Event $event) use ($container) { + if (!($event instanceof ValidatePasswordPolicyEvent)) { + return; + } - $eventDispatcher->addListener('OCP\PasswordPolicy::validate', - function(GenericEvent $event) use ($container) { /** @var PasswordValidator $validator */ $validator = $container->query(PasswordValidator::class); - $validator->validate($event->getSubject()); + $validator->validate($event->getPassword()); } ); - $eventDispatcher->addListener('OCP\PasswordPolicy::generate', - function(GenericEvent $event) use ($container) { + $eventDispatcher->addListener( + GenerateSecurePasswordEvent::class, + function (Event $event) use ($container) { + if (!($event instanceof GenerateSecurePasswordEvent)) { + return; + } + /** @var Generator */ $generator = $container->query(Generator::class); - $event->setArgument('password', $generator->generate()); + $event->setPassword($generator->generate()); } ); } From 8eaa09061c37e913405545527870d77f0e754639 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Wed, 27 Nov 2019 10:45:30 +0100 Subject: [PATCH 2/2] Listen for legacy events as well Signed-off-by: Christoph Wurst --- lib/AppInfo/Application.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index f4033a40..728f4a7c 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -30,6 +30,7 @@ use OCP\AppFramework\App; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; +use OCP\ILogger; use OCP\Security\Events\GenerateSecurePasswordEvent; use OCP\Security\Events\ValidatePasswordPolicyEvent; use Symfony\Component\EventDispatcher\GenericEvent; @@ -70,5 +71,32 @@ function (Event $event) use ($container) { $event->setPassword($generator->generate()); } ); + + // TODO: remove these two legacy event listeners + $symfonyDispatcher = $server->getEventDispatcher(); + $symfonyDispatcher->addListener( + 'OCP\PasswordPolicy::validate', + function (GenericEvent $event) use ($container) { + /** @var ILogger $logger */ + $logger = $container->query(ILogger::class); + $logger->debug('OCP\PasswordPolicy::validate is deprecated. Listen to ' . ValidatePasswordPolicyEvent::class . ' instead'); + + /** @var PasswordValidator $validator */ + $validator = $container->query(PasswordValidator::class); + $validator->validate($event->getSubject()); + } + ); + $symfonyDispatcher->addListener( + 'OCP\PasswordPolicy::generate', + function (GenericEvent $event) use ($container) { + /** @var ILogger $logger */ + $logger = $container->query(ILogger::class); + $logger->debug('OCP\PasswordPolicy::generate is deprecated. Listen to ' . GenerateSecurePasswordEvent::class . ' instead'); + + /** @var Generator */ + $generator = $container->query(Generator::class); + $event->setArgument('password', $generator->generate()); + } + ); } }