| 1 |
""" |
| 2 |
flask_security.recoverable |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Flask-Security recoverable module |
| 6 |
|
| 7 |
:copyright: (c) 2012 by Matt Wright. |
| 8 |
:copyright: (c) 2019-2026 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 |
check_and_get_token_status, |
| 21 |
_config_value as cv, |
| 22 |
hash_data, |
| 23 |
hash_password, |
| 24 |
send_mail, |
| 25 |
url_for_security, |
| 26 |
_td_format, |
| 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 cv("SEND_PASSWORD_RESET_EMAIL"): |
| 43 |
send_mail( |
| 44 |
cv("EMAIL_SUBJECT_PASSWORD_RESET"), |
| 45 |
user.email, |
| 46 |
cv("EMAIL_TEMPLATE_PASSWORD_RESET"), |
| 47 |
user=user, |
| 48 |
reset_link=reset_link, |
| 49 |
reset_token=token, |
| 50 |
within=_td_format(cv("RESET_PASSWORD_WITHIN")), |
| 51 |
) |
| 52 |
|
| 53 |
reset_password_instructions_sent.send( |
| 54 |
current_app._get_current_object(), |
| 55 |
_async_wrapper=current_app.ensure_sync, |
| 56 |
user=user, |
| 57 |
token=token, |
| 58 |
reset_token=token, |
| 59 |
) |
| 60 |
|
| 61 |
|
| 62 |
def send_password_reset_notice(user): |
| 63 |
"""Sends the password reset notice email for the specified user. |
| 64 |
|
| 65 |
:param user: The user to send the notice to |
| 66 |
""" |
| 67 |
if cv("SEND_PASSWORD_RESET_NOTICE_EMAIL"): |
| 68 |
send_mail( |
| 69 |
cv("EMAIL_SUBJECT_PASSWORD_NOTICE"), |
| 70 |
user.email, |
| 71 |
cv("EMAIL_TEMPLATE_PASSWORD_RESET_NOTICE"), |
| 72 |
user=user, |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
def generate_reset_password_token(user): |
| 77 |
"""Generates a unique reset password token for the specified user. |
| 78 |
|
| 79 |
:param user: The user to work with |
| 80 |
""" |
| 81 |
password_hash = hash_data(user.password) if user.password else None |
| 82 |
data = [str(user.fs_uniquifier), password_hash] |
| 83 |
return _security.reset_serializer.dumps(data) |
| 84 |
|
| 85 |
|
| 86 |
def reset_password_token_status(token): |
| 87 |
"""Returns the expired status, invalid status, and user of a password reset |
| 88 |
token. For example:: |
| 89 |
|
| 90 |
expired, invalid, user, data = reset_password_token_status('...') |
| 91 |
|
| 92 |
:param token: The password reset token |
| 93 |
""" |
| 94 |
user = None |
| 95 |
expired, invalid, data = check_and_get_token_status( |
| 96 |
token, "reset", cv("RESET_PASSWORD_WITHIN") |
| 97 |
) |
| 98 |
if data: |
| 99 |
user = _datastore.find_user(fs_uniquifier=data[0]) |
| 100 |
|
| 101 |
return expired, invalid, user |
| 102 |
|
| 103 |
|
| 104 |
def update_password(user, password): |
| 105 |
"""Update the specified user's password |
| 106 |
|
| 107 |
:param user: The user to update_password |
| 108 |
:param password: The unhashed new password |
| 109 |
""" |
| 110 |
user.password = hash_password(password) |
| 111 |
# Change uniquifier - this will cause ALL sessions to be invalidated. |
| 112 |
_datastore.set_uniquifier(user) |
| 113 |
_datastore.put(user) |
| 114 |
send_password_reset_notice(user) |
| 115 |
password_reset.send( |
| 116 |
current_app._get_current_object(), |
| 117 |
_async_wrapper=current_app.ensure_sync, |
| 118 |
user=user, |
| 119 |
) |
| 120 |
|
| 121 |
|
| 122 |
def send_username_recovery_email(user): |
| 123 |
"""Sends the username recovery email for the specified user. |
| 124 |
:param user: The user requesting username recovery |
| 125 |
""" |
| 126 |
if cv("USERNAME_RECOVERY"): |
| 127 |
send_mail( |
| 128 |
cv("EMAIL_SUBJECT_USERNAME_RECOVERY"), |
| 129 |
user.email, |
| 130 |
cv("USERNAME_RECOVERY_EMAIL_TEMPLATE"), |
| 131 |
user=user, |
| 132 |
username=user.username, |
| 133 |
) |
| 134 |
username_recovery_email_sent.send( |
| 135 |
current_app._get_current_object(), |
| 136 |
_async_wrapper=current_app.ensure_sync, |
| 137 |
user=user, |
| 138 |
) |