Debian Codegraph @

flask_security/recoverable.py

raw
1 """
2 flask_security.recoverable
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 Flask-Security recoverable module
6
7 :copyright: (c) 2012 by Matt Wright.
8 :copyright: (c) 2019-2025 by J. Christopher Wagner (jwag).
9 :license: MIT, see LICENSE for more details.
10 """
11
12 from flask import current_app
13 from .proxies import _security, _datastore
14 from .signals import (
15 password_reset,
16 reset_password_instructions_sent,
17 username_recovery_email_sent,
18 )
19 from .utils import (
20 config_value,
21 get_token_status,
22 hash_data,
23 hash_password,
24 send_mail,
25 url_for_security,
26 verify_hash,
27 )
28
29
30 def generate_reset_link(user):
31 token = generate_reset_password_token(user)
32 return url_for_security("reset_password", token=token, _external=True), token
33
34
35 def send_reset_password_instructions(user):
36 """Sends the reset password instructions email for the specified user.
37
38 :param user: The user to send the instructions to
39 """
40 reset_link, token = generate_reset_link(user)
41
42 if config_value("SEND_PASSWORD_RESET_EMAIL"):
43 send_mail(
44 config_value("EMAIL_SUBJECT_PASSWORD_RESET"),
45 user.email,
46 "reset_instructions",
47 user=user,
48 reset_link=reset_link,
49 reset_token=token,
50 )
51
52 reset_password_instructions_sent.send(
53 current_app._get_current_object(),
54 _async_wrapper=current_app.ensure_sync,
55 user=user,
56 token=token,
57 reset_token=token,
58 )
59
60
61 def send_password_reset_notice(user):
62 """Sends the password reset notice email for the specified user.
63
64 :param user: The user to send the notice to
65 """
66 if config_value("SEND_PASSWORD_RESET_NOTICE_EMAIL"):
67 send_mail(
68 config_value("EMAIL_SUBJECT_PASSWORD_NOTICE"),
69 user.email,
70 "reset_notice",
71 user=user,
72 )
73
74
75 def generate_reset_password_token(user):
76 """Generates a unique reset password token for the specified user.
77
78 :param user: The user to work with
79 """
80 password_hash = hash_data(user.password) if user.password else None
81 data = [str(user.fs_uniquifier), password_hash]
82 return _security.reset_serializer.dumps(data)
83
84
85 def reset_password_token_status(token):
86 """Returns the expired status, invalid status, and user of a password reset
87 token. For example::
88
89 expired, invalid, user, data = reset_password_token_status('...')
90
91 :param token: The password reset token
92 """
93 expired, invalid, user, data = get_token_status(
94 token, "reset", "RESET_PASSWORD", return_data=True
95 )
96 # This check looks to see if the password has been changed since the reset token
97 # was created. As of #338 - we reset the fs_uniquifier on each password change
98 # so the token would have been marked invalid above.
99 # This made sure that the token couldn't be used twice.
100 # TODO - look at removing this entire check.
101 if not invalid and user:
102 if user.password:
103 if not verify_hash(data[1], user.password):
104 invalid = True
105
106 return expired, invalid, user
107
108
109 def update_password(user, password):
110 """Update the specified user's password
111
112 :param user: The user to update_password
113 :param password: The unhashed new password
114 """
115 user.password = hash_password(password)
116 # Change uniquifier - this will cause ALL sessions to be invalidated.
117 _datastore.set_uniquifier(user)
118 _datastore.put(user)
119 send_password_reset_notice(user)
120 password_reset.send(
121 current_app._get_current_object(),
122 _async_wrapper=current_app.ensure_sync,
123 user=user,
124 )
125
126
127 def send_username_recovery_email(user):
128 """Sends the username recovery email for the specified user.
129 :param user: The user requesting username recovery
130 """
131 if config_value("USERNAME_RECOVERY"):
132 send_mail(
133 config_value("EMAIL_SUBJECT_USERNAME_RECOVERY"),
134 user.email,
135 "username_recovery",
136 user=user,
137 username=user.username,
138 )
139 username_recovery_email_sent.send(
140 current_app._get_current_object(),
141 _async_wrapper=current_app.ensure_sync,
142 user=user,
143 )