2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using Moq;
|
2019-11-25 21:20:00 +11:00
|
|
|
using NUnit.Framework;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Configuration;
|
|
|
|
|
using Umbraco.Cms.Core.Security;
|
|
|
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
2019-11-25 21:20:00 +11:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Security
|
2019-11-25 21:20:00 +11:00
|
|
|
{
|
|
|
|
|
[TestFixture]
|
2020-10-07 16:56:48 +11:00
|
|
|
public class LegacyPasswordSecurityTests
|
2019-11-25 21:20:00 +11:00
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public void Check_Password_Hashed_Non_KeyedHashAlgorithm()
|
|
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
IPasswordConfiguration passwordConfiguration = Mock.Of<IPasswordConfiguration>(x => x.HashAlgorithmType == "SHA256");
|
2020-10-07 16:56:48 +11:00
|
|
|
var passwordSecurity = new LegacyPasswordSecurity();
|
2019-11-25 21:20:00 +11:00
|
|
|
|
|
|
|
|
var pass = "ThisIsAHashedPassword";
|
2020-12-20 08:36:11 +01:00
|
|
|
var hashed = passwordSecurity.HashNewPassword(passwordConfiguration.HashAlgorithmType, pass, out string salt);
|
2020-10-07 16:56:48 +11:00
|
|
|
var storedPassword = passwordSecurity.FormatPasswordForStorage(passwordConfiguration.HashAlgorithmType, hashed, salt);
|
2019-11-25 21:20:00 +11:00
|
|
|
|
2020-10-07 15:20:43 +11:00
|
|
|
var result = passwordSecurity.VerifyPassword(passwordConfiguration.HashAlgorithmType, "ThisIsAHashedPassword", storedPassword);
|
2019-11-25 21:20:00 +11:00
|
|
|
|
|
|
|
|
Assert.IsTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Check_Password_Hashed_KeyedHashAlgorithm()
|
|
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
IPasswordConfiguration passwordConfiguration = Mock.Of<IPasswordConfiguration>(x => x.HashAlgorithmType == Constants.Security.AspNetUmbraco8PasswordHashAlgorithmName);
|
2020-10-07 16:56:48 +11:00
|
|
|
var passwordSecurity = new LegacyPasswordSecurity();
|
2019-11-25 21:20:00 +11:00
|
|
|
|
|
|
|
|
var pass = "ThisIsAHashedPassword";
|
2020-12-20 08:36:11 +01:00
|
|
|
var hashed = passwordSecurity.HashNewPassword(passwordConfiguration.HashAlgorithmType, pass, out string salt);
|
2020-10-07 16:56:48 +11:00
|
|
|
var storedPassword = passwordSecurity.FormatPasswordForStorage(passwordConfiguration.HashAlgorithmType, hashed, salt);
|
|
|
|
|
|
|
|
|
|
var result = passwordSecurity.VerifyPassword(passwordConfiguration.HashAlgorithmType, "ThisIsAHashedPassword", storedPassword);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Check_Password_Legacy_v4_SHA1()
|
|
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
IPasswordConfiguration passwordConfiguration = Mock.Of<IPasswordConfiguration>(x => x.HashAlgorithmType == Constants.Security.AspNetUmbraco4PasswordHashAlgorithmName);
|
2020-10-07 16:56:48 +11:00
|
|
|
var passwordSecurity = new LegacyPasswordSecurity();
|
|
|
|
|
|
|
|
|
|
var pass = "ThisIsAHashedPassword";
|
2020-12-20 08:36:11 +01:00
|
|
|
var hashed = passwordSecurity.HashNewPassword(passwordConfiguration.HashAlgorithmType, pass, out string salt);
|
2020-10-07 16:56:48 +11:00
|
|
|
var storedPassword = passwordSecurity.FormatPasswordForStorage(passwordConfiguration.HashAlgorithmType, hashed, salt);
|
2019-11-25 21:20:00 +11:00
|
|
|
|
2020-10-07 15:20:43 +11:00
|
|
|
var result = passwordSecurity.VerifyPassword(passwordConfiguration.HashAlgorithmType, "ThisIsAHashedPassword", storedPassword);
|
2019-11-25 21:20:00 +11:00
|
|
|
|
|
|
|
|
Assert.IsTrue(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Format_Pass_For_Storage_Hashed()
|
|
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
IPasswordConfiguration passwordConfiguration = Mock.Of<IPasswordConfiguration>(x => x.HashAlgorithmType == Constants.Security.AspNetUmbraco8PasswordHashAlgorithmName);
|
2020-10-07 16:56:48 +11:00
|
|
|
var passwordSecurity = new LegacyPasswordSecurity();
|
2019-11-25 21:20:00 +11:00
|
|
|
|
2020-05-28 23:24:32 +10:00
|
|
|
var salt = LegacyPasswordSecurity.GenerateSalt();
|
2019-11-25 21:20:00 +11:00
|
|
|
var stored = "ThisIsAHashedPassword";
|
|
|
|
|
|
2020-10-07 16:56:48 +11:00
|
|
|
var result = passwordSecurity.FormatPasswordForStorage(passwordConfiguration.HashAlgorithmType, stored, salt);
|
2019-11-25 21:20:00 +11:00
|
|
|
|
|
|
|
|
Assert.AreEqual(salt + "ThisIsAHashedPassword", result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Get_Stored_Password_Hashed()
|
|
|
|
|
{
|
2020-12-20 08:36:11 +01:00
|
|
|
IPasswordConfiguration passwordConfiguration = Mock.Of<IPasswordConfiguration>(x => x.HashAlgorithmType == Constants.Security.AspNetUmbraco8PasswordHashAlgorithmName);
|
2020-10-07 16:56:48 +11:00
|
|
|
var passwordSecurity = new LegacyPasswordSecurity();
|
2019-11-25 21:20:00 +11:00
|
|
|
|
2020-05-28 23:24:32 +10:00
|
|
|
var salt = LegacyPasswordSecurity.GenerateSalt();
|
2019-11-25 21:20:00 +11:00
|
|
|
var stored = salt + "ThisIsAHashedPassword";
|
|
|
|
|
|
2020-12-20 08:36:11 +01:00
|
|
|
var result = passwordSecurity.ParseStoredHashPassword(passwordConfiguration.HashAlgorithmType, stored, out string initSalt);
|
2019-11-25 21:20:00 +11:00
|
|
|
|
|
|
|
|
Assert.AreEqual("ThisIsAHashedPassword", result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The salt generated is always the same length
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Test]
|
|
|
|
|
public void Check_Salt_Length()
|
|
|
|
|
{
|
|
|
|
|
var lastLength = 0;
|
|
|
|
|
for (var i = 0; i < 10000; i++)
|
|
|
|
|
{
|
2020-05-28 23:24:32 +10:00
|
|
|
var result = LegacyPasswordSecurity.GenerateSalt();
|
2019-11-25 21:20:00 +11:00
|
|
|
|
|
|
|
|
if (i > 0)
|
2020-12-20 08:36:11 +01:00
|
|
|
{
|
2019-11-25 21:20:00 +11:00
|
|
|
Assert.AreEqual(lastLength, result.Length);
|
2020-12-20 08:36:11 +01:00
|
|
|
}
|
2019-11-25 21:20:00 +11:00
|
|
|
|
|
|
|
|
lastLength = result.Length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|