Пример #1
0
        public override void AwakeFromNib()
        {
            base.AwakeFromNib();

            // Get access to database
            DatabaseConnection = SqliteManager.GetDatabaseConnection();
        }
Пример #2
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            DatabaseConnection = SqliteManager.GetDatabaseConnection();

            Authenticator.Initialise(DatabaseConnection, _conn);

            DataSource   = new RecordTableDataSource(DatabaseConnection);
            NSTextFields = new NSTextField[] { websiteField, accountField, passwordField };

            PushView();
        }
        public void RemoveRecord(Record record, SqliteConnection connection)
        {
            Records.Remove(record);
            bool shouldClose;

            (_conn, shouldClose) = SqliteManager.OpenConnection(connection);

            // Execute query
            using (var command = connection.CreateCommand())
            {
                // Create new command
                command.CommandText = "DELETE FROM [Data] WHERE (ID = @COL1)";

                // Delete data from the record
                command.Parameters.AddWithValue("@COL1", record.ID.ToString());

                // Write to database
                command.ExecuteNonQuery();
            }

            _conn = SqliteManager.CloseConnection(shouldClose, connection);
        }
Пример #4
0
        public static void Initialise(SqliteConnection connection, SqliteConnection _conn)
        {
            bool shouldClose;

            (_conn, shouldClose) = SqliteManager.OpenConnection(connection);

            byte[] encryptedPassword = { };
            bool?  isInit            = null;

            using (var command = connection.CreateCommand())
            {
                // Create new command
                command.CommandText = "SELECT * FROM [System]";

                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read() && isInit == null)
                    {
                        // Pull values back into class
                        encryptedPassword = (byte[])reader[1];
                        isInit            = (bool)reader[2];
                    }
                }
            }

            var password = EncryptionTool.Decrypt(encryptedPassword);

            if (isInit == true)
            {
                CheckPassword(connection, _conn, password);
            }
            else
            {
                NewPassword(connection, _conn);
            }

            _conn = SqliteManager.CloseConnection(shouldClose, connection);
        }
Пример #5
0
        private void SaveRecord(SqliteConnection connection)
        {
            bool shouldClose;

            (_conn, shouldClose) = SqliteManager.OpenConnection(connection);

            // Execute query
            using (var command = connection.CreateCommand())
            {
                // Create new command
                command.CommandText = "INSERT INTO [Data] (ID, Website, Account, Password) VALUES (@COL1, @COL2, @COL3, @COL4)";

                // Populate with data from the record
                command.Parameters.AddWithValue("@COL1", ID.ToString());
                command.Parameters.AddWithValue("@COL2", EncryptionTool.Encrypt(Website));
                command.Parameters.AddWithValue("@COL3", EncryptionTool.Encrypt(AccountName));
                command.Parameters.AddWithValue("@COL4", EncryptionTool.Encrypt(Password));

                // Write to database
                command.ExecuteNonQuery();
            }

            _conn = SqliteManager.CloseConnection(shouldClose, connection);
        }
        public RecordTableDataSource(SqliteConnection connection)
        {
            Records = new List <Record>();
            bool shouldClose;

            (_conn, shouldClose) = SqliteManager.OpenConnection(connection);

            // Execute query
            using (var command = connection.CreateCommand())
            {
                // Create new command
                command.CommandText = "SELECT * FROM [Data]";

                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        // Pull values back into class
                        var ID       = (string)reader[0];
                        var website  = EncryptionTool.Decrypt((byte[])reader[1]);
                        var account  = EncryptionTool.Decrypt((byte[])reader[2]);
                        var password = EncryptionTool.Decrypt((byte[])reader[3]);
                        if (Records.Any(x => x.ID.ToString() == ID))
                        {
                            break;
                        }
                        else
                        {
                            Records.Add(new Record(website, account, password, connection, new Guid(ID), false));
                        }
                    }
                }
            }

            _conn = SqliteManager.CloseConnection(shouldClose, connection);
        }
