diff options
| author | Jonathan Weth <git@jonathanweth.de> | 2021-06-23 12:23:41 +0200 |
|---|---|---|
| committer | Jonathan Weth <git@jonathanweth.de> | 2021-06-23 12:23:41 +0200 |
| commit | 74e02d1953c1ee03a4e7dfc73e80318a24ba56a7 (patch) | |
| tree | c204dcc7794b05641b1dd6910b649a754169184d /trigger.py | |
| parent | 0a6543a5bea6838d6e4d80eaa83046cc6576b086 (diff) | |
| download | RWA.Support.SessionService-74e02d1953c1ee03a4e7dfc73e80318a24ba56a7.tar.gz RWA.Support.SessionService-74e02d1953c1ee03a4e7dfc73e80318a24ba56a7.tar.bz2 RWA.Support.SessionService-74e02d1953c1ee03a4e7dfc73e80318a24ba56a7.zip | |
Generate PIN in this service and provide authentication mechanisms
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() |
