Skip to content

Commit

Permalink
Store alternative instance url on mwdb login (#29)
Browse files Browse the repository at this point in the history
* Store alternative instance url on `mwdb login`

* Update docs
  • Loading branch information
nazywam committed Jan 21, 2021
1 parent df00793 commit 3a1f3fb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ If you want, you can also provide your API key instead of storing your password
Just copy your API token from `/profile` view and paste into the CLI. All secrets will be stored in `keyring`
and your username will be saved in `~/.mwdb` file.

.. versionadded:: 3.4.1

If you're using mwdblib to interact with your self-hosted mwdb-core instance, you can now pass the api url using the
standard --api-url option and it will be automatically saved in the config file.


Looking for recent data
-----------------------

Expand Down
6 changes: 5 additions & 1 deletion src/cli/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ def get_authenticated_mwdb(self, api_url=None):
mwdb.login(warn=False)
return mwdb

def store_login(self, username, password, api_key):
def store_login(self, username, password, api_key, api_url=None):
"""
Sets credentials into user configuration file and keyring
:param username: Username to store
:param password: Password to store
:param api_key: API key to store
:param api_url: Alternative API url to store
"""
if api_key is not None:
api = APIClient(api_key=api_key)
Expand All @@ -65,6 +66,9 @@ def store_login(self, username, password, api_key):
self.set_config("username", username)
keyring.set_password("mwdb", username, password)

if api_url is not None:
self.set_config("api_url", api_url)

def reset_login(self):
"""
Removes credentials from user configuration file and keyring
Expand Down
6 changes: 4 additions & 2 deletions src/cli/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ def login_command(ctx, username, password, via_api_key, api_key):
username = click.prompt("Username")
if password is None:
password = click.prompt("Password", hide_input=True)

api_url = ctx.obj.get("api_url", None)
authenticator = MwdbAuthenticator()
authenticator.store_login(username, password, api_key)
authenticator.store_login(username, password, api_key, api_url)
try:
mwdb = authenticator.get_authenticated_mwdb(ctx.obj.get("api_url", None))
mwdb = authenticator.get_authenticated_mwdb(api_url)
# todo: Find more appropriate way to check successful authentication
mwdb.query("", raise_not_found=False)
except InvalidCredentialsError:
Expand Down

0 comments on commit 3a1f3fb

Please sign in to comment.