Skip to content

Commit

Permalink
Add SSL middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Feb 1, 2024
1 parent 4914a3c commit 22055da
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion onadata/libs/utils/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from django.conf import settings
from django.db import OperationalError, connection
from django.http import HttpResponseNotAllowed
from django.http import HttpResponseNotAllowed, HttpResponseRedirect
from django.middleware.locale import LocaleMiddleware
from django.template import loader
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -120,3 +120,11 @@ def process_exception(self, request, exception):
settings.ALREADY_RAISED = False

return None

class SSLMiddleware(LocaleMiddleware):

def process_request(self, request):
if not any([settings.DEBUG, request.is_secure(), request.META.get("HTTP_X_FORWARDED_PROTO", "") == 'https']):
url = request.build_absolute_uri(request.get_full_path())
secure_url = url.replace("http://", "https://")
return HttpResponseRedirect(secure_url)

0 comments on commit 22055da

Please sign in to comment.