From d88bd7005553b51056f12db25aaebac37921a01b Mon Sep 17 00:00:00 2001 From: David Sangrey Date: Thu, 3 Aug 2023 16:32:59 -0400 Subject: [PATCH] #2047 Mask API Keys in Default Plugins --- plugins/edsm.py | 20 +++++++++++++++++++- plugins/inara.py | 21 ++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/plugins/edsm.py b/plugins/edsm.py index 256f89f2..fa135049 100644 --- a/plugins/edsm.py +++ b/plugins/edsm.py @@ -125,6 +125,7 @@ class This: this = This() +show_password_var = tk.BooleanVar() STATION_UNDOCKED: str = '×' # "Station" name to display when not docked = U+00D7 __cleanup = str.maketrans({' ': None, '\n': None}) @@ -275,6 +276,14 @@ def plugin_stop() -> None: logger.debug('Done.') +def toggle_password_visibility(): + """Toggle if the API Key is visible or not.""" + if show_password_var.get(): + this.apikey.config(show="") + else: + this.apikey.config(show="*") + + def plugin_prefs(parent: ttk.Notebook, cmdr: str | None, is_beta: bool) -> tk.Frame: """ Plugin preferences setup hook. @@ -346,11 +355,20 @@ def plugin_prefs(parent: ttk.Notebook, cmdr: str | None, is_beta: bool) -> tk.Fr # LANG: EDSM API key label this.apikey_label = nb.Label(frame, text=_('API Key')) # EDSM setting this.apikey_label.grid(row=cur_row, padx=PADX, sticky=tk.W) - this.apikey = nb.Entry(frame) + this.apikey = nb.Entry(frame, show="*", width=50) this.apikey.grid(row=cur_row, column=1, padx=PADX, pady=PADY, sticky=tk.EW) prefs_cmdr_changed(cmdr, is_beta) + show_password_var.set(False) # Password is initially masked + show_password_checkbox = nb.Checkbutton( + frame, + text="Show API Key", + variable=show_password_var, + command=toggle_password_visibility, + ) + show_password_checkbox.grid(columnspan=2, padx=BUTTONX, pady=(5, 0), sticky=tk.W) + return frame diff --git a/plugins/inara.py b/plugins/inara.py index a5b17bd0..7f7abbd1 100644 --- a/plugins/inara.py +++ b/plugins/inara.py @@ -152,6 +152,8 @@ class This: this = This() +show_password_var = tk.BooleanVar() + # last time we updated, if unset in config this is 0, which means an instant update LAST_UPDATE_CONF_KEY = 'inara_last_update' EVENT_COLLECT_TIME = 31 # Minimum time to take collecting events before requesting a send @@ -242,6 +244,14 @@ def plugin_stop() -> None: logger.debug('Done.') +def toggle_password_visibility(): + """Toggle if the API Key is visible or not.""" + if show_password_var.get(): + this.apikey.config(show="") + else: + this.apikey.config(show="*") + + def plugin_prefs(parent: ttk.Notebook, cmdr: str, is_beta: bool) -> tk.Frame: """Plugin Preferences UI hook.""" x_padding = 10 @@ -281,11 +291,20 @@ def plugin_prefs(parent: ttk.Notebook, cmdr: str, is_beta: bool) -> tk.Frame: # LANG: Inara API key label this.apikey_label = nb.Label(frame, text=_('API Key')) # Inara setting this.apikey_label.grid(row=12, padx=x_padding, sticky=tk.W) - this.apikey = nb.Entry(frame) + this.apikey = nb.Entry(frame, show="*", width=50) this.apikey.grid(row=12, column=1, padx=x_padding, pady=y_padding, sticky=tk.EW) prefs_cmdr_changed(cmdr, is_beta) + show_password_var.set(False) # Password is initially masked + show_password_checkbox = nb.Checkbutton( + frame, + text="Show API Key", + variable=show_password_var, + command=toggle_password_visibility, + ) + show_password_checkbox.grid(columnspan=2, padx=x_padding, pady=(5, 0), sticky=tk.W) + return frame