From e000f9e8daf92789078dce5b380baa557ef5019d Mon Sep 17 00:00:00 2001 From: Andrey Rusakov Date: Tue, 1 Oct 2024 13:00:16 +0200 Subject: [PATCH] Add check for the OIDC configuration --- default_settings.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/default_settings.py b/default_settings.py index d2bb703c..62f85aa7 100644 --- a/default_settings.py +++ b/default_settings.py @@ -1,6 +1,7 @@ import os import requests from dotenv import load_dotenv +from django.core.exceptions import ImproperlyConfigured from django.utils.translation import gettext_lazy as _ load_dotenv() @@ -306,7 +307,18 @@ def discover_endpoints(discovery_url: str) -> dict: "django.contrib.auth.backends.ModelBackend", ) -OIDC_ENABLED = os.environ.get("OIDC_ENABLED", "False") == "True" +def check_oidc() -> bool: + if os.environ.get("OIDC_ENABLED", "False") == "False": + return False + missing = [] + for x in ["OIDC_RP_CLIENT_ID", "ZITADEL_PROJECT", "OIDC_OP_BASE_URL", "OIDC_PRIVATE_KEYFILE"]: + if not os.environ.get(x): + missing.append(x) + if missing: + raise ImproperlyConfigured(f"OIDC is enabled, but missing required parameters {missing}") + return True + +OIDC_ENABLED = check_oidc() if OIDC_ENABLED: INSTALLED_APPS.append('mozilla_django_oidc') MIDDLEWARE.append('mozilla_django_oidc.middleware.SessionRefresh')