apikey.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import os
  2. class apikey:
  3. """
  4. This class represents API key, and it is also creating new apikeys from
  5. device random numbers generator.
  6. Methods
  7. -------
  8. get_raw_size() : int
  9. Return size of bytes which was loaded from device random number
  10. generator.
  11. get_size() : int
  12. Return lenght of the API key as string.
  13. get_prefix() : str
  14. Return prefix of the API keys.
  15. create() : apikey
  16. Create new random API key.
  17. export() : str
  18. Export API key as string.
  19. """
  20. def __init__(self, content: str) -> None:
  21. """
  22. It recreate API key object from key, which was previously exported
  23. to string.
  24. Raises
  25. ------
  26. ValueError
  27. When API key lenght is not correct.
  28. Parameters
  29. ----------
  30. content : str
  31. Preiviously exported API key string.
  32. """
  33. if len(content) != self.get_size():
  34. raise ValueError("API key lenght is not correct.")
  35. self.__content = content
  36. @staticmethod
  37. def get_raw_size() -> int:
  38. """
  39. It return lenght of the random bytes in the API key.
  40. Returns
  41. -------
  42. int
  43. Lenght of the random bytes in the API key.
  44. """
  45. return 128
  46. @staticmethod
  47. def get_size() -> int:
  48. """
  49. It return lenght of the API key string, with prefix and random bytes
  50. converted to the hex string.
  51. Returns
  52. -------
  53. int
  54. Lenght of the API key string.
  55. """
  56. return len(apikey.get_prefix()) + apikey.get_raw_size() * 2
  57. @staticmethod
  58. def get_prefix() -> str:
  59. """
  60. It return API key prefix.
  61. Returns
  62. -------
  63. str
  64. Prefix for the API key.
  65. """
  66. return "cxtk_"
  67. @staticmethod
  68. def create() -> object:
  69. """
  70. It create new API key from random number generator device.
  71. Returns
  72. -------
  73. apikey
  74. New random API key.
  75. """
  76. raw = os.urandom(apikey.get_raw_size())
  77. key = apikey.get_prefix() + raw.hex()
  78. return apikey(key)
  79. def export(self) -> str:
  80. """
  81. It export API key as string.
  82. Returns
  83. -------
  84. str
  85. API key string content.
  86. """
  87. return self.__content
  88. def __repr__(self) -> str:
  89. """
  90. It return readable API key description (first 32 letters).
  91. Returns
  92. -------
  93. str
  94. Readable API key description.
  95. """
  96. return "API key: \"" + self.__content[0:32] + "...\""
  97. def __str__(self) -> str:
  98. """
  99. It return API key string content.
  100. Returns
  101. -------
  102. str
  103. API key as string.
  104. """
  105. return self.__content
  106. def __eq__(self, other) -> bool:
  107. """
  108. It check that API keys are same.
  109. Returns
  110. -------
  111. bool
  112. True when API keys are same.
  113. """
  114. return self.__content == other.__content
  115. def __ne__(self, other) -> bool:
  116. """
  117. It check that API keys are not same.
  118. Returns
  119. -------
  120. bool
  121. True when API keys are not same.
  122. """
  123. return self.__content != other.__content