Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring authentication as main app action #251

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion dtool_lookup_gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import dtoolcore
import dtool_lookup_api.core.config

from dtool_lookup_api.core.LookupClient import authenticate

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GtkSource', '4')
Expand All @@ -56,7 +58,6 @@
import dtool_lookup_gui.widgets.progress_chart
import dtool_lookup_gui.widgets.progress_popover_menu


logger = logging.getLogger(__name__)

from . import __version__
Expand Down Expand Up @@ -225,6 +226,11 @@ def do_startup(self):
export_config_action.connect("activate", self.do_export_config)
self.add_action(export_config_action)

# renew-token action
renew_token_action = Gio.SimpleAction.new("renew-token", GLib.VariantType.new("(sss)"))
renew_token_action.connect("activate", self.do_renew_token)
self.add_action(renew_token_action)

Gtk.Application.do_startup(self)

# custom application-scoped actions
Expand Down Expand Up @@ -308,6 +314,27 @@ def do_dtool_config_changed(self):
first when emitting dtool-config-changed signal."""
logger.debug("method handler for 'dtool-config-changed' called.")

def do_renew_token(self, action, value):
"""Request new token."""

# Unpack the username, password, and auth_url from the tuple variant
username, password, auth_url = value.unpack()

async def retrieve_token(auth_url, username, password):
try:
token = await authenticate(auth_url, username, password)
except Exception as e:
logger.error(str(e))
return

dtool_lookup_api.core.config.Config.token = token
self.emit('dtool-config-changed')

asyncio.create_task(retrieve_token(
auth_url,
username,
password))


def run_gui():
GObject.type_register(GtkSource.View)
Expand Down
28 changes: 9 additions & 19 deletions dtool_lookup_gui/views/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ async def _refresh_list_of_endpoints(self):
logger.debug("Done refreshing settings dialog.")

def on_dtool_config_changed(self, widget):
"""Signal handler for dtool-method-changed."""
"""Signal handler for dtool-config-changed."""
self._refresh_settings_dialog()

# signal handlers
Expand Down Expand Up @@ -205,17 +205,18 @@ def on_delete(self, widget, event):

@Gtk.Template.Callback()
def on_renew_token_clicked(self, widget):
def authenticate(username, password):
asyncio.create_task(self.retrieve_token(
self.authenticator_url_entry.get_text(),
username,
password))

# Reconnect since settings may have been changed
#asyncio.create_task(self.main_application.lookup_tab.connect())
# show authentication dialogue and get username and password
def authenticate(username, password):
auth_url = self.authenticator_url_entry.get_text()
user_pass_auth_variant = GLib.Variant.new_tuple(GLib.Variant.new_string(username),
GLib.Variant.new_string(password),
GLib.Variant.new_string(auth_url))
self.get_action_group("app").activate_action('renew-token', user_pass_auth_variant)

AuthenticationDialog(authenticate, Config.username, Config.password).show()


@Gtk.Template.Callback()
def on_reset_config_clicked(self, widget):
"""Process clicked signal from reset-config button."""
Expand Down Expand Up @@ -275,17 +276,6 @@ def on_item_download_directory_file_chooser_button_file_set(self, widget):
logger.debug("Selected default item download directory '%s'.", item_download_directory)
settings.item_download_directory = item_download_directory

async def retrieve_token(self, auth_url, username, password):
#self.main_application.error_bar.hide()
try:
token = await authenticate(auth_url, username, password)
except Exception as e:
logger.error(str(e))
return
#self.builder.get_object('token-entry').set_text(token)
self.token_entry.set_text(token)
await self._refresh_list_of_endpoints()

_configuration_dialogs = {
's3': S3ConfigurationDialog,
'smb': SMBConfigurationDialog,
Expand Down
1 change: 1 addition & 0 deletions test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ async def test_app_list_actions(app):
assert set(app.list_actions()) == set([
'toggle-logging',
'reset-config',
'renew-token',
'set-loglevel',
'set-logfile',
'export-config',
Expand Down
Loading