From 4d70f016ad8f4cd153b046b00184dea576205650 Mon Sep 17 00:00:00 2001 From: Jeremy Epstein Date: Fri, 28 Nov 2014 10:36:31 +1100 Subject: [PATCH 1/2] re #343: Add slash before or after token in flask-security URLs correctly --- flask_security/utils.py | 6 ++++++ flask_security/views.py | 8 ++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/flask_security/utils.py b/flask_security/utils.py index 86bb24a..b1996d8 100644 --- a/flask_security/utils.py +++ b/flask_security/utils.py @@ -188,6 +188,12 @@ def get_url(endpoint_or_url): return endpoint_or_url +def slash_url_suffix(url, suffix): + """Adds a slash either to the beginning or the end of a suffix (which is to be appended to a URL), depending on whether or not the URL ends with a slash.""" + + return url.endswith('/') and ('%s/' % suffix) or ('/%s' % suffix) + + def get_security_endpoint_name(endpoint): return '%s.%s' % (_security.blueprint_name, endpoint) diff --git a/flask_security/views.py b/flask_security/views.py index 864c9e3..7ec0cac 100644 --- a/flask_security/views.py +++ b/flask_security/views.py @@ -26,7 +26,7 @@ from .changeable import change_user_password from .registerable import register_user from .utils import config_value, do_flash, get_url, get_post_login_redirect, \ get_post_register_redirect, get_message, login_user, logout_user, \ - url_for_security as url_for + url_for_security as url_for, slash_url_suffix # Convenient references _security = LocalProxy(lambda: current_app.extensions['security']) @@ -333,7 +333,7 @@ def create_blueprint(state, import_name): bp.route(state.login_url, methods=['GET', 'POST'], endpoint='login')(send_login) - bp.route(state.login_url + '/', + bp.route(state.login_url + slash_url_suffix(state.login_url, ''), endpoint='token_login')(token_login) else: bp.route(state.login_url, @@ -349,7 +349,7 @@ def create_blueprint(state, import_name): bp.route(state.reset_url, methods=['GET', 'POST'], endpoint='forgot_password')(forgot_password) - bp.route(state.reset_url + '/', + bp.route(state.reset_url + slash_url_suffix(state.reset_url, ''), methods=['GET', 'POST'], endpoint='reset_password')(reset_password) @@ -362,7 +362,7 @@ def create_blueprint(state, import_name): bp.route(state.confirm_url, methods=['GET', 'POST'], endpoint='send_confirmation')(send_confirmation) - bp.route(state.confirm_url + '/', + bp.route(state.confirm_url + slash_url_suffix(state.confirm_url, ''), methods=['GET', 'POST'], endpoint='confirm_email')(confirm_email) From 665b164618c40be47eda8da46b9a055fbd3b1a30 Mon Sep 17 00:00:00 2001 From: Jeremy Epstein Date: Fri, 28 Nov 2014 13:50:25 +1100 Subject: [PATCH 2/2] split docstring into multiple lines to make travis CI happy --- flask_security/utils.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/flask_security/utils.py b/flask_security/utils.py index b1996d8..3ec962c 100644 --- a/flask_security/utils.py +++ b/flask_security/utils.py @@ -189,7 +189,9 @@ def get_url(endpoint_or_url): def slash_url_suffix(url, suffix): - """Adds a slash either to the beginning or the end of a suffix (which is to be appended to a URL), depending on whether or not the URL ends with a slash.""" + """Adds a slash either to the beginning or the end of a suffix + (which is to be appended to a URL), depending on whether or not + the URL ends with a slash.""" return url.endswith('/') and ('%s/' % suffix) or ('/%s' % suffix)