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

feat: DIA-1839: Add JWT auth for API tokens #6996

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
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 @@
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 @@
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
Loading