Skip to content

Commit e4c2632

Browse files
committed
move functionality to functions
1 parent b86b36c commit e4c2632

2 files changed

Lines changed: 56 additions & 40 deletions

File tree

Mergin/configuration_dialog.py

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
LoginType,
1616
get_login_type,
1717
get_mergin_sso_email,
18-
json_response,
1918
get_stored_mergin_server_url,
2019
get_mergin_username_password,
2120
login_sso,
@@ -24,7 +23,8 @@
2423
set_mergin_auth_password,
2524
validate_sso_login,
2625
sso_oauth_client_id,
27-
validate_mergin_url,
26+
sso_login_allowed,
27+
sso_ask_for_email,
2828
)
2929
from .utils import (
3030
MERGIN_URL,
@@ -157,22 +157,12 @@ def test_connection(self):
157157
def allow_sso_login(self) -> None:
158158
self.ui.button_sign_sso.setVisible(False)
159159

160-
if validate_mergin_url(self.server_url()) is not None:
160+
allowed, msg = sso_login_allowed(self.server_url())
161+
if msg:
162+
QMessageBox.critical(self, "SSO allowed check", msg)
161163
return
162164

163-
try:
164-
server_config_data = json_response(f"{self.server_url()}/config")
165-
except URLError as e:
166-
QMessageBox.critical(self, "SSO allowed check", f"Could not connect to server: {str(e)}")
167-
return
168-
except ValueError as e:
169-
QMessageBox.critical(self, "SSO allowed check", f"Could not parse server response: {str(e)}")
170-
return
171-
172-
if "sso_enabled" in server_config_data:
173-
sso_enabled = server_config_data["sso_enabled"]
174-
if sso_enabled:
175-
self.ui.button_sign_sso.setVisible(True)
165+
self.ui.button_sign_sso.setVisible(allowed)
176166

177167
def check_sso_availability(self) -> None:
178168
self.sso_timer = QTimer(self)
@@ -195,27 +185,8 @@ def get_sso_email(self) -> typing.Optional[str]:
195185
return None
196186

197187
def sso_ask_for_email(self) -> bool:
198-
199-
try:
200-
json_data = json_response(f"{self.server_url()}/v2/sso/config")
201-
except URLError as e:
202-
QMessageBox.critical(self, "SSO configuration check", f"Could not connect to server: {str(e)}")
203-
return True
204-
except ValueError as e:
205-
QMessageBox.critical(self, "SSO configuration check", f"Could not parse server response: {str(e)}")
206-
return True
207-
208-
if "tenant_flow_type" not in json_data:
209-
QMessageBox.critical(
210-
self, "Server Response Error", "Server response did not contain required tenant_flow_type data"
211-
)
212-
return True
213-
214-
if json_data["tenant_flow_type"] not in ["multi", "single"]:
215-
QMessageBox.critical(self, "Server Response Error", "SSO tenant_flow_type is not valid")
216-
return True
217-
218-
if json_data["tenant_flow_type"] == "multi":
219-
return True
220-
221-
return False
188+
ask_for_email, msg = sso_ask_for_email(self.server_url())
189+
if msg:
190+
QMessageBox.critical(self, "SSO email check", msg)
191+
return ask_for_email
192+
return ask_for_email

Mergin/utils_auth.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import uuid
44
import json
55
from urllib.error import URLError
6+
import requests
67

78
from qgis.core import QgsApplication, QgsAuthMethodConfig, QgsBlockingNetworkRequest, QgsNetworkAccessManager
89
from qgis.PyQt.QtCore import QSettings, QUrl
@@ -367,3 +368,47 @@ def validate_mergin_url(url):
367368
except ValueError:
368369
return "Invalid URL"
369370
return None
371+
372+
373+
def sso_login_allowed(url: str) -> typing.Tuple[bool, typing.Optional[str]]:
374+
"""Tests if SSO login is allowed on the server. Returns a tuple with a boolean and an optional error message."""
375+
try:
376+
requests.get(url, timeout=3)
377+
except requests.RequestException:
378+
return False, None
379+
380+
try:
381+
server_config_data = json_response(f"{url}/config")
382+
except URLError as e:
383+
return False, f"Could not connect to server: {str(e)}"
384+
except ValueError as e:
385+
return False, f"Could not parse server response: {str(e)}"
386+
387+
if "sso_enabled" in server_config_data:
388+
sso_enabled = server_config_data["sso_enabled"]
389+
if sso_enabled:
390+
return True, None
391+
392+
return False, None
393+
394+
395+
def sso_ask_for_email(url: str) -> typing.Tuple[bool, typing.Optional[str]]:
396+
"""Tests if SSO login should ask for email. Returns a tuple with a boolean and an optional error message."""
397+
398+
try:
399+
json_data = json_response(f"{url}/v2/sso/config")
400+
except URLError as e:
401+
return True, f"Could not connect to server: {str(e)}"
402+
except ValueError as e:
403+
return True, f"Could not parse server response: {str(e)}"
404+
405+
if "tenant_flow_type" not in json_data:
406+
return True, "Server response did not contain required tenant_flow_type data"
407+
408+
if json_data["tenant_flow_type"] not in ["multi", "single"]:
409+
return True, "SSO tenant_flow_type is not valid"
410+
411+
if json_data["tenant_flow_type"] == "multi":
412+
return True, None
413+
414+
return False, None

0 commit comments

Comments
 (0)