monotreme.org/scripts/logout.cgi
Tristan Smith e5a06090c5 updates
2024-09-23 02:00:19 -04:00

34 lines
No EOL
1,020 B
Python

#!/usr/bin/python3
import os
import sqlite3
import http.cookies
# Set HTTP headers
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')
# Log the logout process for debugging
with open("/tmp/logout_session.log", "a") as f:
f.write(f"Session ID: {session_id.value if session_id else 'None'}\n")
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()
# Output the HTML for the logout page
print("<html><head><title>Logout</title></head>")
print("<body><h1>You have been logged out!</h1>")
print("<a href='/login/'>Login again</a>")
print("</body></html>")