Files
flask-security/flask_security/tokens.py
T

78 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
"""
flask.ext.security.tokens
~~~~~~~~~~~~~~~~~~~~~~~~~
Flask-Security tokens module
:copyright: (c) 2012 by Matt Wright.
:license: MIT, see LICENSE for more details.
"""
from datetime import datetime
from flask import current_app as app
from werkzeug.local import LocalProxy
from .exceptions import BadCredentialsError, UserNotFoundError
from .utils import generate_token
# Convenient references
_datastore = LocalProxy(lambda: app.security.datastore)
def find_user_by_authentication_token(token):
"""Returns a user with a matching authentication token.
:param token: The authentication token
"""
if not token:
raise BadCredentialsError('Authentication token required')
return _datastore.find_user(authentication_token=token)
def generate_authentication_token(user):
"""Generates a unique authentication token for the specified user.
:param user: The user to work with
"""
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_created_at'] = now
except TypeError:
user.authentication_token = token
user.authentication_token_created_at = now
return user
def reset_authentication_token(user):
"""Resets a user's authentication token and returns the new token value.
:param user: The user to work with
"""
user = generate_authentication_token(user)
_datastore._save_model(user)
return user.authentication_token
def ensure_authentication_token(user):
"""Ensures that a user has an authentication token. If the user has an
authentication token already, nothing is performed.
:param user: The user to work with
"""
if not user.authentication_token:
reset_authentication_token(user)
return user.authentication_token