1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-11-19 22:10:09 +01:00

Persistent secrets

This commit is contained in:
Piero Toffanin 2023-03-11 12:06:36 -05:00
parent 683d155b1e
commit 3fe629f8cf
2 changed files with 15 additions and 2 deletions

View File

@ -24,5 +24,9 @@ def get_current_secret():
def setup(args): def setup(args):
if args.api_keys and args.require_api_key_secret: if args.api_keys and args.require_api_key_secret:
s = get_storage() s = get_storage()
s.set_str("secret_0", generate_secret())
s.set_str("secret_1", generate_secret()) if not s.exists("secret_0"):
s.set_str("secret_0", generate_secret())
if not s.exists("secret_1"):
s.set_str("secret_1", generate_secret())

View File

@ -5,6 +5,9 @@ def get_storage():
return storage return storage
class Storage: class Storage:
def exists(self, key):
raise Exception("not implemented")
def set_bool(self, key, value): def set_bool(self, key, value):
raise Exception("not implemented") raise Exception("not implemented")
def get_bool(self, key): def get_bool(self, key):
@ -38,6 +41,9 @@ class MemoryStorage(Storage):
def __init__(self): def __init__(self):
self.store = {} self.store = {}
def exists(self, key):
return key in self.store
def set_bool(self, key, value): def set_bool(self, key, value):
self.store[key] = bool(value) self.store[key] = bool(value)
@ -98,6 +104,9 @@ class RedisStorage(Storage):
self.conn = redis.from_url(redis_uri) self.conn = redis.from_url(redis_uri)
self.conn.ping() self.conn.ping()
def exists(self, key):
return bool(self.conn.exists(key))
def set_bool(self, key, value): def set_bool(self, key, value):
self.conn.set(key, "1" if value else "0") self.conn.set(key, "1" if value else "0")