From 41be25557f41124f732d88a70a01a4360fbe3dd3 Mon Sep 17 00:00:00 2001
From: Sylvain Le Bon <sylvain@startinblox.com>
Date: Wed, 12 Jul 2023 18:34:10 +0200
Subject: [PATCH] update: Django 4.2 compatibility

---
 djangoldp_account/admin.py          |  5 ++---
 djangoldp_account/auth/backends.py  |  4 ++--
 djangoldp_account/djangoldp_urls.py | 12 ++++++------
 djangoldp_account/permissions.py    |  2 +-
 djangoldp_account/views.py          |  4 ++--
 5 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/djangoldp_account/admin.py b/djangoldp_account/admin.py
index 3a67e51..4f8938c 100644
--- a/djangoldp_account/admin.py
+++ b/djangoldp_account/admin.py
@@ -6,6 +6,7 @@ from djangoldp_account.models import LDPUser
 from .models import Account, ChatProfile
 
 
+@admin.register(Account, ChatProfile)
 class EmptyAdmin(admin.ModelAdmin):
     def get_model_perms(self, request):
         return {}
@@ -28,6 +29,7 @@ class ChatProfileInline(admin.StackedInline):
     extra = 0
 
 
+@admin.register(LDPUser)
 class LDPUserAdmin(DjangoLDPUserAdmin):
     exclude = ('is_backlink', 'allow_create_backlink')
     form = LDPUserChangeForm
@@ -45,6 +47,3 @@ class LDPUserAdmin(DjangoLDPUserAdmin):
     )
 
 
-admin.site.register(LDPUser, LDPUserAdmin)
-admin.site.register(Account, EmptyAdmin)
-admin.site.register(ChatProfile, EmptyAdmin)
\ No newline at end of file
diff --git a/djangoldp_account/auth/backends.py b/djangoldp_account/auth/backends.py
index 27230b5..1188b6a 100644
--- a/djangoldp_account/auth/backends.py
+++ b/djangoldp_account/auth/backends.py
@@ -157,8 +157,8 @@ class ExternalUserBackend(ModelBackend):
         return self._get_or_create_then_authenticate({}, jwt['webid'])
 
     def authenticate(self, request, username=None, password=None, **kwargs):
-        if 'HTTP_AUTHORIZATION' in request.META:
-            jwt = request.META['HTTP_AUTHORIZATION']
+        if 'authorization' in request.headers:
+            jwt = request.headers['authorization']
             if jwt.lower().startswith("dpop"):
                 jwt = jwt[5:]
                 return self.validate_dpop_token(request, jwt)
diff --git a/djangoldp_account/djangoldp_urls.py b/djangoldp_account/djangoldp_urls.py
index 93633ad..b601d04 100644
--- a/djangoldp_account/djangoldp_urls.py
+++ b/djangoldp_account/djangoldp_urls.py
@@ -34,16 +34,16 @@ urlpatterns = [
         name='django_registration_register',
     ),
     path('auth/login/', LDPAccountLoginView.as_view(),name='login'),
-    re_path(r'^auth/', include('django_registration.backends.activation.urls')),
-    re_path(r'^auth/', include('django.contrib.auth.urls')),
-    re_path(r'^accounts/', LDPViewSet.urls(model=Account, permission_classes=[LDPPermissions], model_prefix='pk_lookup',
+    path('auth/', include('django_registration.backends.activation.urls')),
+    path('auth/', include('django.contrib.auth.urls')),
+    path('accounts/', LDPViewSet.urls(model=Account, permission_classes=[LDPPermissions], model_prefix='pk_lookup',
                                        lookup_field='pk')),
-    re_path(r'^chat-profile/', LDPViewSet.urls(model=ChatProfile, permission_classes=[LDPPermissions],
+    path('chat-profile/', LDPViewSet.urls(model=ChatProfile, permission_classes=[LDPPermissions],
                                            model_prefix='pk_lookup', lookup_field='pk')),
     re_path(r'^oidc/login/callback/?$', RPLoginCallBackView.as_view(), name='oidc_login_callback'),
     re_path(r'^oidc/login/?$', RPLoginView.as_view(), name='oidc_login'),
     re_path(r'^userinfo/?$', csrf_exempt(userinfocustom)),
     re_path(r'^check-user/?$', csrf_exempt(check_user)),
-    re_path(r'^redirect-default/$', RedirectView.as_view(),name='redirect-default'),
-    re_path(r'^', include('oidc_provider.urls', namespace='oidc_provider'))
+    path('redirect-default/', RedirectView.as_view(),name='redirect-default'),
+    path('', include('oidc_provider.urls', namespace='oidc_provider'))
 ]
diff --git a/djangoldp_account/permissions.py b/djangoldp_account/permissions.py
index 28dfad1..7692ee3 100644
--- a/djangoldp_account/permissions.py
+++ b/djangoldp_account/permissions.py
@@ -10,7 +10,7 @@ if hasattr(settings, 'XMPP_SERVER_IP'):
     XMPP_SERVERS = XMPP_SERVERS.union(getattr(settings, 'XMPP_SERVER_IP'))
 
 def check_client_ip(request):
-    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
+    x_forwarded_for = request.headers.get('x-forwarded-for')
     if x_forwarded_for:
         ip = x_forwarded_for.replace(' ', '').split(',')
     else:
diff --git a/djangoldp_account/views.py b/djangoldp_account/views.py
index 82039b4..df7306b 100644
--- a/djangoldp_account/views.py
+++ b/djangoldp_account/views.py
@@ -37,7 +37,7 @@ def check_user(request, *args, **kwargs):
     '''Returns user if they are authenticated with this server, else 404'''
     response = HttpResponse({}, status=200)
     if request.method in ['GET', 'HEAD', 'OPTIONS']:
-        response['Access-Control-Allow-Origin'] = request.META.get('HTTP_ORIGIN')
+        response['Access-Control-Allow-Origin'] = request.headers.get('origin')
         response["Access-Control-Allow-Headers"] = \
             oidc_settings.get('OIDC_ACCESS_CONTROL_ALLOW_HEADERS')
         response["Access-Control-Allow-Credentials"] = 'true'
@@ -54,7 +54,7 @@ def check_user(request, *args, **kwargs):
             response_body['dpop'] = request.headers['Dpop']
 
         response = JsonResponse(settings.userinfo(response_body, request.user))
-        response['Access-Control-Allow-Origin'] = request.META.get('HTTP_ORIGIN')
+        response['Access-Control-Allow-Origin'] = request.headers.get('origin')
         response["Access-Control-Allow-Headers"] = \
             oidc_settings.get('OIDC_ACCESS_CONTROL_ALLOW_HEADERS')
         response["Access-Control-Expose-Headers"] = "Location, User"
-- 
GitLab