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 Microsoft.EntityFrameworkCore;
using CannedNet.Services;
using CannedNet.Services.Infrastructure;
using Microsoft.AspNetCore.Mvc;
namespace CannedNet.Services.Controllers;
public class AuthController
[ApiController]
public class AuthController : ControllerBase
{
public WebApplicationBuilder Initialize(string[]? args = null) => ServiceExtensions.CreateRecNetBuilder(args);
public void MapEndpoints(WebApplication app)
{
var jwtService = app.Services.GetRequiredService<JwtTokenService>();
app.MapGet("/eac/challenge", () =>
{
var file = File.ReadAllText("JSON/eacchallenge.txt");
[HttpGet("eac/challenge")]
public async Task<IResult> EacChallenge() {
string file = await System.IO.File.ReadAllTextAsync("JSON/eacchallenge.txt");
return Results.Content(file, "text/plain");
});
}
app.MapGet("/cachedlogin/forplatformid/{platform}/{id}", async (string platform, string id, AppDbContext db) =>
{
var platformType = int.Parse(platform);
var logins = await db.CachedLogins
[HttpGet("cachedlogin/forplatformid/{platform}/{id}")]
public async Task<IResult> CachedLoginForPlatformId(string platform, string id, AppDbContext db) {
int platformType = int.Parse(platform);
List<CachedLogin> 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) =>
{
[HttpPost("connect/token")]
public async Task<IResult> ConnectToken(HttpRequest httpRequest, AppDbContext db, JwtTokenService jwtService) {
string accountId = "";
string platformId = "";
string platform = "";
if (httpRequest.ContentLength.HasValue && httpRequest.ContentLength > 0)
if (httpRequest.ContentLength is > 0)
{
try
{
httpRequest.EnableBuffering();
using var reader = new StreamReader(httpRequest.Body, leaveOpen: true);
var body = await reader.ReadToEndAsync();
using StreamReader reader = new (httpRequest.Body, leaveOpen: true);
string body = await reader.ReadToEndAsync();
if (!string.IsNullOrWhiteSpace(body))
{
foreach (var pair in body.Split('&'))
foreach (string pair in body.Split('&'))
{
var keyValue = pair.Split('=');
if (keyValue.Length == 2)
{
var key = Uri.UnescapeDataString(keyValue[0]);
var value = Uri.UnescapeDataString(keyValue[1]);
string[] keyValue = pair.Split('=');
if (keyValue.Length != 2) continue;
string key = Uri.UnescapeDataString(keyValue[0]);
string value = Uri.UnescapeDataString(keyValue[1]);
if (key == "account_id")
switch (key) {
case "account_id":
accountId = value;
else if (key == "platform_id")
break;
case "platform_id":
platformId = value;
break;
}
}
}
@@ -66,11 +61,11 @@ public class AuthController
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))
{
var roomInstance = await db.RoomInstances.FirstOrDefaultAsync(r => r.OwnerAccountId == id);
RoomInstance? roomInstance = await db.RoomInstances.FirstOrDefaultAsync(r => r.OwnerAccountId == id);
if (roomInstance != null)
{
db.RoomInstances.Remove(roomInstance);
@@ -87,41 +82,35 @@ 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",
key = "8oQ+e+WQaOBPbEcakhqs3dwZZdOmmyDUmJSD9u4AHMY="
});
});
}
app.MapGet("/role/developer/{id}", async (HttpRequest request, AppDbContext db, string id) =>
{
// TODO: implement
return Results.Ok(RecNetResult.Ok());
});
[HttpPost("api/accounts/v1/forplatformids")]
public async Task<IResult> AccountsForPlatformIds(HttpRequest request, AppDbContext db) {
app.MapPost("/api/accounts/v1/forplatformids", async (HttpRequest request, AppDbContext db) =>
{
request.EnableBuffering();
request.Body.Position = 0;
using var reader = new StreamReader(request.Body);
var body = await reader.ReadToEndAsync();
string body = await reader.ReadToEndAsync();
var ids = new List<string>();
List<string> ids = [];
if (!string.IsNullOrWhiteSpace(body))
{
foreach (var pair in body.Split('&'))
foreach (string pair in body.Split('&'))
{
var keyValue = pair.Split('=');
if (keyValue.Length == 2 && keyValue[0] == "Ids")
{
var idString = Uri.UnescapeDataString(keyValue[1]);
string[] keyValue = pair.Split('=');
if (keyValue.Length != 2 || keyValue[0] != "Ids") continue;
string idString = Uri.UnescapeDataString(keyValue[1]);
ids = idString.Split(',').ToList();
break;
}
}
}
var results = new List<object>();
foreach (var platformId in ids)
List<object> results = [];
foreach (string platformId in ids)
{
var cachedLogin = await db.CachedLogins.FirstOrDefaultAsync(c => c.PlatformID == platformId);
CachedLogin? cachedLogin = await db.CachedLogins.FirstOrDefaultAsync(c => c.PlatformID == platformId);
if (cachedLogin != null)
{
results.Add(new { accountId = cachedLogin.AccountId, platformId = platformId });
@@ -129,6 +118,10 @@ public class AuthController
}
return Results.Json(results);
});
}
[HttpGet("role/developer/{id}")]
public async Task<IResult> GetDeveloperRole(string id) {
return Results.Ok(RecNetResult.Ok());
}
}