Repository: privacyidea/privacyidea

Issue:
Add event handler for /auth endpoint
Currently there is no event handler for the auth endpoint.
We should add an **event** decorator to the auth endpoint in ``api/auth.py``.



Discussion:

Resolving patch:
diff --git a/privacyidea/api/auth.py b/privacyidea/api/auth.py
--- a/privacyidea/api/auth.py
+++ b/privacyidea/api/auth.py
@@ -65,6 +65,7 @@
                                        verify_auth_token, getParam)
 from privacyidea.lib.utils import get_client_ip, hexlify_and_unicode
 from privacyidea.lib.config import get_from_config, SYSCONF, update_config_object
+from privacyidea.lib.event import event, EventConfiguration
 from privacyidea.lib import _
 import logging
 
@@ -85,6 +86,7 @@ def before_request():
                          request.host
     g.policy_object = PolicyClass()
     g.audit_object = getAudit(current_app.config)
+    g.event_config = EventConfiguration()
     # access_route contains the ip adresses of all clients, hops and proxies.
     g.client_ip = get_client_ip(request,
                                 get_from_config(SYSCONF.OVERRIDECLIENT))
@@ -99,6 +101,7 @@ def before_request():
 
 @jwtauth.route('', methods=['POST'])
 @postpolicy(get_webui_settings)
+@event("auth", request, g)
 def get_auth_token():
     """
     This call verifies the credentials of the user and issues an
@@ -207,6 +210,8 @@ def get_auth_token():
     loginname, realm = split_user(username)
     realm = realm or get_default_realm()
 
+    user_obj = User()
+
     # Check if the remote user is allowed
     if (request.remote_user == username) and is_remote_user_allowed(request):
         # Authenticated by the Web Server
@@ -274,6 +279,7 @@ def get_auth_token():
                         details=details or {})
     else:
         g.audit_object.log({"success": True})
+        request.User = user_obj
 
     # If the HSM is not ready, we need to create the nonce in another way!
     hsm = init_hsm()
diff --git a/privacyidea/lib/eventhandler/base.py b/privacyidea/lib/eventhandler/base.py
--- a/privacyidea/lib/eventhandler/base.py
+++ b/privacyidea/lib/eventhandler/base.py
@@ -275,22 +275,24 @@ def events(cls):
 
     @staticmethod
     def _get_tokenowner(request):
-        user = request.User
-        serial = request.all_data.get("serial")
-        if user.is_empty() and serial:
-            # maybe the user is empty, but a serial was passed.
-            # Then we determine the user by the serial
-            try:
-                user = get_token_owner(serial) or User()
-            except Exception as exx:
+        user = User()
+        if hasattr(request, "User"):
+            user = request.User
+            serial = request.all_data.get("serial")
+            if user.is_empty() and serial:
+                # maybe the user is empty, but a serial was passed.
+                # Then we determine the user by the serial
+                try:
+                    user = get_token_owner(serial) or User()
+                except Exception as exx:
+                    user = User()
+                    # This can happen for orphaned tokens.
+                    log.info("Could not determine tokenowner for {0!s}. Maybe the "
+                             "user does not exist anymore.".format(serial))
+                    log.debug(exx)
+            # If the user does not exist, we set an empty user
+            if not user.exist():
                 user = User()
-                # This can happen for orphaned tokens.
-                log.info("Could not determine tokenowner for {0!s}. Maybe the "
-                         "user does not exist anymore.".format(serial))
-                log.debug(exx)
-        # If the user does not exist, we set an empty user
-        if not user.exist():
-            user = User()
 
         return user
 


Test patch: