2024-06-20 14:46:23 +00:00
|
|
|
import secrets
|
2021-07-25 11:35:36 +00:00
|
|
|
import string
|
|
|
|
|
2019-06-10 21:24:35 +00:00
|
|
|
|
2024-06-20 14:46:23 +00:00
|
|
|
def get_random_string(length):
|
2019-06-10 21:24:35 +00:00
|
|
|
"""
|
2024-06-20 14:46:23 +00:00
|
|
|
Create a random crypto-safe sequence of 'length' or more characters
|
|
|
|
Possible characters: alphanumeric, '-' and '_'
|
|
|
|
Make sure that it starts from alphanumeric for better compatibility with yaml files
|
2019-06-10 21:24:35 +00:00
|
|
|
"""
|
2024-06-20 14:46:23 +00:00
|
|
|
token = secrets.token_urlsafe(length)
|
|
|
|
for _ in range(10):
|
|
|
|
if not (token.startswith("-") or token.startswith("_")):
|
|
|
|
break
|
|
|
|
token = secrets.token_urlsafe(length)
|
2019-06-10 21:24:35 +00:00
|
|
|
|
2024-06-20 14:46:23 +00:00
|
|
|
return token
|
2019-06-10 21:24:35 +00:00
|
|
|
|
|
|
|
|
2024-06-20 14:46:23 +00:00
|
|
|
def get_client_id(
|
|
|
|
length: int = 30, allowed_chars: str = string.ascii_uppercase + string.digits
|
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
Create a random client id composed of 'length' upper case characters or digits
|
2019-06-10 21:24:35 +00:00
|
|
|
"""
|
2024-06-20 14:46:23 +00:00
|
|
|
return "".join(secrets.choice(allowed_chars) for _ in range(length))
|
2019-06-10 21:24:35 +00:00
|
|
|
|
|
|
|
|
2021-07-25 11:35:36 +00:00
|
|
|
def get_secret_key(length: int = 50) -> str:
|
2019-06-10 21:24:35 +00:00
|
|
|
"""
|
2024-06-20 14:46:23 +00:00
|
|
|
Create a random secret key
|
2019-06-10 21:24:35 +00:00
|
|
|
"""
|
2024-06-20 14:46:23 +00:00
|
|
|
return get_random_string(length)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print(get_client_id())
|
|
|
|
print(get_secret_key())
|