prepare route protection

This commit is contained in:
Jakob Ketterl 2020-02-23 19:23:18 +01:00
parent aa9737498a
commit 0a20cb5e41
4 changed files with 40 additions and 0 deletions

View File

@ -21,6 +21,11 @@ class Controller(ABC):
content = content.encode() content = content.encode()
self.handler.wfile.write(content) 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): def handle_request(self):
action = "indexAction" action = "indexAction"
if "action" in self.options: if "action" in self.options:

View File

@ -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")

View File

@ -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")

View File

@ -11,6 +11,8 @@ from owrx.controllers.assets import (
from owrx.controllers.websocket import WebSocketController from owrx.controllers.websocket import WebSocketController
from owrx.controllers.api import ApiController from owrx.controllers.api import ApiController
from owrx.controllers.metrics import MetricsController from owrx.controllers.metrics import MetricsController
from owrx.controllers.settings import SettingsController
from owrx.controllers.session import SessionController
from http.server import BaseHTTPRequestHandler from http.server import BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs from urllib.parse import urlparse, parse_qs
import re import re
@ -91,6 +93,9 @@ class Router(object):
StaticRoute("/features", FeatureController), StaticRoute("/features", FeatureController),
StaticRoute("/api/features", ApiController), StaticRoute("/api/features", ApiController),
StaticRoute("/metrics", MetricsController), StaticRoute("/metrics", MetricsController),
StaticRoute("/settings", SettingsController),
StaticRoute("/login", SessionController, {"action": "loginAction"}),
StaticRoute("/logout", SessionController, {"action": "logoutAction"}),
] ]
def find_route(self, request): def find_route(self, request):