diff options
| author | Dennis Fink | 2026-11-12 19:57:08 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-12 19:57:08 +0100 |
| commit | 4bd1d9e348918b2e55db47a1ac3489d035cf55e2 (patch) | |
| tree | f7ed41a374a8a4136792039a262dc62aa8cf8c59 /webmentions_ssg/views/session.py | |
| parent | 7035e9f9467684cccae2b7ff3faaa62e6a9f91aa (diff) | |
| download | webmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.tar.gz webmentions-ssg-4bd1d9e348918b2e55db47a1ac3489d035cf55e2.zip | |
refactor(core): split modules by responsibility
Break up monolithic views, task helpers, networking, CLI, and
application extension setup so each module owns a focused concern.
Reorganize the test suite around the new boundaries while preserving
existing Webmention behavior and coverage.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/views/session.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/webmentions_ssg/views/session.py b/webmentions_ssg/views/session.py new file mode 100644 index 0000000..7bd8a10 --- /dev/null +++ b/webmentions_ssg/views/session.py @@ -0,0 +1,93 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Authentication and session routes.""" + +import sqlalchemy as sa +from flask import current_app, flash, redirect, render_template, request, url_for +from flask.typing import ResponseReturnValue +from flask_login import current_user, login_required, login_user, logout_user + +from .. import forms +from ..auth import DUMMY_USER, is_safe_next_url, login_username_limit_key +from ..extensions import DATABASE as db +from ..extensions import LIMITER +from ..models import User +from . import root_page + + +@root_page.route("/") +def index() -> ResponseReturnValue: + """ + Redirect to the received Webmentions view. + + :return: Redirect response to the received Webmentions view. + """ + return redirect(url_for("root.received")) + + +@root_page.route("/login", methods=["GET", "POST"]) +@LIMITER.limit( + lambda: current_app.config["LOGIN_USERNAME_RATE_LIMIT"], + key_func=login_username_limit_key, + methods=["POST"], + error_message="Too many login attempts. Please try again later.", +) +@LIMITER.limit( + lambda: current_app.config["LOGIN_RATE_LIMIT"], + methods=["POST"], + error_message="Too many login attempts. Please try again later.", +) +def login() -> ResponseReturnValue: + """ + Authenticate a user and start a login session. + + Authenticated users are redirected to the application index. After a + successful login, the user is redirected to the requested local URL when + provided. + + :return: Rendered login page or redirect response. + """ + if current_user.is_authenticated: + return redirect(url_for("root.index")) + + form = forms.LoginForm() + + if form.validate_on_submit(): + user = db.session.scalar( + sa.select(User).where(User.username == form.username.data) + ) + + user_exists = user is not None + + if user is None: + user = DUMMY_USER + + if ( + form.password.data is None + or not user.check_password(form.password.data) + or not user_exists + ): + flash("Invalid username or password", "danger") + return redirect(url_for("root.login")) + login_user(user) + + next_url = request.args.get("next") + if not next_url or not is_safe_next_url(next_url): + next_url = url_for("root.index") + return redirect(next_url) + + return render_template("login.html", form=form) + + +@root_page.route("/logout", methods=("POST",)) +@login_required +def logout() -> ResponseReturnValue: + """ + End the current user's login session. + + :return: Redirect response to the application index. + """ + logout_user() + return redirect(url_for("root.index")) |
