Added a password rule engine
This commit is contained in:
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Bitvault;
|
||||
|
||||
public interface IPasswordRule
|
||||
{
|
||||
int CalculateScore(string password);
|
||||
}
|
||||
@@ -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
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Bitvault;
|
||||
|
||||
public class LengthRule :
|
||||
IPasswordRule
|
||||
{
|
||||
public int CalculateScore(string password) =>
|
||||
password.Length >= 8 ? 5 : 0;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user