created password length constraint

This commit is contained in:
David Sparer
2017-01-28 12:42:46 -07:00
parent 03e8342c90
commit 11b67208f4
7 changed files with 130 additions and 1 deletions

View File

@@ -0,0 +1,64 @@
using System;
using mRemoteNG.Security;
using mRemoteNG.Security.PasswordCreation;
using NUnit.Framework;
namespace mRemoteNGTests.Security.PasswordCreation
{
public class PasswordLengthConstraintTests
{
private PasswordLengthConstraint _lengthConstraint;
[Test]
public void PasswordLessThanMinimumLengthFailsValidation()
{
var password = "123456789".ConvertToSecureString();
_lengthConstraint = new PasswordLengthConstraint(10);
Assert.That(_lengthConstraint.Validate(password), Is.False);
}
[Test]
public void PasswordThatEqualsMinimumLengthPassesValidation()
{
var password = "12345".ConvertToSecureString();
_lengthConstraint = new PasswordLengthConstraint(5);
Assert.That(_lengthConstraint.Validate(password), Is.True);
}
[Test]
public void PasswordGreaterThanMaxLengthFailsValidation()
{
var password = "123456".ConvertToSecureString();
_lengthConstraint = new PasswordLengthConstraint(1, 5);
Assert.That(_lengthConstraint.Validate(password), Is.False);
}
[Test]
public void PasswordThatEqualsMaxLengthPassesValidation()
{
var password = "12345".ConvertToSecureString();
_lengthConstraint = new PasswordLengthConstraint(1, 5);
Assert.That(_lengthConstraint.Validate(password), Is.True);
}
[Test]
public void MinimumLengthMustBeAPositiveValue()
{
Assert.Throws<ArgumentException>(() => new PasswordLengthConstraint(-1));
}
[Test]
public void MaximumLengthMustBeAPositiveValue()
{
Assert.Throws<ArgumentException>(() => new PasswordLengthConstraint(1, -1));
}
[Test]
public void MaximumLengthMustBeGreaterThanMinimumLength()
{
Assert.Throws<ArgumentException>(() => new PasswordLengthConstraint(4, 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\PasswordLengthConstraintTests.cs" />
<Compile Include="Security\SecureStringExtensionsTests.cs" />
<Compile Include="Security\XmlCryptoProviderBuilderTests.cs" />
<Compile Include="Tools\ExternalToolsArgumentParserTests.cs" />

View File

@@ -3820,6 +3820,15 @@ namespace mRemoteNG {
}
}
/// <summary>
/// Looks up a localized string similar to Password length must be between {0} and {1}.
/// </summary>
internal static string strPasswordLengthConstraintHint {
get {
return ResourceManager.GetString("strPasswordLengthConstraintHint", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Password protect.
/// </summary>

View File

@@ -2463,4 +2463,7 @@ mRemoteNG will now quit and begin with the installation.</value>
<data name="strPropertyNameRDPAlertIdleTimeout" xml:space="preserve">
<value>Alert on Idle Disconnect</value>
</data>
</root>
<data name="strPasswordLengthConstraintHint" xml:space="preserve">
<value>Password length must be between {0} and {1}</value>
</data>
</root>

View File

@@ -0,0 +1,12 @@
using System.Security;
namespace mRemoteNG.Security.PasswordCreation
{
public interface IPasswordConstraint
{
string ConstraintHint { get; }
bool Validate(SecureString password);
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Security;
namespace mRemoteNG.Security.PasswordCreation
{
public class PasswordLengthConstraint : IPasswordConstraint
{
private readonly int _minLength;
private readonly int _maxLength;
public string ConstraintHint { get; }
public PasswordLengthConstraint(int minimumLength, int maxLength = int.MaxValue)
{
if (minimumLength < 0)
throw new ArgumentException($"{nameof(minimumLength)} must be a positive value.");
if (maxLength <= 0)
throw new ArgumentException($"{nameof(maxLength)} must be a positive, non-zero value.");
if (maxLength < minimumLength)
throw new ArgumentException($"{nameof(maxLength)} must be greater than or equal to {nameof(minimumLength)}.");
_minLength = minimumLength;
_maxLength = maxLength;
ConstraintHint = string.Format(Language.strPasswordLengthConstraintHint, _minLength, _maxLength);
}
public bool Validate(SecureString password)
{
if (password.Length < _minLength)
return false;
if (password.Length > _maxLength)
return false;
return true;
}
}
}

View File

@@ -223,8 +223,10 @@
<Compile Include="Security\EncryptedSecureString.cs" />
<Compile Include="Security\Authentication\IAuthenticator.cs" />
<Compile Include="Security\EncryptionException.cs" />
<Compile Include="Security\PasswordCreation\IPasswordConstraint.cs" />
<Compile Include="Security\KeyDerivation\IKeyDerivationFunction.cs" />
<Compile Include="Security\KeyDerivation\Pkcs5S2KeyGenerator.cs" />
<Compile Include="Security\PasswordCreation\PasswordLengthConstraint.cs" />
<Compile Include="Security\SymmetricEncryption\AeadCryptographyProvider.cs" />
<Compile Include="Security\SymmetricEncryption\LegacyRijndaelCryptographyProvider.cs" />
<Compile Include="Security\ICryptographyProvider.cs" />