dump-things-server/dump_things_service/auth/config.py
Christian Monch 8b98cda7e6
Some checks failed
Test execution / Test-all (push) Failing after 57s
[temp] move token-based authentication to abstract_config
2026-04-28 11:12:53 +02:00

44 lines
1.4 KiB
Python

"""Use configuration information to fetch token permissions, ids, and incoming_label """
from dump_things_service.abstract_config import Configuration
from dump_things_service.auth import (
AuthenticationInfo,
AuthenticationSource,
InvalidTokenError,
)
from dump_things_service.abstract_config import (
get_permissions,
get_token_config_for_representation_and_collection,
)
class ConfigAuthenticationSource(AuthenticationSource):
def __init__(
self,
abstract_configuration: Configuration,
collection_name: str,
):
self.abstract_configuration = abstract_configuration
self.collection_name = collection_name
def authenticate(
self,
token_representation: str,
) -> AuthenticationInfo:
result = get_token_config_for_representation_and_collection(
self.abstract_configuration,
self.collection_name,
token_representation,
)
if not result:
msg = f'Token not valid for collection `{self.collection_name}`'
raise InvalidTokenError(msg)
_, token_config, token_collection_config = result
return AuthenticationInfo(
token_permission=get_permissions(token_collection_config.mode),
user_id=token_config.user_id,
incoming_label=token_collection_config.incoming_label,
)