Skip to content

Commit

Permalink
Merge pull request #626 from hargata/Hargata/info.endpoint.update
Browse files Browse the repository at this point in the history
#482 - Allow Root Users to Login via OIDC
  • Loading branch information
hargata authored Sep 22, 2024
2 parents 1e832f0 + 512852d commit 6916161
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 12 deletions.
7 changes: 6 additions & 1 deletion Controllers/APIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public IActionResult VehicleInfo(int vehicleId)
var upgradeRecords = _upgradeRecordDataAccess.GetUpgradeRecordsByVehicleId(vehicle.Id);
var gasRecords = _gasRecordDataAccess.GetGasRecordsByVehicleId(vehicle.Id);
var taxRecords = _taxRecordDataAccess.GetTaxRecordsByVehicleId(vehicle.Id);
var planRecords = _planRecordDataAccess.GetPlanRecordsByVehicleId(vehicle.Id);

var resultToAdd = new VehicleInfo()
{
Expand All @@ -156,7 +157,11 @@ public IActionResult VehicleInfo(int vehicleId)
VeryUrgentReminderCount = results.Count(x => x.Urgency == ReminderUrgency.VeryUrgent),
PastDueReminderCount = results.Count(x => x.Urgency == ReminderUrgency.PastDue),
UrgentReminderCount = results.Count(x => x.Urgency == ReminderUrgency.Urgent),
NotUrgentReminderCount = results.Count(x => x.Urgency == ReminderUrgency.NotUrgent)
NotUrgentReminderCount = results.Count(x => x.Urgency == ReminderUrgency.NotUrgent),
PlanRecordBackLogCount = planRecords.Count(x=>x.Progress == PlanProgress.Backlog),
PlanRecordInProgressCount = planRecords.Count(x=>x.Progress == PlanProgress.InProgress),
PlanRecordTestingCount = planRecords.Count(x=>x.Progress == PlanProgress.Testing),
PlanRecordDoneCount = planRecords.Count(x=>x.Progress == PlanProgress.Done)
};
//set next reminder
if (results.Any(x => (x.Metric == ReminderMetric.Date || x.Metric == ReminderMetric.Both) && x.Date >= DateTime.Now.Date))
Expand Down
12 changes: 12 additions & 0 deletions Helper/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IConfigHelper
UserConfig GetUserConfig(ClaimsPrincipal user);
bool SaveUserConfig(ClaimsPrincipal user, UserConfig configData);
bool AuthenticateRootUser(string username, string password);
bool AuthenticateRootUserOIDC(string email);
string GetWebHookUrl();
string GetMOTD();
string GetLogoUrl();
Expand Down Expand Up @@ -90,6 +91,16 @@ public bool AuthenticateRootUser(string username, string password)
}
return username == rootUsername && password == rootPassword;
}
public bool AuthenticateRootUserOIDC(string email)
{
var rootEmail = _config[nameof(UserConfig.DefaultReminderEmail)] ?? string.Empty;
var rootUserOIDC = bool.Parse(_config[nameof(UserConfig.EnableRootUserOIDC)]);
if (!rootUserOIDC || string.IsNullOrWhiteSpace(rootEmail))
{
return false;
}
return email == rootEmail;
}
public string GetServerLanguage()
{
var serverLanguage = _config[nameof(UserConfig.UserLanguage)] ?? "en_US";
Expand Down Expand Up @@ -171,6 +182,7 @@ public UserConfig GetUserConfig(ClaimsPrincipal user)
UseMPG = bool.Parse(_config[nameof(UserConfig.UseMPG)]),
UseDescending = bool.Parse(_config[nameof(UserConfig.UseDescending)]),
EnableAuth = bool.Parse(_config[nameof(UserConfig.EnableAuth)]),
EnableRootUserOIDC = bool.Parse(_config[nameof(UserConfig.EnableRootUserOIDC)]),
HideZero = bool.Parse(_config[nameof(UserConfig.HideZero)]),
UseUKMPG = bool.Parse(_config[nameof(UserConfig.UseUKMPG)]),
UseMarkDownOnSavedNotes = bool.Parse(_config[nameof(UserConfig.UseMarkDownOnSavedNotes)]),
Expand Down
27 changes: 19 additions & 8 deletions Logic/LoginLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,7 @@ public UserData ValidateUserCredentials(LoginModel credentials)
{
if (UserIsRoot(credentials))
{
return new UserData()
{
Id = -1,
UserName = credentials.UserName,
IsAdmin = true,
IsRootUser = true,
EmailAddress = string.Empty
};
return GetRootUserData(credentials.UserName);
}
else
{
Expand All @@ -271,6 +264,13 @@ public UserData ValidateUserCredentials(LoginModel credentials)
}
public UserData ValidateOpenIDUser(LoginModel credentials)
{
//validate for root user
var isRootUser = _configHelper.AuthenticateRootUserOIDC(credentials.EmailAddress);
if (isRootUser)
{
return GetRootUserData(credentials.EmailAddress);
}

var result = _userData.GetUserRecordByEmailAddress(credentials.EmailAddress);
if (result.Id != default)
{
Expand Down Expand Up @@ -420,6 +420,17 @@ private bool UserIsRoot(LoginModel credentials)
var hashedPassword = GetHash(credentials.Password);
return _configHelper.AuthenticateRootUser(hashedUserName, hashedPassword);
}
private UserData GetRootUserData(string username)
{
return new UserData()
{
Id = -1,
UserName = username,
IsAdmin = true,
IsRootUser = true,
EmailAddress = string.Empty
};
}
#endregion
private static string GetHash(string value)
{
Expand Down
4 changes: 4 additions & 0 deletions Models/API/VehicleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ public class VehicleInfo
public int GasRecordCount { get; set; }
public decimal GasRecordCost { get; set; }
public int LastReportedOdometer { get; set; }
public int PlanRecordBackLogCount { get; set; }
public int PlanRecordInProgressCount { get; set; }
public int PlanRecordTestingCount { get; set; }
public int PlanRecordDoneCount { get; set; }
}
}
1 change: 1 addition & 0 deletions Models/UserConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class UserConfig
public bool UseDescending { get; set; }
public bool EnableAuth { get; set; }
public bool DisableRegistration { get; set; }
public bool EnableRootUserOIDC { get; set; }
public bool HideZero { get; set; }
public bool UseUKMPG {get;set;}
public bool UseThreeDecimalGasCost { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions Views/Home/_Settings.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
<input class="form-check-input" onChange="updateSettings()" type="checkbox" role="switch" id="disableRegistration" checked="@Model.UserConfig.DisableRegistration">
<label class="form-check-label" for="disableRegistration">@translator.Translate(userLanguage, "Disable Registration")</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" onChange="updateSettings()" type="checkbox" role="switch" id="enableRootUserOIDC" checked="@Model.UserConfig.EnableRootUserOIDC">
<label class="form-check-label" for="enableRootUserOIDC">@translator.Translate(userLanguage, "Enable OIDC for Root User")<br /><small class="text-body-secondary">@translator.Translate(userLanguage, "Enable OIDC Login for Root User using the Default Reminder Email")</small></label>
</div>
}
}
</div>
Expand Down
3 changes: 2 additions & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"UseMPG": true,
"UseDescending": false,
"EnableAuth": false,
"DisableRegistration": false,
"DisableRegistration": false,
"EnableRootUserOIDC": false,
"HideZero": false,
"EnableAutoReminderRefresh": false,
"EnableAutoOdometerInsert": false,
Expand Down
Loading

0 comments on commit 6916161

Please sign in to comment.