Newer
Older
# Copyright (c) 2019 Nico Alt
# SPDX-License-Identifier: AGPL-3.0-only
# License-Filename: LICENSE.md
"""
Central API wrapper handling login and registration
"""
from briar_wrapper.api_thread import ApiThread
from briar_wrapper.constants import BRIAR_AUTH_TOKEN, BRIAR_DB
from briar_wrapper.models.socket_listener import SocketListener
"""
Briar's authentication token
[Upstream documentation](https://code.briarproject.org/briar/briar/blob/master/briar-headless/README.md#how-to-use)
"""
"""
`briar_wrapper.api.Api`'s instance of
`briar_wrapper.models.socket_listener.SocketListener`
"""
"""
Initialize with path to Briar Headless JAR `headless_jar`
"""
self._api_thread = ApiThread(self, headless_jar)
"""
Checks if `briar_wrapper.constants.BRIAR_DB` exists
.. versionadded:: 0.0.3
"""
"""
Returns `True` if `briar_wrapper.api_thread.ApiThread` is running
.. versionadded:: 0.0.3
"""
def login(self, password, callback):
"""
Login to Briar API with `password`.
Calls `callback` once login process finished.
.. versionadded:: 0.0.3
"""
startup_thread = Thread(target=self._api_thread.login,
args=(password,), daemon=True)
def register(self, credentials, callback):
"""
Register at Briar API with 2-tuple `credentials`.
Calls `callback` once registration process finished.
.. versionadded:: 0.0.3
"""
if len(credentials) != 2:
raise Exception("Can't process credentials")
self._start_and_watch(callback)
startup_thread = Thread(target=self._api_thread.register,
args=(credentials,), daemon=True)
"""
Stops API wrapper
.. versionadded:: 0.0.3
"""
self._api_thread.start()
self._api_thread.watch(callback)
def on_successful_startup(self, callback):
"""
Called by `briar_wrapper.api_thread.ApiThread` if startup finished
successfully.
Should not be called from outside `briar_wrapper`.
"""
self._load_auth_token()
self.socket_listener = SocketListener(self)
callback(True)
raise Exception("Can't load authentication token")
with open(BRIAR_AUTH_TOKEN, 'r') as file: