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

fix: InactivitySessionTimeoutMiddleware: Check for last_login (allow Django admin login + access) #6800

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions label_studio/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@ def process_request(self, request) -> None:
current_time = time.time()
last_login = request.session['last_login'] if 'last_login' in request.session else 0

# Check if this request is too far from when the login happened
if (current_time - last_login) > settings.MAX_SESSION_AGE:
logger.info(
# Check if this request is too far from when the login happened,
# but only when last_login was set before
if last_login and (current_time - last_login) > settings.MAX_SESSION_AGE:
logger.warn(
f'Request is too far from last login {current_time - last_login:.0f} > {settings.MAX_SESSION_AGE}; logout'
)
logout(request)
Expand Down
25 changes: 25 additions & 0 deletions label_studio/tests/test_django_admin_login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.urls import reverse


def test_django_admin_login(admin_user, client, live_server):
response = client.get(reverse("admin:index"))

# Make sure we are redirected to the django admin login page
assert response.status_code == 302
assert response.url.startswith(reverse("admin:login"))

credentials = {"username": admin_user.email, "password": "password"}
response = client.post(response.url, credentials)

# We should be redirected to the django index
assert response.status_code == 302
assert response.url.startswith(reverse("admin:index"))

# No last_login key has been set in the session as we do not
# login via the regular /users/login page
assert not "last_login" in client.session

# And our logged in session will still be valid
response = client.get(reverse("admin:index"))

assert response.status_code == 200
Loading