feat: add TOML config file support with client registrations
This commit is contained in:
parent
94f777fc8f
commit
61ca3063ca
2 changed files with 111 additions and 2 deletions
|
|
@ -1,7 +1,10 @@
|
|||
# src/porchlight/config.py
|
||||
import os
|
||||
from enum import StrEnum
|
||||
from typing import Any
|
||||
|
||||
from pydantic_settings import BaseSettings
|
||||
from pydantic import BaseModel
|
||||
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource, TomlConfigSettingsSource
|
||||
|
||||
|
||||
class StorageBackend(StrEnum):
|
||||
|
|
@ -9,8 +12,21 @@ class StorageBackend(StrEnum):
|
|||
MONGODB = "mongodb"
|
||||
|
||||
|
||||
class ClientConfig(BaseModel):
|
||||
client_secret: str
|
||||
redirect_uris: list[str]
|
||||
response_types: list[str] = ["code"]
|
||||
scope: list[str] = ["openid"]
|
||||
token_endpoint_auth_method: str = "client_secret_basic"
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
model_config = {"env_prefix": "OIDC_OP_"}
|
||||
model_config = {"env_prefix": "OIDC_OP_", "toml_file": "porchlight.toml"}
|
||||
|
||||
# Class-level bridge to pass _toml_file into the classmethod
|
||||
# settings_customise_sources. Not thread-safe, but Settings is
|
||||
# only instantiated once at startup.
|
||||
_toml_file_override: str | None = None
|
||||
|
||||
# Core
|
||||
issuer: str
|
||||
|
|
@ -40,3 +56,33 @@ class Settings(BaseSettings):
|
|||
|
||||
# Theme
|
||||
theme: str = "default"
|
||||
|
||||
# OIDC clients
|
||||
clients: dict[str, ClientConfig] = {}
|
||||
|
||||
def __init__(self, _toml_file: str | None = None, **kwargs: Any) -> None:
|
||||
Settings._toml_file_override = _toml_file
|
||||
try:
|
||||
super().__init__(**kwargs)
|
||||
finally:
|
||||
Settings._toml_file_override = None
|
||||
|
||||
@classmethod
|
||||
def settings_customise_sources(
|
||||
cls,
|
||||
settings_cls: type[BaseSettings],
|
||||
init_settings: PydanticBaseSettingsSource,
|
||||
env_settings: PydanticBaseSettingsSource,
|
||||
dotenv_settings: PydanticBaseSettingsSource,
|
||||
file_secret_settings: PydanticBaseSettingsSource,
|
||||
) -> tuple[PydanticBaseSettingsSource, ...]:
|
||||
toml_file = (
|
||||
cls._toml_file_override
|
||||
or os.environ.get("OIDC_OP_CONFIG_FILE")
|
||||
or settings_cls.model_config.get("toml_file")
|
||||
)
|
||||
return (
|
||||
env_settings,
|
||||
TomlConfigSettingsSource(settings_cls, toml_file=toml_file),
|
||||
init_settings,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue