| 1 |
""" |
| 2 |
flask_security.passwordless |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Flask-Security passwordless module |
| 6 |
|
| 7 |
:copyright: (c) 2012 by Matt Wright. |
| 8 |
:copyright: (c) 2021 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 |
| 15 |
from .signals import login_instructions_sent |
| 16 |
from .utils import config_value, get_token_status, send_mail, url_for_security |
| 17 |
|
| 18 |
|
| 19 |
def send_login_instructions(user): |
| 20 |
"""Sends the login instructions email for the specified user. |
| 21 |
|
| 22 |
:param user: The user to send the instructions to |
| 23 |
""" |
| 24 |
token = generate_login_token(user) |
| 25 |
login_link = url_for_security("token_login", token=token, _external=True) |
| 26 |
|
| 27 |
send_mail( |
| 28 |
config_value("EMAIL_SUBJECT_PASSWORDLESS"), |
| 29 |
user.email, |
| 30 |
"login_instructions", |
| 31 |
user=user, |
| 32 |
login_link=login_link, |
| 33 |
login_token=token, |
| 34 |
) |
| 35 |
|
| 36 |
login_instructions_sent.send( |
| 37 |
app._get_current_object(), |
| 38 |
_async_wrapper=app.ensure_sync, |
| 39 |
user=user, |
| 40 |
login_token=token, |
| 41 |
) |
| 42 |
|
| 43 |
|
| 44 |
def generate_login_token(user): |
| 45 |
"""Generates a unique login token for the specified user. |
| 46 |
|
| 47 |
:param user: The user the token belongs to |
| 48 |
""" |
| 49 |
return _security.login_serializer.dumps([str(user.fs_uniquifier)]) |
| 50 |
|
| 51 |
|
| 52 |
def login_token_status(token): |
| 53 |
"""Returns the expired status, invalid status, and user of a login token. |
| 54 |
For example:: |
| 55 |
|
| 56 |
expired, invalid, user = login_token_status('...') |
| 57 |
|
| 58 |
:param token: The login token |
| 59 |
""" |
| 60 |
return get_token_status(token, "login", "LOGIN") |