namespace SOR.Website.Pages.Auth;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.RateLimiting;
using SOR.Database.Entities.Identity;
///
/// Handles player login via email + password.
///
/// Security controls:
/// - [EnableRateLimiting]: 10 attempts per IP per 5 minutes.
/// - lockoutOnFailure: true in PasswordSignInAsync — Identity locks the account
/// after 5 failures, defeating unlimited brute-force.
/// - ReturnUrl validated with Url.IsLocalUrl before redirect — no open redirect.
/// - Anti-forgery token validated by Razor Pages on every POST (default behavior).
/// - Generic error message: does NOT confirm whether the email exists (no enumeration).
/// - SignInWithClaimsAsync adds the display_name custom claim so the nav bar can
/// show the player's handle without a DB round-trip per request.
///
[EnableRateLimiting("auth-login")]
public sealed class LoginModel(
SignInManager signInManager,
UserManager userManager) : PageModel
{
[BindProperty]
public InputModel Input { get; set; } = new();
// returnUrl is injected by ASP.NET Core's cookie auth middleware when it
// redirects an unauthenticated request to the login page.
[BindProperty(SupportsGet = true)]
public string? ReturnUrl { get; set; }
public string? ErrorMessage { get; private set; }
public sealed class InputModel
{
[Required(ErrorMessage = "Email is required.")]
[EmailAddress(ErrorMessage = "Enter a valid email address.")]
[MaxLength(254)]
public string Email { get; set; } = string.Empty;
[Required(ErrorMessage = "Password is required.")]
[DataType(DataType.Password)]
[MaxLength(128)]
public string Password { get; set; } = string.Empty;
public bool RememberMe { get; set; }
}
public IActionResult OnGet()
{
// Already signed in — send them where they were going.
if (User.Identity?.IsAuthenticated == true)
return SafeRedirect(ReturnUrl);
return Page();
}
public async Task OnPostAsync()
{
if (!ModelState.IsValid)
return Page();
// PasswordSignInAsync with lockoutOnFailure:true — after 5 failures the
// account is locked for 30 minutes (configured in Program.cs).
var result = await signInManager.PasswordSignInAsync(
Input.Email,
Input.Password,
Input.RememberMe,
lockoutOnFailure: true);
if (result.Succeeded)
{
// Add the display_name claim to the cookie so the layout can show
// the player's handle without a DB hit on every request.
var user = await userManager.FindByEmailAsync(Input.Email);
if (user is not null)
{
var extraClaims = new[]
{
new Claim("display_name", user.DisplayName ?? user.Email ?? string.Empty),
};
await signInManager.SignInWithClaimsAsync(user, Input.RememberMe, extraClaims);
}
return SafeRedirect(ReturnUrl);
}
if (result.IsLockedOut)
{
// Generic message — does not reveal whether the email exists.
ErrorMessage = "Too many failed attempts. Please try again later.";
return Page();
}
// Generic: do not confirm whether the email is registered (prevents enumeration).
ErrorMessage = "Invalid email or password.";
return Page();
}
// Ensures the redirect target is local to this application.
// Url.IsLocalUrl rejects absolute URLs and protocol-relative URLs,
// preventing open redirect attacks where an attacker crafts a link like
// /auth/login?returnUrl=https://evil.com
private IActionResult SafeRedirect(string? url)
=> Url.IsLocalUrl(url) ? LocalRedirect(url) : RedirectToPage("/Index");
}