Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set session ID when setting next #403

Merged
merged 1 commit into from Jul 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions flask_login/login_manager.py
Expand Up @@ -169,6 +169,7 @@ def unauthorized(self):
config = current_app.config
if config.get('USE_SESSION_FOR_NEXT', USE_SESSION_FOR_NEXT):
login_url = expand_login_view(login_view)
session['_id'] = current_app.login_manager._session_identifier_generator()
session['next'] = make_next_param(login_url, request.url)
redirect_url = make_login_url(login_view)
else:
Expand Down Expand Up @@ -281,6 +282,7 @@ def needs_refresh(self):
config = current_app.config
if config.get('USE_SESSION_FOR_NEXT', USE_SESSION_FOR_NEXT):
login_url = expand_login_view(self.refresh_view)
session['_id'] = current_app.login_manager._session_identifier_generator()
session['next'] = make_next_param(login_url, request.url)
redirect_url = make_login_url(self.refresh_view)
else:
Expand Down
19 changes: 19 additions & 0 deletions test_login.py
Expand Up @@ -518,6 +518,25 @@ def login():
'http://localhost/login')
self.assertEqual(c.get('/login').data.decode('utf-8'), '/secret')

def test_unauthorized_with_next_in_strong_session_where_current_user_is_called(self):
self.login_manager.login_view = 'login'
self.app.config['SESSION_PROTECTION'] = 'strong'
self.app.config['USE_SESSION_FOR_NEXT'] = True

@self.app.route('/login')
def login():
if(current_user.is_authenticated):
# Or anything that touches current_user
pass
return session.pop('next', '')

with self.app.test_client() as c:
result = c.get('/secret')
self.assertEqual(result.status_code, 302)
self.assertEqual(result.location,
'http://localhost/login')
self.assertEqual(c.get('/login').data.decode('utf-8'), '/secret')

def test_unauthorized_uses_blueprint_login_view(self):
with self.app.app_context():

Expand Down