diff options
Diffstat (limited to 'trigger.py')
-rw-r--r-- | trigger.py | 35 |
1 files changed, 27 insertions, 8 deletions
@@ -24,17 +24,17 @@ # along with this program. If not, see <https://www.gnu.org/licenses/>. import threading -from typing import Any, Callable +from typing import Any, Callable, Optional, Union from wsgiref.simple_server import make_server import port_for -from flask import Flask, abort, request +from flask import Flask, abort, request, jsonify class TriggerServerThread(threading.Thread): """Simple Flask server (wrapped as thread) for triggering actions on sessions.""" - def __init__(self, trigger_method: Callable[[str], Any]): + def __init__(self, trigger_method: Callable[[int, dict, Optional[str]], Union[dict, bool]]): super().__init__() self.port = port_for.select_random() @@ -44,11 +44,30 @@ class TriggerServerThread(threading.Thread): def trigger(): json = request.json token = json.get("token", "") - r = trigger_method(token) - if r: - return "Successful triggered" - else: - return abort(403) + try: + session_id = int(json.get("session_id")) + r = trigger_method(session_id, {"token": token}, "trigger") + if r: + return "Successful triggered" + else: + return abort(403) + except (ValueError, TypeError): + return abort(404) + + @app.route("/authenticate/", methods=["POST"]) + def authenticate(): + json = request.json + try: + session_id = int(json.get("session_id")) + pin = int(json.get("pin", "")) + r = trigger_method(session_id, {"pin": pin}, "authenticate") + if r: + return jsonify(r) + else: + return abort(403) + except (ValueError, TypeError): + return abort(404) + self.srv = make_server("0.0.0.0", self.port, app) self.ctx = app.app_context() |