created a has-upper-case constraint

This commit is contained in:
David Sparer
2017-01-28 13:39:51 -07:00
parent 69be19c02b
commit bd2054e5a4
6 changed files with 93 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
using System;
using mRemoteNG.Security;
using mRemoteNG.Security.PasswordCreation;
using NUnit.Framework;
namespace mRemoteNGTests.Security.PasswordCreation
{
public class PasswordIncludesUpperCaseConstraintTests
{
private PasswordIncludesUpperCaseConstraint _lowerCaseConstraint;
[Test]
public void PasswordThatExceedsMinimumLowerCasePassesValidation()
{
var password = "HELLO".ConvertToSecureString();
_lowerCaseConstraint = new PasswordIncludesUpperCaseConstraint();
Assert.That(_lowerCaseConstraint.Validate(password), Is.True);
}
[Test]
public void PasswordThatMeetsMinimumLowerCasePassesValidation()
{
var password = "HELLO".ConvertToSecureString();
_lowerCaseConstraint = new PasswordIncludesUpperCaseConstraint(5);
Assert.That(_lowerCaseConstraint.Validate(password), Is.True);
}
[Test]
public void PasswordWithFewerThanMinimumLowerCaseFailsValidation()
{
var password = "Hello".ConvertToSecureString();
_lowerCaseConstraint = new PasswordIncludesUpperCaseConstraint(2);
Assert.That(_lowerCaseConstraint.Validate(password), Is.False);
}
[Test]
public void PasswordWithoutUpperCaseFailsValidation()
{
var password = "hello".ConvertToSecureString();
_lowerCaseConstraint = new PasswordIncludesUpperCaseConstraint();
Assert.That(_lowerCaseConstraint.Validate(password), Is.False);
}
[Test]
public void CountToRequireMustBeAPositiveValue()
{
Assert.Throws<ArgumentException>(() => new PasswordIncludesLowerCaseConstraint(-1));
}
}
}

View File

@@ -140,6 +140,7 @@
<Compile Include="NUnitExtensions\SecureTextBoxTester.cs" />
<Compile Include="Security\Authentication\PasswordAuthenticatorTests.cs" />
<Compile Include="Security\KeyDerivation\Pkcs5S2KeyGeneratorTests.cs" />
<Compile Include="Security\PasswordCreation\PasswordIncludesUpperCaseConstraintTests.cs" />
<Compile Include="Security\PasswordCreation\PasswordIncludesLowerCaseConstraintTests.cs" />
<Compile Include="Security\PasswordCreation\PasswordIncludesNumbersConstraintTests.cs" />
<Compile Include="Security\PasswordCreation\PasswordLengthConstraintTests.cs" />

View File

@@ -3838,6 +3838,15 @@ namespace mRemoteNG {
}
}
/// <summary>
/// Looks up a localized string similar to Password must contain at least {0} upper case character(s).
/// </summary>
internal static string strPasswordContainsUpperCaseConstraintHint {
get {
return ResourceManager.GetString("strPasswordContainsUpperCaseConstraintHint", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Password length must be between {0} and {1}.
/// </summary>

View File

@@ -2469,6 +2469,9 @@ mRemoteNG will now quit and begin with the installation.</value>
<data name="strPasswordContainsNumbersConstraint" xml:space="preserve">
<value>Password must contain at least {0} number(s)</value>
</data>
<data name="strPasswordContainsUpperCaseConstraintHint" xml:space="preserve">
<value>Password must contain at least {0} upper case character(s)</value>
</data>
<data name="strPasswordLengthConstraintHint" xml:space="preserve">
<value>Password length must be between {0} and {1}</value>
</data>

View File

@@ -0,0 +1,28 @@
using System;
using System.Security;
using System.Text.RegularExpressions;
namespace mRemoteNG.Security.PasswordCreation
{
public class PasswordIncludesUpperCaseConstraint : IPasswordConstraint
{
private readonly int _minimumCount;
public string ConstraintHint { get; }
public PasswordIncludesUpperCaseConstraint(int minimumCount = 1)
{
if (minimumCount < 0)
throw new ArgumentException($"{nameof(minimumCount)} must be a positive value");
_minimumCount = minimumCount;
ConstraintHint = string.Format(Language.strPasswordContainsUpperCaseConstraintHint, _minimumCount);
}
public bool Validate(SecureString password)
{
var regex = new Regex(@"[A-Z]");
return regex.Matches(password.ConvertToUnsecureString()).Count >= _minimumCount;
}
}
}

View File

@@ -226,6 +226,7 @@
<Compile Include="Security\PasswordCreation\IPasswordConstraint.cs" />
<Compile Include="Security\KeyDerivation\IKeyDerivationFunction.cs" />
<Compile Include="Security\KeyDerivation\Pkcs5S2KeyGenerator.cs" />
<Compile Include="Security\PasswordCreation\PasswordIncludesUpperCaseConstraint.cs" />
<Compile Include="Security\PasswordCreation\PasswordIncludesLowerCaseConstraint.cs" />
<Compile Include="Security\PasswordCreation\PasswordIncludesNumbersConstraint.cs" />
<Compile Include="Security\PasswordCreation\PasswordLengthConstraint.cs" />