diff --git a/Helper/ConfigHelper.cs b/Helper/ConfigHelper.cs index 437684f..61fc417 100644 --- a/Helper/ConfigHelper.cs +++ b/Helper/ConfigHelper.cs @@ -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,12 @@ namespace CarCareTracker.Helper } return logoUrl; } + public bool AuthenticateRootUser(string username, string password) + { + var rootUsername = _config["UserNameHash"]; + var rootPassword = _config["UserPasswordHash"]; + return username == rootUsername && password == rootPassword; + } public string GetServerLanguage() { var serverLanguage = _config[nameof(UserConfig.UserLanguage)] ?? "en_US"; diff --git a/Logic/LoginLogic.cs b/Logic/LoginLogic.cs index 01ea532..d3d6f23 100644 --- a/Logic/LoginLogic.cs +++ b/Logic/LoginLogic.cs @@ -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(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)