Пример #7
0
        public override NSView GetViewForItem(NSTableView tableView, NSTableColumn tableColumn, nint row)
        {
            // This pattern allows you reuse existing views when they are no-longer in use.
            // If the returned view is null, you instance up a new view
            // If a non-null view is returned, you modify it enough to reflect the new data
            NSTableCellView view = (NSTableCellView)tableView.MakeView(tableColumn.Title, this);

            view = new NSTableCellView();
            var showPassword = DataSource.Records[(int)row].ShowPassword;

            // Configure the view
            view.Identifier = tableColumn.Title;

            // Take action based on title
            switch (tableColumn.Title)
            {
            case "Website/Service":
            case "Account Name":
                view.TextField = new NSTextField(new CGRect(0, 0, 400, 16));
                ConfigureTextField(view, row);
                break;

            case "Password":
                view.TextField = showPassword ? new NSTextField(new CGRect(0, 0, 400, 16)) : new NSSecureTextField(new CGRect(0, 0, 400, 16));
                ConfigureTextField(view, row);
                break;

            case "Actions":
                // Create new button
                var removeButton = new NSButton(new CGRect(0, 0, 60, 16));
                removeButton.Tag      = row;
                removeButton.Bordered = false;
                removeButton.Cell     = new NSButtonCell {
                    BackgroundColor = NSColor.DarkGray, Title = "Remove"
                };

                // Wireup events
                removeButton.Activated += (sender, e) =>
                {
                    // Get button and product
                    var btn        = sender as NSButton;
                    var record     = DataSource.Records[(int)btn.Tag];
                    var connection = SqliteManager.GetDatabaseConnection();

                    // Configure alert
                    var alert = new NSAlert()
                    {
                        AlertStyle      = NSAlertStyle.Informational,
                        InformativeText = $"Are you sure you want to delete data for {record.Website}? This operation cannot be undone.",
                        MessageText     = $"Delete data for {record.Website}?",
                    };
                    alert.AddButton("Cancel");
                    alert.AddButton("Delete");
                    alert.BeginSheetForResponse(Controller.View.Window, (result) =>
                    {
                        // Should we delete the requested row?
                        if (result == 1001)
                        {
                            // Remove the given row from the dataset
                            DataSource.RemoveRecord(record, connection);
                            Controller.PushView();
                        }
                    });
                };

                view.AddSubview(removeButton);

                var showButton = new NSButton(new CGRect(70, 0, 60, 16));
                showButton.Tag  = row;
                showButton.Cell = new NSButtonCell {
                    BackgroundColor = NSColor.DarkGray, Title = showPassword ? "Hide" : "Show"
                };

                showButton.Activated += (sender, e) =>
                {
                    var btn            = sender as NSButton;
                    var selectedRecord = DataSource.Records[(int)btn.Tag];

                    foreach (var record in DataSource.Records)
                    {
                        record.ShowPassword = false;
                    }

                    selectedRecord.ShowPassword = showPassword ? false : true;
                    Controller.PushView();
                };

                view.AddSubview(showButton);

                var copyButton = new NSButton(new CGRect(140, 0, 60, 16));
                copyButton.Tag  = row;
                copyButton.Cell = new NSButtonCell {
                    BackgroundColor = NSColor.DarkGray, Title = "Copy"
                };

                copyButton.Activated += (sender, e) =>
                {
                    var btn            = sender as NSButton;
                    var selectedRecord = DataSource.Records[(int)btn.Tag];

                    var pasteboard = NSPasteboard.GeneralPasteboard;
                    pasteboard.ClearContents();
                    pasteboard.WriteObjects(new NSString[] { (NSString)selectedRecord.Password });
                };

                view.AddSubview(copyButton);

                var editButton = new NSButton(new CGRect(210, 0, 60, 16));
                editButton.Tag  = row;
                editButton.Cell = new NSButtonCell {
                    BackgroundColor = NSColor.DarkGray, Title = "Edit"
                };

                editButton.Activated += (sender, e) =>
                {
                    var btn            = sender as NSButton;
                    var selectedRecord = DataSource.Records[(int)btn.Tag];
                    var connection     = SqliteManager.GetDatabaseConnection();

                    Controller.RefillRecord(selectedRecord, btn.Tag);
                    DataSource.RemoveRecord(selectedRecord, connection);
                };

                view.AddSubview(editButton);
                break;
            }

            // Setup view based on the column selected
            switch (tableColumn.Title)
            {
            case "Website/Service":
                view.TextField.StringValue = DataSource.Records[(int)row].Website;
                break;

            case "Account Name":
                view.TextField.StringValue = DataSource.Records[(int)row].AccountName;
                break;

            case "Password":
                view.TextField.StringValue = DataSource.Records[(int)row].Password;
                break;

            case "Actions":
                foreach (NSView subview in view.Subviews)
                {
                    var btn = subview as NSButton;
                    if (btn != null)
                    {
                        btn.Tag = row;
                    }
                }
                break;
            }

            return(view);
        }
