diff options
| author | Dennis Fink | 2026-11-17 21:23:57 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-17 21:23:57 +0100 |
| commit | acd1f42847786963fc4aaeed884c3091700c9e35 (patch) | |
| tree | 5a126bdfd96e69c985137bb2a3db8d9066ecd12d /webmentions_ssg | |
| parent | c4c17e3e046a627a7d2353c734bc69f05d4fc3b1 (diff) | |
| download | webmentions-ssg-acd1f42847786963fc4aaeed884c3091700c9e35.tar.gz webmentions-ssg-acd1f42847786963fc4aaeed884c3091700c9e35.zip | |
feat(core): add user account page
Add an authenticated user page showing the current username and remote
address, including guidance for validating reverse-proxy hop
configuration.
Allow users to change their own password through a CSRF-protected
WTForms form while preserving the active session and invalidating other
existing sessions.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/forms/user.py | 37 | ||||
| -rw-r--r-- | webmentions_ssg/migrations/versions/ef8a9b6c2d41_received_status_history.py | 4 | ||||
| -rw-r--r-- | webmentions_ssg/templates/base.html | 2 | ||||
| -rw-r--r-- | webmentions_ssg/templates/user.html | 35 | ||||
| -rw-r--r-- | webmentions_ssg/views/__init__.py | 12 | ||||
| -rw-r--r-- | webmentions_ssg/views/user.py | 39 |
6 files changed, 122 insertions, 7 deletions
diff --git a/webmentions_ssg/forms/user.py b/webmentions_ssg/forms/user.py index 3bb6421..ae1736d 100644 --- a/webmentions_ssg/forms/user.py +++ b/webmentions_ssg/forms/user.py @@ -2,11 +2,13 @@ # # SPDX-License-Identifier: BSD-3-Clause -"""User authentication forms.""" +"""User authentication and password-change forms.""" from flask_wtf import FlaskForm from wtforms import PasswordField, StringField, SubmitField -from wtforms.validators import InputRequired +from wtforms.validators import EqualTo, InputRequired, ValidationError + +from ..auth import validate_new_password class LoginForm(FlaskForm): @@ -15,3 +17,34 @@ class LoginForm(FlaskForm): username = StringField("Username", validators=[InputRequired()]) password = PasswordField("Password", validators=[InputRequired()]) submit = SubmitField("Sign In") + + +class ChangePasswordForm(FlaskForm): + """Verify the existing password and validate a replacement.""" + + current_password = PasswordField( + "Current password", + validators=[InputRequired()], + render_kw={"autocomplete": "current-password"}, + ) + new_password = PasswordField( + "New password", + validators=[InputRequired()], + render_kw={"autocomplete": "new-password"}, + ) + confirm_password = PasswordField( + "Confirm new password", + validators=[ + InputRequired(), + EqualTo("new_password", message="Passwords must match."), + ], + render_kw={"autocomplete": "new-password"}, + ) + submit = SubmitField("Change password") + + def validate_new_password(self, field: PasswordField) -> None: + """Apply the same password policy as the user-management CLI.""" + try: + validate_new_password(field.data or "") + except ValueError as exc: + raise ValidationError(str(exc)) from exc diff --git a/webmentions_ssg/migrations/versions/ef8a9b6c2d41_received_status_history.py b/webmentions_ssg/migrations/versions/ef8a9b6c2d41_received_status_history.py index db63011..92b8d25 100644 --- a/webmentions_ssg/migrations/versions/ef8a9b6c2d41_received_status_history.py +++ b/webmentions_ssg/migrations/versions/ef8a9b6c2d41_received_status_history.py @@ -40,9 +40,7 @@ def upgrade(): sa.Column("recovery_attempts", sa.Integer(), nullable=False), sa.Column("occurred_at", sa.DateTime(timezone=True), nullable=False), sa.ForeignKeyConstraint( - ["webmention_uuid"], - ["received_webmentions.uuid"], - ondelete="CASCADE", + ["webmention_uuid"], ["received_webmentions.uuid"], ondelete="CASCADE" ), ) op.create_index( diff --git a/webmentions_ssg/templates/base.html b/webmentions_ssg/templates/base.html index c7af077..4fffb38 100644 --- a/webmentions_ssg/templates/base.html +++ b/webmentions_ssg/templates/base.html @@ -49,6 +49,8 @@ SPDX-License-Identifier: BSD-3-Clause href="{{ url_for('webmentions.moderation') }}">Source rules</a> <a class="nav-link{% if request.endpoint == 'webmentions.source_list' %} active{% endif %}" href='{{ url_for("webmentions.source_list") }}'>Sent</a> + <a class="nav-link{% if request.endpoint == 'webmentions.user_page' %} active{% endif %}" + href="{{ url_for('webmentions.user_page') }}">User</a> <form action="{{ url_for('webmentions.logout') }}" method="post" class="m-0"> diff --git a/webmentions_ssg/templates/user.html b/webmentions_ssg/templates/user.html new file mode 100644 index 0000000..ecd5ba9 --- /dev/null +++ b/webmentions_ssg/templates/user.html @@ -0,0 +1,35 @@ +<!-- +SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> + +SPDX-License-Identifier: BSD-3-Clause +--> +{% extends "base.html" %} + +{% set title = "User" %} +{% from "bootstrap5/form.html" import render_form %} + +{% block content %} + <h1 class="mb-4">User</h1> + <section class="mb-4" aria-labelledby="account-heading"> + <h2 class="h4" id="account-heading">Account</h2> + <dl class="row"> + <dt class="col-sm-3">Username</dt> + <dd class="col-sm-9"> + {{ current_user.username }} + </dd> + <dt class="col-sm-3">Remote address</dt> + <dd class="col-sm-9"> + <code>{{ remote_address or "Unavailable" }}</code> + </dd> + </dl> + <p class="text-body-secondary"> + The remote address is the client IP seen by the application for this request. + If it shows your reverse proxy instead, check the + <code>PROXY_FIX_TRUSTED_HOPS</code> configuration. + </p> + </section> + <section aria-labelledby="password-heading"> + <h2 class="h4" id="password-heading">Change password</h2> + <div class="col-12 col-sm-8 col-md-6 col-lg-4">{{ render_form(form) }}</div> + </section> +{% endblock %} diff --git a/webmentions_ssg/views/__init__.py b/webmentions_ssg/views/__init__.py index cdf024a..a4954b6 100644 --- a/webmentions_ssg/views/__init__.py +++ b/webmentions_ssg/views/__init__.py @@ -4,7 +4,15 @@ """Collect and expose the routes for the blueprint.""" -from . import endpoint, moderation, received, session, sources +from . import endpoint, moderation, received, session, sources, user from .blueprint import webmentions_bp -__all__ = ["endpoint", "moderation", "received", "session", "sources", "webmentions_bp"] +__all__ = [ + "endpoint", + "moderation", + "received", + "session", + "sources", + "user", + "webmentions_bp", +] diff --git a/webmentions_ssg/views/user.py b/webmentions_ssg/views/user.py new file mode 100644 index 0000000..4675ade --- /dev/null +++ b/webmentions_ssg/views/user.py @@ -0,0 +1,39 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Authenticated user account page.""" + +from typing import cast + +from flask import flash, redirect, render_template, request, url_for +from flask.typing import ResponseReturnValue +from flask_login import current_user, login_required, login_user + +from ..extensions import db +from ..forms.user import ChangePasswordForm +from ..models.user import User +from .blueprint import webmentions_bp + + +@webmentions_bp.route("/user", methods=["GET", "POST"]) +@login_required +def user_page() -> ResponseReturnValue: + """Display account details and let the signed-in user change their password.""" + form = ChangePasswordForm() + + if form.validate_on_submit(): + user = cast(User, current_user._get_current_object()) + if not user.check_password(form.current_password.data or ""): + form.current_password.errors.append("Current password is incorrect.") + else: + user.password = form.new_password.data or "" + db.session.commit() + + # Password changes revoke all old session identifiers. Refresh this + # session with the new fingerprint so the user stays signed in. + login_user(user) + flash("Password changed successfully.", "success") + return redirect(url_for("webmentions.user_page")) + + return render_template("user.html", form=form, remote_address=request.remote_addr) |
