Files
flask-security/flask_security/tokens.py
T

45 lines
1.1 KiB
Python

from datetime import datetime
from flask import current_app
from flask.ext.security.exceptions import BadCredentialsError, \
UserNotFoundError
from flask.ext.security.utils import generate_token
from werkzeug.local import LocalProxy
security = LocalProxy(lambda: current_app.security)
def find_user_by_authentication_token(token):
if not token:
raise BadCredentialsError('Authentication token required')
return security.datastore.find_user(authentication_token=token)
def generate_authentication_token(user):
while True:
token = generate_token()
try:
find_user_by_authentication_token(token)
except UserNotFoundError:
break
now = datetime.utcnow()
try:
user['authentication_token'] = token
user['authentication_token_generated_at'] = now
except TypeError:
user.authentication_token = token
user.authentication_token_generated_at = now
return user
def login_by_token(token):
pass
def reset_authentication_token(user):
security.datastore._save_model(generate_authentication_token(user))