token-loader.patch 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. From 01a0d62b7e5246867d456de5515a1fe09eb0f2ed Mon Sep 17 00:00:00 2001
  2. From: Alan Hamlett <alan.hamlett@gmail.com>
  3. Date: Thu, 27 Oct 2016 15:04:07 +0200
  4. Subject: [PATCH 1/2] replace token loader with request loader to fix #81
  5. ---
  6. flask_security/core.py | 12 ++++++++++--
  7. flask_security/decorators.py | 10 +---------
  8. tests/test_common.py | 2 +-
  9. 3 files changed, 12 insertions(+), 12 deletions(-)
  10. diff --git a/flask_security/core.py b/flask_security/core.py
  11. index 3256668..3bcef52 100644
  12. --- a/flask_security/core.py
  13. +++ b/flask_security/core.py
  14. @@ -191,7 +191,15 @@ def _user_loader(user_id):
  15. return _security.datastore.find_user(id=user_id)
  16. -def _token_loader(token):
  17. +def _request_loader(request):
  18. + header_key = _security.token_authentication_header
  19. + args_key = _security.token_authentication_key
  20. + header_token = request.headers.get(header_key, None)
  21. + token = request.args.get(args_key, header_token)
  22. + if request.get_json(silent=True):
  23. + if not isinstance(request.json, list):
  24. + token = request.json.get(args_key, token)
  25. +
  26. try:
  27. data = _security.remember_token_serializer.loads(token, max_age=_security.token_max_age)
  28. user = _security.datastore.find_user(id=data[0])
  29. @@ -223,7 +231,7 @@ def _get_login_manager(app, anonymous_user):
  30. lm.anonymous_user = anonymous_user or AnonymousUser
  31. lm.login_view = '%s.login' % cv('BLUEPRINT_NAME', app=app)
  32. lm.user_loader(_user_loader)
  33. - lm.token_loader(_token_loader)
  34. + lm.request_loader(_request_loader)
  35. if cv('FLASH_MESSAGES', app=app):
  36. lm.login_message, lm.login_message_category = cv('MSG_LOGIN', app=app)
  37. diff --git a/flask_security/decorators.py b/flask_security/decorators.py
  38. index 89e62e8..81fa2f0 100644
  39. --- a/flask_security/decorators.py
  40. +++ b/flask_security/decorators.py
  41. @@ -58,15 +58,7 @@ def _get_unauthorized_view():
  42. def _check_token():
  43. - header_key = _security.token_authentication_header
  44. - args_key = _security.token_authentication_key
  45. - header_token = request.headers.get(header_key, None)
  46. - token = request.args.get(args_key, header_token)
  47. - if request.get_json(silent=True):
  48. - if not isinstance(request.json, list):
  49. - token = request.json.get(args_key, token)
  50. -
  51. - user = _security.login_manager.token_callback(token)
  52. + user = _security.login_manager.request_callback(request)
  53. if user and user.is_authenticated:
  54. app = current_app._get_current_object()
  55. diff --git a/tests/test_common.py b/tests/test_common.py
  56. index e884ab5..531090f 100644
  57. --- a/tests/test_common.py
  58. +++ b/tests/test_common.py
  59. @@ -271,7 +271,7 @@ def test_remember_token(client):
  60. assert b'profile' in response.data
  61. -def test_token_loader_does_not_fail_with_invalid_token(client):
  62. +def test_request_loader_does_not_fail_with_invalid_token(client):
  63. c = Cookie(version=0, name='remember_token', value='None', port=None,
  64. port_specified=False, domain='www.example.com',
  65. domain_specified=False, domain_initial_dot=False, path='/',
  66. From 6d56450a2b88c7196eeeba503b8e4ae3cdc857aa Mon Sep 17 00:00:00 2001
  67. From: Alan Hamlett <alan.hamlett@gmail.com>
  68. Date: Fri, 28 Oct 2016 00:45:51 +0200
  69. Subject: [PATCH 2/2] remove max version requirement from Flask-Login
  70. ---
  71. requirements.txt | 2 +-
  72. 1 file changed, 1 insertion(+), 1 deletion(-)
  73. diff --git a/requirements.txt b/requirements.txt
  74. index c2df730..bd79ce4 100644
  75. --- a/requirements.txt
  76. +++ b/requirements.txt
  77. @@ -1,5 +1,5 @@
  78. Flask>=0.9
  79. -Flask-Login>=0.3.0,<0.4
  80. +Flask-Login>=0.3.0
  81. Flask-Mail>=0.7.3
  82. Flask-Principal>=0.3.3
  83. Flask-WTF>=0.8