apiVersion: v1
kind: ConfigMap
metadata:
  name: django-configmap
  namespace: eav
data:
  production.py: |
    # production.py


    from .base import *

    from datetime import timedelta

    import os

    from django.core.exceptions import ImproperlyConfigured


    INSTALLED_APPS.append("corsheaders")

    #MIDDLEWARE = ["corsheaders.middleware.CorsMiddleware"] + MIDDLEWARE


    # DEBUG SETTINGS START

    DEBUG = True

    ALLOWED_HOSTS = ['*']

    # DEBUG SETTINGS END


    # DATABASE SETTINGS START

    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.postgresql",
            "NAME": os.getenv("DJANGO_POSTGRES_DATABASE"),
            "USER": os.getenv("DJANGO_POSTGRES_USER"),
            "PASSWORD": os.getenv("DJANGO_POSTGRES_PASSWORD"),
            "HOST": os.getenv("DJANGO_POSTGRES_HOST"),
            "PORT": "5432",
        }
    }

    # DATABASE SETTINGS END


    # RESPONSE HEADERS START


    CORS_ORIGIN_ALLOW_ALL = True


    CORS_ALLOWED_ORIGINS = [
        "https://srx.wb.ru",
    ]


    CORS_TRUSTED_ORIGINS = [
        "https://srx.wb.ru",
    ]


    CSRF_TRUSTED_ORIGINS = [
        "https://srx.wb.ru",
    ]


    CORS_ALLOW_METHODS = (
        'DELETE',
        'GET',
        'OPTIONS',
        'PATCH',
        'POST',
        'PUT',
    )


    CORS_ALLOW_HEADERS = (
        'accept',
        'accept-encoding',
        'authorization',
        'content-type',
        'user-agent',
        'x-csrftoken',
        'x-requested-with',
        'x-token',
        'Bearer'
    )

    # RESPONSE HEADERS END


    REST_FRAMEWORK = {
        "DEFAULT_PAGINATION_CLASS": (
            "rest_framework.pagination.LimitOffsetPagination"
        ),
        "DEFAULT_SCHEMA_CLASS": "rest_framework.schemas.coreapi.AutoSchema",
        "PAGE_SIZE": 10000,
        "DEFAULT_FILTER_BACKENDS": [
            "django_filters.rest_framework.DjangoFilterBackend"
        ],
        "DEFAULT_AUTHENTICATION_CLASSES": [
            "core.auth.ZitadelJWTAuthentication",
            "rest_framework_simplejwt.authentication.JWTStatelessUserAuthentication",
            #"rest_framework_simplejwt.authentication.JWTAuthentication",
            #"rest_framework.authentication.SessionAuthentication",
            #"rest_framework.authentication.BasicAuthentication",
        ],
        "DEFAULT_PERMISSION_CLASSES": [
            "rest_framework.permissions.AllowAny",
        ]
    }


    # JWT SETTINGS START

    def get_env_variable(var_name, default=None):
        try:
            return os.getenv(var_name, default)
        except KeyError:
            error_msg = f"Set the {var_name} environment variable"
            if default:
                return default
            raise ImproperlyConfigured(error_msg)

    SIMPLE_JWT_ISSUER = get_env_variable("SIMPLE_JWT_ISSUER", default="default_issuer")


    SIMPLE_JWT = {
        "ACCESS_TOKEN_LIFETIME": timedelta(minutes=5),
        "REFRESH_TOKEN_LIFETIME": timedelta(days=1),
        "ROTATE_REFRESH_TOKENS": False,
        "UPDATE_LAST_LOGIN": False,

        "ALGORITHM": "RS512",
        "SIGNING_KEY": get_env_variable("JWT_PRIVATE_KEY").replace("\\\n", "\n"),
        "VERIFYING_KEY": get_env_variable("JWT_PUBLIC_KEY").replace("\\\n", "\n"),
        "AUDIENCE": None,
        "ISSUER": SIMPLE_JWT_ISSUER,

        "AUTH_HEADER_TYPES": ("Bearer",),
        "AUTH_HEADER_NAME": "HTTP_AUTHORIZATION",
        "USER_ID_FIELD": "id",
        "USER_ID_CLAIM": "user_id",

        "AUTH_TOKEN_CLASSES": ("rest_framework_simplejwt.tokens.AccessToken",),
        "TOKEN_TYPE_CLAIM": "token_type",

        "JTI_CLAIM": "jti",

        "SLIDING_TOKEN_REFRESH_EXP_CLAIM": "refresh_exp",
        "SLIDING_TOKEN_LIFETIME": timedelta(minutes=5),
        "SLIDING_TOKEN_REFRESH_LIFETIME": timedelta(days=1),
    }


    # JWT SETTINGS END


    STATIC_ROOT = '/static/'

    STATIC_URL = '/static/'

    STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'


    SESSION_COOKIE_NAME = 'eav-sessionid'

    CSRF_COOKIE_NAME = 'eav-csrftoken'
