mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-05-13 13:22:28 +00:00
Refactor Database into DatabaseContext Moved into separate folder so it actually has a namespace instead sitting in the root
68 lines
No EOL
2.4 KiB
C#
68 lines
No EOL
2.4 KiB
C#
using LBPUnion.ProjectLighthouse.Configuration;
|
|
using LBPUnion.ProjectLighthouse.Database;
|
|
using LBPUnion.ProjectLighthouse.Helpers;
|
|
using LBPUnion.ProjectLighthouse.Localization.StringLists;
|
|
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
|
|
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.TwoFactor;
|
|
|
|
public class DisableTwoFactorPage : BaseLayout
|
|
{
|
|
public DisableTwoFactorPage(DatabaseContext database) : base(database) { }
|
|
|
|
public string Error { get; set; } = "";
|
|
|
|
public IActionResult OnGet()
|
|
{
|
|
if (!ServerConfiguration.Instance.TwoFactorConfiguration.TwoFactorEnabled) return this.Redirect("~/login");
|
|
|
|
User? user = this.Database.UserFromWebRequest(this.Request);
|
|
if (user == null) return this.Redirect("~/login");
|
|
|
|
if (!user.IsTwoFactorSetup) return this.Redirect("~/user/" + user.UserId + "/settings");
|
|
|
|
return this.Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPost([FromForm] string? code, [FromForm] string? backup)
|
|
{
|
|
if (!ServerConfiguration.Instance.TwoFactorConfiguration.TwoFactorEnabled) return this.Redirect("~/login");
|
|
|
|
User? user = this.Database.UserFromWebRequest(this.Request);
|
|
if (user == null) return this.Redirect("~/login");
|
|
|
|
if (!user.IsTwoFactorSetup) return this.Redirect("~/user/" + user.UserId + "/settings");
|
|
|
|
// if both are null or neither are null, there should only be one at at time
|
|
if (string.IsNullOrWhiteSpace(code) == string.IsNullOrWhiteSpace(backup))
|
|
{
|
|
this.Error = this.Translate(TwoFactorStrings.InvalidCode);
|
|
return this.Page();
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(backup))
|
|
{
|
|
if (!CryptoHelper.VerifyCode(code, user.TwoFactorSecret))
|
|
{
|
|
this.Error = this.Translate(TwoFactorStrings.InvalidCode);
|
|
return this.Page();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if(!CryptoHelper.VerifyBackup(backup, user.TwoFactorBackup))
|
|
{
|
|
this.Error = this.Translate(TwoFactorStrings.InvalidBackupCode);
|
|
return this.Page();
|
|
}
|
|
}
|
|
|
|
user.TwoFactorBackup = null;
|
|
user.TwoFactorSecret = null;
|
|
await this.Database.SaveChangesAsync();
|
|
|
|
return this.Redirect("~/user/" + user.UserId + "/settings");
|
|
}
|
|
} |