UniAuth/main.py

103 lines
2.9 KiB
Python
Raw Normal View History

2024-01-21 09:10:07 +00:00
#!/usr/bin/python3
"""Single sign-on application."""
import functools
import typing
from datetime import datetime, timedelta
from typing import Any, Callable
import flask
import werkzeug
import UniAuth.auth
2024-01-21 09:10:07 +00:00
app = flask.Flask(__name__)
app.debug = True
app.config.from_object("config.default")
def login_required(f: Callable[..., Any]) -> Callable[..., Any]:
"""Route requires login decorator."""
@functools.wraps(f)
def decorated_function(*args, **kwargs) -> werkzeug.Response: # type: ignore
token = flask.request.cookies.get("auth_token")
if not token or UniAuth.auth.verify_auth_token(token) is None:
2024-01-21 09:10:07 +00:00
# Save the original URL in the session and redirect to login
flask.session["next"] = flask.request.url
return flask.redirect(flask.url_for("login_page"))
return typing.cast(werkzeug.Response, f(*args, **kwargs))
return decorated_function
@app.route("/")
@login_required
def root_page() -> str:
"""Root page."""
return flask.render_template("auth_good.html")
def check_user_auth() -> dict[str, Any] | None:
"""Load username and password and check if valid."""
# Extract username and password from POST request
username = flask.request.form.get("username", type=str)
password = flask.request.form.get("password", type=str)
# Retrieve users from app config
users = app.config["USERS"]
# Check if the username and password match any user in the list
return next(
(u for u in users if u["username"] == username and u["password"] == password),
None,
)
@app.route("/login", methods=["GET", "POST"])
def login_page() -> str | werkzeug.Response:
"""Login page."""
if flask.request.method == "GET":
return flask.render_template("login.html")
if not (user := check_user_auth()):
# Login failed: Show an error message on the login page
return flask.render_template("login.html", error="Invalid credentials")
redirect_to = (
flask.request.args.get("next")
or flask.session.get("next")
or flask.url_for("dashboard")
)
2024-01-21 09:10:07 +00:00
expire_date = datetime.now() + timedelta(days=180)
2024-01-21 15:24:33 +00:00
flask.flash("Welcome back! You have successfully logged in.")
2024-01-21 09:10:07 +00:00
token = UniAuth.auth.generate_auth_token(user["username"])
response = flask.redirect(redirect_to)
2024-01-21 09:10:07 +00:00
response.set_cookie(
"auth_token",
token,
2024-01-21 09:10:07 +00:00
expires=expire_date,
httponly=True,
secure=True,
samesite="Lax",
)
return response
2024-01-21 15:24:33 +00:00
@app.route("/logout")
def logout() -> werkzeug.Response:
"""Handle user logout by clearing the authentication cookie."""
2024-01-22 12:41:38 +00:00
after_login = flask.request.args.get("next")
response = flask.redirect(flask.url_for("login_page", next=after_login))
2024-01-21 15:24:33 +00:00
response.set_cookie("auth_token", "", expires=0)
flask.flash("You have been successfully logged out.", "info")
return response
2024-01-21 09:10:07 +00:00
if __name__ == "__main__":
app.run(host="0.0.0.0")