Inline get_config, rewrite update_config, DB init Config and rp_id defaults changed.
This commit is contained in:
@@ -47,12 +47,11 @@ from paskia.db.operations import (
|
|||||||
delete_session,
|
delete_session,
|
||||||
delete_sessions_for_user,
|
delete_sessions_for_user,
|
||||||
delete_user,
|
delete_user,
|
||||||
get_config,
|
|
||||||
login,
|
login,
|
||||||
remove_permission_from_org,
|
remove_permission_from_org,
|
||||||
remove_permission_from_role,
|
remove_permission_from_role,
|
||||||
set_config,
|
|
||||||
set_session_host,
|
set_session_host,
|
||||||
|
update_config,
|
||||||
update_credential_sign_count,
|
update_credential_sign_count,
|
||||||
update_org_name,
|
update_org_name,
|
||||||
update_permission,
|
update_permission,
|
||||||
@@ -64,6 +63,7 @@ from paskia.db.operations import (
|
|||||||
)
|
)
|
||||||
from paskia.db.structs import (
|
from paskia.db.structs import (
|
||||||
DB,
|
DB,
|
||||||
|
Config,
|
||||||
Credential,
|
Credential,
|
||||||
Org,
|
Org,
|
||||||
Permission,
|
Permission,
|
||||||
@@ -82,6 +82,7 @@ def data() -> DB:
|
|||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
# Types
|
# Types
|
||||||
|
"Config",
|
||||||
"Credential",
|
"Credential",
|
||||||
"DB",
|
"DB",
|
||||||
"Org",
|
"Org",
|
||||||
@@ -107,7 +108,6 @@ __all__ = [
|
|||||||
"build_session",
|
"build_session",
|
||||||
"build_user",
|
"build_user",
|
||||||
# Read ops
|
# Read ops
|
||||||
"get_config",
|
|
||||||
# Write ops
|
# Write ops
|
||||||
"add_permission_to_org",
|
"add_permission_to_org",
|
||||||
"add_permission_to_role",
|
"add_permission_to_role",
|
||||||
@@ -132,8 +132,8 @@ __all__ = [
|
|||||||
"login",
|
"login",
|
||||||
"remove_permission_from_org",
|
"remove_permission_from_org",
|
||||||
"remove_permission_from_role",
|
"remove_permission_from_role",
|
||||||
"set_config",
|
|
||||||
"set_session_host",
|
"set_session_host",
|
||||||
|
"update_config",
|
||||||
"update_credential_sign_count",
|
"update_credential_sign_count",
|
||||||
"update_org_name",
|
"update_org_name",
|
||||||
"update_permission",
|
"update_permission",
|
||||||
|
|||||||
+7
-17
@@ -32,7 +32,7 @@ from paskia.db.structs import (
|
|||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Global database instance (empty until init() loads data)
|
# Global database instance (empty until init() loads data)
|
||||||
_db = DB()
|
_db = DB(config=Config(rp_id="uninitialized.invalid"))
|
||||||
_store = JsonlStore(_db)
|
_store = JsonlStore(_db)
|
||||||
_db._store = _store
|
_db._store = _store
|
||||||
_initialized = False
|
_initialized = False
|
||||||
@@ -43,6 +43,12 @@ _initialized = False
|
|||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
async def update_config(config: Config) -> None:
|
||||||
|
"""Update the stored configuration."""
|
||||||
|
with _db.transaction("update_config"):
|
||||||
|
_db.config = config
|
||||||
|
|
||||||
|
|
||||||
def create_permission(perm: Permission, *, ctx: SessionContext | None = None) -> None:
|
def create_permission(perm: Permission, *, ctx: SessionContext | None = None) -> None:
|
||||||
"""Create a new permission."""
|
"""Create a new permission."""
|
||||||
if perm.uuid in _db.permissions:
|
if perm.uuid in _db.permissions:
|
||||||
@@ -580,19 +586,3 @@ def create_credential_session(
|
|||||||
if token:
|
if token:
|
||||||
token.delete()
|
token.delete()
|
||||||
return session.key
|
return session.key
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
|
||||||
# Config operations
|
|
||||||
# -------------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
def get_config() -> Config:
|
|
||||||
"""Get the stored configuration."""
|
|
||||||
return _db.config
|
|
||||||
|
|
||||||
|
|
||||||
async def set_config(config: Config) -> None:
|
|
||||||
"""Update the stored configuration."""
|
|
||||||
with _db.transaction("update_config"):
|
|
||||||
_db.config = config
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ from uuid import UUID
|
|||||||
|
|
||||||
import msgspec
|
import msgspec
|
||||||
import uuid7
|
import uuid7
|
||||||
from msgspec import field
|
|
||||||
|
|
||||||
from paskia import db
|
from paskia import db
|
||||||
from paskia.util import hostutil
|
from paskia.util import hostutil
|
||||||
@@ -534,6 +533,7 @@ class Config(msgspec.Struct, frozen=True, dict=True, omit_defaults=True):
|
|||||||
class DB(msgspec.Struct, dict=True, omit_defaults=False):
|
class DB(msgspec.Struct, dict=True, omit_defaults=False):
|
||||||
"""In-memory database. Access fields directly for reads."""
|
"""In-memory database. Access fields directly for reads."""
|
||||||
|
|
||||||
|
config: Config
|
||||||
permissions: dict[UUID, Permission] = {}
|
permissions: dict[UUID, Permission] = {}
|
||||||
orgs: dict[UUID, Org] = {}
|
orgs: dict[UUID, Org] = {}
|
||||||
roles: dict[UUID, Role] = {}
|
roles: dict[UUID, Role] = {}
|
||||||
@@ -541,7 +541,6 @@ class DB(msgspec.Struct, dict=True, omit_defaults=False):
|
|||||||
credentials: dict[UUID, Credential] = {}
|
credentials: dict[UUID, Credential] = {}
|
||||||
sessions: dict[str, Session] = {}
|
sessions: dict[str, Session] = {}
|
||||||
reset_tokens: dict[bytes, ResetToken] = {}
|
reset_tokens: dict[bytes, ResetToken] = {}
|
||||||
config: Config = field(default_factory=lambda: Config(rp_id="localhost"))
|
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
# Store reference for persistence (not serialized)
|
# Store reference for persistence (not serialized)
|
||||||
|
|||||||
@@ -10,10 +10,10 @@ from uvicorn import Config as UvicornConfig
|
|||||||
from uvicorn import Server
|
from uvicorn import Server
|
||||||
from uvicorn import run as uvicorn_run
|
from uvicorn import run as uvicorn_run
|
||||||
|
|
||||||
|
from paskia import db
|
||||||
from paskia import globals as _globals
|
from paskia import globals as _globals
|
||||||
from paskia.bootstrap import bootstrap_if_needed
|
from paskia.bootstrap import bootstrap_if_needed
|
||||||
from paskia.config import PaskiaConfig
|
from paskia.config import PaskiaConfig
|
||||||
from paskia.db import get_config, set_config
|
|
||||||
from paskia.db import init as db_init
|
from paskia.db import init as db_init
|
||||||
from paskia.db.background import flush
|
from paskia.db.background import flush
|
||||||
from paskia.db.structs import Config
|
from paskia.db.structs import Config
|
||||||
@@ -107,7 +107,7 @@ def main():
|
|||||||
|
|
||||||
# Init db and load stored config
|
# Init db and load stored config
|
||||||
asyncio.run(db_init(rp_id=args.rp_id))
|
asyncio.run(db_init(rp_id=args.rp_id))
|
||||||
stored_config = get_config()
|
stored_config = db.data().config
|
||||||
|
|
||||||
# Apply defaults from stored config
|
# Apply defaults from stored config
|
||||||
if args.rp_name is None and stored_config.rp_name is not None:
|
if args.rp_name is None and stored_config.rp_name is not None:
|
||||||
@@ -232,7 +232,7 @@ def main():
|
|||||||
await bootstrap_if_needed(config=cli_config)
|
await bootstrap_if_needed(config=cli_config)
|
||||||
# Also save config if --save was explicitly used (even without bootstrap)
|
# Also save config if --save was explicitly used (even without bootstrap)
|
||||||
if args.save:
|
if args.save:
|
||||||
await set_config(cli_config)
|
await db.update_config(cli_config)
|
||||||
await flush()
|
await flush()
|
||||||
|
|
||||||
if len(endpoints) > 1:
|
if len(endpoints) > 1:
|
||||||
|
|||||||
+2
-1
@@ -23,6 +23,7 @@ import paskia.db.operations as ops_db
|
|||||||
from paskia import globals as paskia_globals
|
from paskia import globals as paskia_globals
|
||||||
from paskia.authsession import reset_expires
|
from paskia.authsession import reset_expires
|
||||||
from paskia.db import (
|
from paskia.db import (
|
||||||
|
Config,
|
||||||
Credential,
|
Credential,
|
||||||
Org,
|
Org,
|
||||||
Permission,
|
Permission,
|
||||||
@@ -61,7 +62,7 @@ async def test_db() -> AsyncGenerator[DB, None]:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(suffix=".jsonl", delete=True) as f:
|
with tempfile.NamedTemporaryFile(suffix=".jsonl", delete=True) as f:
|
||||||
db = DB()
|
db = DB(config=Config(rp_id="test.example.com"))
|
||||||
store = JsonlStore(db, f.name)
|
store = JsonlStore(db, f.name)
|
||||||
db._store = store
|
db._store = store
|
||||||
await store.load()
|
await store.load()
|
||||||
|
|||||||
Reference in New Issue
Block a user