aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/forms/user.py
blob: ae1736de928d3818bca2cc95a0a162b9715a76ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

"""User authentication and password-change forms."""

from flask_wtf import FlaskForm
from wtforms import PasswordField, StringField, SubmitField
from wtforms.validators import EqualTo, InputRequired, ValidationError

from ..auth import validate_new_password


class LoginForm(FlaskForm):
    """Form for authenticating a user."""

    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