Skip to content

Commit b37bdd7

Browse files
committed
Misc auth/login fixes
- raise ClientError in MerginClient constructor if token is bad - fix CLI's creation of MerginClient with auth token (double "bearer") - nicely handle bad token error in CLI - only show token in CLI's "login" command
1 parent 79edc12 commit b37bdd7

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

mergin/cli.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def pretty_summary(summary):
8585
)
8686

8787

88-
def get_token(url, username, password, show_token=True):
88+
def get_token(url, username, password):
8989
"""Get authorization token for given user and password."""
9090
mc = MerginClient(url)
9191
if not mc.is_server_compatible():
@@ -96,15 +96,17 @@ def get_token(url, username, password, show_token=True):
9696
except LoginError as e:
9797
click.secho("Unable to log in: " + str(e), fg="red")
9898
return None
99-
if show_token:
100-
click.secho(f'To set the MERGIN_AUTH variable run:\nexport MERGIN_AUTH="{session["token"]}"')
10199
return session["token"]
102100

103101

104102
def get_client(url=None, auth_token=None, username=None, password=None):
105103
"""Return Mergin client."""
106104
if auth_token is not None:
107-
mc = MerginClient(url, auth_token=f"Bearer {auth_token}")
105+
try:
106+
mc = MerginClient(url, auth_token=auth_token)
107+
except ClientError as e:
108+
click.secho(str(e), fg="red")
109+
return None
108110
# Check if the token has expired or is just about to expire
109111
delta = mc._auth_session["expire"] - datetime.now(timezone.utc)
110112
if delta.total_seconds() > 5:
@@ -164,6 +166,8 @@ def login(ctx):
164166
mc = ctx.obj["client"]
165167
if mc is not None:
166168
click.secho("Login successful!", fg="green")
169+
token = mc._auth_session["token"]
170+
click.secho(f'To set the MERGIN_AUTH variable run:\nexport MERGIN_AUTH="{token}"')
167171

168172

169173
@cli.command()

mergin/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def __init__(self, url=None, auth_token=None, login=None, password=None, plugin_
6969
self._user_info = {"username": token_data["username"]}
7070
except TokenError as e:
7171
self.log.error(e)
72+
raise ClientError("Auth token error: " + str(e))
7273
handlers = []
7374

7475
# Create handlers for proxy, if needed

0 commit comments

Comments
 (0)