import os from pathlib import Path from datetime import timedelta BASE_DIR = Path(__file__).resolve().parent.parent # Load a .env file if present (development convenience; production uses real env vars) try: from dotenv import load_dotenv load_dotenv(BASE_DIR / '.env') except ImportError: pass # ── Core ─────────────────────────────────────────────────────────────────────── SECRET_KEY = os.environ.get( 'DJANGO_SECRET_KEY', 'django-insecure-dev-secret-key-change-in-production-xyz123', ) DEBUG = os.environ.get('DJANGO_DEBUG', 'True').lower() in ('true', '1', 'yes') ALLOWED_HOSTS = [ h.strip() for h in os.environ.get('DJANGO_ALLOWED_HOSTS', 'localhost,127.0.0.1').split(',') if h.strip() ] INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'rest_framework', 'oauth2_provider', 'corsheaders', 'api', ] MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'oauth2_provider.middleware.OAuth2TokenMiddleware', ] ROOT_URLCONF = 'config.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [BASE_DIR / 'templates'], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'config.wsgi.application' DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } AUTH_PASSWORD_VALIDATORS = [ {'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'}, {'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator'}, {'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator'}, {'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator'}, ] LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_TZ = True STATIC_URL = 'static/' STATIC_ROOT = BASE_DIR / 'staticfiles' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' # ── Auth redirects ───────────────────────────────────────────────────────────── LOGIN_URL = '/accounts/login/' LOGIN_REDIRECT_URL = '/o/authorize/' LOGOUT_REDIRECT_URL = os.environ.get('LOGOUT_REDIRECT_URL', 'http://localhost:5173/') # ── Django REST Framework ────────────────────────────────────────────────────── REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'oauth2_provider.contrib.rest_framework.OAuth2Authentication', ], 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.IsAuthenticated', ], } # ── Django OAuth Toolkit ─────────────────────────────────────────────────────── OAUTH2_PROVIDER = { 'PKCE_REQUIRED': True, 'ACCESS_TOKEN_EXPIRE_SECONDS': 3600, 'REFRESH_TOKEN_EXPIRE_SECONDS': 60 * 60 * 24 * 7, 'ROTATE_REFRESH_TOKEN': True, 'SCOPES': { 'read': 'Read access', 'write': 'Write access', }, 'DEFAULT_SCOPES': ['read'], 'ALLOWED_REDIRECT_URI_SCHEMES': ['http', 'https'], } AUTHENTICATION_BACKENDS = [ 'oauth2_provider.backends.OAuth2Backend', 'django.contrib.auth.backends.ModelBackend', ] # ── CORS ─────────────────────────────────────────────────────────────────────── # In production the frontend is co-located (same origin), so CORS isn't needed. # Keep it configured here so the dev setup (Vite on :5173) still works. CORS_ALLOWED_ORIGINS = [ o.strip() for o in os.environ.get( 'CORS_ALLOWED_ORIGINS', 'http://localhost:5173,http://127.0.0.1:5173', ).split(',') if o.strip() ] CORS_ALLOW_CREDENTIALS = True CORS_ALLOW_HEADERS = [ 'accept', 'accept-encoding', 'authorization', 'content-type', 'dnt', 'origin', 'user-agent', 'x-csrftoken', 'x-requested-with', ] # ── WebAuthn / Touch ID ──────────────────────────────────────────────────────── # RP_ID must be the effective domain of the origin (no port, no scheme). # WEBAUTHN_ORIGIN must be the exact origin browsers will use to reach the app. WEBAUTHN_RP_ID = os.environ.get('WEBAUTHN_RP_ID', 'localhost') WEBAUTHN_ORIGIN = os.environ.get('WEBAUTHN_ORIGIN', 'http://localhost:5173') # ── Production HTTPS hardening ───────────────────────────────────────────────── # These only activate when DEBUG=False. Assumes nginx terminates TLS and sets # the X-Forwarded-Proto header before proxying to Gunicorn/Django. if not DEBUG: SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') SECURE_HSTS_SECONDS = 31536000 SECURE_HSTS_INCLUDE_SUBDOMAINS = True SECURE_HSTS_PRELOAD = True SESSION_COOKIE_SECURE = True CSRF_COOKIE_SECURE = True