diff --git a/.env.dev b/.env.dev index f13026e2615327d574325d16672719a9c2867baa..84d8ca19bb679436854bf6da8885a81ce31da7c9 100644 --- a/.env.dev +++ b/.env.dev @@ -12,3 +12,6 @@ MUSIC_DIRECTORY_PATH=/music BROWSABLE_API_ENABLED=True FORWARDED_PROTO=http LDAP_ENABLED=False + +# Uncomment this if you're using traefik/https +# FORCE_HTTPS_URLS=True diff --git a/api/config/settings/local.py b/api/config/settings/local.py index 91a202b6465b099588002c5ee4b99a07c9a9ad3a..ce0a72cfc7942238e74728943dbc10fda16a4e59 100644 --- a/api/config/settings/local.py +++ b/api/config/settings/local.py @@ -14,6 +14,7 @@ from .common import * # noqa # DEBUG # ------------------------------------------------------------------------------ DEBUG = env.bool("DJANGO_DEBUG", default=True) +FORCE_HTTPS_URLS = env.bool("FORCE_HTTPS_URLS", default=False) TEMPLATES[0]["OPTIONS"]["debug"] = DEBUG # SECRET CONFIGURATION @@ -80,3 +81,5 @@ CSRF_TRUSTED_ORIGINS = [o for o in ALLOWED_HOSTS] if env.bool("WEAK_PASSWORDS", default=False): # Faster during tests PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",) + +MIDDLEWARE = ("funkwhale_api.common.middleware.DevHttpsMiddleware",) + MIDDLEWARE diff --git a/api/funkwhale_api/common/middleware.py b/api/funkwhale_api/common/middleware.py index 090d67c9e5c7a6370379649d1a0c3d4719d9c493..96e9c45a667dbda91800c443b08a4b5191c7baa5 100644 --- a/api/funkwhale_api/common/middleware.py +++ b/api/funkwhale_api/common/middleware.py @@ -135,3 +135,25 @@ class SPAFallbackMiddleware: return serve_spa(request) return response + + +class DevHttpsMiddleware: + """ + In development, it's sometimes difficult to have django use HTTPS + when we have django behind nginx behind traefix. + + We thus use a simple setting (in dev ONLY) to control that. + """ + + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + if settings.FORCE_HTTPS_URLS: + setattr(request.__class__, "scheme", "https") + setattr( + request, + "get_host", + lambda: request.__class__.get_host(request).replace(":80", ":443"), + ) + return self.get_response(request)