Skip to content

Commit

Permalink
Put request update on login in separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
stveit committed Feb 21, 2024
1 parent db86a2a commit 6c74a1f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
10 changes: 10 additions & 0 deletions python/nav/web/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,13 @@ def logout(request, sudo=False):
LogEntry.add_log_entry(account, 'log-out', '{actor} logged out', before=account)
_logger.debug('logout: redirect to "/" after logout')
return get_post_logout_redirect_url(request)


def set_request_account(request, account):
"""Updates request with new account.
Cycles the session ID to avoid session fixation.
This function is meant to be called during login.
"""
request.session[ACCOUNT_ID_VAR] = account.id
request.session.cycle_key()
request.account = account
5 changes: 2 additions & 3 deletions python/nav/web/auth/remote_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from nav.config import NAVConfigParser
from nav.models.profiles import Account
from nav.web.auth.utils import ACCOUNT_ID_VAR
from nav.web.auth import set_request_account

try:
# Python 3.6+
Expand Down Expand Up @@ -122,9 +123,7 @@ def login(request):
# Get or create an account from the REMOTE_USER http header
account = authenticate(request)
if account:
request.session[ACCOUNT_ID_VAR] = account.id
request.session.cycle_key()
request.account = account
set_request_account(request, account)
return account
return None

Expand Down
4 changes: 1 addition & 3 deletions python/nav/web/webfront/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,7 @@ def do_login(request):
)

try:
request.session[ACCOUNT_ID_VAR] = account.id
request.session.cycle_key()
request.account = account
auth.set_request_account(request, account)
except ldap.Error as error:
errors.append('Error while talking to LDAP:\n%s' % error)
else:
Expand Down

0 comments on commit 6c74a1f

Please sign in to comment.