secret.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import sqlmodel
  2. import hashlib
  3. import Crypto
  4. from .user import user
  5. from .secret_crypto import secret_crypto
  6. class secret(sqlmodel.SQLModel, table = True):
  7. """
  8. This class represents secret in the database.
  9. """
  10. id: int | None = sqlmodel.Field(default = None, primary_key = True)
  11. name: str | None = sqlmodel.Field(default = None, index = True)
  12. domain: str | None = sqlmodel.Field(default = None, index = True)
  13. crypted: bytes | None = sqlmodel.Field(default = None)
  14. nonce: bytes | None = sqlmodel.Field(default = None)
  15. owner: int | None = sqlmodel.Field(default = None, foreign_key = "user.id")
  16. class secret_builder:
  17. """
  18. This class is responsible for creating new secrets for the user.
  19. """
  20. def __init__(self) -> None:
  21. """
  22. This function create new clean builder.
  23. """
  24. self.clean()
  25. def clean(self) -> None:
  26. """
  27. This function clean builder, and prepare it to build new secret.
  28. """
  29. self.__target = secret()
  30. @property
  31. def ready(self) -> bool:
  32. """ True when secret is ready, false when not. """
  33. if self.__target.name is None or self.__target.domain is None:
  34. return False
  35. if self.__target.crypted is None or self.__target.nonce is None:
  36. return False
  37. if self.__target.owner is None:
  38. return False
  39. return True
  40. @property
  41. def owner(self) -> int | None:
  42. """ This return ID of the secret owner, or None if not set. """
  43. return self.__target.owner
  44. @owner.setter
  45. def owner(self, target: user):
  46. """ This set new owner of the secret. """
  47. self.__target.owner = user.id
  48. @property
  49. def result(self) -> secret:
  50. """ This return ready secret if it is ready, or raise Exception. """
  51. if not self.ready:
  52. raise TypeError("Secret is not ready to load.")
  53. return self.__target
  54. @property
  55. def name(self) -> str | None:
  56. """ This return name of the secret or None if not set. """
  57. return self.__target.name
  58. @property
  59. def domain(self) -> str | None:
  60. """ This return domain of the secret or None if not set. """
  61. return self.__target.domain
  62. @name.setter
  63. def name(self, target: str) -> None:
  64. """ This set name of the secret. """
  65. self.__target.name = target
  66. @domain.setter
  67. def domain(self, target: str) -> None:
  68. """ This set domain of the secret. """
  69. self.__target.domain = target
  70. def crypt(self, key: str, target: str) -> None:
  71. """
  72. This function crypt secret. It require password which could decrypt
  73. it laser, and target secret. It automatic set crypted and nonce secret
  74. fields. If secret already has nonce, then it use it. When secret nonce
  75. is empty, then it would provide new random nonce for secret.
  76. Parameters:
  77. key (str): Password to protect secret
  78. targer (str): Secret to encrypt
  79. """
  80. crypter = secret_crypto(key)
  81. if self.__target.nonce is not None:
  82. crypter.set_iv(self.__target.nonce)
  83. crypted, nonce = crypter.crypted(target)
  84. self.__target.crypted = crypted
  85. self.__target.nonce = nonce