oidc-fastapi-test/src/oidc_test/settings.py

151 lines
4.1 KiB
Python
Raw Normal View History

from os import environ
2025-01-02 11:23:53 +01:00
import string
import random
from typing import Type, Tuple
from pathlib import Path
2025-01-02 11:23:53 +01:00
2025-01-20 04:35:33 +01:00
from pydantic import BaseModel, computed_field, AnyUrl
2025-01-02 11:23:53 +01:00
from pydantic_settings import (
BaseSettings,
2025-01-10 19:18:57 +01:00
SettingsConfigDict,
2025-01-02 11:23:53 +01:00
PydanticBaseSettingsSource,
YamlConfigSettingsSource,
)
from starlette.requests import Request
2025-01-02 11:23:53 +01:00
2025-01-19 01:48:00 +01:00
class Resource(BaseModel):
"""A resource with an URL that can be accessed with an OAuth2 access token"""
resource_name: str
2025-01-19 01:48:00 +01:00
name: str
url: str
2025-01-19 01:48:00 +01:00
class AuthProviderSettings(BaseModel):
"""Auth provider, can also be a resource server"""
2025-01-19 01:48:00 +01:00
2025-01-10 00:09:12 +01:00
id: str
name: str
url: str
client_id: str
2025-01-02 11:23:53 +01:00
client_secret: str = ""
2025-01-16 05:43:26 +01:00
# For PKCE (not implemented yet)
code_challenge_method: str | None = None
hint: str = "No hint"
2025-01-19 01:48:00 +01:00
resources: list[Resource] = []
account_url_template: str | None = None
info_url: str | None = (
None # Used eg. for Keycloak's public key (see https://stackoverflow.com/questions/54318633/getting-keycloaks-public-key)
)
public_key: str | None = None
public_key_url: str | None = None
2025-02-02 15:54:44 +01:00
signature_alg: str = "RS256"
2025-02-04 02:27:32 +01:00
resource_provider_scopes: list[str] = []
session_key: str = "sid"
skip_verify_signature: bool = True
disabled: bool = False
2025-01-02 11:23:53 +01:00
@computed_field
@property
2025-01-09 23:41:32 +01:00
def openid_configuration(self) -> str:
2025-01-02 11:23:53 +01:00
return self.url + "/.well-known/openid-configuration"
@computed_field
@property
def token_url(self) -> str:
return "auth/" + self.id
2025-01-02 11:23:53 +01:00
2025-02-10 14:14:32 +01:00
def get_account_url(self, request: Request, user: dict) -> str | None:
if self.account_url_template:
if not (self.url.endswith("/") or self.account_url_template.startswith("/")):
sep = "/"
else:
sep = ""
return self.url + sep + self.account_url_template.format(request=request, user=user)
else:
return None
2025-01-20 01:16:17 +01:00
class ResourceProvider(BaseModel):
id: str
name: str
2025-01-20 04:35:33 +01:00
base_url: AnyUrl
2025-01-20 01:16:17 +01:00
resources: list[Resource] = []
class AuthSettings(BaseModel):
2025-01-02 11:23:53 +01:00
show_session_details: bool = False
providers: list[AuthProviderSettings] = []
2025-01-02 11:23:53 +01:00
swagger_provider: str = ""
class Insecure(BaseModel):
"""Warning: changing these defaults are only suitable for debugging"""
skip_verify_signature: bool = False
2025-02-17 02:42:38 +01:00
class DB(BaseModel):
host: str = "localhost"
port: int = 5432
db: str = "oidc-test"
user: str = "oidc-test"
password: str = "oidc-test"
debug: bool = False
pool_size: int = 10
max_overflow: int = 10
echo: bool = False
@property
def sqla_url(self):
return (
f"postgresql+asyncpg://{self.user}:{self.password}@{self.host}:{self.port}/{self.db}"
)
def get_pg_url(self):
return f"postgresql://{self.user}:{self.password}@{self.host}:{self.port}/{self.db}"
2025-01-02 11:23:53 +01:00
class Settings(BaseSettings):
"""Settings wil be read from an .env file"""
model_config = SettingsConfigDict(env_nested_delimiter="__")
auth: AuthSettings = AuthSettings()
2025-01-20 04:35:33 +01:00
resource_providers: list[ResourceProvider] = []
2025-01-02 11:23:53 +01:00
secret_key: str = "".join(random.choice(string.ascii_letters) for _ in range(16))
log: bool = False
insecure: Insecure = Insecure()
2025-02-17 02:42:38 +01:00
db: DB = DB()
2025-01-31 00:12:50 +01:00
cors_origins: list[str] = []
debug_token: bool = False
2025-02-08 01:55:36 +01:00
show_token: bool = False
2025-01-02 11:23:53 +01:00
@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, ...]:
return (
init_settings,
env_settings,
file_secret_settings,
YamlConfigSettingsSource(
settings_cls,
Path(
Path(
environ.get("OIDC_TEST_SETTINGS_FILE", Path.cwd() / "settings.yaml"),
)
),
),
2025-01-02 11:23:53 +01:00
dotenv_settings,
)
settings = Settings()