diff --git a/SS14.Web/Areas/Identity/Pages/Account/Manage/GenerateRecoveryCodes.cshtml b/SS14.Web/Areas/Identity/Pages/Account/Manage/GenerateRecoveryCodes.cshtml index 284ab59..2c07cac 100644 --- a/SS14.Web/Areas/Identity/Pages/Account/Manage/GenerateRecoveryCodes.cshtml +++ b/SS14.Web/Areas/Identity/Pages/Account/Manage/GenerateRecoveryCodes.cshtml @@ -7,13 +7,14 @@

@ViewData["Title"]

- diff --git a/SS14.Web/Areas/Identity/Pages/Account/Manage/GenerateRecoveryCodes.cshtml.cs b/SS14.Web/Areas/Identity/Pages/Account/Manage/GenerateRecoveryCodes.cshtml.cs index b6a858e..a613cfa 100644 --- a/SS14.Web/Areas/Identity/Pages/Account/Manage/GenerateRecoveryCodes.cshtml.cs +++ b/SS14.Web/Areas/Identity/Pages/Account/Manage/GenerateRecoveryCodes.cshtml.cs @@ -71,14 +71,14 @@ public async Task OnPostAsync() await _accountLogManager.LogAndSave(user, new AccountLogRecoveryCodesGenerated()); await _userManager.UpdateAsync(user); - + var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10); RecoveryCodes = recoveryCodes.ToArray(); await tx.CommitAsync(); - + _logger.LogInformation("User with ID '{UserId}' has generated new 2FA recovery codes.", userId); StatusMessage = "You have generated new recovery codes."; return RedirectToPage("./ShowRecoveryCodes"); } -} \ No newline at end of file +} diff --git a/SS14.Web/Areas/Identity/Pages/Account/Manage/ShowRecoveryCodes.cshtml b/SS14.Web/Areas/Identity/Pages/Account/Manage/ShowRecoveryCodes.cshtml index 23fa27b..446b1c2 100644 --- a/SS14.Web/Areas/Identity/Pages/Account/Manage/ShowRecoveryCodes.cshtml +++ b/SS14.Web/Areas/Identity/Pages/Account/Manage/ShowRecoveryCodes.cshtml @@ -7,13 +7,19 @@

@ViewData["Title"]

- +
+
+
+ +
+
diff --git a/SS14.Web/Areas/Identity/Pages/Account/Manage/ShowRecoveryCodes.cshtml.cs b/SS14.Web/Areas/Identity/Pages/Account/Manage/ShowRecoveryCodes.cshtml.cs index e1ff308..1c91632 100644 --- a/SS14.Web/Areas/Identity/Pages/Account/Manage/ShowRecoveryCodes.cshtml.cs +++ b/SS14.Web/Areas/Identity/Pages/Account/Manage/ShowRecoveryCodes.cshtml.cs @@ -1,16 +1,27 @@ using System; -using System.Collections.Generic; using System.Linq; +using System.Net.Mime; +using System.Text; using System.Threading.Tasks; -using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; -using Microsoft.Extensions.Logging; +using SS14.Auth.Shared.Data; namespace SS14.Web.Areas.Identity.Pages.Account.Manage; public class ShowRecoveryCodesModel : PageModel { + private readonly SpaceUserManager _userManager; + private readonly ApplicationDbContext _dbContext; + + public ShowRecoveryCodesModel( + SpaceUserManager userManager, + ApplicationDbContext dbContext) + { + _userManager = userManager; + _dbContext = dbContext; + } + [TempData] public string[] RecoveryCodes { get; set; } @@ -26,4 +37,27 @@ public IActionResult OnGet() return Page(); } -} \ No newline at end of file + + public async Task OnPostDownloadRecoveryCodes() + { + var user = await _userManager.GetUserAsync(User); + if (user == null) + { + return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); + } + + var rawValue = _dbContext.UserTokens + .Where(x => x.UserId == user.Id && x.Name == "RecoveryCodes") + .Select(q => q.Value) + .FirstOrDefault(); + + var recoveryCodes = rawValue?.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) + ?? []; + + var header = $"These are the 2fa recovery codes for the Space Station 14 account {user.UserName}. Keep them in a safe place.\n\n"; + var text = header + string.Join("\n", recoveryCodes); + + Response.Headers.Add("Content-Disposition", $"attachment; filename=SS14-{user.UserName}-Recovery.txt"); + return new FileContentResult(Encoding.UTF8.GetBytes(text), MediaTypeNames.Text.Plain); + } +}