Skip to content

Commit e876fd1

Browse files
authored
Merge pull request #237 from MerginMaps/login-sso
Login sso
2 parents 847975e + 2f903d9 commit e876fd1

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

mergin/client.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class TokenError(Exception):
4545
pass
4646

4747

48+
class AuthTokenExpiredError(Exception):
49+
pass
50+
51+
4852
class ServerType(Enum):
4953
OLD = auto() # Server is old and does not support workspaces
5054
CE = auto() # Server is Community Edition
@@ -80,7 +84,15 @@ class MerginClient:
8084
Currently, only HTTP proxies are supported.
8185
"""
8286

83-
def __init__(self, url=None, auth_token=None, login=None, password=None, plugin_version=None, proxy_config=None):
87+
def __init__(
88+
self,
89+
url=None,
90+
auth_token=None,
91+
login=None,
92+
password=None,
93+
plugin_version=None,
94+
proxy_config=None,
95+
):
8496
self.url = url if url is not None else MerginClient.default_url()
8597
self._auth_params = None
8698
self._auth_session = None
@@ -134,15 +146,20 @@ def __init__(self, url=None, auth_token=None, login=None, password=None, plugin_
134146
self.opener = urllib.request.build_opener(*handlers, https_handler)
135147
urllib.request.install_opener(self.opener)
136148

137-
if login and not password:
138-
raise ClientError("Unable to log in: no password provided for '{}'".format(login))
139-
if password and not login:
140-
raise ClientError("Unable to log in: password provided but no username/email")
149+
if login or password:
150+
if login and not password:
151+
raise ClientError("Unable to log in: no password provided for '{}'".format(login))
152+
if password and not login:
153+
raise ClientError("Unable to log in: password provided but no username/email")
141154

142-
if login and password:
143-
self._auth_params = {"login": login, "password": password}
155+
if login and password:
156+
self._auth_params = {"login": login, "password": password}
157+
if not self._auth_session:
158+
self.login(login, password)
159+
160+
else:
144161
if not self._auth_session:
145-
self.login(login, password)
162+
raise ClientError("Unable to log in: no auth token provided for login")
146163

147164
def setup_logging(self):
148165
"""Setup Mergin Maps client logging."""
@@ -190,11 +207,19 @@ def wrapper(self, *args):
190207
delta = self._auth_session["expire"] - datetime.now(timezone.utc)
191208
if delta.total_seconds() < 5:
192209
self.log.info("Token has expired - refreshing...")
193-
self.login(self._auth_params["login"], self._auth_params["password"])
210+
if self._auth_params.get("login", None) and self._auth_params.get("password", None):
211+
self.log.info("Token has expired - refreshing...")
212+
self.login(self._auth_params["login"], self._auth_params["password"])
213+
else:
214+
raise AuthTokenExpiredError("Token has expired - please re-login")
194215
else:
195216
# Create a new authorization token
196217
self.log.info(f"No token - login user: {self._auth_params['login']}")
197-
self.login(self._auth_params["login"], self._auth_params["password"])
218+
if self._auth_params.get("login", None) and self._auth_params.get("password", None):
219+
self.login(self._auth_params["login"], self._auth_params["password"])
220+
else:
221+
raise ClientError("Missing login or password")
222+
198223
return f(self, *args)
199224

200225
return wrapper

0 commit comments

Comments
 (0)