Пример #8
0
        public static void NewPassword(SqliteConnection connection, SqliteConnection _conn)
        {
            var newPasswordInput = new NSStackView(new CGRect(0, 0, 300, 50));

            var originalPassword = new NSSecureTextField(new CGRect(0, 25, 300, 20));

            originalPassword.PlaceholderAttributedString = new Foundation.NSAttributedString("Type new password...");
            var confirmedPassword = new NSSecureTextField(new CGRect(0, 0, 300, 20));

            confirmedPassword.PlaceholderAttributedString = new Foundation.NSAttributedString("Confirm password...");

            newPasswordInput.AddSubview(originalPassword);
            newPasswordInput.AddSubview(confirmedPassword);

            var newPasswordAlert = new NSAlert()
            {
                AlertStyle      = NSAlertStyle.Informational,
                InformativeText = "Enter new password to secure SittingDucks",
                MessageText     = "Adding New Password",
            };
            var enterButton = newPasswordAlert.AddButton("Enter");

            originalPassword.NextKeyView  = confirmedPassword;
            confirmedPassword.NextKeyView = enterButton;

            newPasswordAlert.AccessoryView = newPasswordInput;
            newPasswordAlert.Layout();
            var result = newPasswordAlert.RunModal();

            if (result == 1000 && originalPassword.StringValue == confirmedPassword.StringValue)
            {
                bool shouldClose;
                var  encryptedPassword = EncryptionTool.Encrypt(originalPassword.StringValue);

                (_conn, shouldClose) = SqliteManager.OpenConnection(connection);

                // Execute query
                using (var command = connection.CreateCommand())
                {
                    // Create new command
                    command.CommandText = "UPDATE [System] SET ID = @COL1, Password = @COL2, INIT = @COL3";

                    // Populate with data from the record
                    command.Parameters.AddWithValue("@COL1", new Guid());
                    command.Parameters.AddWithValue("@COL2", encryptedPassword);
                    command.Parameters.AddWithValue("@COL3", true);

                    // Write to database
                    command.ExecuteNonQuery();
                }

                _conn = SqliteManager.CloseConnection(shouldClose, connection);

                newPasswordAlert.Dispose();

                var confirmPasswordAlert = new NSAlert()
                {
                    AlertStyle      = NSAlertStyle.Informational,
                    InformativeText = "Remember this password, store it somewhere safe! You will not be able to recover it if lost.",
                    MessageText     = "Password sucessfully saved",
                };
                confirmPasswordAlert.AddButton("OK");
                var confirmResult = confirmPasswordAlert.RunModal();

                if (confirmResult == 1000)
                {
                    confirmPasswordAlert.Dispose();
                }
            }
            else if (result == 1000 && originalPassword.StringValue != confirmedPassword.StringValue)
            {
                newPasswordAlert.AlertStyle      = NSAlertStyle.Warning;
                newPasswordAlert.InformativeText = "Passwords do not match";
            }
        }