| 1 |
""" |
| 2 |
flask_security.passwordless |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Flask-Security passwordless module |
| 6 |
|
| 7 |
:copyright: (c) 2012 by Matt Wright. |
| 8 |
:copyright: (c) 2021-2026 by Chris Wagner. |
| 9 |
:license: MIT, see LICENSE for more details. |
| 10 |
""" |
| 11 |
|
| 12 |
from flask import current_app as app |
| 13 |
|
| 14 |
from .proxies import _security, _datastore |
| 15 |
from .signals import login_instructions_sent |
| 16 |
from .utils import ( |
| 17 |
_config_value as cv, |
| 18 |
send_mail, |
| 19 |
url_for_security, |
| 20 |
check_and_get_token_status, |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
def send_login_instructions(user): |
| 25 |
"""Sends the login instructions email for the specified user. |
| 26 |
|
| 27 |
:param user: The user to send the instructions to |
| 28 |
""" |
| 29 |
token = generate_login_token(user) |
| 30 |
login_link = url_for_security("token_login", token=token, _external=True) |
| 31 |
|
| 32 |
send_mail( |
| 33 |
cv("EMAIL_SUBJECT_PASSWORDLESS"), |
| 34 |
user.email, |
| 35 |
cv("EMAIL_TEMPLATE_PASSWORDLESS"), |
| 36 |
user=user, |
| 37 |
login_link=login_link, |
| 38 |
login_token=token, |
| 39 |
) |
| 40 |
|
| 41 |
login_instructions_sent.send( |
| 42 |
app._get_current_object(), |
| 43 |
_async_wrapper=app.ensure_sync, |
| 44 |
user=user, |
| 45 |
login_token=token, |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
def generate_login_token(user): |
| 50 |
"""Generates a unique login token for the specified user. |
| 51 |
|
| 52 |
:param user: The user the token belongs to |
| 53 |
""" |
| 54 |
return _security.login_serializer.dumps([str(user.fs_uniquifier)]) |
| 55 |
|
| 56 |
|
| 57 |
def login_token_status(token): |
| 58 |
"""Returns the expired status, invalid status, and user of a login token. |
| 59 |
For example:: |
| 60 |
|
| 61 |
expired, invalid, user = login_token_status('...') |
| 62 |
|
| 63 |
:param token: The login token |
| 64 |
""" |
| 65 |
expired, invalid, data = check_and_get_token_status( |
| 66 |
token, "login", cv("LOGIN_WITHIN") |
| 67 |
) |
| 68 |
if invalid or not data: |
| 69 |
return expired, invalid, None |
| 70 |
user = _datastore.find_user(fs_uniquifier=data[0]) |
| 71 |
return expired, invalid, user |