Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions SS14.Auth.Shared/ModelShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,74 @@ await emailSender.SendEmailAsync(
$"\n<p><small>If the above link is not working, try this one {HtmlEncoder.Default.Encode(callbackUrl)}</small></p");
}

public static async Task Send2FaDisabledEmail(IEmailSender emailSender, string email)
{
await emailSender.SendEmailAsync(email,
"Your Space Station 14 account 2fa was disabled",
$"This email was sent to you to confirm that 2fa has been disabled on your account. If this was you feel free to ignore this email." +
$"\n\nIf this was not you, send an email to support@spacestation14.com immediately.");
}

public static async Task Send2FaEnabledEmail(IEmailSender emailSender, string email)
{
await emailSender.SendEmailAsync(email,
"Your Space Station 14 account 2fa was enabled",
$"This email was sent to you to confirm that 2fa has been enabled on your account. If this was you feel free to ignore this email." +
$"(And make sure you wrote down your recovery codes)" +
$"\n\nIf this was not you, send an email to support@spacestation14.com immediately.");
}

public static async Task Send2FaCodesRegeneratedEmail(IEmailSender emailSender, string email)
{
await emailSender.SendEmailAsync(email,
"Your Space Station 14 account 2fa recovery codes were regenerated",
$"This email was sent to you to confirm that 2fa recovery codes have been regenerated on your account. If this was you feel free to ignore this email." +
$"\n\nIf this was not you, send an email to support@spacestation14.com immediately.");
}

public static async Task Send2FaResetEmail(IEmailSender emailSender, string email)
{
await emailSender.SendEmailAsync(email,
"Your Space Station 14 account 2fa was reset",
$"This email was sent to you to confirm that 2fa has been reset on your account. If this was you feel free to ignore this email." +
$"\n\nIf this was not you, send an email to support@spacestation14.com immediately.");
}

public static async Task SendPersonalDataEmail(IEmailSender emailSender, string email)
{
await emailSender.SendEmailAsync(email,
"Your Space Station 14 account data was requested",
$"This email was sent to you to confirm your account data was requested. If this was you feel free to ignore this email." +
$"\n\nIf this was not you, send an email to support@spacestation14.com immediately.");
}

public static async Task SendUsernameChangeEmail(IEmailSender emailSender, string email, string oldName, string newname)
{
await emailSender.SendEmailAsync(email,
"Your Space Station 14 account username was changed",
$"This email was sent to you to confirm your username change, you were known as {oldName} but from now on will be known as {newname}. " +
$"If this was you feel free to ignore this email." +
$"\n\nIf this was not you, send an email to support@spacestation14.com immediately.");
}

public static async Task SendPasswordChangedEmail(IEmailSender emailSender, string email)
{
await emailSender.SendEmailAsync(email,
"Your Space Station 14 account password was changed",
$"This email was sent to you to confirm your password has successfully been changed. If this was you feel free to ignore this email." +
$"\n\nIf this was not you, send an email to support@spacestation14.com immediately.");
}

public static async Task SendEmailChangedEmail(IEmailSender emailSender, string email, string oldEmail)
{
await emailSender.SendEmailAsync(
oldEmail,
"Your Space Station 14 account email was changed",
$"This email was sent to the old email address for security, if this was you feel free to ignore this email." +
$"\n\nFurther emails from this point forward will go to {email}." +
$"\n\nIf this was not you, send an email to support@spacestation14.com immediately.");
}

