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-2024 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(subject, user.email, "change_notice", user=user)
36
37
38 def change_user_password(
39 user: UserMixin, password: str | None, notify: bool = True, autologin: bool = True
40 ) -> None:
41 """Change the specified user's password
42
43 :param user: The user object
44 :param password: The unhashed new password
45 :param notify: if True send notification (if configured) to user
46 :param autologin: if True, login user
47 """
48
49 if password:
50 user.password = hash_password(password)
51 else:
52 user.password = None
53 # Change uniquifier - this will cause ALL sessions to be invalidated.
54 _datastore.set_uniquifier(user)
55 _datastore.put(user)
56
57 if autologin:
58 # re-login user - this will update session, optional remember etc.
59 remember_cookie_name = current_app.config.get(
60 "REMEMBER_COOKIE_NAME", REMEMBER_COOKIE_NAME
61 )
62 has_remember_cookie = (
63 remember_cookie_name in request.cookies
64 and session.get("remember") != "clear"
65 )
66 login_user(user, remember=has_remember_cookie, authn_via=["change"])
67 if notify:
68 send_password_changed_notice(user)
69 password_changed.send(
70 current_app._get_current_object(), # type: ignore
71 _async_wrapper=current_app.ensure_sync, # type: ignore[arg-type]
72 user=user,
73 )
74
75
76 def admin_change_password(
77 user: UserMixin, new_passwd: str, notify: bool = True
78 ) -> None:
79 """
80 Administratively change a user's password.
81 Note that this will immediately render the user's existing sessions (and possibly
82 authentication tokens) invalid.
83
84 It is up to the caller to inform the user of their new password by some
85 out-of-band means.
86
87 :param user: The user object to change
88 :param new_passwd: The new plain-text password to assign to the user.
89 :param notify: If True and :py:data:`SECURITY_SEND_PASSWORD_CHANGE_EMAIL` is True
90 send the 'change_notice' email to the user.
91 """
92 change_user_password(user, new_passwd, notify=notify, autologin=False)