Start work on token authentication

This commit is contained in:
Matt Wright
2012-06-08 14:17:33 -04:00
parent 7263388b7c
commit ca25c253d5
+44
View File
@@ -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))