44 lines
1.4 KiB
Python
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,
|
|
)
|