Skip to content

Commit

Permalink
Add DRFSessionAuthentication401Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatz committed Dec 20, 2024
1 parent 1964929 commit d9ad639
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 38 deletions.
18 changes: 18 additions & 0 deletions django_utils_lib/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,21 @@ def __call__(self, request: HttpRequest) -> HttpResponse:
response["Location"] = redirect_url.geturl()

return response


try:
from rest_framework.authentication import SessionAuthentication
from rest_framework.request import Request as DRFRequest

class DRFSessionAuthentication401Middleware(SessionAuthentication):
"""
Small shim around DRF's SessionAuthentication, so it returns 401s for
unauthorized, instead of 403.
https://github.com/encode/django-rest-framework/issues/5968
"""

def authenticate_header(self, request: DRFRequest):
return "Session"
except ImportError:
pass
137 changes: 99 additions & 38 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pytest-playwright = "^0.5.2"
filelock = "^3.16.0"
debugpy = "^1.8.5"
django = "*"
djangorestframework = "^3.15.2"
djangorestframework-stubs = {extras = ["compatible-mypy"], version = "^3.15.2"}

[tool.ruff]
line-length = 120
Expand Down

0 comments on commit d9ad639

Please sign in to comment.