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(auth): handle DRF requests properly in Django middleware #125

Merged
merged 2 commits into from
Oct 18, 2023
Merged
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
9 changes: 6 additions & 3 deletions bento_lib/auth/middleware/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from asgiref.sync import iscoroutinefunction, markcoroutinefunction
from django.http import JsonResponse, HttpRequest, HttpResponse
from rest_framework.request import Request as DrfRequest
from typing import Awaitable, Callable

from bento_lib.responses.errors import http_error
Expand All @@ -21,12 +22,14 @@ def __init__(self, *args, **kwargs):
if self._logger is None:
self._logger = logging.getLogger(__name__)

def get_authz_header_value(self, request: HttpRequest) -> str | None:
def get_authz_header_value(self, request: DrfRequest | HttpRequest) -> str | None:
return request.headers.get("Authorization")

@staticmethod
def mark_authz_done(request: HttpRequest):
request.bento_determined_authz = True
def mark_authz_done(request: DrfRequest | HttpRequest):
# noinspection PyProtectedMember
req = request._request if isinstance(request, DrfRequest) else request
req.bento_determined_authz = True

def make_django_middleware(self):
# noinspection PyMethodParameters
Expand Down
2 changes: 1 addition & 1 deletion bento_lib/package.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = bento_lib
version = 8.0.0
version = 8.1.0
authors = David Lougheed, Paul Pillot
author_emails = [email protected], [email protected]
Loading