From 1a3f4e2aab12427f2151e4e788b01102b412c564 Mon Sep 17 00:00:00 2001 From: Daniel Mursa Date: Mon, 16 Dec 2024 10:49:06 +0100 Subject: [PATCH] [#485] Create Step and Model --- .../setup_configuration/models/token_auth.py | 24 +++++++ .../setup_configuration/steps/token_auth.py | 72 +++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/objects/setup_configuration/models/token_auth.py create mode 100644 src/objects/setup_configuration/steps/token_auth.py diff --git a/src/objects/setup_configuration/models/token_auth.py b/src/objects/setup_configuration/models/token_auth.py new file mode 100644 index 00000000..f85d1730 --- /dev/null +++ b/src/objects/setup_configuration/models/token_auth.py @@ -0,0 +1,24 @@ +from django_setup_configuration.models import ConfigurationModel +from pydantic import Field + +from objects.token.models import TokenAuth + + +class TokenAuthConfigurationModel(ConfigurationModel): + class Meta: + django_model_refs = { + TokenAuth: ( + "identifier", + "token", + "contact_person", + "email", + "organization", + "application", + "administration", + "is_superuser", + ) + } + + +class TokenAuthGroupConfigurationModel(ConfigurationModel): + items: list[TokenAuthConfigurationModel] diff --git a/src/objects/setup_configuration/steps/token_auth.py b/src/objects/setup_configuration/steps/token_auth.py new file mode 100644 index 00000000..817acf3b --- /dev/null +++ b/src/objects/setup_configuration/steps/token_auth.py @@ -0,0 +1,72 @@ +import logging + +from django.core.exceptions import ValidationError +from django.db import IntegrityError + +from django_setup_configuration.configuration import BaseConfigurationStep +from django_setup_configuration.exceptions import ConfigurationRunFailed + +from objects.setup_configuration.models.token_auth import ( + TokenAuthGroupConfigurationModel, +) +from objects.token.models import TokenAuth + +logger = logging.getLogger(__name__) + + +class TokenAuthConfigurationStep( + BaseConfigurationStep[TokenAuthGroupConfigurationModel] +): + """ + Configure tokens for other applications to access Objects API + """ + + namespace = "token_tokenauth" + enable_setting = "token_tokenauth_config_enable" + + verbose_name = "Configuration to set up authentication tokens for objects" + config_model = TokenAuthGroupConfigurationModel + + def execute(self, model: TokenAuthGroupConfigurationModel) -> None: + for item in model.items: + logger.info(f"Configuring {item.identifier}") + + model_kwargs = { + "identifier": item.identifier, + "token": item.token, + "contact_person": item.contact_person, + "email": item.email, + "organization": item.organization, + "application": item.application, + "administration": item.administration, + "is_superuser": item.is_superuser, + } + + token_instance = TokenAuth(**model_kwargs) + + try: + token_instance.full_clean(exclude=("id",), validate_unique=False) + except ValidationError as exception: + exception_message = ( + f"Validation error(s) occured for {item.identifier}." + ) + raise ConfigurationRunFailed(exception_message) from exception + + logger.debug(f"No validation errors found for {item.identifier}") + + try: + logger.debug(f"Saving {item.identifier}") + + TokenAuth.objects.update_or_create( + identifier=item.identifier, + defaults={ + key: value + for key, value in model_kwargs.items() + if key != "identifier" + }, + ) + except IntegrityError as exception: + exception_message = f"Failed configuring token {item.identifier}." + raise ConfigurationRunFailed(exception_message) from exception + + logger.info(f"Configured {item.identifier}")