diff --git a/src/briar/api/api.py b/src/briar/api/api.py index b47d6de7f5be502649d25413b58c46d646c8fc99..a5aab812b361cf0e75df4d671ca49e73b1f99c5e 100644 --- a/src/briar/api/api.py +++ b/src/briar/api/api.py @@ -2,6 +2,8 @@ # SPDX-License-Identifier: AGPL-3.0-only # License-Filename: LICENSE.md +from briar.api.constants import Constants + from subprocess import Popen, PIPE, STDOUT from threading import Thread from time import sleep @@ -11,10 +13,13 @@ from urllib.request import urlopen class Api: + auth_token = None _process = None def __init__(self, headless_jar): self._command = ['java', '-jar', headless_jar] + self.constants = Constants() + self._load_auth_token() def has_account(self): from pathlib import Path @@ -57,9 +62,10 @@ class Api: while self.is_running(): try: sleep(0.1) - print(urlopen("http://localhost:7000/").getcode()) + print(urlopen(self.constants._get_base_url()).getcode()) except HTTPError as e: if(e.code == 404): + self._load_auth_token() callback(True) return except URLError as e: @@ -78,3 +84,10 @@ class Api: self._process.communicate((credentials[0] + '\n' + credentials[1] + '\n' + credentials[1] + '\n').encode("utf-8")) + + def _load_auth_token(self): + if not self.has_account(): + return + with open(self.constants.get_auth_token(), 'r') as file: + self.auth_token = file.read() + diff --git a/src/briar/api/constants.py b/src/briar/api/constants.py new file mode 100644 index 0000000000000000000000000000000000000000..aab8391ebfa75b4d354707ac85e7f6797ab810a8 --- /dev/null +++ b/src/briar/api/constants.py @@ -0,0 +1,22 @@ +# Copyright (c) 2019 Nico Alt +# SPDX-License-Identifier: AGPL-3.0-only +# License-Filename: LICENSE.md + +from os.path import join +from pathlib import Path +from urllib.parse import urljoin + + +class Constants: + + _BRIAR_AUTH_TOKEN = 'auth_token' + _BRIAR_DIR = '.briar' + + _HOST = 'http://localhost:7000' + _VERSION_SUFFIX = 'v1' + + def get_auth_token(self): + return join(Path.home(), self._BRIAR_DIR, self._BRIAR_AUTH_TOKEN) + + def _get_base_url(self): + return urljoin(self._HOST, self._VERSION_SUFFIX)