From d2e62585c00da5ed603ffee7b72852eaf692e85e Mon Sep 17 00:00:00 2001 From: Tristan Smith Date: Sun, 22 Sep 2024 17:37:04 -0400 Subject: [PATCH] added scripts --- login/index.html | 2 +- scripts/logout.cgi | 25 +++++++++++++++++++++++++ scripts/user_panel.cgi | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 scripts/logout.cgi create mode 100644 scripts/user_panel.cgi diff --git a/login/index.html b/login/index.html index 935310e..4c22edd 100644 --- a/login/index.html +++ b/login/index.html @@ -43,7 +43,7 @@

Login to monotreme.org

-
+
diff --git a/scripts/logout.cgi b/scripts/logout.cgi new file mode 100644 index 0000000..1e36b0f --- /dev/null +++ b/scripts/logout.cgi @@ -0,0 +1,25 @@ +# logout.cgi + +import sqlite3 +import os +import http.cookies + +print("Content-Type: text/html") +print("Set-Cookie: session_id=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT") +print() + +# Get the session ID from the cookie +cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) +session_id = cookie.get('session_id') + +if session_id: + session_id = session_id.value + # Connect to SQLite and remove the session + db = sqlite3.connect('/var/lib/monotreme/data/monotreme.db') + cursor = db.cursor() + cursor.execute("DELETE FROM sessions WHERE session_id=?", (session_id,)) + db.commit() + db.close() + +print("

You have been logged out!

") +print("Login again") diff --git a/scripts/user_panel.cgi b/scripts/user_panel.cgi new file mode 100644 index 0000000..7fa5220 --- /dev/null +++ b/scripts/user_panel.cgi @@ -0,0 +1,32 @@ +import sqlite3 +import os +import http.cookies +import time + +print("Content-Type: text/html") +print() + +# Get the session ID from the cookie +cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) +session_id = cookie.get('session_id') + +if session_id: + session_id = session_id.value + # Connect to SQLite and check the session + db = sqlite3.connect('/var/lib/monotreme/data/monotreme.db') + cursor = db.cursor() + + # Check if the session exists and is still valid + cursor.execute("SELECT username FROM sessions WHERE session_id=? AND expires_at > ?", (session_id, int(time.time()))) + result = cursor.fetchone() + + if result: + username = result[0] + print(f"

Welcome, {username}!

") + print("

This is your user panel.

") + else: + print("

Session expired or invalid!

") + print("Login again") +else: + print("

No session found!

") + print("Login again")