using Microsoft.AspNetCore.Mvc;
using DorkNet.Models.Auth;
using DorkNet.Server.Data.Entities;
using DorkNet.Server.Services;
namespace DorkNet.Server.Controllers.Auth;
///
/// auth.rec.net game-specific auth helpers. OAuth/OIDC endpoints are owned by Duende IdentityServer.
///
[ApiController]
public class AuthController(
PlayerService playerService,
AuthService authService) : ControllerBase
{
// 2020.12 added an EAC challenge step in the login flow. The client GETs
// a challenge string and passes it to EACManager.GenerateChallengeResponse,
// which calls System.Convert.FromBase64CharPtr on the string. So the
// returned challenge MUST be valid base64 — sending arbitrary text throws
// FormatException client-side and kicks the login.
[HttpGet("/eac/challenge")]
[HttpPost("/eac/challenge")]
public IActionResult EacChallenge()
=> Content("\"AAAAAAAAAAAAAAAAAAAAAAAA\"", "application/json");
// Cached-logins lookup — boot-sequence call from ScreenAccountSelectionManager.
// URL: GET auth.rec.net/cachedlogin/forplatformid/{platform}/{platformId}
// Returns List. Empty array = no remembered accounts.
[HttpGet("/cachedlogin/forplatformid/{platform:int}/{platformId}")]
public async Task CachedLoginsForPlatform(int platform, string platformId)
{
var players = await playerService.GetCachedLoginsAsync(platform, platformId);
return Ok(players.Select(p => ToCachedLogin(p, platform, platformId)).ToList());
}
/// POST cachedlogin/forplatformids — bulk variant
/// the watch uses when hydrating multiple platform-id mappings
/// at once. Body is form-urlencoded platform=&platformIds=a,b,c
/// or JSON with the same shape.
[HttpPost("/cachedlogin/forplatformids")]
[HttpGet("/cachedlogin/forplatformids")]
public async Task CachedLoginsForPlatformIds()
{
int platform = 0;
string? rawIds = null;
try
{
if (Request.HasFormContentType)
{
int.TryParse(Request.Form["platform"].ToString(), out platform);
rawIds = Request.Form["platformIds"].ToString();
if (string.IsNullOrWhiteSpace(rawIds))
rawIds = Request.Form["PlatformIds"].ToString();
}
}
catch { /* not form */ }
if (string.IsNullOrWhiteSpace(rawIds))
{
int.TryParse(Request.Query["platform"], out platform);
rawIds = Request.Query["platformIds"].ToString();
if (string.IsNullOrWhiteSpace(rawIds))
rawIds = Request.Query["PlatformIds"].ToString();
}
if (string.IsNullOrWhiteSpace(rawIds)) return Ok(Array.Empty