|
|
@@ -0,0 +1,117 @@
|
|
|
+import typing
|
|
|
+
|
|
|
+from .user import user
|
|
|
+from .user import user_builder
|
|
|
+from .user_loader import user_loader
|
|
|
+from .secret import secret
|
|
|
+from .secret import secret_builder
|
|
|
+from .secret_coder import secret_coder
|
|
|
+from .secret_loader import secret_loader
|
|
|
+from .application_part import application_part
|
|
|
+from .apikey import bad_apikey_exception
|
|
|
+from .validators import name_validator
|
|
|
+from .validators import domain_validator
|
|
|
+
|
|
|
+class secret_response:
|
|
|
+ def __init__(self, target: secret | secret_builder) -> None:
|
|
|
+ if isinstance(target, secret):
|
|
|
+ self.__target = target
|
|
|
+ return
|
|
|
+
|
|
|
+ self.__target = target.result
|
|
|
+
|
|
|
+ @property
|
|
|
+ def response(self) -> dict:
|
|
|
+ return {
|
|
|
+ "name": self.target.name,
|
|
|
+ "domain": self.target.domain,
|
|
|
+ "coded" : self.target.coded
|
|
|
+ }
|
|
|
+
|
|
|
+ @property
|
|
|
+ def target(self) -> secret:
|
|
|
+ return self.__target
|
|
|
+
|
|
|
+ @property
|
|
|
+ def builder(self) -> secret_builder:
|
|
|
+ return secret_builder(self.__target)
|
|
|
+
|
|
|
+class secret_collection_response:
|
|
|
+ def __init__(self, target: typing.Iterable[secret] | None = None) -> None:
|
|
|
+ self.__collection = []
|
|
|
+
|
|
|
+ if target is None:
|
|
|
+ return
|
|
|
+
|
|
|
+ if type(target) is list:
|
|
|
+ self.__collection = target.copy()
|
|
|
+ return
|
|
|
+
|
|
|
+ for count in target:
|
|
|
+ self.__collection.append(count)
|
|
|
+
|
|
|
+ @property
|
|
|
+ def response(self) -> dict:
|
|
|
+ return [ count.response for count in self.collection ]
|
|
|
+
|
|
|
+ @property
|
|
|
+ def collection(self) -> list:
|
|
|
+ return self.__collection.copy()
|
|
|
+
|
|
|
+ def append(self, target: secret | secret_builder) -> object:
|
|
|
+ if isinstance(target, secret_builder):
|
|
|
+ target = target.result
|
|
|
+
|
|
|
+ self.__collection.append(target)
|
|
|
+ return self
|
|
|
+
|
|
|
+class application_secret(application_part):
|
|
|
+ def get(self, apikey: str, name: str) -> dict:
|
|
|
+ with self.__secret_database(apikey) as loader:
|
|
|
+ target = loader.load_by_name(name)
|
|
|
+
|
|
|
+ if target is None:
|
|
|
+ return self.__not_found_response()
|
|
|
+
|
|
|
+ return secret_response(target).response
|
|
|
+
|
|
|
+ def create(self, apikey: str, name: str, domain: str, coded: str) -> dict:
|
|
|
+ validation = self._validation("name", name_validator(name))
|
|
|
+ validation = validation or self._validation(
|
|
|
+ "domain",
|
|
|
+ domain_validator(domain)
|
|
|
+ )
|
|
|
+
|
|
|
+ if validation is not None:
|
|
|
+ return validation
|
|
|
+
|
|
|
+ if not secret_coder.validate(coded):
|
|
|
+ return self._fail_response(cause = "Invalid coded secret")
|
|
|
+
|
|
|
+ with self.__secret_database(apikey) as loader:
|
|
|
+ builder = secret_builder()
|
|
|
+ builder.name = name
|
|
|
+ builder.domain = domain
|
|
|
+ builder.coded = coded
|
|
|
+ builder.owner = loader.owner
|
|
|
+
|
|
|
+ loader.append(builder.result)
|
|
|
+
|
|
|
+ return self._success_response()
|
|
|
+
|
|
|
+ @property
|
|
|
+ def __not_found_response(self) -> dict:
|
|
|
+ return self._fail_response(cause = "Secret not found.")
|
|
|
+
|
|
|
+ @property
|
|
|
+ def __user_database(self) -> user_loader:
|
|
|
+ return user_loader(self._connector)
|
|
|
+
|
|
|
+ def __secret_database(self, apikey: str) -> secret_loader:
|
|
|
+ with self.__user_database as loader:
|
|
|
+ target = loader.get_by_apikey(apikey)
|
|
|
+
|
|
|
+ if target is None:
|
|
|
+ raise bad_apikey_exception()
|
|
|
+
|
|
|
+ return secret_loader(self._connector, target)
|