From 0a20cb5e41ec1a62755303247d9031cc486a6b37 Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Sun, 23 Feb 2020 19:23:18 +0100 Subject: [PATCH] prepare route protection --- owrx/controllers/__init__.py | 5 +++++ owrx/controllers/session.py | 9 +++++++++ owrx/controllers/settings.py | 21 +++++++++++++++++++++ owrx/http.py | 5 +++++ 4 files changed, 40 insertions(+) create mode 100644 owrx/controllers/session.py create mode 100644 owrx/controllers/settings.py diff --git a/owrx/controllers/__init__.py b/owrx/controllers/__init__.py index ff687ed..99bfad2 100644 --- a/owrx/controllers/__init__.py +++ b/owrx/controllers/__init__.py @@ -21,6 +21,11 @@ class Controller(ABC): content = content.encode() self.handler.wfile.write(content) + def send_redirect(self, location, code=303): + self.handler.send_response(code) + self.handler.send_header("Location", location) + self.handler.end_headers() + def handle_request(self): action = "indexAction" if "action" in self.options: diff --git a/owrx/controllers/session.py b/owrx/controllers/session.py new file mode 100644 index 0000000..1cd9ce4 --- /dev/null +++ b/owrx/controllers/session.py @@ -0,0 +1,9 @@ +from . import Controller + + +class SessionController(Controller): + def loginAction(self): + self.send_response("login happening here") + + def logoutAction(self): + self.send_redirect("logout happening here") diff --git a/owrx/controllers/settings.py b/owrx/controllers/settings.py new file mode 100644 index 0000000..2a8e768 --- /dev/null +++ b/owrx/controllers/settings.py @@ -0,0 +1,21 @@ +from . import Controller + + +class Authentication(object): + def isAuthenticated(self, request): + return False + + +class SettingsController(Controller): + def __init__(self, handler, request, options): + self.authentication = Authentication() + super().__init__(handler, request, options) + + def handle_request(self): + if self.authentication.isAuthenticated(self.request): + super().handle_request() + else: + self.send_redirect("/login") + + def indexAction(self): + self.send_response("actual content here") diff --git a/owrx/http.py b/owrx/http.py index 60e4e8d..a1e26c5 100644 --- a/owrx/http.py +++ b/owrx/http.py @@ -11,6 +11,8 @@ from owrx.controllers.assets import ( from owrx.controllers.websocket import WebSocketController from owrx.controllers.api import ApiController from owrx.controllers.metrics import MetricsController +from owrx.controllers.settings import SettingsController +from owrx.controllers.session import SessionController from http.server import BaseHTTPRequestHandler from urllib.parse import urlparse, parse_qs import re @@ -91,6 +93,9 @@ class Router(object): StaticRoute("/features", FeatureController), StaticRoute("/api/features", ApiController), StaticRoute("/metrics", MetricsController), + StaticRoute("/settings", SettingsController), + StaticRoute("/login", SessionController, {"action": "loginAction"}), + StaticRoute("/logout", SessionController, {"action": "logoutAction"}), ] def find_route(self, request):