public ConfigurationForm(ScrobblerConfig existingConfig) { InitializeComponent(); BtnSave.Enabled = false; ScrobblerConfig = existingConfig; string alreadyLoggedUserText = ""; if (!string.IsNullOrWhiteSpace(existingConfig?.UserName)) { alreadyLoggedUserText = $"Last authenticated user: {existingConfig.UserName}\n"; } TxtStatus.Text = $"{alreadyLoggedUserText}Click the '{BtnReAuth.Text}' button\nto start a new authentication process..."; }
public ScrobblerConfig AskUserForNewAuthorizedSessionKey(IntPtr ownerWindowHandle) { using (var configurationForm = new ConfigurationForm(ScrobblerConfig)) { if (configurationForm.ShowDialog(new Win32Window(ownerWindowHandle)) == DialogResult.OK) { // refresh with the new session key ScrobblerConfig = configurationForm.ScrobblerConfig; return(ScrobblerConfig); } else { return(null); } } }
private async void BtnReAuth_Click(object sender, EventArgs e) { BtnReAuth.Enabled = false; try { const string getTokenErrorMessage = "An error occured while trying to get an authentication token from Last.fm!"; const string getSessionKeyErrorMessage = "An error occured while trying to get an authenticated session key from Last.fm!"; void ShowFatalError(string message) { MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); TxtStatus.Text = "Authentication aborted."; } // First we have to get a token to authorize... ApiResponse <string> tokenResponse; try { TxtStatus.Text = "Requesting a token from Last.fm..."; tokenResponse = await Auth.GetToken(); } catch (Exception ex) { ShowFatalError($"{getTokenErrorMessage}\n\nDetails: {ex}"); return; } if (!tokenResponse.Success) { ShowFatalError($"{getTokenErrorMessage}\n\nDetails: {tokenResponse.Error.Code}: {tokenResponse.Error.Message}"); return; } // Then the user has to authorize the token in their browser, // and then we can complete the process by getting a session key... var url = Auth.GetAuthorizeTokenUrl(tokenResponse.Result); Process.Start(url); TxtStatus.Text = "Please click the 'Complete authentication' button\nonce you have authorized the plugin in your browser..."; // Enable the 'Complete authentication' button and wait until the user clicks it. TaskCompletionSource <bool> waitForUserClick = new TaskCompletionSource <bool>(); void CompleteButtonClicked(object s, EventArgs e2) => waitForUserClick.TrySetResult(true); BtnGetSessionKey.Click += CompleteButtonClicked; BtnGetSessionKey.Enabled = true; BtnGetSessionKey.Focus(); await waitForUserClick.Task; BtnGetSessionKey.Enabled = false; BtnGetSessionKey.Click -= CompleteButtonClicked; ApiResponse <Session> sessionKeyResponse; try { TxtStatus.Text = "Completing authentication..."; sessionKeyResponse = await Auth.GetSession(tokenResponse.Result); } catch (Exception ex) { ShowFatalError($"{getSessionKeyErrorMessage}\n\nDetails: {ex}"); return; } if (!sessionKeyResponse.Success) { ShowFatalError($"{getSessionKeyErrorMessage}\n\nDetails: {sessionKeyResponse.Error.Code}: {sessionKeyResponse.Error.Message}"); return; } // We have a new valid session key! ScrobblerConfig = new ScrobblerConfig { SessionKey = sessionKeyResponse.Result.Key, UserName = sessionKeyResponse.Result.UserName }; BtnSave.Enabled = true; TxtStatus.Text = $"{sessionKeyResponse.Result.UserName} is now successfully authenticated."; } finally { BtnReAuth.Enabled = true; } }
public void SetSessionKey(ScrobblerConfig scrobblerConfig) { ScrobblerConfig = scrobblerConfig ?? new ScrobblerConfig(); }