Added a password rule engine

This commit is contained in:
TheXamlGuy
2024-06-09 13:10:57 +01:00
parent e8372f875f
commit 84e89e1c47
12 changed files with 199 additions and 4 deletions
+13
View File
@@ -0,0 +1,13 @@
using System.Text.RegularExpressions;
namespace Bitvault;
public partial class DigitRule :
IPasswordRule
{
public int CalculateScore(string password) =>
Regex().IsMatch(password) ? 2 : 0;
[GeneratedRegex(@"\d")]
private static partial Regex Regex();
}
+29
View File
@@ -0,0 +1,29 @@
using System.Text.RegularExpressions;
namespace Bitvault;
public partial class DiversityBonusRule :
IPasswordRule
{
public int CalculateScore(string password)
{
bool hasUppercase = UppercaseRegex().IsMatch(password);
bool hasLowercase = LowercaseRegex().IsMatch(password);
bool hasDigit = DigitRegex().IsMatch(password);
bool hasSpecialChar = SpecialCharRegex().IsMatch(password);
return (hasUppercase && hasLowercase && hasDigit && hasSpecialChar) ? 2 : 0;
}
[GeneratedRegex(@"[A-Z]")]
private static partial Regex UppercaseRegex();
[GeneratedRegex(@"[a-z]")]
private static partial Regex LowercaseRegex();
[GeneratedRegex(@"\d")]
private static partial Regex DigitRegex();
[GeneratedRegex(@"[^a-zA-Z0-9]")]
private static partial Regex SpecialCharRegex();
}
+6
View File
@@ -0,0 +1,6 @@
namespace Bitvault;
public interface IPasswordRule
{
int CalculateScore(string password);
}
+4
View File
@@ -719,6 +719,7 @@ public record ItemConfiguration
{
Label = "Type",
Values = ["American Express", "Discover", "Maestro", "Mastercard", "Visa"],
Width = 120
},
new MaskedTextEntryConfiguration
{
@@ -731,18 +732,21 @@ public record ItemConfiguration
Label = "Expiry Date",
Pattern = "00/00",
Value = "__/__",
Width = 120
},
new MaskedTextEntryConfiguration
{
Label = "Valid From",
Pattern = "00/00",
Value = "__/__",
Width = 120
},
new MaskedTextEntryConfiguration
{
Label = "Card verification code",
Pattern = "000",
Value = "___",
Width = 120
},
}
},
+8
View File
@@ -0,0 +1,8 @@
namespace Bitvault;
public class LengthRule :
IPasswordRule
{
public int CalculateScore(string password) =>
password.Length >= 8 ? 5 : 0;
}
+14
View File
@@ -0,0 +1,14 @@
using System.Text.RegularExpressions;
namespace Bitvault;
public partial class LowercaseRule :
IPasswordRule
{
public int CalculateScore(string password) =>
Regex().IsMatch(password) ? 2 : 0;
[GeneratedRegex(@"[a-z]")]
private static partial Regex Regex();
}
+12
View File
@@ -0,0 +1,12 @@
using System.Text.RegularExpressions;
namespace Bitvault;
public partial class SpecialCharRule : IPasswordRule
{
public int CalculateScore(string password) =>
Regex().IsMatch(password) ? 2 : 0;
[GeneratedRegex(@"[^a-zA-Z0-9]")]
private static partial Regex Regex();
}
+13
View File
@@ -0,0 +1,13 @@
using System.Text.RegularExpressions;
namespace Bitvault;
public partial class UppercaseRule :
IPasswordRule
{
public int CalculateScore(string password) =>
Regex().IsMatch(password) ? 2 : 0;
[GeneratedRegex(@"[A-Z]")]
private static partial Regex Regex();
}