BusinessMgr

This commit is contained in:
Andy Howard
2025-01-22 08:02:18 +00:00
parent dea70843c5
commit 82a9d3497a
5 changed files with 33 additions and 26 deletions
@@ -4,6 +4,7 @@ using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Bus;
using Sarand.Net.Standard.Base.Dbas;
using Sarand.Net.Standard.Base.UpdateLog;
using static Sarand.Net.Standard.Base.Security.SaSecurityMgr;
namespace Sarand.Net.Standard.Base.Security
{
@@ -22,11 +23,12 @@ namespace Sarand.Net.Standard.Base.Security
Right = 4,
}
// Password storage methods - must be kept up to date with legacy application.
public enum PasswordMethod
public enum PasswordStorageMethod
{
Legacy = 0,
MsSql = 10,
}
public enum PasswordHashMethod { MsSql }
public enum FilterAttrType
{
Code, // Partial code.
@@ -166,6 +168,12 @@ namespace Sarand.Net.Standard.Base.Security
/// <summary>Does the password match that currently defined against the user.</summary>
public abstract bool PasswordMatches(SaSecurityItem securityItemUser, String password, DbUpdateInfo dbUpdateInfo);
/// <summary>Generate password hash using the specified method.</summary>
public abstract String EncodePassword_Hex(String password, SaSecurity.PasswordHashMethod method = SaSecurity.PasswordHashMethod.MsSql);
/// <summary>Compare password to hashed value using the specified method.</summary>
public abstract bool ComparePassword_Hex(String password, String passwordHashHex, SaSecurity.PasswordHashMethod method = SaSecurity.PasswordHashMethod.MsSql);
/// <summary>Show form to change password for currently logged in user.</summary>
/// <returns>Flag to indicate password changed</returns>
public abstract bool ShowChangePasswordForm(DbUpdateInfo dbUpdateInfo);
@@ -366,16 +366,16 @@ namespace Sarand.Net.Standard.Base.Security
// Password utilities.
/// <summary>Extract password storage method from data.</summary>
public SaSecurity.PasswordMethod GetPasswordMethod()
public SaSecurity.PasswordStorageMethod GetPasswordMethod()
{
if (PasswordSalt == PasswordSalt_MsSql && !StringUtil.IsBlank(PasswordHash))
return SaSecurity.PasswordMethod.MsSql;
return SaSecurity.PasswordMethod.Legacy;
return SaSecurity.PasswordStorageMethod.MsSql;
return SaSecurity.PasswordStorageMethod.Legacy;
}
public void SetEncodedPassword(SaSecurity.PasswordMethod method, String encodedPassword)
public void SetEncodedPassword(SaSecurity.PasswordStorageMethod method, String encodedPassword)
{
if (method == SaSecurity.PasswordMethod.MsSql)
if (method == SaSecurity.PasswordStorageMethod.MsSql)
{
#pragma warning disable CS0618 // Deprecated property
PasswordEncrypted = "";
@@ -35,7 +35,6 @@ namespace Sarand.Net.Standard.Base.Security
//-------------------------------------------------------------------
// Password utilities.
public enum PasswordHashMethod { MsSql }
/// <summary>Store the password against against the user.</summary>
public abstract bool SetPassword(SaSecurityItem securityItemUser, String password);
@@ -47,10 +46,10 @@ namespace Sarand.Net.Standard.Base.Security
public abstract bool PasswordIsBlank(SaSecurityItem securityItemUser);
/// <summary>Generate password hash using the specified method.</summary>
public abstract String EncodePassword_Hex(String password, PasswordHashMethod method = PasswordHashMethod.MsSql);
public abstract String EncodePassword_Hex(String password, SaSecurity.PasswordHashMethod method = SaSecurity.PasswordHashMethod.MsSql);
/// <summary>Compare password to hashed value using the specified method.</summary>
public abstract bool ComparePassword_Hex(String password, String passwordHashHex, PasswordHashMethod method = PasswordHashMethod.MsSql);
public abstract bool ComparePassword_Hex(String password, String passwordHashHex, SaSecurity.PasswordHashMethod method = SaSecurity.PasswordHashMethod.MsSql);
//-------------------------------------------------------------------
}
@@ -103,13 +103,13 @@ namespace Sarand.Net.Standard.Dbas.Security
}
/// <summary>Generate password hash using the specified method.</summary>
public String EncodePassword_Hex(String password, PasswordHashMethod method = PasswordHashMethod.MsSql)
public override String EncodePassword_Hex(String password, SaSecurity.PasswordHashMethod method = SaSecurity.PasswordHashMethod.MsSql)
{
return securityMgr.EncodePassword_Hex(password, method);
}
/// <summary>Compare password to hashed value using the specified method.</summary>
public bool ComparePassword_Hex(String password, String passwordHashHex, PasswordHashMethod method = PasswordHashMethod.MsSql)
public override bool ComparePassword_Hex(String password, String passwordHashHex, SaSecurity.PasswordHashMethod method = SaSecurity.PasswordHashMethod.MsSql)
{
return securityMgr.ComparePassword_Hex(password, passwordHashHex, method);
}
@@ -48,26 +48,26 @@ namespace Sarand.Net.Standard.Dbas.Security
/// <summary>Store the password against against the user.</summary>
public override bool SetPassword(SaSecurityItem securityItemUser, String password)
{
SaSecurity.PasswordMethod method = SaSecurity.PasswordMethod.Legacy;
SaSecurity.PasswordStorageMethod method = SaSecurity.PasswordStorageMethod.Legacy;
// If the Password Salt/Hash columns exist, attempt to encode password using non-legacy method.
if (ColumnExists(SaSecurityDbDfn.ColumnIdx.SaSecItem_PwordSalt) && ColumnExists(SaSecurityDbDfn.ColumnIdx.SaSecItem_PwordHash))
{
// NB Do not use Microsoft.AspNetCore.Identity.PasswordHasher.HashPassword() while legacy (C++) desktop application still in use.
// Encode password using MS SQL utilities.
String passwordHashHex_MsSql = EncodePassword_Hex(password, PasswordHashMethod.MsSql);
if (!StringUtil.IsBlank(passwordHashHex_MsSql) && ComparePassword_Hex(password, passwordHashHex_MsSql, PasswordHashMethod.MsSql))
String passwordHashHex_MsSql = EncodePassword_Hex(password, SaSecurity.PasswordHashMethod.MsSql);
if (!StringUtil.IsBlank(passwordHashHex_MsSql) && ComparePassword_Hex(password, passwordHashHex_MsSql, SaSecurity.PasswordHashMethod.MsSql))
{
// MS SQL encoded password is valid.
securityItemUser.SetEncodedPassword(SaSecurity.PasswordMethod.MsSql, passwordHashHex_MsSql);
method = SaSecurity.PasswordMethod.MsSql;
securityItemUser.SetEncodedPassword(SaSecurity.PasswordStorageMethod.MsSql, passwordHashHex_MsSql);
method = SaSecurity.PasswordStorageMethod.MsSql;
}
}
if (method == SaSecurity.PasswordMethod.Legacy)
if (method == SaSecurity.PasswordStorageMethod.Legacy)
{
// Default to legacy method.
securityItemUser.SetEncodedPassword(SaSecurity.PasswordMethod.Legacy, EncodePassword_Legacy(password));
securityItemUser.SetEncodedPassword(SaSecurity.PasswordStorageMethod.Legacy, EncodePassword_Legacy(password));
}
return false;
}
@@ -82,11 +82,11 @@ namespace Sarand.Net.Standard.Dbas.Security
public override bool PasswordMatches(SaSecurityItem securityItemUser, String password, DbUpdateInfo dbUpdateInfo)
{
if (securityItemUser == null) return false;
SaSecurity.PasswordMethod method = securityItemUser.GetPasswordMethod();
if (method == SaSecurity.PasswordMethod.MsSql)
SaSecurity.PasswordStorageMethod method = securityItemUser.GetPasswordMethod();
if (method == SaSecurity.PasswordStorageMethod.MsSql)
{
// MsSql method.
return ComparePassword_Hex(password, securityItemUser.PasswordHash, PasswordHashMethod.MsSql);
return ComparePassword_Hex(password, securityItemUser.PasswordHash, SaSecurity.PasswordHashMethod.MsSql);
}
// Legacy method.
@@ -96,7 +96,7 @@ namespace Sarand.Net.Standard.Dbas.Security
if (matches && !StringUtil.IsBlank(password))
{
SetPassword(securityItemUser, password);
if (securityItemUser.GetPasswordMethod() != SaSecurity.PasswordMethod.Legacy)
if (securityItemUser.GetPasswordMethod() != SaSecurity.PasswordStorageMethod.Legacy)
UpdateSecurityItem(securityItemUser, dbUpdateInfo);
}
return matches;
@@ -167,10 +167,10 @@ namespace Sarand.Net.Standard.Dbas.Security
/// </summary>
/// <param name="password"></param>
/// <returns>String of hex digits corresponding to encrypted password.</returns>
public override String EncodePassword_Hex(String password, PasswordHashMethod method = PasswordHashMethod.MsSql)
public override String EncodePassword_Hex(String password, SaSecurity.PasswordHashMethod method = SaSecurity.PasswordHashMethod.MsSql)
{
ClearErrorState();
if (method == PasswordHashMethod.MsSql)
if (method == SaSecurity.PasswordHashMethod.MsSql)
{
if (Database.DbmsType == DbmsType.MsSql)
{
@@ -204,10 +204,10 @@ namespace Sarand.Net.Standard.Dbas.Security
/// <param name="password"></param>
/// <param name="passwordHashHex"></param>
/// <returns>True if the password matches the encrypted hash value.</returns>
public override bool ComparePassword_Hex(String password, String passwordHashHex, PasswordHashMethod method = PasswordHashMethod.MsSql)
public override bool ComparePassword_Hex(String password, String passwordHashHex, SaSecurity.PasswordHashMethod method = SaSecurity.PasswordHashMethod.MsSql)
{
ClearErrorState();
if (method == PasswordHashMethod.MsSql)
if (method == SaSecurity.PasswordHashMethod.MsSql)
{
if (Database.DbmsType == DbmsType.MsSql)
{