diff --git a/src/fw_api.py b/src/fw_api.py index 3dcae95..c631dac 100644 --- a/src/fw_api.py +++ b/src/fw_api.py @@ -1,9 +1,27 @@ from src.mpv_control import set_http_header import requests, json, time +import os from loguru import logger -with open('.auth.json', 'rt') as f: - auth = json.loads(f.read()) +auth_file = '.auth.json' +if os.path.exists(auth_file): + with open('.auth.json', 'rt') as f: + auth = json.loads(f.read()) +else: + # The default umask is 0o22 which turns off write permission of group and others + os.umask(0) + + descriptor = os.open( + path=auth_file, + flags=( + os.O_WRONLY # access mode: write only + | os.O_CREAT # create if not exists + | os.O_TRUNC # truncate the file to zero + ), + mode=0o600) + with open(descriptor, 'wt') as f: + f.write('{}') + auth = {} s = requests.Session() instance = 'fw.ponychord.rocks' @@ -24,7 +42,7 @@ else: def select_instance(new_instance=None): global instance instance = new_instance - with open('.auth.json', 'rt') as f: + with open(auth_file, 'rt') as f: auth = json.loads(f.read()) new_token = auth.get(instance) s.headers.update({"Authorization": None,