Skip to content

Commit

Permalink
Merge pull request #63 from SELab-2/csrf_fixes
Browse files Browse the repository at this point in the history
Oplossing CSRF probleem
  • Loading branch information
LGDTimtou authored Mar 14, 2024
2 parents d244e99 + 814e1a2 commit 437e06a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 7 additions & 0 deletions api/middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.conf import settings
from django.shortcuts import redirect
from rest_framework.authentication import SessionAuthentication


class RedirectAnonymousUserMiddleware:
Expand Down Expand Up @@ -30,3 +31,9 @@ def __call__(self, request):
return redirect(settings.LOGIN_URL)

return self.get_response(request)


class CsrfExemptSessionAuthentication(SessionAuthentication):

def enforce_csrf(self, request):
return # To not perform the csrf check previously happening
7 changes: 7 additions & 0 deletions api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,10 @@

LOGIN_URL = "django_auth_adfs:login"
LOGIN_REDIRECT_URL = "/login_redirect"

REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.BasicAuthentication",
"api.middleware.CsrfExemptSessionAuthentication",
]
}
4 changes: 2 additions & 2 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "api.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand All @@ -18,5 +18,5 @@ def main():
execute_from_command_line(sys.argv)


if __name__ == '__main__':
if __name__ == "__main__":
main()

0 comments on commit 437e06a

Please sign in to comment.