Migrate AuthController to use proper Controllers

This commit is contained in:
notabird
2026-06-07 11:25:02 -07:00
parent a6165f5054
commit a1097c4070
@@ -1,63 +1,58 @@
using CannedNet;
using CannedNet.Data; using CannedNet.Data;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using CannedNet.Services; using Microsoft.AspNetCore.Mvc;
using CannedNet.Services.Infrastructure;
namespace CannedNet.Services.Controllers; namespace CannedNet.Services.Controllers;
public class AuthController [ApiController]
public class AuthController : ControllerBase
{ {
public WebApplicationBuilder Initialize(string[]? args = null) => ServiceExtensions.CreateRecNetBuilder(args); [HttpGet("eac/challenge")]
public async Task<IResult> EacChallenge() {
string file = await System.IO.File.ReadAllTextAsync("JSON/eacchallenge.txt");
return Results.Content(file, "text/plain");
}
public void MapEndpoints(WebApplication app) [HttpGet("cachedlogin/forplatformid/{platform}/{id}")]
{ public async Task<IResult> CachedLoginForPlatformId(string platform, string id, AppDbContext db) {
var jwtService = app.Services.GetRequiredService<JwtTokenService>(); int platformType = int.Parse(platform);
List<CachedLogin> logins = await db.CachedLogins
.Where(c => c.Platform == (PlatformType)platformType && c.PlatformID == id)
.ToListAsync();
app.MapGet("/eac/challenge", () => return Results.Json(logins.Any() ? logins : new List<object>());
{ }
var file = File.ReadAllText("JSON/eacchallenge.txt");
return Results.Content(file, "text/plain");
});
app.MapGet("/cachedlogin/forplatformid/{platform}/{id}", async (string platform, string id, AppDbContext db) => [HttpPost("connect/token")]
{ public async Task<IResult> ConnectToken(HttpRequest httpRequest, AppDbContext db, JwtTokenService jwtService) {
var platformType = int.Parse(platform);
var logins = await db.CachedLogins
.Where(c => c.Platform == (PlatformType)platformType && c.PlatformID == id)
.ToListAsync();
return Results.Json(logins.Any() ? logins : new List<object>());
});
app.MapPost("/connect/token", async (HttpRequest httpRequest, AppDbContext db) =>
{
string accountId = ""; string accountId = "";
string platformId = ""; string platformId = "";
string platform = ""; string platform = "";
if (httpRequest.ContentLength.HasValue && httpRequest.ContentLength > 0) if (httpRequest.ContentLength is > 0)
{ {
try try
{ {
httpRequest.EnableBuffering(); httpRequest.EnableBuffering();
using var reader = new StreamReader(httpRequest.Body, leaveOpen: true); using StreamReader reader = new (httpRequest.Body, leaveOpen: true);
var body = await reader.ReadToEndAsync(); string body = await reader.ReadToEndAsync();
if (!string.IsNullOrWhiteSpace(body)) if (!string.IsNullOrWhiteSpace(body))
{ {
foreach (var pair in body.Split('&')) foreach (string pair in body.Split('&'))
{ {
var keyValue = pair.Split('='); string[] keyValue = pair.Split('=');
if (keyValue.Length == 2) if (keyValue.Length != 2) continue;
{ string key = Uri.UnescapeDataString(keyValue[0]);
var key = Uri.UnescapeDataString(keyValue[0]); string value = Uri.UnescapeDataString(keyValue[1]);
var value = Uri.UnescapeDataString(keyValue[1]);
if (key == "account_id") switch (key) {
case "account_id":
accountId = value; accountId = value;
else if (key == "platform_id") break;
case "platform_id":
platformId = value; platformId = value;
break;
} }
} }
} }
@@ -66,11 +61,11 @@ public class AuthController
catch { } catch { }
} }
var accessToken = jwtService.GenerateToken(accountId, platformId, platform); string accessToken = jwtService.GenerateToken(accountId, platformId, platform);
if (!string.IsNullOrEmpty(accountId) && int.TryParse(accountId, out var id)) if (!string.IsNullOrEmpty(accountId) && int.TryParse(accountId, out var id))
{ {
var roomInstance = await db.RoomInstances.FirstOrDefaultAsync(r => r.OwnerAccountId == id); RoomInstance? roomInstance = await db.RoomInstances.FirstOrDefaultAsync(r => r.OwnerAccountId == id);
if (roomInstance != null) if (roomInstance != null)
{ {
db.RoomInstances.Remove(roomInstance); db.RoomInstances.Remove(roomInstance);
@@ -87,48 +82,46 @@ public class AuthController
scope = "offline_access profile rn rn.accounts rn.accounts.gc rn.api rn.chat rn.clubs rn.commerce rn.match.read rn.match.write rn.notify rn.rooms rn.storage", scope = "offline_access profile rn rn.accounts rn.accounts.gc rn.api rn.chat rn.clubs rn.commerce rn.match.read rn.match.write rn.notify rn.rooms rn.storage",
key = "8oQ+e+WQaOBPbEcakhqs3dwZZdOmmyDUmJSD9u4AHMY=" key = "8oQ+e+WQaOBPbEcakhqs3dwZZdOmmyDUmJSD9u4AHMY="
}); });
}); }
app.MapGet("/role/developer/{id}", async (HttpRequest request, AppDbContext db, string id) => [HttpPost("api/accounts/v1/forplatformids")]
public async Task<IResult> AccountsForPlatformIds(HttpRequest request, AppDbContext db) {
request.EnableBuffering();
request.Body.Position = 0;
using var reader = new StreamReader(request.Body);
string body = await reader.ReadToEndAsync();
List<string> ids = [];
if (!string.IsNullOrWhiteSpace(body))
{ {
// TODO: implement foreach (string pair in body.Split('&'))
return Results.Ok(RecNetResult.Ok()); {
}); string[] keyValue = pair.Split('=');
if (keyValue.Length != 2 || keyValue[0] != "Ids") continue;
app.MapPost("/api/accounts/v1/forplatformids", async (HttpRequest request, AppDbContext db) => string idString = Uri.UnescapeDataString(keyValue[1]);
ids = idString.Split(',').ToList();
break;
}
}
List<object> results = [];
foreach (string platformId in ids)
{ {
request.EnableBuffering(); CachedLogin? cachedLogin = await db.CachedLogins.FirstOrDefaultAsync(c => c.PlatformID == platformId);
request.Body.Position = 0; if (cachedLogin != null)
using var reader = new StreamReader(request.Body);
var body = await reader.ReadToEndAsync();
var ids = new List<string>();
if (!string.IsNullOrWhiteSpace(body))
{ {
foreach (var pair in body.Split('&')) results.Add(new { accountId = cachedLogin.AccountId, platformId = platformId });
{
var keyValue = pair.Split('=');
if (keyValue.Length == 2 && keyValue[0] == "Ids")
{
var idString = Uri.UnescapeDataString(keyValue[1]);
ids = idString.Split(',').ToList();
break;
}
}
} }
}
var results = new List<object>(); return Results.Json(results);
foreach (var platformId in ids) }
{
var cachedLogin = await db.CachedLogins.FirstOrDefaultAsync(c => c.PlatformID == platformId);
if (cachedLogin != null)
{
results.Add(new { accountId = cachedLogin.AccountId, platformId = platformId });
}
}
return Results.Json(results); [HttpGet("role/developer/{id}")]
}); public async Task<IResult> GetDeveloperRole(string id) {
return Results.Ok(RecNetResult.Ok());
} }
} }