|
| 1 | +import json |
1 | 2 | import secrets |
2 | | -from typing import Literal |
| 3 | +from typing import Annotated, Any, Literal |
3 | 4 |
|
4 | 5 | from jose.constants import ALGORITHMS |
| 6 | +from pydantic import AnyUrl, BeforeValidator, HttpUrl, computed_field |
5 | 7 | from pydantic_settings import BaseSettings, SettingsConfigDict |
6 | 8 |
|
7 | 9 |
|
| 10 | +def parse_cors_origins(v: Any) -> list[str] | str: # noqa: ANN401 |
| 11 | + if isinstance(v, str): |
| 12 | + if v == "*": |
| 13 | + return "*" |
| 14 | + if not v.startswith("["): |
| 15 | + return [i.strip() for i in v.split(",") if i.strip()] |
| 16 | + if isinstance(v, list): |
| 17 | + for idx, item in enumerate(v): # type: ignore[misc] |
| 18 | + if not isinstance(item, str): |
| 19 | + raise TypeError(f"CORS_ALLOW_ORIGINS list item at index {idx} must be string, got {type(item)}") # type: ignore[arg-type] |
| 20 | + return v # type: ignore[return-value] |
| 21 | + raise ValueError(f"CORS_ALLOW_ORIGINS must be string or list, got {type(v)}") |
| 22 | + |
| 23 | + |
| 24 | +def parse_sidebar_links(v: Any) -> list[dict[str, str]]: # noqa: ANN401 |
| 25 | + if isinstance(v, str): |
| 26 | + if not v.strip(): |
| 27 | + return [] |
| 28 | + try: |
| 29 | + parsed: Any = json.loads(v) |
| 30 | + except json.JSONDecodeError as e: |
| 31 | + raise ValueError(f"Invalid JSON in SIDEBAR_LINKS_JSON: {e}") |
| 32 | + |
| 33 | + if not isinstance(parsed, list): |
| 34 | + raise ValueError("SIDEBAR_LINKS_JSON must be a JSON array") |
| 35 | + |
| 36 | + for idx, link in enumerate(parsed): # type: ignore[misc] |
| 37 | + if not isinstance(link, dict): |
| 38 | + raise ValueError(f"Link at index {idx} must be an object") |
| 39 | + required = {"icon", "url", "title"} |
| 40 | + if not required.issubset(link.keys()): # type: ignore[arg-type] |
| 41 | + raise ValueError(f"Link at index {idx} missing fields: {required - link.keys()}") |
| 42 | + |
| 43 | + # Validate URL is valid http/https |
| 44 | + try: |
| 45 | + HttpUrl(link["url"]) # type: ignore[arg-type] |
| 46 | + except Exception as e: |
| 47 | + raise ValueError(f"Link at index {idx} has invalid URL: {e}") |
| 48 | + |
| 49 | + return parsed # type: ignore[return-value] |
| 50 | + |
| 51 | + if isinstance(v, list): |
| 52 | + return v # type: ignore[return-value] |
| 53 | + |
| 54 | + raise ValueError(f"SIDEBAR_LINKS_JSON must be string or list, got {type(v)}") |
| 55 | + |
| 56 | + |
8 | 57 | class Settings(BaseSettings): |
9 | | - model_config = SettingsConfigDict(env_file=("dev.env", ".env", "prod.env"), extra="ignore") |
| 58 | + model_config = SettingsConfigDict( |
| 59 | + env_file=("dev.env", ".env", "prod.env"), |
| 60 | + env_file_encoding="utf-8", |
| 61 | + extra="ignore", |
| 62 | + case_sensitive=True, |
| 63 | + ) |
| 64 | + |
10 | 65 | API_V1_STR: str = "/api/v1" |
11 | 66 | SECRET_KEY: str = secrets.token_urlsafe(32) |
12 | | - |
13 | 67 | DEBUG: bool = False |
14 | | - |
15 | 68 | ENVIRONMENT: Literal["dev", "prod"] = "prod" |
16 | 69 |
|
| 70 | + # OpenID Connect |
17 | 71 | OIDC_CLIENT_ID: str = "bureaublad-frontend" |
18 | 72 | OIDC_CLIENT_SECRET: str | None = None |
19 | | - OIDC_AUTHORIZATION_ENDPOINT: str = ( |
20 | | - "https://id.la-suite.apps.digilab.network/realms/lasuite/protocol/openid-connect/auth" |
21 | | - ) |
22 | | - OIDC_TOKEN_ENDPOINT: str = "https://id.la-suite.apps.digilab.network/realms/lasuite/protocol/openid-connect/token" # noqa: S105 |
23 | | - OIDC_JWKS_ENDPOINT: str = "https://id.la-suite.apps.digilab.network/realms/lasuite/protocol/openid-connect/certs" |
| 73 | + OIDC_AUTHORIZATION_ENDPOINT: str = "" |
| 74 | + OIDC_TOKEN_ENDPOINT: str = "" |
| 75 | + OIDC_JWKS_ENDPOINT: str = "" |
| 76 | + OIDC_USERNAME_CLAIM: str = "preferred_username" |
24 | 77 | OIDC_SCOPES: dict[str, str] = {"openid": "", "profile": "", "email": ""} |
25 | 78 | OIDC_AUDIENCE: str = "account" |
26 | | - OIDC_ISSUER: str = "https://id.la-suite.apps.digilab.network/realms/lasuite" |
| 79 | + OIDC_ISSUER: str = "" |
27 | 80 | OIDC_SIGNATURE_ALGORITM: str | list[str] = [ALGORITHMS.RS256, ALGORITHMS.HS256] |
28 | 81 |
|
29 | | - OCS_URL: str = "https://files.la-suite.apps.digilab.network" |
| 82 | + # La Suite Services |
| 83 | + OCS_URL: str | None = None |
30 | 84 | OCS_AUDIENCE: str = "files" |
31 | | - DOCS_URL: str = "https://docs.la-suite.apps.digilab.network" |
| 85 | + DOCS_URL: str | None = None |
32 | 86 | DOCS_AUDIENCE: str = "docs" |
33 | | - DRIVE_URL: str = "http://localhost:3001" |
34 | | - DRIVE_AUDIENCE: str = "drive" |
35 | | - MEET_URL: str = "http://localhost:8085" |
36 | | - MEET_AUDIENCE: str = "meet" |
37 | | - CALENDAR_URL: str = "https://files.la-suite.apps.digilab.network" |
| 87 | + CALENDAR_URL: str | None = None |
38 | 88 | CALENDAR_AUDIENCE: str = "files" |
39 | | - TASK_URL: str = "https://files.la-suite.apps.digilab.network" |
| 89 | + TASK_URL: str | None = None |
40 | 90 | TASK_AUDIENCE: str = "files" |
41 | | - AI_BASE_URL: str = "https://api.openai.com/v1/" |
42 | | - AI_MODEL: str = "gpt-4o" |
| 91 | + DRIVE_URL: str | None = None |
| 92 | + DRIVE_AUDIENCE: str = "drive" |
| 93 | + MEET_URL: str | None = None |
| 94 | + MEET_AUDIENCE: str = "meet" |
| 95 | + |
| 96 | + # AI Integration |
| 97 | + AI_BASE_URL: str | None = "https://api.openai.com/v1/" |
| 98 | + AI_MODEL: str | None = "gpt-4o" |
43 | 99 | AI_API_KEY: str | None = None |
44 | | - ZAKEN_URL: str = "https://open-zaak.commonground.apps.digilab.network" |
45 | | - ZAKEN_AUDIENCE: str = "openzaak" |
46 | | - OPENZAAK_CLIENT_ID: str = "" |
47 | | - OPENZAAK_SECRET: str = "" |
48 | 100 |
|
49 | | - CORS_ALLOW_ORIGINS: str | list[str] = "*" |
| 101 | + # Grist |
| 102 | + GRIST_BASE_URL: str | None = None |
| 103 | + |
| 104 | + THEME_CSS_URL: str = "" |
| 105 | + |
| 106 | + SIDEBAR_LINKS_JSON: Annotated[list[dict[str, str]], BeforeValidator(parse_sidebar_links)] = [] |
| 107 | + |
| 108 | + CORS_ALLOW_ORIGINS: Annotated[list[AnyUrl] | str, BeforeValidator(parse_cors_origins)] = [] |
50 | 109 | CORS_ALLOW_CREDENTIALS: bool = False |
51 | 110 | CORS_ALLOW_METHODS: list[str] = ["*"] |
52 | 111 | CORS_ALLOW_HEADERS: list[str] = ["*"] |
53 | 112 |
|
| 113 | + @computed_field # type: ignore[prop-decorator] |
| 114 | + @property |
| 115 | + def ocs_enabled(self) -> bool: |
| 116 | + return self.OCS_URL is not None |
| 117 | + |
| 118 | + @computed_field # type: ignore[prop-decorator] |
| 119 | + @property |
| 120 | + def docs_enabled(self) -> bool: |
| 121 | + return self.DOCS_URL is not None |
| 122 | + |
| 123 | + @computed_field # type: ignore[prop-decorator] |
| 124 | + @property |
| 125 | + def calendar_enabled(self) -> bool: |
| 126 | + return self.CALENDAR_URL is not None |
| 127 | + |
| 128 | + @computed_field # type: ignore[prop-decorator] |
| 129 | + @property |
| 130 | + def task_enabled(self) -> bool: |
| 131 | + return self.TASK_URL is not None |
| 132 | + |
| 133 | + @computed_field # type: ignore[prop-decorator] |
| 134 | + @property |
| 135 | + def drive_enabled(self) -> bool: |
| 136 | + return self.DRIVE_URL is not None |
| 137 | + |
| 138 | + @computed_field # type: ignore[prop-decorator] |
| 139 | + @property |
| 140 | + def meet_enabled(self) -> bool: |
| 141 | + return self.MEET_URL is not None |
| 142 | + |
| 143 | + @computed_field # type: ignore[prop-decorator] |
| 144 | + @property |
| 145 | + def ai_enabled(self) -> bool: |
| 146 | + return self.AI_BASE_URL is not None and self.AI_MODEL is not None and self.AI_API_KEY is not None |
| 147 | + |
| 148 | + @computed_field # type: ignore[prop-decorator] |
| 149 | + @property |
| 150 | + def grist_enabled(self) -> bool: |
| 151 | + return self.GRIST_BASE_URL is not None |
| 152 | + |
| 153 | + @computed_field # type: ignore[prop-decorator] |
| 154 | + @property |
| 155 | + def oidc_discovery_endpoint(self) -> str: |
| 156 | + if self.OIDC_ISSUER: |
| 157 | + return f"{self.OIDC_ISSUER.rstrip('/')}/.well-known/openid-configuration" |
| 158 | + return "" |
| 159 | + |
| 160 | + @computed_field # type: ignore[prop-decorator] |
| 161 | + @property |
| 162 | + def all_cors_origins(self) -> list[str]: |
| 163 | + if isinstance(self.CORS_ALLOW_ORIGINS, str): |
| 164 | + return [self.CORS_ALLOW_ORIGINS] |
| 165 | + return [str(origin).rstrip("/") for origin in self.CORS_ALLOW_ORIGINS] |
| 166 | + |
54 | 167 |
|
55 | 168 | settings = Settings() # type: ignore[reportCallIssue] |
0 commit comments