Debian Codegraph @

flask_security/changeable.py

raw
1 """
2 flask_security.changeable
3 ~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 Flask-Security change password module
6
7 :copyright: (c) 2012 by Matt Wright.
8 :copyright: (c) 2019-2026 by J. Christopher Wagner (jwag).
9 :author: Eskil Heyn Olsen
10 :license: MIT, see LICENSE for more details.
11 """
12
13 from __future__ import annotations
14
15 import typing as t
16
17 from flask import current_app, request, session
18 from flask_login import COOKIE_NAME as REMEMBER_COOKIE_NAME
19
20 from .proxies import _datastore
21 from .signals import password_changed
22 from .utils import _config_value as cv, hash_password, login_user, send_mail
23
24 if t.TYPE_CHECKING: # pragma: no cover
25 from flask_security import UserMixin
26
27
28 def send_password_changed_notice(user):
29 """Sends the password changed notice email for the specified user.
30
31 :param user: The user to send the notice to
32 """
33 if cv("SEND_PASSWORD_CHANGE_EMAIL"):
34 subject = cv("EMAIL_SUBJECT_PASSWORD_CHANGE_NOTICE")
35 send_mail(
36 subject, user.email, cv("EMAIL_TEMPLATE_PASSWORD_CHANGE_NOTICE"), user=user
37 )
38
39
40 def change_user_password(
41 user: UserMixin, password: str | None, notify: bool = True, autologin: bool = True
42 ) -> None:
43 """Change the specified user's password
44
45 :param user: The user object
46 :param password: The unhashed new password
47 :param notify: if True send notification (if configured) to user
48 :param autologin: if True, login user
49 """
50
51 if password:
52 user.password = hash_password(password)
53 else:
54 user.password = None
55 # Change uniquifier - this will cause ALL sessions to be invalidated.
56 _datastore.set_uniquifier(user)
57 _datastore.put(user)
58
59 if autologin:
60 # re-login user - this will update session, optional remember etc.
61 remember_cookie_name = current_app.config.get(
62 "REMEMBER_COOKIE_NAME", REMEMBER_COOKIE_NAME
63 )
64 has_remember_cookie = (
65 remember_cookie_name in request.cookies
66 and session.get("remember") != "clear"
67 )
68 login_user(user, remember=has_remember_cookie, authn_via=["change"])
69 if notify:
70 send_password_changed_notice(user)
71 password_changed.send(
72 current_app._get_current_object(), # type: ignore
73 _async_wrapper=current_app.ensure_sync, # type: ignore[arg-type]
74 user=user,
75 )
76
77
78 def admin_change_password(
79 user: UserMixin, new_passwd: str, notify: bool = True
80 ) -> None:
81 """
82 Administratively change a user's password.
83 Note that this will immediately render the user's existing sessions (and possibly
84 authentication tokens) invalid.
85
86 It is up to the caller to inform the user of their new password by some
87 out-of-band means.
88
89 :param user: The user object to change
90 :param new_passwd: The new plain-text password to assign to the user.
91 :param notify: If True and :py:data:`SECURITY_SEND_PASSWORD_CHANGE_EMAIL` is True
92 send the 'change_notice' email to the user.
93 """
94 change_user_password(user, new_passwd, notify=notify, autologin=False)