| 1 |
""" |
| 2 |
flask_security.change_username |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Flask-Security Change Username module |
| 6 |
|
| 7 |
:copyright: (c) 2025-2025 by J. Christopher Wagner (jwag). |
| 8 |
:license: MIT, see LICENSE for more details. |
| 9 |
|
| 10 |
Allow user to change their username. |
| 11 |
This is really just for when username is used as an authenticating |
| 12 |
identity (and therefore has to be unique). |
| 13 |
|
| 14 |
The basic feature allows an authenticated user to change their username |
| 15 |
to any available username. Normalization and validation take place |
| 16 |
using username_util. |
| 17 |
This doesn't offer any defense against username enumeration by another user or |
| 18 |
preventing a user from constantly changing their username. Applications |
| 19 |
could use the UsernameUtil.check_username() method to implement this. |
| 20 |
|
| 21 |
Think about: username is normally considered 'public' however as an identity |
| 22 |
should we follow a change protocol more like email - require a confirmation from |
| 23 |
the registered email. |
| 24 |
|
| 25 |
""" |
| 26 |
|
| 27 |
from __future__ import annotations |
| 28 |
|
| 29 |
import typing as t |
| 30 |
|
| 31 |
from flask import after_this_request, request |
| 32 |
from flask import current_app |
| 33 |
from flask_login import current_user |
| 34 |
from wtforms import Field, SubmitField |
| 35 |
|
| 36 |
from .decorators import auth_required |
| 37 |
from .forms import ( |
| 38 |
Form, |
| 39 |
_build_form_from_request, |
| 40 |
_get_form_field_label, |
| 41 |
) |
| 42 |
from .proxies import _security, _datastore |
| 43 |
from .quart_compat import get_quart_status |
| 44 |
from .signals import username_changed |
| 45 |
from .utils import ( |
| 46 |
base_render_json, |
| 47 |
_config_value as cv, |
| 48 |
do_flash, |
| 49 |
get_message, |
| 50 |
get_url, |
| 51 |
send_mail, |
| 52 |
_view_commit, |
| 53 |
) |
| 54 |
|
| 55 |
if t.TYPE_CHECKING: # pragma: no cover |
| 56 |
from flask.typing import ResponseValue |
| 57 |
|
| 58 |
if get_quart_status(): # pragma: no cover |
| 59 |
from quart import redirect |
| 60 |
else: |
| 61 |
from flask import redirect |
| 62 |
|
| 63 |
|
| 64 |
class ChangeUsernameForm(Form): |
| 65 |
"""Change Username Form. |
| 66 |
There is a single element - 'username' |
| 67 |
that will be validated by calling :meth:`.UsernameUtil.validate` |
| 68 |
then verified to be unique in the DB. |
| 69 |
|
| 70 |
username field value injected at init_app time with build_username_field() |
| 71 |
""" |
| 72 |
|
| 73 |
username: t.ClassVar[Field] |
| 74 |
submit: SubmitField = SubmitField(label=_get_form_field_label("submit")) |
| 75 |
|
| 76 |
def __init__(self, *args: t.Any, **kwargs: t.Any): |
| 77 |
super().__init__(*args, **kwargs) |
| 78 |
|
| 79 |
|
| 80 |
@auth_required( |
| 81 |
lambda: cv("API_ENABLED_METHODS"), |
| 82 |
within=lambda: cv("FRESHNESS"), |
| 83 |
grace=lambda: cv("FRESHNESS_GRACE_PERIOD"), |
| 84 |
) |
| 85 |
def change_username() -> ResponseValue: |
| 86 |
"""Start Change Username for an existing authenticated user""" |
| 87 |
payload: dict[str, t.Any] |
| 88 |
|
| 89 |
form: ChangeUsernameForm = t.cast( |
| 90 |
ChangeUsernameForm, _build_form_from_request("change_username_form") |
| 91 |
) |
| 92 |
|
| 93 |
if form.validate_on_submit(): |
| 94 |
# simple - just change username |
| 95 |
form.user = current_user |
| 96 |
after_this_request(_view_commit) |
| 97 |
update_username(form.user, form.username.data) |
| 98 |
if _security._want_json(request): |
| 99 |
return base_render_json(form) |
| 100 |
|
| 101 |
do_flash(*get_message("USERNAME_CHANGE")) |
| 102 |
return redirect( |
| 103 |
get_url(cv("POST_CHANGE_USERNAME_VIEW")) or get_url(cv("POST_LOGIN_VIEW")) |
| 104 |
) |
| 105 |
|
| 106 |
if _security._want_json(request): |
| 107 |
form.user = current_user |
| 108 |
payload = dict(current_username=current_user.username) |
| 109 |
return base_render_json(form, additional=payload) |
| 110 |
|
| 111 |
return _security.render_template( |
| 112 |
cv("CHANGE_USERNAME_TEMPLATE"), |
| 113 |
change_username_form=form, |
| 114 |
current_username=current_user.username, |
| 115 |
**_security._run_ctx_processor("change_username"), |
| 116 |
) |
| 117 |
|
| 118 |
|
| 119 |
def update_username(user, new_username): |
| 120 |
old_username = user.username |
| 121 |
user.username = new_username |
| 122 |
_datastore.put(user) |
| 123 |
_send_username_changed_notice(user) |
| 124 |
username_changed.send( |
| 125 |
current_app._get_current_object(), |
| 126 |
_async_wrapper=current_app.ensure_sync, |
| 127 |
user=user, |
| 128 |
old_username=old_username, |
| 129 |
) |
| 130 |
|
| 131 |
|
| 132 |
def _send_username_changed_notice(user): |
| 133 |
"""Sends the username changed notice email for the specified user. |
| 134 |
|
| 135 |
:param user: The user to send the notice to |
| 136 |
""" |
| 137 |
if cv("SEND_USERNAME_CHANGE_EMAIL"): |
| 138 |
subject = cv("EMAIL_SUBJECT_USERNAME_CHANGE_NOTICE") |
| 139 |
send_mail(subject, user.email, cv("CHANGE_USERNAME_EMAIL_TEMPLATE"), user=user) |