Skip to content

Commit

Permalink
Adding Username/Email to Password Entry (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
vikkio88 authored Nov 7, 2022
1 parent 500b99c commit 8a4a0f8
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 197 deletions.
10 changes: 5 additions & 5 deletions Muscurdi.Tests/Models/MasterPasswordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public void MasterPasswordMakeFromBitsHappyPath()
var password = MasterPassword.Make(new() { "nope", "yeah", "yeah" }, "mamm", 9999);
Assert.Equal("nope-yeah-yeah-mamm-9999", password.ToMemorable());
}


[Fact]
public void MasterPasswordMakeFromBitsFailsWithNotEnoughBits()
{
var result = Assert.Throws<MasterPasswordException>(() => MasterPassword.Make(new() { "nope", "yeah" }, "mamm", 9999));
Assert.Contains("prefix", result.Message);
}

[Fact]
public void MasterPasswordMakeFromBitsFailsWithWordsNotLongEnough()
{
Expand All @@ -73,14 +73,14 @@ public void MasterPasswordMakeFromBitsFailsWithFinalTooLong()
var result = Assert.Throws<MasterPasswordException>(() => MasterPassword.Make(new() { "nope", "yeah", "yoyo" }, "mammamia", 9999));
Assert.Contains("words", result.Message);
}

[Fact]
public void MasterPasswordMakeFromBitsFailsWithAppendixTooBig()
{
var result = Assert.Throws<MasterPasswordException>(() => MasterPassword.Make(new() { "nope", "yeah", "yoyo" }, "mammamia", 10000));
Assert.Contains("appendix", result.Message);
}

[Fact]
public void MasterPasswordMakeFromBitsFailsWithAppendixTooShort()
{
Expand Down
164 changes: 132 additions & 32 deletions Muscurdi/Assets/Styles/Main.xaml
Original file line number Diff line number Diff line change
@@ -1,59 +1,159 @@
<Styles xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style Selector="Button">
<Setter Property="Background" Value="#3F72AF" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Background"
Value="#3F72AF" />
<Setter Property="Foreground"
Value="White" />
<Setter Property="Template">
<ControlTemplate>
<ContentPresenter x:Name="PART_ContentPresenter"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Padding="{TemplateBinding Padding}"
RecognizesAccessKey="True"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
Padding="{TemplateBinding Padding}"
RecognizesAccessKey="True"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" />
</ControlTemplate>
</Setter>
</Style>
<Style Selector="Button:pointerover /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="#112D4E" />
<Setter Property="TextBlock.Foreground" Value="White" />
<Setter Property="Background"
Value="#112D4E" />
<Setter Property="TextBlock.Foreground"
Value="White" />
</Style>
<Style Selector="Button:disabled /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Opacity" Value=".7" />
<Setter Property="Background" Value="#3F72AF" />
<Setter Property="TextBlock.Foreground" Value="White" />
<Setter Property="Opacity"
Value=".7" />
<Setter Property="Background"
Value="#3F72AF" />
<Setter Property="TextBlock.Foreground"
Value="White" />
</Style>
<Style Selector="Border">
<Setter Property="BorderBrush" Value="#112D4E" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush"
Value="#112D4E" />
<Setter Property="BorderThickness"
Value="1" />
</Style>
<Style Selector="Border.Header">
<Setter Property="Background" Value="#DBE2EF" />
<Setter Property="Height" Value="50" />
<Setter Property="Background"
Value="#DBE2EF" />
<Setter Property="Height"
Value="50" />
</Style>
<Style Selector="Border.Row">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="#DBE2EF" />
<Setter Property="Padding" Value="5" />
<Setter Property="Width" Value="350" />
<Setter Property="Height" Value="50" />
<Setter Property="Margin" Value="5" />
<Setter Property="BorderThickness"
Value="0" />
<Setter Property="Background"
Value="#DBE2EF" />
<Setter Property="Padding"
Value="5" />
<Setter Property="Width"
Value="350" />
<Setter Property="Height"
Value="50" />
<Setter Property="Margin"
Value="5" />
</Style>
<Style Selector="Border.Error">
<Setter Property="Background" Value="#E64848" />
<Setter Property="Background"
Value="#E64848" />
</Style>
<Style Selector="Border.Success">
<Setter Property="Background" Value="#6de648" />
<Setter Property="Background"
Value="#6de648" />
<Setter Property="BorderThickness"
Value="0" />
</Style>
<Style Selector="TextBlock">
<Setter Property="Foreground" Value="#112D4E" />
<Setter Property="Foreground"
Value="#112D4E" />
</Style>

<Style Selector="Button.IconBtn">
<Setter Property="Width" Value="50" />
<Setter Property="Width"
Value="50" />
</Style>
<!-- Border -->
<Style Selector="Border.VCent">
<Setter Property="VerticalAlignment"
Value="Center" />
</Style>
<Style Selector="Border.HCent">
<Setter Property="HorizontalAlignment"
Value="Center" />
</Style>
<!-- TextBlock -->
<Style Selector="TextBlock.VCent">
<Setter Property="VerticalAlignment"
Value="Center" />
</Style>
<Style Selector="TextBlock.HCent">
<Setter Property="HorizontalAlignment"
Value="Center" />
</Style>

<Style Selector="TextBlock.TCent">
<Setter Property="TextAlignment"
Value="Center" />
</Style>

<!-- Panels -->
<Style Selector="StackPanel.VCent">
<Setter Property="VerticalAlignment"
Value="Center" />
</Style>
<Style Selector="StackPanel.HCent">
<Setter Property="HorizontalAlignment"
Value="Center" />
</Style>

<Style Selector="StackPanel.H">
<Setter Property="Orientation"
Value="Horizontal" />
</Style>

<Style Selector="StackPanel.S3">
<Setter Property="Spacing"
Value="3" />
</Style>
<Style Selector="StackPanel.S5">
<Setter Property="Spacing"
Value="5" />
</Style>

<!-- Buttons -->
<Style Selector="Button.VCent">
<Setter Property="VerticalAlignment"
Value="Center" />
</Style>
<Style Selector="Button.HCent">
<Setter Property="HorizontalAlignment"
Value="Center" />
</Style>
<Style Selector="Button.HCCent">
<Setter Property="HorizontalContentAlignment"
Value="Center" />
</Style>
<Style Selector="Button.VCCent">
<Setter Property="VerticalContentAlignment"
Value="Center" />
</Style>


<!-- MaskedTextBox -->
<Style Selector="MaskedTextBox.VCent">
<Setter Property="VerticalAlignment"
Value="Center" />
</Style>
<Style Selector="MaskedTextBox.HCent">
<Setter Property="HorizontalAlignment"
Value="Center" />
</Style>
</Styles>
2 changes: 1 addition & 1 deletion Muscurdi/Exceptions/InvalidPasswordException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Muscurdi.Exceptions;
public class InvalidPasswordException: DbException
public class InvalidPasswordException : DbException
{
public InvalidPasswordException(string? message) : base(message) { }
}
2 changes: 1 addition & 1 deletion Muscurdi/Libs/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static string Encrypt(string plainText, MasterPassword password)
}
}
}
catch (Exception exc)
catch (Exception)
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion Muscurdi/Libs/Singleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Muscurdi.Libs;

