codekingpro/portable-devtools
114k
1"""2 flask_security.passwordless3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~4 5 Flask-Security passwordless module6 7 :copyright: (c) 2012 by Matt Wright.8 :copyright: (c) 2021 by Chris Wagner.9 :license: MIT, see LICENSE for more details.10"""11 12from flask import current_app as app13 14from .proxies import _security15from .signals import login_instructions_sent16from .utils import config_value, get_token_status, send_mail, url_for_security17 18 19def send_login_instructions(user):20 """Sends the login instructions email for the specified user.21 22 :param user: The user to send the instructions to23 """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 44def generate_login_token(user):45 """Generates a unique login token for the specified user.46 47 :param user: The user the token belongs to48 """49 return _security.login_serializer.dumps([str(user.fs_uniquifier)])50 51 52def 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 token59 """60 return get_token_status(token, "login", "LOGIN")61 