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..728f4a7c 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -28,6 +28,11 @@ use OCA\Password_Policy\Generator; use OCA\Password_Policy\PasswordValidator; 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; class Application extends App { @@ -36,21 +41,58 @@ 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; + } + + /** @var PasswordValidator $validator */ + $validator = $container->query(PasswordValidator::class); + $validator->validate($event->getPassword()); + } + ); + $eventDispatcher->addListener( + GenerateSecurePasswordEvent::class, + function (Event $event) use ($container) { + if (!($event instanceof GenerateSecurePasswordEvent)) { + return; + } + + /** @var Generator */ + $generator = $container->query(Generator::class); + $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'); - $eventDispatcher->addListener('OCP\PasswordPolicy::validate', - function(GenericEvent $event) use ($container) { /** @var PasswordValidator $validator */ $validator = $container->query(PasswordValidator::class); $validator->validate($event->getSubject()); } ); - $eventDispatcher->addListener('OCP\PasswordPolicy::generate', - function(GenericEvent $event) use ($container) { + $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());