session cookie handling

This commit is contained in:
Jakob Ketterl 2020-02-23 21:52:13 +01:00
parent fb7422e5a8
commit bd8b8ca410
3 changed files with 12 additions and 7 deletions

View File

@ -2,9 +2,6 @@ from .template import WebpageController
from urllib.parse import parse_qs from urllib.parse import parse_qs
from uuid import uuid4 from uuid import uuid4
from http.cookies import SimpleCookie from http.cookies import SimpleCookie
import logging
logger = logging.getLogger(__name__)
class SessionStorage(object): class SessionStorage(object):
@ -43,14 +40,13 @@ class SessionController(WebpageController):
def processLoginAction(self): def processLoginAction(self):
data = parse_qs(self.get_body().decode("utf-8")) data = parse_qs(self.get_body().decode("utf-8"))
data = {k: v[0] for k, v in data.items()} data = {k: v[0] for k, v in data.items()}
logger.debug(data)
if "user" in data and "password" in data: if "user" in data and "password" in data:
# TODO actually check user and password # TODO actually check user and password
if data["user"] == "admin" and data["password"] == "password": if data["user"] == "admin" and data["password"] == "password":
# TODO pass the final destination # TODO pass the final destination
key = SessionStorage.getSharedInstance().startSession({"user": data["user"]}) key = SessionStorage.getSharedInstance().startSession({"user": data["user"]})
cookie = SimpleCookie() cookie = SimpleCookie()
cookie["session"] = key cookie["owrx-session"] = key
self.send_redirect("/settings", cookies=cookie) self.send_redirect("/settings", cookies=cookie)
else: else:
self.send_redirect("/login") self.send_redirect("/login")

View File

@ -1,8 +1,12 @@
from . import Controller from . import Controller
from .session import SessionStorage
class Authentication(object): class Authentication(object):
def isAuthenticated(self, request): def isAuthenticated(self, request):
if "owrx-session" in request.cookies:
session = SessionStorage.getSharedInstance().getSession(request.cookies["owrx-session"].value)
return session is not None
return False return False

View File

@ -17,6 +17,7 @@ from http.server import BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs from urllib.parse import urlparse, parse_qs
import re import re
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from http.cookies import SimpleCookie
import logging import logging
@ -40,11 +41,12 @@ class RequestHandler(BaseHTTPRequestHandler):
class Request(object): class Request(object):
def __init__(self, url, method): def __init__(self, url, method, cookies):
self.path = url.path self.path = url.path
self.query = parse_qs(url.query) self.query = parse_qs(url.query)
self.matches = None self.matches = None
self.method = method self.method = method
self.cookies = cookies
def setMatches(self, matches): def setMatches(self, matches):
self.matches = matches self.matches = matches
@ -111,7 +113,10 @@ class Router(object):
def route(self, handler, method): def route(self, handler, method):
url = urlparse(handler.path) url = urlparse(handler.path)
request = Request(url, method) cookies = SimpleCookie()
if "Cookie" in handler.headers:
cookies.load(handler.headers["Cookie"])
request = Request(url, method, cookies)
route = self.find_route(request) route = self.find_route(request)
if route is not None: if route is not None:
controller = route.controller controller = route.controller