public class Singleton<T> where T : class, new()
{
private static volatile T _instance;
private static volatile T? _instance;
private static readonly object _lock = new object();
public static T Instance
{
Expand Down
4 changes: 2 additions & 2 deletions Muscurdi/Models/Key.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Muscurdi.Models;
public class Key
{
public string Id { get; set; }
public string Value { get; set; }
public string? Id { get; set; }
public string? Value { get; set; }
}
7 changes: 4 additions & 3 deletions Muscurdi/Models/PasswordEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ namespace Muscurdi.Models;
public class PasswordEntry
{
[LiteDB.BsonId]
public LiteDB.ObjectId Id { get; set; }
public string Name { get; set; }
public string Password { get; set; }
public LiteDB.ObjectId? Id { get; set; }
public string? Name { get; set; }
public string? Username { get; set; }
public string? Password { get; set; }
}
1 change: 1 addition & 0 deletions Muscurdi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Program
[STAThread]
public static void Main(string[] args)
{
// maybe we can disable this on debug?
SingleAppInstance.Check("muscurd-i");
BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);
Expand Down
26 changes: 24 additions & 2 deletions Muscurdi/Services/Db.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Init(MasterPassword masterPassword)
{
_key = (Crypto.Decrypt(key.Value, masterPassword) == masterPassword.ToString()) ? masterPassword : null;
}
catch (Exception exc)
catch (Exception)
{
throw new InvalidPasswordException("Wrong Master Password");
}
Expand All @@ -56,6 +56,28 @@ public void Init(MasterPassword masterPassword)
Passwords = _db.GetCollection<PasswordEntry>(PASSWORD_ENTRIES_COLLECTION);
}

public int Count() => Passwords.Count();

public PasswordEntry? GetById(LiteDB.ObjectId id)
{
var password = Passwords.FindById(id);
password.Password = Crypto.Decrypt(password.Password, _key);
return password;
}

public List<PasswordEntry>? GetAll() => Passwords.FindAll().Select(p =>
{
p.Password = Crypto.Decrypt(p.Password, _key) ?? "ERROR-DECRYPTING";
return p;
}).ToList();


public void UpdatePassword(PasswordEntry password)
{
password.Password = Crypto.Encrypt(password.Password, _key);
Passwords.Update(password);
}

public bool AddPassword(PasswordEntry password)
{
Passwords.EnsureIndex(x => x.Name, true);
Expand All @@ -65,7 +87,7 @@ public bool AddPassword(PasswordEntry password)
{
result = Passwords.Insert(password);
}
catch (LiteDB.LiteException _)
catch (LiteDB.LiteException)
{
result = null;

Expand Down
Loading

0 comments on commit 8a4a0f8

Please sign in to comment.