Repository: privacyidea/privacyidea

Issue:
Post Request don't have a ThreadID
 Hello

In the Audit Log there is a ThreadID only for GET Requests, but not for POST Requests.

Example of a Post request:
http --verify no POST https://sever_IP/validate/check user=hannes3 pass=PIN_AND_TOKEN
 No ThreadID in the audit log.

Thank you.

Sebastien



Discussion:
I can confirm this, the `thread_id` is missing for all `validate` endpoints:
https://github.com/privacyidea/privacyidea/blob/49f64d01b2bffeacabeac721a405d46d9a62725d/privacyidea/api/validate.py#L146
Right, if we have a before-all we should put it here!
@plettich please describe what @NNLemling should do.
So basically each [Flask Blueprint](https://flask.palletsprojects.com/en/1.1.x/blueprints/#blueprints) (an API endpoint) can have a function which runs before the actual request is run. These functions are registered with the [`before_request`](https://flask.palletsprojects.com/en/1.1.x/api/#flask.Blueprint.before_request) decorator.
We currently have four main `before_request` functions:
https://github.com/privacyidea/privacyidea/blob/bffc54f23d1444706bbb343101b283127954be6e/privacyidea/api/before_after.py#L150
https://github.com/privacyidea/privacyidea/blob/bffc54f23d1444706bbb343101b283127954be6e/privacyidea/api/auth.py#L88
https://github.com/privacyidea/privacyidea/blob/bffc54f23d1444706bbb343101b283127954be6e/privacyidea/api/validate.py#L125
https://github.com/privacyidea/privacyidea/blob/bffc54f23d1444706bbb343101b283127954be6e/privacyidea/api/ttype.py#L57

We should create a single `before_request` function (or reuse the [`before_app_request` handler](https://github.com/privacyidea/privacyidea/blob/bffc54f23d1444706bbb343101b283127954be6e/privacyidea/api/before_after.py#L87)) which contains all the common functionality like:
- create and initialize the audit object
- create (and initialize) other common objects (policies, events, client ip, ...)
- fetch the parameters
- ...


Resolving patch:
diff --git a/privacyidea/api/ttype.py b/privacyidea/api/ttype.py
--- a/privacyidea/api/ttype.py
+++ b/privacyidea/api/ttype.py
@@ -31,6 +31,8 @@
 The TiQR Token uses this API to implement its special functionalities. See
 :ref:`code_tiqr_token`.
 """
+import threading
+
 from flask import (Blueprint,
                    request)
 from .lib.utils import getParam
@@ -77,6 +79,7 @@ def before_request():
                         "client_user_agent": request.user_agent.browser,
                         "privacyidea_server": privacyidea_server,
                         "action": "{0!s} {1!s}".format(request.method, request.url_rule),
+                        "thread_id": "{0!s}".format(threading.current_thread().ident),
                         "info": ""})
 
 
diff --git a/privacyidea/api/validate.py b/privacyidea/api/validate.py
--- a/privacyidea/api/validate.py
+++ b/privacyidea/api/validate.py
@@ -66,6 +66,9 @@
  * :func:`privacyidea.lib.tokenclass.TokenClass.check_otp`
 
 """
+
+import threading
+
 from flask import (Blueprint, request, g, current_app)
 from privacyidea.lib.user import get_user_from_param, log_used_user
 from .lib.utils import send_result, getParam
@@ -150,6 +153,7 @@ def before_request():
                         "client_user_agent": request.user_agent.browser,
                         "privacyidea_server": privacyidea_server,
                         "action": "{0!s} {1!s}".format(request.method, request.url_rule),
+                        "thread_id": "{0!s}".format(threading.current_thread().ident),
                         "info": ""})
 
 


Test patch: