This commit applies a subset of the fixes that were applied via `hatch check code --unsafe-fixes --fix`.
59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from fastapi import (
|
|
HTTPException,
|
|
)
|
|
|
|
from dump_things_service import (
|
|
HTTP_401_UNAUTHORIZED,
|
|
)
|
|
from dump_things_service.abstract_config import (
|
|
check_collection,
|
|
read_config,
|
|
)
|
|
from dump_things_service.backends.schema_type_layer import _SchemaTypeLayer
|
|
from dump_things_service.instance_state import get_instance_state
|
|
from dump_things_service.utils import (
|
|
authenticate_token,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from dump_things_service.auth import AuthenticationInfo
|
|
from dump_things_service.backends import StorageBackend
|
|
from dump_things_service.store.model_store import _ModelStore
|
|
|
|
|
|
def get_store_and_backend(
|
|
collection: str,
|
|
plain_token: str | None,
|
|
) -> tuple[_ModelStore, StorageBackend, AuthenticationInfo]:
|
|
# A token is required
|
|
if plain_token is None:
|
|
raise HTTPException(
|
|
status_code=HTTP_401_UNAUTHORIZED,
|
|
detail='token required',
|
|
)
|
|
|
|
instance_state = get_instance_state()
|
|
abstract_config = read_config(instance_state.store_path)
|
|
|
|
# Check that the collection exists
|
|
check_collection(abstract_config=abstract_config, collection=collection)
|
|
|
|
# Get token permissions
|
|
auth_info = authenticate_token(instance_state, collection, plain_token)
|
|
permissions = auth_info.token_permission
|
|
if permissions.curated_write is False:
|
|
raise HTTPException(
|
|
status_code=HTTP_401_UNAUTHORIZED,
|
|
detail=f'no write access to curated area of collection `{collection}`',
|
|
)
|
|
|
|
# Get the curated model store
|
|
model_store = instance_state.curated_stores[collection]
|
|
backend = model_store.backend
|
|
if isinstance(backend, _SchemaTypeLayer):
|
|
return model_store, backend.backend, auth_info
|
|
return model_store, backend, auth_info
|