/// <summary> /// Processes login response from the FxA API and adds a new Account device. /// </summary> /// <param name="fxaJson">JSON string received from FxA containing login data.</param> /// <returns>True on successful processing of the login response.</returns> public bool ProcessLogin(string fxaJson) { // Initialize a new login session configuration Manager.Account.Config = new FxA.Config(fxaJson); // Generate a new WireGuard keypair in preparation for adding a new Account device var keys = WireGuard.Keypair.Generate(); Manager.Account.Config.FxALogin.PublicKey = keys.Public; // Save login session to user's appdata folder Manager.Account.Config.SaveFxAToken(); Manager.Account.Config.WriteFxAUserToFile(ProductConstants.FxAUserFile); // Set the account login state to logged in Manager.Account.LoginState = FxA.LoginState.LoggedIn; // Added a new account device through the FxA API, using the newly generated keypair var devices = new FxA.Devices(); var deviceName = string.Format("{0} ({1} {2})", System.Environment.MachineName, System.Environment.OSVersion.Platform, System.Environment.OSVersion.Version); var deviceAddResponse = devices.AddDevice(deviceName, keys.Public); // Upon successful addition of a new device, save the device interface to the WireGuard configuration file if (deviceAddResponse != null) { var conf = new WireGuard.Config(keys.Private, deviceAddResponse.IPv4Address + "," + deviceAddResponse.IPv6Address, string.Empty); conf.WriteToFile(ProductConstants.FirefoxPrivateNetworkConfFile); return(true); } return(false); }
/// <summary> /// Logs the user out, removes their current device if the removeDevice flag is set, and handles local files cleanup. /// Note: Removing a device will also effectively invalidate the user's current FxA token. /// </summary> /// <param name="removeDevice">Optional: Indicates the removal of the current user's device when set to true.</param> public void Logout(bool removeDevice = false) { try { // Disconnect the VPN tunnel Manager.Tunnel.Disconnect(false); // Remove the current account device if (removeDevice) { var devices = new FxA.Devices(); devices.RemoveDevice(Manager.Account.Config.FxALogin.PublicKey, silent: true); } } catch (Exception) { ErrorHandler.Handle(new UserFacingMessage("toast-remove-device-error"), UserFacingErrorType.Toast, UserFacingSeverity.ShowWarning, LogLevel.Debug); } finally { // Clear up login session files from user's appdata folder Config.RemoveFxAToken(); File.Delete(ProductConstants.FxAUserFile); File.Delete(ProductConstants.FirefoxPrivateNetworkConfFile); // Set logged out state and terminate UI Updater threads LoginState = LoginState.LoggedOut; Manager.TerminateUIUpdaters(); } }
/// <summary> /// Behaviour for when the remove button in the device removal popup is clicked. /// </summary> private void DeleteDevicePopup_Remove(object sender, EventArgs e) { // Set delete button state to deleting ButtonExtensions.SetDeleting(currentDeleteButton, true); var deleteButton = currentDeleteButton; // Initiate the async device removal task var devices = new FxA.Devices(); var removeDeviceTask = devices.RemoveDeviceTask(DeleteDevices.DeviceToDelete.Pubkey, safeRemove: true, silent: true); // Check if device removal task was successful removeDeviceTask.ContinueWith(task => { if (!task.Result) { ErrorHandling.ErrorHandler.Handle(new ErrorHandling.UserFacingMessage("toast-remove-device-error"), ErrorHandling.UserFacingErrorType.Toast, ErrorHandling.UserFacingSeverity.ShowError, ErrorHandling.LogLevel.Error); Application.Current.Dispatcher.Invoke(() => { ButtonExtensions.SetMarkForDeletion(deleteButton, false); ButtonExtensions.SetDeleting(deleteButton, false); EnableDeleteDeviceButton(deleteButton); }); } }); // If on the device limit reached page, reprocess the login response upon device removal if (DeviceLimitReached && !string.IsNullOrEmpty(fxaJson)) { removeDeviceTask.ContinueWith(task => { if (task.Result) { var reprocessLoginResult = Manager.Account.ProcessLogin(fxaJson); if (reprocessLoginResult) { Application.Current.Dispatcher.Invoke(() => { var owner = Application.Current.MainWindow; if (owner != null) { ((UI.MainWindow)owner).NavigateToView(new UI.QuickAccessView(), UI.MainWindow.SlideDirection.Left); ((UI.MainWindow)owner).Activate(); } }); } } }); } // Close the delete device popup DeleteDevicePopup_Cancel(sender, e); }
/// <summary> /// Remove device. /// </summary> /// <param name="req">WCF device request.</param> /// <returns>WCF response.</returns> public Response RemoveDevice(DeviceRequest req) { try { var devices = new FxA.Devices(); devices.RemoveDevice(req.PublicKey, false, true); return(new Response(200, "Success")); } catch (Exception ex) { return(new Response(500, ex.Message)); } }
/// <summary> /// Add device. /// </summary> /// <param name="req">WCF device request.</param> /// <returns>WCF response.</returns> public Response AddDevice(DeviceRequest req) { try { var devices = new FxA.Devices(); var addDeviceResponse = devices.AddDevice(req.DeviceName, req.PublicKey); return(new Response(200, "Success")); } catch (Exception ex) { return(new Response(500, ex.Message)); } }