Skip to content

Commit

Permalink
wip: add jwt auth
Browse files Browse the repository at this point in the history
  • Loading branch information
pakelley committed Jan 29, 2025
1 parent 6d010f7 commit eb1f662
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions label_studio/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def process_request(self, request) -> None:
or
# scim assign request.user implicitly, check CustomSCIMAuthCheckMiddleware
(hasattr(request, 'is_scim') and request.is_scim)
or (hasattr(request, 'is_jwt') and request.is_jwt)
):
return

Expand Down Expand Up @@ -248,3 +249,38 @@ def process_response(self, request, response):
del response['Content-Security-Policy-Report-Only']
delattr(response, '_override_report_only_csp')
return response


def get_user_jwt(request):
from django.contrib.auth.middleware import get_user
from rest_framework_simplejwt.authentication import JWTAuthentication
user = get_user(request)
if user.is_authenticated:
return user

Check warning on line 259 in label_studio/core/middleware.py

View check run for this annotation

Codecov / codecov/patch

label_studio/core/middleware.py#L255-L259

Added lines #L255 - L259 were not covered by tests

jwt_authentication = JWTAuthentication()
auth_header = jwt_authentication.get_header(request)
if not auth_header:
return None
if isinstance(auth_header, str):
auth_header = auth_header.encode()

Check warning on line 266 in label_studio/core/middleware.py

View check run for this annotation

Codecov / codecov/patch

label_studio/core/middleware.py#L261-L266

Added lines #L261 - L266 were not covered by tests

raw_token = jwt_authentication.get_raw_token(auth_header)
validated_token = jwt_authentication.get_validated_token(

Check warning on line 269 in label_studio/core/middleware.py

View check run for this annotation

Codecov / codecov/patch

label_studio/core/middleware.py#L268-L269

Added lines #L268 - L269 were not covered by tests
raw_token
)
user = jwt_authentication.get_user(validated_token)
if user:
return user

Check warning on line 274 in label_studio/core/middleware.py

View check run for this annotation

Codecov / codecov/patch

label_studio/core/middleware.py#L272-L274

Added lines #L272 - L274 were not covered by tests

class JWTAuthenticationMiddleware:
def __init__(self, get_response):
self.get_response = get_response

Check warning on line 278 in label_studio/core/middleware.py

View check run for this annotation

Codecov / codecov/patch

label_studio/core/middleware.py#L278

Added line #L278 was not covered by tests

def __call__(self, request):
from django.utils.functional import SimpleLazyObject
user = SimpleLazyObject(lambda: get_user_jwt(request))
if user:
request.user = user
request.is_jwt = True
return self.get_response(request)

Check warning on line 286 in label_studio/core/middleware.py

View check run for this annotation

Codecov / codecov/patch

label_studio/core/middleware.py#L281-L286

Added lines #L281 - L286 were not covered by tests

0 comments on commit eb1f662

Please sign in to comment.