| 1 |
""" |
| 2 |
flask_security.confirmable |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Flask-Security confirmable module |
| 6 |
|
| 7 |
:copyright: (c) 2012 by Matt Wright. |
| 8 |
:copyright: (c) 2017 by CERN. |
| 9 |
:copyright: (c) 2021-2026 by J. Christopher Wagner (jwag). |
| 10 |
:license: MIT, see LICENSE for more details. |
| 11 |
""" |
| 12 |
|
| 13 |
from flask import current_app |
| 14 |
|
| 15 |
from .proxies import _security, _datastore |
| 16 |
from .signals import confirm_instructions_sent, user_confirmed |
| 17 |
from .utils import ( |
| 18 |
_config_value as cv, |
| 19 |
hash_data, |
| 20 |
send_mail, |
| 21 |
url_for_security, |
| 22 |
check_and_get_token_status, |
| 23 |
verify_hash, |
| 24 |
_td_format, |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
def generate_confirmation_link(user): |
| 29 |
token = generate_confirmation_token(user) |
| 30 |
return url_for_security("confirm_email", token=token, _external=True), token |
| 31 |
|
| 32 |
|
| 33 |
def send_confirmation_instructions(user): |
| 34 |
"""Sends the confirmation instructions email for the specified user. |
| 35 |
|
| 36 |
:param user: The user to send the instructions to |
| 37 |
""" |
| 38 |
|
| 39 |
confirmation_link, token = generate_confirmation_link(user) |
| 40 |
|
| 41 |
send_mail( |
| 42 |
cv("EMAIL_SUBJECT_CONFIRM"), |
| 43 |
user.email, |
| 44 |
cv("EMAIL_TEMPLATE_CONFIRM"), |
| 45 |
user=user, |
| 46 |
confirmation_link=confirmation_link, |
| 47 |
confirmation_token=token, |
| 48 |
within=_td_format(cv("CONFIRM_EMAIL_WITHIN")), |
| 49 |
) |
| 50 |
|
| 51 |
confirm_instructions_sent.send( |
| 52 |
current_app._get_current_object(), |
| 53 |
_async_wrapper=current_app.ensure_sync, |
| 54 |
user=user, |
| 55 |
token=token, |
| 56 |
confirmation_token=token, |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
def generate_confirmation_token(user): |
| 61 |
"""Generates a unique confirmation token for the specified user. |
| 62 |
|
| 63 |
:param user: The user to work with |
| 64 |
""" |
| 65 |
data = [str(user.fs_uniquifier), hash_data(user.email)] |
| 66 |
return _security.confirm_serializer.dumps(data) |
| 67 |
|
| 68 |
|
| 69 |
def requires_confirmation(user): |
| 70 |
"""Returns `True` if the user requires confirmation.""" |
| 71 |
return ( |
| 72 |
_security.confirmable |
| 73 |
and not cv("LOGIN_WITHOUT_CONFIRMATION") |
| 74 |
and user.confirmed_at is None |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
def confirm_email_token_status(token): |
| 79 |
"""Returns the expired status, invalid status, and user of a confirmation |
| 80 |
token. For example:: |
| 81 |
|
| 82 |
expired, invalid, user = confirm_email_token_status('...') |
| 83 |
|
| 84 |
Always check invalid and expiration first - 'user' could be returned in any case |
| 85 |
:param token: The confirmation token |
| 86 |
""" |
| 87 |
expired, invalid, data = check_and_get_token_status( |
| 88 |
token, "confirm", cv("CONFIRM_EMAIL_WITHIN") |
| 89 |
) |
| 90 |
if invalid or expired or not data: |
| 91 |
return expired, invalid, None |
| 92 |
if not (user := _datastore.find_user(fs_uniquifier=data[0])): |
| 93 |
return expired, True, None |
| 94 |
invalid = not verify_hash(data[1], user.email) |
| 95 |
return expired, invalid, user |
| 96 |
|
| 97 |
|
| 98 |
def confirm_user(user): |
| 99 |
"""Confirms the specified user |
| 100 |
|
| 101 |
:param user: The user to confirm |
| 102 |
""" |
| 103 |
if user.confirmed_at is not None: |
| 104 |
return False |
| 105 |
user.confirmed_at = _security.datetime_factory() |
| 106 |
_datastore.put(user) |
| 107 |
user_confirmed.send( |
| 108 |
current_app._get_current_object(), |
| 109 |
_async_wrapper=current_app.ensure_sync, |
| 110 |
user=user, |
| 111 |
) |
| 112 |
return True |