Add PoC events to user authentication
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 17 Feb 2021 10:25:46 +0000 (11:25 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 7 Jun 2021 11:51:05 +0000 (13:51 +0200)
wcfsetup/install/files/lib/acp/form/LoginForm.class.php
wcfsetup/install/files/lib/action/FacebookAuthAction.class.php
wcfsetup/install/files/lib/action/GithubAuthAction.class.php
wcfsetup/install/files/lib/action/GoogleAuthAction.class.php
wcfsetup/install/files/lib/action/TwitterAuthAction.class.php
wcfsetup/install/files/lib/form/LoginForm.class.php
wcfsetup/install/files/lib/form/MultifactorAuthenticationForm.class.php
wcfsetup/install/files/lib/system/user/authentication/UserLoggedIn.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/user/authentication/UserMultifactorRequested.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/user/authentication/UserMultifactorSucceeded.class.php [new file with mode: 0644]

index 7d36887dec0a647defbb639091303d7e5a89e352..ed038fb849d2804e3b9cf8368c31e6ac8497e6d4 100755 (executable)
@@ -8,6 +8,7 @@ use wcf\data\user\User;
 use wcf\data\user\UserProfile;
 use wcf\form\AbstractCaptchaForm;
 use wcf\system\application\ApplicationHandler;
+use wcf\system\event\EventHandler;
 use wcf\system\exception\NamedUserException;
 use wcf\system\exception\UserInputException;
 use wcf\system\request\LinkHandler;
@@ -15,6 +16,8 @@ use wcf\system\request\RequestHandler;
 use wcf\system\request\RouteHandler;
 use wcf\system\user\authentication\EmailUserAuthentication;
 use wcf\system\user\authentication\UserAuthenticationFactory;
+use wcf\system\user\authentication\UserLoggedIn;
+use wcf\system\user\authentication\UserMultifactorRequested;
 use wcf\system\WCF;
 use wcf\util\HeaderUtil;
 use wcf\util\StringUtil;
@@ -224,6 +227,12 @@ class LoginForm extends AbstractCaptchaForm
 
         // change user
         $needsMultifactor = WCF::getSession()->changeUserAfterMultifactorAuthentication($this->user);
+        if ($needsMultifactor) {
+            EventHandler::getInstance()->fire(new UserMultifactorRequested($this->user));
+        } else {
+            EventHandler::getInstance()->fire(new UserLoggedIn($this->user));
+        }
+
         if (!$needsMultifactor) {
             WCF::getSession()->registerReauthentication();
         }
index 901a855584592b3ba77a398f9413c74f9ad76bea..e038193efc169b1aa8be04c0f378b80ea511f143 100644 (file)
@@ -5,9 +5,11 @@ namespace wcf\action;
 use GuzzleHttp\Psr7\Request;
 use wcf\data\user\User;
 use wcf\form\RegisterForm;
+use wcf\system\event\EventHandler;
 use wcf\system\exception\NamedUserException;
 use wcf\system\request\LinkHandler;
 use wcf\system\user\authentication\oauth\User as OauthUser;
+use wcf\system\user\authentication\UserLoggedIn;
 use wcf\system\WCF;
 use wcf\util\HeaderUtil;
 use wcf\util\JSON;
@@ -132,6 +134,7 @@ final class FacebookAuthAction extends AbstractOauth2Action
                 // Perform the login.
 
                 WCF::getSession()->changeUser($user);
+                EventHandler::getInstance()->fire(new UserLoggedIn($user));
                 WCF::getSession()->update();
                 HeaderUtil::redirect(LinkHandler::getInstance()->getLink());
 
index a8a034e3b1b4224768bf0b6bd422b7c103d3c40d..98e5de3657085fb175c212889008cc0a902863a6 100644 (file)
@@ -6,9 +6,11 @@ use GuzzleHttp\Psr7\Request;
 use Psr\Http\Client\ClientExceptionInterface;
 use wcf\data\user\User;
 use wcf\form\RegisterForm;
+use wcf\system\event\EventHandler;
 use wcf\system\exception\NamedUserException;
 use wcf\system\request\LinkHandler;
 use wcf\system\user\authentication\oauth\User as OauthUser;
+use wcf\system\user\authentication\UserLoggedIn;
 use wcf\system\WCF;
 use wcf\util\HeaderUtil;
 use wcf\util\JSON;
@@ -124,6 +126,7 @@ final class GithubAuthAction extends AbstractOauth2Action
                 // Perform the login.
 
                 WCF::getSession()->changeUser($user);
+                EventHandler::getInstance()->fire(new UserLoggedIn($user));
                 WCF::getSession()->update();
                 HeaderUtil::redirect(LinkHandler::getInstance()->getLink());
 
index 078e9b64259bee7b1af328f28e34af8fcfdde2d0..5cab421cfbe1df84f5d518337cc5c5aa2b38bfc1 100644 (file)
@@ -5,9 +5,11 @@ namespace wcf\action;
 use GuzzleHttp\Psr7\Request;
 use wcf\data\user\User;
 use wcf\form\RegisterForm;
+use wcf\system\event\EventHandler;
 use wcf\system\exception\NamedUserException;
 use wcf\system\request\LinkHandler;
 use wcf\system\user\authentication\oauth\User as OauthUser;
+use wcf\system\user\authentication\UserLoggedIn;
 use wcf\system\WCF;
 use wcf\util\HeaderUtil;
 use wcf\util\JSON;
@@ -146,6 +148,7 @@ final class GoogleAuthAction extends AbstractOauth2Action
                 // Perform the login.
 
                 WCF::getSession()->changeUser($user);
+                EventHandler::getInstance()->fire(new UserLoggedIn($user));
                 WCF::getSession()->update();
                 HeaderUtil::redirect(LinkHandler::getInstance()->getLink());
 
index 447450027ab28fdfa9c194bcfc98e907195fd761..efb7ff09762af9d6ff285cee5eb0eaf262c76d34 100644 (file)
@@ -4,11 +4,13 @@ namespace wcf\action;
 
 use ParagonIE\ConstantTime\Hex;
 use wcf\data\user\User;
+use wcf\system\event\EventHandler;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\NamedUserException;
 use wcf\system\exception\PermissionDeniedException;
 use wcf\system\exception\SystemException;
 use wcf\system\request\LinkHandler;
+use wcf\system\user\authentication\UserLoggedIn;
 use wcf\system\WCF;
 use wcf\util\HeaderUtil;
 use wcf\util\HTTPRequest;
@@ -104,6 +106,7 @@ class TwitterAuthAction extends AbstractAction
                 } // perform login
                 else {
                     WCF::getSession()->changeUser($user);
+                    EventHandler::getInstance()->fire(new UserLoggedIn($user));
                     WCF::getSession()->update();
                     HeaderUtil::redirect(LinkHandler::getInstance()->getLink());
                 }
