dump-things-service/dump_things_service/auth/__init__.py
Christian Monch 1d1a70fbd5 feat: add policy handling
To support exact specification of user-IDs,
`AuthenticationInfo` now contains a
`source_identifier`. This will identify the
authentication source that was used. All
`AuthenticationSource`s are instantiated with
a `source_identifier`, chosen by the caller.

Add user-id -> policy mapping to collection
configuration

Adapt code to extended user ID information, i.e. to
(authentication_instance_id, user_id),

feat: add policy handling to SQLite backend

This commit implements policy handling in the
SQLite backend. That means, result records
are filtered by an optional default policy, a
set of active policies, and the policies that
are set in the annotations of a record.
2026-10-01 22:19:10 +02:00

57 lines
1.3 KiB
Python

"""Token-based authentication handlers
The authentication handlers are used to authenticate a token and to
determine:
- the permissions associated with the token
- the user id associated with the token
- the incoming_label to be used with the token
"""
from __future__ import annotations
import abc
import dataclasses
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from dump_things_service.abstract_config import TokenPermission
class AuthenticationError(Exception):
"""Exception for dumpthings authentication errors."""
class InvalidTokenError(AuthenticationError):
"""Exception for invalid token errors."""
@dataclasses.dataclass
class AuthenticationInfo:
source_identifier: str
token_permission: TokenPermission
user_id: str
incoming_label: str
class AuthenticationSource(metaclass=abc.ABCMeta):
def __init__(
self,
source_identifier: str,
):
self.source_identifier = source_identifier
@abc.abstractmethod
def authenticate(
self,
token: str,
) -> AuthenticationInfo:
"""
Authenticate a user based on the provided token and collection.
:param token: The authentication token.
:return: AuthenticationInfo
:raises AuthenticationError: If authentication fails.
"""
raise NotImplementedError