Merge pull request #360 from hargata/Hargata/persist.columns

authenticate root user via configHelper
This commit is contained in:
Hargata Softworks
2024-03-02 07:13:43 -07:00
committed by GitHub
2 changed files with 17 additions and 15 deletions

View File

@@ -10,6 +10,7 @@ namespace CarCareTracker.Helper
OpenIDConfig GetOpenIDConfig();
UserConfig GetUserConfig(ClaimsPrincipal user);
bool SaveUserConfig(ClaimsPrincipal user, UserConfig configData);
bool AuthenticateRootUser(string username, string password);
string GetLogoUrl();
string GetServerLanguage();
bool GetServerEnableShopSupplies();
@@ -43,6 +44,16 @@ namespace CarCareTracker.Helper
}
return logoUrl;
}
public bool AuthenticateRootUser(string username, string password)
{
var rootUsername = _config["UserNameHash"];
var rootPassword = _config["UserPasswordHash"];
if (string.IsNullOrWhiteSpace(rootUsername) || string.IsNullOrWhiteSpace(rootPassword))
{
return false;
}
return username == rootUsername && password == rootPassword;
}
public string GetServerLanguage()
{
var serverLanguage = _config[nameof(UserConfig.UserLanguage)] ?? "en_US";

View File

@@ -35,15 +35,18 @@ namespace CarCareTracker.Logic
private readonly IUserRecordDataAccess _userData;
private readonly ITokenRecordDataAccess _tokenData;
private readonly IMailHelper _mailHelper;
private readonly IConfigHelper _configHelper;
private IMemoryCache _cache;
public LoginLogic(IUserRecordDataAccess userData,
ITokenRecordDataAccess tokenData,
IMailHelper mailHelper,
IConfigHelper configHelper,
IMemoryCache memoryCache)
{
_userData = userData;
_tokenData = tokenData;
_mailHelper = mailHelper;
_configHelper = configHelper;
_cache = memoryCache;
}
public bool CheckIfUserIsValid(int userId)
@@ -412,21 +415,9 @@ namespace CarCareTracker.Logic
}
private bool UserIsRoot(LoginModel credentials)
{
var configFileContents = File.ReadAllText(StaticHelper.UserConfigPath);
var existingUserConfig = JsonSerializer.Deserialize<UserConfig>(configFileContents);
if (existingUserConfig is not null)
{
//create hashes of the login credentials.
var hashedUserName = GetHash(credentials.UserName);
var hashedPassword = GetHash(credentials.Password);
//compare against stored hash.
if (hashedUserName == existingUserConfig.UserNameHash &&
hashedPassword == existingUserConfig.UserPasswordHash)
{
return true;
}
}
return false;
var hashedUserName = GetHash(credentials.UserName);
var hashedPassword = GetHash(credentials.Password);
return _configHelper.AuthenticateRootUser(hashedUserName, hashedPassword);
}
#endregion
private static string GetHash(string value)