mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-26 03:49:23 +08:00
created a password-has-numbers constraint
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using mRemoteNG.Security;
|
||||
using mRemoteNG.Security.PasswordCreation;
|
||||
using NUnit.Framework;
|
||||
|
||||
|
||||
namespace mRemoteNGTests.Security.PasswordCreation
|
||||
{
|
||||
public class PasswordIncludesNumbersConstraintTests
|
||||
{
|
||||
private PasswordIncludesNumbersConstraint _includesNumbersConstraint;
|
||||
|
||||
[Test]
|
||||
public void PasswordWithANumberPassesConstraint()
|
||||
{
|
||||
var password = "hello1".ConvertToSecureString();
|
||||
_includesNumbersConstraint = new PasswordIncludesNumbersConstraint();
|
||||
Assert.That(_includesNumbersConstraint.Validate(password), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PasswordWithFewerThanTheMinimumCountOfNumbersFailsConstraint()
|
||||
{
|
||||
var password = "hello1".ConvertToSecureString();
|
||||
_includesNumbersConstraint = new PasswordIncludesNumbersConstraint(2);
|
||||
Assert.That(_includesNumbersConstraint.Validate(password), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PasswordWithoutNumbersFailsConstraint()
|
||||
{
|
||||
var password = "hello".ConvertToSecureString();
|
||||
_includesNumbersConstraint = new PasswordIncludesNumbersConstraint();
|
||||
Assert.That(_includesNumbersConstraint.Validate(password), Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CountOfNumbersToRequireMustBeAPositiveValue()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => new PasswordIncludesNumbersConstraint(-1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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\PasswordIncludesNumbersConstraintTests.cs" />
|
||||
<Compile Include="Security\PasswordCreation\PasswordLengthConstraintTests.cs" />
|
||||
<Compile Include="Security\SecureStringExtensionsTests.cs" />
|
||||
<Compile Include="Security\XmlCryptoProviderBuilderTests.cs" />
|
||||
|
||||
@@ -3820,6 +3820,15 @@ namespace mRemoteNG {
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Password must contain at least {0} number(s).
|
||||
/// </summary>
|
||||
internal static string strPasswordContainsNumbersConstraint {
|
||||
get {
|
||||
return ResourceManager.GetString("strPasswordContainsNumbersConstraint", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Looks up a localized string similar to Password length must be between {0} and {1}.
|
||||
/// </summary>
|
||||
|
||||
@@ -2463,6 +2463,9 @@ mRemoteNG will now quit and begin with the installation.</value>
|
||||
<data name="strPropertyNameRDPAlertIdleTimeout" xml:space="preserve">
|
||||
<value>Alert on Idle Disconnect</value>
|
||||
</data>
|
||||
<data name="strPasswordContainsNumbersConstraint" xml:space="preserve">
|
||||
<value>Password must contain at least {0} number(s)</value>
|
||||
</data>
|
||||
<data name="strPasswordLengthConstraintHint" xml:space="preserve">
|
||||
<value>Password length must be between {0} and {1}</value>
|
||||
</data>
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Security;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace mRemoteNG.Security.PasswordCreation
|
||||
{
|
||||
public class PasswordIncludesNumbersConstraint : IPasswordConstraint
|
||||
{
|
||||
private readonly int _minimumCount;
|
||||
|
||||
public string ConstraintHint { get; }
|
||||
|
||||
public PasswordIncludesNumbersConstraint(int minimumCount = 1)
|
||||
{
|
||||
if (minimumCount < 0)
|
||||
throw new ArgumentException($"{nameof(minimumCount)} must be a positive value");
|
||||
|
||||
_minimumCount = minimumCount;
|
||||
ConstraintHint = string.Format(Language.strPasswordContainsNumbersConstraint, _minimumCount);
|
||||
}
|
||||
|
||||
public bool Validate(SecureString password)
|
||||
{
|
||||
var regex = new Regex(@"\d");
|
||||
return regex.Matches(password.ConvertToUnsecureString()).Count >= _minimumCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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\PasswordIncludesNumbersConstraint.cs" />
|
||||
<Compile Include="Security\PasswordCreation\PasswordLengthConstraint.cs" />
|
||||
<Compile Include="Security\SymmetricEncryption\AeadCryptographyProvider.cs" />
|
||||
<Compile Include="Security\SymmetricEncryption\LegacyRijndaelCryptographyProvider.cs" />
|
||||
|
||||
Reference in New Issue
Block a user