private void Register_Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Reset();
                var currentPw          = string.Empty;
                var currentReenteredPw = string.Empty;
                if (_pwIsVisible)
                {
                    currentPw          = Master_VisibleTextbox.Text;
                    currentReenteredPw = MasterReentered_Textbox.Text;
                }
                else
                {
                    currentPw          = Master_PasswordBox.Password;
                    currentReenteredPw = MasterReentered_PasswordBox.Password;
                }

                // Check if all criteria are correct
                if (Info_Checkbox.IsChecked == false)
                {
                    Output_Textblock.Text    = "You need to varify first that you have read the text on the left.";
                    Info_Checkbox.Foreground = Brushes.Red;
                    return;
                }

                if (FortressName_Textbox.Text == string.Empty)
                {
                    Output_Textblock.Text            = "You need to name your fortress.";
                    FortressName_Textbox.BorderBrush = Brushes.Red;
                    return;
                }

                if (WellKnownSpecialCharacters.ContainsSpecialCharacters(FortressName_Textbox.Text))
                {
                    Output_Textblock.Text            = "Special characters in the name are not allowed.";
                    FortressName_Textbox.BorderBrush = Brushes.Red;
                    return;
                }

                if (currentPw.Length < 8 ||
                    !(currentPw.Any(char.IsUpper)) ||
                    !(currentPw.Any(char.IsDigit)))
                {
                    Output_Textblock.Text            = "The masterkey has to match the following criteria: Minumum 8 characters long; Contain at least one upper case character and one digit.";
                    Master_PasswordBox.Foreground    = Brushes.Red;
                    Master_VisibleTextbox.Foreground = Brushes.Red;
                    return;
                }

                if (currentReenteredPw != currentPw)
                {
                    Output_Textblock.Text = "Masterkey doesn't match the reentered one.";
                    MasterReentered_PasswordBox.Foreground = Brushes.Red;
                    MasterReentered_Textbox.Foreground     = Brushes.Red;
                    return;
                }

                // If they are - continue to make the fortress.
                var aesHelper = new AesHelper();
                var salt      = aesHelper.GenerateSalt();
                var hashedKey = aesHelper.CreateKey(Master_PasswordBox.Password, 256, salt);
                var fullPath  = string.Empty;

                // If the user has entered a custom path -> Disabled for now
                if (_fullPath != string.Empty)
                {
                    fullPath = $"{_fullPath}\\{FortressName_Textbox.Text}";
                }
                else // else use the default.
                {
                    fullPath = $"{IOPathHelper.GetDefaultFortressDirectory()}\\{FortressName_Textbox.Text}";
                }

                var name     = "NOT GIVEN";
                var lastName = "NOT GIVEN";
                var userName = "******";
                var eMail    = "*****@*****.**";
                var fortress = new Fortress(salt, hashedKey, fullPath, name, lastName, userName, eMail);

                DataAccessService.Instance.CreateNewFortress(fortress); // Create the new fortress.

                ClearPasswords();

                Navigation.LoginManagementInstance.LoadFortresses(); // Refresh the list.

                Communication.InformUser($"{FortressName_Textbox.Text} has been successfully built.");
            }
            catch (Exception ex)
            {
                ClearPasswords();
                Logger.log.Error($"Error while trying to register a new fortress: {ex}");
                ex.SetUserMessage("There was a problem creating the fortress. The given passwords have been flushed out of memory.");
                Communication.InformUserAboutError(ex);
            }
        }
Exemple #2
0
        /// <summary>
        /// Loads all fortresses: Default and linked.
        /// </summary>
        public void LoadFortresses()
        {
            try
            {
                // If the fortress folder doesn't exist, a fortress hasn't been created into the default location
                if (!Directory.Exists(IOPathHelper.GetDefaultFortressDirectory()) && !File.Exists(IOPathHelper.GetLinkedFortressListFile()))
                {
                    return;
                }

                var allFortresses = new List <string>();

                // First get all fortresses in the default location
                var defaultFortresses = Directory.GetFiles(IOPathHelper.GetDefaultFortressDirectory()).Where(f => f.EndsWith(TermHelper.GetZippedFileEnding())).ToList();
                allFortresses.AddRange(defaultFortresses);

                // Now look for externally added fortress in the fortress config
                if (File.Exists(IOPathHelper.GetLinkedFortressListFile()))
                {
                    var linkedFortresses = File.ReadAllLines(IOPathHelper.GetLinkedFortressListFile()).ToList();
                    var emptyPaths       = new List <string>();

                    foreach (var path in linkedFortresses)
                    {
                        // If the file exists, we add it to the UI.
                        if (File.Exists(path))
                        {
                            allFortresses.Add(path);
                        }
                        // If the given path does not exist anymore, because the fortress got moved, delete it.
                        else
                        {
                            emptyPaths.Add(path);
                        }
                    }

                    // When empty paths have been found, delete them and tell the User.
                    if (emptyPaths.Count > 0)
                    {
                        foreach (var path in emptyPaths)
                        {
                            linkedFortresses.Remove(path);
                        }
                        File.WriteAllLines(IOPathHelper.GetLinkedFortressListFile(), linkedFortresses);
                        Communication.InformUser($"Old or corrupted paths have been found - they were de-linked from the fortress list.");
                    }
                }

                Fortresses.Clear();

                foreach (var fortress in allFortresses)
                {
                    // If the files does not have the default database ending, skip it.
                    if (!fortress.EndsWith(TermHelper.GetZippedFileEnding()))
                    {
                        break;
                    }

                    var created    = File.GetCreationTime(fortress);
                    var modified   = File.GetLastWriteTime(fortress);
                    var fortressVm = new FortressViewModel(fortress, created, modified, this);
                    if (!fortress.Contains(IOPathHelper.GetDefaultFortressDirectory()))
                    {
                        fortressVm.IsDefaultLocated = false;
                    }

                    Fortresses.Add(fortressVm);
                }
            }
            catch (Exception ex)
            {
                Logger.log.Error($"Error while loading the fortress list: {ex}");
                ex.SetUserMessage("An error occured while trying to load all known fortresses. If the fortress has been moved, try to select it again or restart the program.");
                Communication.InformUserAboutError(ex);
            }
        }