Skip to content

Commit 2cb313e

Browse files
authored
Add token validation (#164)
* Add token format validation. * Update version.
1 parent f231be1 commit 2cb313e

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

configure_dialog.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
from . import utils
1111

1212

13+
def _token_format_valid(token):
14+
if "_" in token:
15+
parts = token.split("_")
16+
if len(parts) == 2:
17+
if len(parts[0]) == 32 and len(parts[1]) >= 64:
18+
return True
19+
return False
20+
21+
1322
class ConfigureDialog(QtWidgets.QDialog):
1423
def __init__(self):
1524
super().__init__()
@@ -52,17 +61,24 @@ def __init__(self):
5261

5362
def _accepted(self):
5463
# get and store UI values
55-
smanager = SettingsManager()
56-
am = QgsApplication.authManager()
57-
auth_cfg_id = smanager.get_setting('auth_cfg_id')
5864
token = self.ui.token_txt.text()
59-
cfg = QgsAuthMethodConfig('MapTilerHmacSha256')
6065
if token:
61-
if auth_cfg_id:
62-
(res, cfg) = am.loadAuthenticationConfig(auth_cfg_id, cfg, True)
63-
if res:
64-
saved_token = cfg.configMap().get("token")
65-
if not saved_token == token:
66+
cfg = QgsAuthMethodConfig('MapTilerHmacSha256')
67+
if _token_format_valid(token):
68+
smanager = SettingsManager()
69+
am = QgsApplication.authManager()
70+
auth_cfg_id = smanager.get_setting('auth_cfg_id')
71+
if auth_cfg_id:
72+
(res, cfg) = am.loadAuthenticationConfig(auth_cfg_id, cfg, True)
73+
if res:
74+
saved_token = cfg.configMap().get("token")
75+
if not saved_token == token:
76+
cfg.setConfigMap({'token': token})
77+
(res, cfg) = am.storeAuthenticationConfig(cfg, True)
78+
if res:
79+
smanager.store_setting('auth_cfg_id', cfg.id())
80+
else:
81+
cfg.setName('qgis-maptiler-plugin')
6682
cfg.setConfigMap({'token': token})
6783
(res, cfg) = am.storeAuthenticationConfig(cfg, True)
6884
if res:
@@ -73,15 +89,11 @@ def _accepted(self):
7389
(res, cfg) = am.storeAuthenticationConfig(cfg, True)
7490
if res:
7591
smanager.store_setting('auth_cfg_id', cfg.id())
92+
prefervector = str(int(self.ui.vtileCheckBox.isChecked()))
93+
smanager.store_setting('prefervector', prefervector)
94+
self.close()
7695
else:
77-
cfg.setName('qgis-maptiler-plugin')
78-
cfg.setConfigMap({'token': token})
79-
(res, cfg) = am.storeAuthenticationConfig(cfg, True)
80-
if res:
81-
smanager.store_setting('auth_cfg_id', cfg.id())
82-
prefervector = str(int(self.ui.vtileCheckBox.isChecked()))
83-
smanager.store_setting('prefervector', prefervector)
84-
self.close()
96+
self.ui.label_6.setText(f"Not a valid token format. (Use token, not API key.)")
8597
else:
8698
self.ui.label_6.setText(f"Token is required.")
8799

configure_dialog_base.ui

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>423</width>
9+
<width>300</width>
1010
<height>226</height>
1111
</rect>
1212
</property>
@@ -24,7 +24,7 @@
2424
</property>
2525
<property name="maximumSize">
2626
<size>
27-
<width>423</width>
27+
<width>300</width>
2828
<height>226</height>
2929
</size>
3030
</property>
@@ -95,10 +95,17 @@
9595
</property>
9696
</widget>
9797
</item>
98+
<item>
99+
<widget class="QLabel" name="label_7">
100+
<property name="text">
101+
<string>Get FREE credentials at:</string>
102+
</property>
103+
</widget>
104+
</item>
98105
<item>
99106
<widget class="QLabel" name="label_3">
100107
<property name="text">
101-
<string>Get FREE credentials at &lt;a href=&quot;https://maptiler.link/qgiscredentials&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#419cff;&quot;&gt;https://maptiler.link/qgiscredentials&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
108+
<string>&lt;a href=&quot;https://maptiler.link/qgiscredentials&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#419cff;&quot;&gt;https://cloud.maptiler.com/account/credentials/&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
102109
</property>
103110
<property name="textFormat">
104111
<enum>Qt::RichText</enum>

metadata.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
name=MapTiler
77
qgisMinimumVersion=3.24
88
description=Street and satellite base maps with vector tiles
9-
version=3.0.0
9+
version=3.0.1
1010
author=MapTiler
1111
email=info@maptiler.com
1212

0 commit comments

Comments
 (0)