Skip to content

Commit ccb16b0

Browse files
nmanovicChris Lee-Messer
authored and
Chris Lee-Messer
committed
Restore session id (cvat-ai#905)
* Restore session id when we use token authorization.
1 parent b6edd4b commit ccb16b0

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

cvat/apps/authentication/auth.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
import os
65
from django.conf import settings
76
from django.db.models import Q
87
import rules
@@ -11,6 +10,20 @@
1110
from rest_framework.permissions import BasePermission
1211
from django.core import signing
1312
from rest_framework import authentication, exceptions
13+
from rest_framework.authentication import TokenAuthentication as _TokenAuthentication
14+
from django.contrib.auth import login
15+
16+
# Even with token authorization it is very important to have a valid session id
17+
# in cookies because in some cases we cannot use token authorization (e.g. when
18+
# we redirect to the server in UI using just URL). To overkill that we override
19+
# the class to call `login` method which restores the session id in cookies.
20+
class TokenAuthentication(_TokenAuthentication):
21+
def authenticate(self, request):
22+
auth = super().authenticate(request)
23+
session = getattr(request, 'session')
24+
if auth is not None and session.session_key is None:
25+
login(request, auth[0], 'django.contrib.auth.backends.ModelBackend')
26+
return auth
1427

1528
def register_signals():
1629
from django.db.models.signals import post_migrate, post_save

cvat/apps/authentication/decorators.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.contrib.auth import REDIRECT_FIELD_NAME
99
from django.http import JsonResponse
1010
from django.conf import settings
11-
from rest_framework.authentication import TokenAuthentication
11+
from cvat.apps.authentication.auth import TokenAuthentication
1212

1313
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME,
1414
login_url=None, redirect_methods=['GET']):
@@ -21,7 +21,6 @@ def _wrapped_view(request, *args, **kwargs):
2121
tokenAuth = TokenAuthentication()
2222
auth = tokenAuth.authenticate(request)
2323
if auth is not None:
24-
request.user = auth[0]
2524
return view_func(request, *args, **kwargs)
2625

2726
login_url = '{}/login'.format(settings.UI_URL)

cvat/settings/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def generate_ssh_keys():
124124
'rest_framework.permissions.IsAuthenticated',
125125
],
126126
'DEFAULT_AUTHENTICATION_CLASSES': [
127-
'rest_framework.authentication.TokenAuthentication',
127+
'cvat.apps.authentication.auth.TokenAuthentication',
128128
'cvat.apps.authentication.auth.SignatureAuthentication',
129129
'rest_framework.authentication.SessionAuthentication',
130130
'rest_framework.authentication.BasicAuthentication'

0 commit comments

Comments
 (0)