Skip to content

Commit

Permalink
Fix crash after import and restart
Browse files Browse the repository at this point in the history
Fixes #23 and closes #27
  • Loading branch information
timokoessler committed Jul 25, 2024
1 parent 94897e9 commit e5e00c4
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 6 deletions.
3 changes: 3 additions & 0 deletions Guard.Core/Storage/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public static void Init()
{
return;
}
// Prevent empty strings from being converted to null
BsonMapper.Global.EmptyStringToNull = false;
// Initialize the database
db = new LiteDatabase($"Filename={GetDBPath()}") { UserVersion = 1 };

tokens = db.GetCollection<DBTOTPToken>("tokens");
Expand Down
2 changes: 1 addition & 1 deletion Guard.WPF/Core/Icons/IconManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static string[] GetIconNames()

public static TotpIcon GetIcon(string name, IconType type)
{
if (name == null || name == "default" || type == IconType.Default)
if (string.IsNullOrEmpty(name) || name == "default" || type == IconType.Default)
{
return defaultIcon;
}
Expand Down
7 changes: 6 additions & 1 deletion Guard.WPF/Core/Import/Importer/AegisAuthenticatorImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public bool RequiresPassword(string? path)
new()
{
Id = TokenManager.GetNextId(),
Issuer = string.IsNullOrEmpty(token.Issuer) ? "???" : token.Issuer,
Issuer = string.IsNullOrEmpty(token.Issuer) ? "" : token.Issuer,
EncryptedSecret = encryption.EncryptStringToBytes(normalizedSecret),
CreationTime = DateTime.Now
};
Expand All @@ -270,6 +270,11 @@ public bool RequiresPassword(string? path)
dbToken.EncryptedUsername = encryption.EncryptStringToBytes(token.Name);
}

if (string.IsNullOrEmpty(token.Issuer) && string.IsNullOrEmpty(token.Name))
{
throw new Exception("Token must have either issuer or name set.");
}

if (icon != null && icon.Type != IconType.Default)
{
dbToken.Icon = icon.Name;
Expand Down
2 changes: 1 addition & 1 deletion Guard.WPF/Core/Import/Importer/AuthenticatorProImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private enum BackupType
EncryptionHelper encryption = Auth.GetMainEncryptionHelper();
foreach (var token in backup.Authenticators)
{
if (token.Issuer == null)
if (string.IsNullOrEmpty(token.Issuer))
{
throw new Exception("Invalid AuthenticatorPro backup: No issuer found");
}
Expand Down
2 changes: 1 addition & 1 deletion Guard.WPF/Core/Import/Importer/BitwardenImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static BitwardenEncryptionType GetEncryptionType(BitwardenExportFile exp
}
else
{
if (item.Name == null)
if (string.IsNullOrEmpty(item.Name))
{
throw new Exception("Invalid Bitwarden export file: No item name found");
}
Expand Down
4 changes: 2 additions & 2 deletions Guard.WPF/Core/Import/OTPUriParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ internal static OTPUri Parse(string uriString)
}
}

if (otpUri.Issuer == null)
if (string.IsNullOrEmpty(otpUri.Issuer))
{
throw new Exception($"Missing issuer in URI");
}

if (otpUri.Secret == null)
if (string.IsNullOrEmpty(otpUri.Secret))
{
throw new Exception($"Missing secret in URI from {otpUri.Issuer}");
}
Expand Down
5 changes: 5 additions & 0 deletions Guard.WPF/Views/Controls/TokenCard.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ internal TokenCard(TOTPTokenHelper token)

mainWindow = (MainWindow)Application.Current.MainWindow;

if (string.IsNullOrEmpty(token.dBToken.Issuer))
{
token.dBToken.Issuer = "???";
}

Issuer.Text = token.dBToken.Issuer;
if (token.dBToken.Issuer.Length > 18)
{
Expand Down
4 changes: 4 additions & 0 deletions Guard.WPF/Views/Pages/Add/TokenSettings.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public TokenSettings()
// Set values if action is edit
if (existingToken != null)
{
if (string.IsNullOrEmpty(existingToken.dBToken.Issuer))
{
existingToken.dBToken.Issuer = "???";
}
EncryptionHelper encryptionHelper = Auth.GetMainEncryptionHelper();
Issuer.Text = existingToken.dBToken.Issuer;
Secret.Password = encryptionHelper.DecryptBytesToString(
Expand Down

0 comments on commit e5e00c4

Please sign in to comment.