diff options
Diffstat (limited to 'webmentions_ssg/forms/user.py')
| -rw-r--r-- | webmentions_ssg/forms/user.py | 37 |
1 files changed, 35 insertions, 2 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 |