public static SpaceUser CreateNewUser(string userName, string email, ISystemClock systemClock)
{
return new SpaceUser {UserName = userName, Email = email, CreatedTime = systemClock.UtcNow};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;
using SS14.Auth.Shared;
using SS14.Auth.Shared.Data;
using SS14.Auth.Shared.Emails;

namespace SS14.Web.Areas.Identity.Pages.Account;

Expand All @@ -17,17 +20,20 @@ public class ConfirmEmailChangeModel : PageModel
{
private readonly SpaceUserManager _userManager;
private readonly SignInManager<SpaceUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ApplicationDbContext _dbContext;
private readonly AccountLogManager _accountLogManager;

public ConfirmEmailChangeModel(
SpaceUserManager userManager,
SignInManager<SpaceUser> signInManager,
IEmailSender emailSender,
ApplicationDbContext dbContext,
AccountLogManager accountLogManager)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_dbContext = dbContext;
_accountLogManager = accountLogManager;
}
Expand Down Expand Up @@ -68,6 +74,9 @@ await _accountLogManager.LogAndSave(

await _signInManager.RefreshSignInAsync(user);
StatusMessage = "Thank you for confirming your email change.";

await ModelShared.SendEmailChangedEmail(_emailSender, email, oldEmail);

return Page();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using SS14.Auth.Shared;
using SS14.Auth.Shared.Data;
using SS14.Auth.Shared.Emails;
using SS14.Auth.Shared.Sessions;

namespace SS14.Web.Areas.Identity.Pages.Account.Manage;
Expand All @@ -18,17 +20,20 @@ public class ChangePasswordModel : PageModel
private readonly SessionManager _sessionManager;
private readonly AccountLogManager _logManager;
private readonly SignInManager<SpaceUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ILogger<ChangePasswordModel> _logger;

public ChangePasswordModel(
UserManager<SpaceUser> userManager,
SignInManager<SpaceUser> signInManager,
IEmailSender emailSender,
ILogger<ChangePasswordModel> logger,
SessionManager sessionManager,
AccountLogManager logManager)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
_sessionManager = sessionManager;
_logManager = logManager;
Expand Down Expand Up @@ -105,6 +110,9 @@ public async Task<IActionResult> OnPostAsync()
_logger.LogInformation("User changed their password successfully.");
StatusMessage = "Your password has been changed.";

var userEmail = await _userManager.GetEmailAsync(user);
await ModelShared.SendPasswordChangedEmail(_emailSender, userEmail);

return RedirectToPage();
}
}
15 changes: 12 additions & 3 deletions SS14.Web/Areas/Identity/Pages/Account/Manage/Disable2fa.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,32 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using SS14.Auth.Shared;
using SS14.Auth.Shared.Data;
using SS14.Auth.Shared.Emails;

namespace SS14.Web.Areas.Identity.Pages.Account.Manage;

public class Disable2faModel : PageModel
{
private readonly SpaceUserManager _userManager;
private readonly SignInManager<SpaceUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ILogger<Disable2faModel> _logger;
private readonly ApplicationDbContext _dbContext;
private readonly AccountLogManager _accountLogManager;

public Disable2faModel(
SpaceUserManager userManager,
SignInManager<SpaceUser> signInManager,
IEmailSender emailSender,
ILogger<Disable2faModel> logger,
ApplicationDbContext dbContext,
AccountLogManager accountLogManager)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
_dbContext = dbContext;
_accountLogManager = accountLogManager;
Expand Down Expand Up @@ -70,11 +75,15 @@ public async Task<IActionResult> OnPostAsync()
}

await tx.CommitAsync();

await _signInManager.RefreshSignInAsync(user);

_logger.LogInformation("User with ID '{UserId}' has disabled 2FA.", _userManager.GetUserId(User));
StatusMessage = "2FA has been disabled. You can re-enable 2FA when you setup an authenticator app";

var userEmail = await _userManager.GetEmailAsync(user);
await ModelShared.Send2FaDisabledEmail(_emailSender, userEmail);

return RedirectToPage("./TwoFactorAuthentication");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using SS14.Auth.Shared;
using SS14.Auth.Shared.Data;
using SS14.Auth.Shared.Emails;

namespace SS14.Web.Areas.Identity.Pages.Account.Manage;

Expand All @@ -20,6 +22,7 @@ public class EnableAuthenticatorModel : PageModel
private readonly ILogger<EnableAuthenticatorModel> _logger;
private readonly UrlEncoder _urlEncoder;
private readonly SignInManager<SpaceUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ApplicationDbContext _dbContext;
private readonly AccountLogManager _accountLogManager;

Expand All @@ -28,6 +31,7 @@ public class EnableAuthenticatorModel : PageModel
public EnableAuthenticatorModel(
SpaceUserManager userManager,
ILogger<EnableAuthenticatorModel> logger,
IEmailSender emailSender,
UrlEncoder urlEncoder,
SignInManager<SpaceUser> signInManager,
ApplicationDbContext dbContext,
Expand All @@ -37,6 +41,7 @@ public EnableAuthenticatorModel(
_logger = logger;
_urlEncoder = urlEncoder;
_signInManager = signInManager;
_emailSender = emailSender;
_dbContext = dbContext;
_accountLogManager = accountLogManager;
}
Expand Down Expand Up @@ -91,7 +96,7 @@ public async Task<IActionResult> OnPostAsync()
}