index 95fadd5b494cc89271b73b5053dc494d844b054b..9f7c0286401f030346a78703ece34a316be97190 100644 (file)
@@ -2,7 +2,10 @@
 
 namespace wcf\form;
 
+use wcf\system\event\EventHandler;
 use wcf\system\request\LinkHandler;
+use wcf\system\user\authentication\UserLoggedIn;
+use wcf\system\user\authentication\UserMultifactorRequested;
 use wcf\system\WCF;
 
 /**
@@ -31,6 +34,12 @@ class LoginForm extends \wcf\acp\form\LoginForm
         // change user
         $needsMultifactor = WCF::getSession()->changeUserAfterMultifactorAuthentication($this->user);
 
+        if ($needsMultifactor) {
+            EventHandler::getInstance()->fire(new UserMultifactorRequested($this->user));
+        } else {
+            EventHandler::getInstance()->fire(new UserLoggedIn($this->user));
+        }
+
         $this->saved();
 
         // redirect to url
index ffeb52fef6cfe93d599abc81264964b44c1fbd3e..aa9bc2c8931c61ac521e1a4fc6a07f986e9cc399 100644 (file)
@@ -6,10 +6,13 @@ use wcf\data\object\type\ObjectType;
 use wcf\data\user\User;
 use wcf\system\application\ApplicationHandler;
 use wcf\system\cache\runtime\UserProfileRuntimeCache;
+use wcf\system\event\EventHandler;
 use wcf\system\exception\IllegalLinkException;
 use wcf\system\exception\NamedUserException;
 use wcf\system\form\builder\TemplateFormNode;
 use wcf\system\request\LinkHandler;
+use wcf\system\user\authentication\UserLoggedIn;
+use wcf\system\user\authentication\UserMultifactorSucceeded;
 use wcf\system\user\multifactor\IMultifactorMethod;
 use wcf\system\user\multifactor\Setup;
 use wcf\system\WCF;
@@ -144,6 +147,8 @@ class MultifactorAuthenticationForm extends AbstractFormBuilderForm
         WCF::getDB()->commitTransaction();
 
         WCF::getSession()->applyPendingUserChange($this->user);
+        EventHandler::getInstance()->fire(new UserLoggedIn($this->user));
+        EventHandler::getInstance()->fire(new UserMultifactorSucceeded($this->user, $setup));
         WCF::getSession()->registerReauthentication();
 
         $this->saved();
diff --git a/wcfsetup/install/files/lib/system/user/authentication/UserLoggedIn.class.php b/wcfsetup/install/files/lib/system/user/authentication/UserLoggedIn.class.php
new file mode 100644 (file)
index 0000000..1f76a76
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+
+namespace wcf\system\user\authentication;
+
+use wcf\data\user\User;
+use wcf\system\event\IEvent;
+
+/**
+ * Indicates that the user logged in.
+ *
+ * Differs from SessionHandler::changeUser() in that it is fired for active logins only and not for user changes
+ * that are required for technical reasons.
+ *
+ * @author  Tim Duesterhus
+ * @copyright   2001-2021 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\User\Authentication
+ * @since   5.5
+ */
+final class UserLoggedIn implements IEvent
+{
+    /**
+     * @var User
+     */
+    private $user;
+
+    public function __construct(User $user)
+    {
+        $this->user = $user;
+    }
+
+    public function getUser(): User
+    {
+        return $this->user;
+    }
+}
diff --git a/wcfsetup/install/files/lib/system/user/authentication/UserMultifactorRequested.class.php b/wcfsetup/install/files/lib/system/user/authentication/UserMultifactorRequested.class.php
new file mode 100644 (file)
index 0000000..a4051b3
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace wcf\system\user\authentication;
+
+use wcf\data\user\User;
+use wcf\system\event\IEvent;
+
+/**
+ * Indicates that the user entered their password successfully and needs to perform MF authentication.
+ *
+ * @author  Tim Duesterhus
+ * @copyright   2001-2021 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\User\Authentication
+ * @since   5.5
+ */
+final class UserMultifactorRequested implements IEvent
+{
+    /**
+     * @var User
+     */
+    private $user;
+
+    public function __construct(User $user)
+    {
+        $this->user = $user;
+    }
+
+    public function getUser(): User
+    {
+        return $this->user;
+    }
+}
diff --git a/wcfsetup/install/files/lib/system/user/authentication/UserMultifactorSucceeded.class.php b/wcfsetup/install/files/lib/system/user/authentication/UserMultifactorSucceeded.class.php
new file mode 100644 (file)
index 0000000..82fe5d4
--- /dev/null
@@ -0,0 +1,45 @@
+<?php
+
+namespace wcf\system\user\authentication;
+
+use wcf\data\user\User;
+use wcf\system\event\IEvent;
+use wcf\system\user\multifactor\Setup;
+
+/**
+ * Indicates that the user successfully performed MF authentication with the given setup ("method").
+ *
+ * @author  Tim Duesterhus
+ * @copyright   2001-2021 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\User\Authentication
+ * @since   5.5
+ */
+final class UserMultifactorSucceeded implements IEvent
+{
+    /**
+     * @var User
+     */
+    private $user;
+
+    /**
+     * @var Setup
+     */
+    private $setup;
+
+    public function __construct(User $user, Setup $setup)
+    {
+        $this->user = $user;
+        $this->setup = $setup;
+    }
+
+    public function getUser(): User
+    {
+        return $this->user;
+    }
+
+    public function getSetup(): Setup
+    {
+        return $this->setup;
+    }
+}