From ca25c253d5940c57b7797aa154ae309b9a534ec2 Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Fri, 8 Jun 2012 14:17:33 -0400 Subject: [PATCH] Start work on token authentication --- flask_security/tokens.py | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 flask_security/tokens.py diff --git a/flask_security/tokens.py b/flask_security/tokens.py new file mode 100644 index 0000000..7386662 --- /dev/null +++ b/flask_security/tokens.py @@ -0,0 +1,44 @@ + +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))