await using var tx = await _dbContext.Database.BeginTransactionAsync();

// Strip spaces and hypens
var verificationCode = Input.Code.Replace(" ", string.Empty).Replace("-", string.Empty);

Expand All @@ -110,15 +115,18 @@ public async Task<IActionResult> OnPostAsync()
await _userManager.SetTwoFactorEnabledAsync(user, true);
var userId = await _userManager.GetUserIdAsync(user);
_logger.LogInformation("User with ID '{UserId}' has enabled 2FA with an authenticator app.", userId);

StatusMessage = "Your authenticator app has been verified.";
await _signInManager.RefreshSignInAsync(user);

var userEmail = await _userManager.GetEmailAsync(user);
await ModelShared.Send2FaEnabledEmail(_emailSender, userEmail);

if (await _userManager.CountRecoveryCodesAsync(user) == 0)
{
var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);
RecoveryCodes = recoveryCodes.ToArray();

await tx.CommitAsync();
return RedirectToPage("./ShowRecoveryCodes");
}
Expand All @@ -137,7 +145,7 @@ private async Task LoadSharedKeyAndQrCodeUriAsync(SpaceUser user)
{
await _userManager.ResetAuthenticatorKeyAsync(user);
unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

await _signInManager.RefreshSignInAsync(user);
}

Expand Down Expand Up @@ -171,4 +179,4 @@ private string GenerateQrCodeUri(string userName, string unformattedKey)
_urlEncoder.Encode(userName),
unformattedKey);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using SS14.Auth.Shared;
using SS14.Auth.Shared.Data;
using SS14.Auth.Shared.Emails;

namespace SS14.Web.Areas.Identity.Pages.Account.Manage;

public class GenerateRecoveryCodesModel : PageModel
{
private readonly SpaceUserManager _userManager;
private readonly IEmailSender _emailSender;
private readonly ApplicationDbContext _dbContext;
private readonly ILogger<GenerateRecoveryCodesModel> _logger;
private readonly AccountLogManager _accountLogManager;

public GenerateRecoveryCodesModel(
SpaceUserManager userManager,
IEmailSender emailSender,
ApplicationDbContext dbContext,
ILogger<GenerateRecoveryCodesModel> logger,
AccountLogManager accountLogManager)
{
_userManager = userManager;
_emailSender = emailSender;
_dbContext = dbContext;
_logger = logger;
_accountLogManager = accountLogManager;
Expand Down Expand Up @@ -79,6 +84,10 @@ public async Task<IActionResult> OnPostAsync()

_logger.LogInformation("User with ID '{UserId}' has generated new 2FA recovery codes.", userId);
StatusMessage = "You have generated new recovery codes.";

var userEmail = await _userManager.GetEmailAsync(user);
await ModelShared.Send2FaCodesRegeneratedEmail(_emailSender, userEmail);

return RedirectToPage("./ShowRecoveryCodes");
}
}
8 changes: 8 additions & 0 deletions SS14.Web/Areas/Identity/Pages/Account/Manage/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Options;
using SS14.Auth.Shared;
using SS14.Auth.Shared.Data;
using SS14.Auth.Shared.Emails;

namespace SS14.Web.Areas.Identity.Pages.Account.Manage;

public partial class IndexModel : PageModel
{
private readonly SpaceUserManager _userManager;
private readonly SignInManager<SpaceUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly IOptions<AccountOptions> _options;
private readonly ApplicationDbContext _dbContext;
private readonly AccountLogManager _accountLogManager;
Expand All @@ -24,12 +27,14 @@ public partial class IndexModel : PageModel
public IndexModel(
SpaceUserManager userManager,
SignInManager<SpaceUser> signInManager,
IEmailSender emailSender,
IOptions<AccountOptions> options,
ApplicationDbContext dbContext,
AccountLogManager accountLogManager)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_options = options;
_dbContext = dbContext;
_accountLogManager = accountLogManager;
Expand Down Expand Up @@ -131,6 +136,9 @@ public async Task<IActionResult> OnPostUsernameAsync()
await _dbContext.SaveChangesAsync();
await tx.CommitAsync();

var userEmail = await _userManager.GetEmailAsync(user);
await ModelShared.SendUsernameChangeEmail(_emailSender, userEmail, oldName, user.UserName);

return RedirectToPage();
}
}
Loading
Loading