1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-12-18 08:27:03 +02:00
LibreTranslate/libretranslate/secret.py

35 lines
964 B
Python
Raw Normal View History

2023-03-10 05:00:27 +02:00
import atexit
import random
import string
2023-03-10 06:07:12 +02:00
from multiprocessing.dummy import Value
2023-03-10 05:00:27 +02:00
from libretranslate.storage import get_storage
from apscheduler.schedulers.background import BackgroundScheduler
setup_secrets = Value('b', False)
def generate_secret():
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=7))
def rotate_secrets():
s = get_storage()
secret_1 = s.get_str("secret_1")
s.set_str("secret_0", secret_1)
s.set_str("secret_1", generate_secret())
2023-03-10 06:07:12 +02:00
print(s.get_str("secret_0"))
print(s.get_str("secret_1"))
2023-03-10 05:00:27 +02:00
def secret_match(secret):
s = get_storage()
return secret == s.get_str("secret_0") or secret == s.get_str("secret_1")
def get_current_secret():
return get_storage().get_str("secret_1")
2023-03-10 06:07:12 +02:00
def setup(args):
if args.api_keys and args.require_api_key_secret:
2023-03-10 05:00:27 +02:00
s = get_storage()
s.set_str("secret_0", generate_secret())
s.set_str("secret_1", generate_secret())