From ca25c253d5940c57b7797aa154ae309b9a534ec2 Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Fri, 8 Jun 2012 14:17:33 -0400 Subject: [PATCH 1/3] 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)) From c4ada0d99870458c70ac44c96fc13eb8be1faf2e Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Fri, 8 Jun 2012 14:28:38 -0400 Subject: [PATCH 2/3] add query string and header config --- flask_security/core.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/flask_security/core.py b/flask_security/core.py index f741ac1..4c49356 100644 --- a/flask_security/core.py +++ b/flask_security/core.py @@ -55,7 +55,9 @@ _default_config = { 'CONFIRM_EMAIL_WITHIN': '5 days', 'RESET_PASSWORD_WITHIN': '2 days', 'LOGIN_WITHOUT_CONFIRMATION': False, - 'EMAIL_SENDER': 'no-reply@localhost' + 'EMAIL_SENDER': 'no-reply@localhost', + 'TOKEN_AUTHENTICATION_KEY': 'auth_token', + 'TOKEN_AUTHENTICATION_HEADER': 'X-Auth-Token' } @@ -251,6 +253,8 @@ class Security(object): self.login_without_confirmation = utils.config_value(app, 'LOGIN_WITHOUT_CONFIRMATION') self.confirm_email = utils.config_value(app, 'CONFIRM_EMAIL') self.email_sender = utils.config_value(app, 'EMAIL_SENDER') + self.token_authentication_key = utils.config_value(app, 'TOKEN_AUTHENTICATION_KEY') + self.token_authentication_header = utils.config_value(app, 'TOKEN_AUTHENTICATION_HEADER') self.confirm_email_within_text = utils.config_value(app, 'CONFIRM_EMAIL_WITHIN') values = self.confirm_email_within_text.split() From 90d52c656114ad9ca7bc5a80f205b412f63b7e8a Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Mon, 11 Jun 2012 18:06:47 -0400 Subject: [PATCH 3/3] token auth methods --- flask_security/tokens.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/flask_security/tokens.py b/flask_security/tokens.py index 7386662..f6695d2 100644 --- a/flask_security/tokens.py +++ b/flask_security/tokens.py @@ -3,7 +3,7 @@ from datetime import datetime from flask import current_app from flask.ext.security.exceptions import BadCredentialsError, \ - UserNotFoundError + UserNotFoundError, AuthenticationError from flask.ext.security.utils import generate_token from werkzeug.local import LocalProxy @@ -36,9 +36,15 @@ def generate_authentication_token(user): return user -def login_by_token(token): - pass +def authenticate_by_token(token): + try: + return find_user_by_authentication_token(token) + except UserNotFoundError: + raise BadCredentialsError('Invalid authentication token') + except Exception, e: + raise AuthenticationError(str(e)) def reset_authentication_token(user): - security.datastore._save_model(generate_authentication_token(user)) + user = generate_authentication_token(user) + security.datastore._save_model(user)