| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import os
- class apikey:
- """
- This class represents API key, and it is also creating new apikeys from
- device random numbers generator.
- Methods
- -------
- get_raw_size() : int
- Return size of bytes which was loaded from device random number
- generator.
- get_size() : int
- Return lenght of the API key as string.
- get_prefix() : str
- Return prefix of the API keys.
- create() : apikey
- Create new random API key.
- export() : str
- Export API key as string.
- """
- def __init__(self, content: str) -> None:
- """
- It recreate API key object from key, which was previously exported
- to string.
- Raises
- ------
- ValueError
- When API key lenght is not correct.
- Parameters
- ----------
- content : str
- Preiviously exported API key string.
- """
-
- if len(content) != self.get_size():
- raise ValueError("API key lenght is not correct.")
- self.__content = content
-
- @staticmethod
- def get_raw_size() -> int:
- """
- It return lenght of the random bytes in the API key.
- Returns
- -------
- int
- Lenght of the random bytes in the API key.
- """
- return 128
- @staticmethod
- def get_size() -> int:
- """
- It return lenght of the API key string, with prefix and random bytes
- converted to the hex string.
- Returns
- -------
- int
- Lenght of the API key string.
- """
- return len(apikey.get_prefix()) + apikey.get_raw_size() * 2
- @staticmethod
- def get_prefix() -> str:
- """
- It return API key prefix.
- Returns
- -------
- str
- Prefix for the API key.
- """
- return "cxtk_"
- @staticmethod
- def create() -> object:
- """
- It create new API key from random number generator device.
- Returns
- -------
- apikey
- New random API key.
- """
- raw = os.urandom(apikey.get_raw_size())
- key = apikey.get_prefix() + raw.hex()
- return apikey(key)
- def export(self) -> str:
- """
- It export API key as string.
- Returns
- -------
- str
- API key string content.
- """
- return self.__content
- def __repr__(self) -> str:
- """
- It return readable API key description (first 32 letters).
- Returns
- -------
- str
- Readable API key description.
- """
- return "API key: \"" + self.__content[0:32] + "...\""
- def __str__(self) -> str:
- """
- It return API key string content.
- Returns
- -------
- str
- API key as string.
- """
- return self.__content
- def __eq__(self, other) -> bool:
- """
- It check that API keys are same.
- Returns
- -------
- bool
- True when API keys are same.
- """
- return self.__content == other.__content
- def __ne__(self, other) -> bool:
- """
- It check that API keys are not same.
- Returns
- -------
- bool
- True when API keys are not same.
- """
- return self.__content != other.__content
|