dump-things-service/dump_things_service/auth/__init__.py
Christian Monch bdf67a9efb
Some checks failed
Codespell / Check for spelling errors (pull_request) Failing after 35s
Ruff / Code linting (push) Successful in 40s
Codespell / Check for spelling errors (push) Failing after 42s
Ruff / Code linting (pull_request) Successful in 43s
Type annotation (PR) / check (pull_request) Successful in 1m9s
Test execution / Test-all (push) Successful in 1m9s
refactor: reformat code, remove unused imports
2026-10-17 11:45:09 +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