mirror of
https://github.com/CannedNet/CannedNet.git
synced 2026-10-27 16:09:03 -07:00
Migrate AuthController to use proper Controllers
This commit is contained in:
@@ -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() {
|
||||||
public void MapEndpoints(WebApplication app)
|
string file = await System.IO.File.ReadAllTextAsync("JSON/eacchallenge.txt");
|
||||||
{
|
|
||||||
var jwtService = app.Services.GetRequiredService<JwtTokenService>();
|
|
||||||
|
|
||||||
app.MapGet("/eac/challenge", () =>
|
|
||||||
{
|
|
||||||
var file = File.ReadAllText("JSON/eacchallenge.txt");
|
|
||||||
return Results.Content(file, "text/plain");
|
return Results.Content(file, "text/plain");
|
||||||
});
|
}
|
||||||
|
|
||||||
app.MapGet("/cachedlogin/forplatformid/{platform}/{id}", async (string platform, string id, AppDbContext db) =>
|
[HttpGet("cachedlogin/forplatformid/{platform}/{id}")]
|
||||||
{
|
public async Task<IResult> CachedLoginForPlatformId(string platform, string id, AppDbContext db) {
|
||||||
var platformType = int.Parse(platform);
|
int platformType = int.Parse(platform);
|
||||||
var logins = await db.CachedLogins
|
List<CachedLogin> logins = await db.CachedLogins
|
||||||
.Where(c => c.Platform == (PlatformType)platformType && c.PlatformID == id)
|
.Where(c => c.Platform == (PlatformType)platformType && c.PlatformID == id)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
return Results.Json(logins.Any() ? logins : new List<object>());
|
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 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,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",
|
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) {
|
||||||
// TODO: implement
|
|
||||||
return Results.Ok(RecNetResult.Ok());
|
|
||||||
});
|
|
||||||
|
|
||||||
app.MapPost("/api/accounts/v1/forplatformids", async (HttpRequest request, AppDbContext db) =>
|
|
||||||
{
|
|
||||||
request.EnableBuffering();
|
request.EnableBuffering();
|
||||||
request.Body.Position = 0;
|
request.Body.Position = 0;
|
||||||
using var reader = new StreamReader(request.Body);
|
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))
|
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 && keyValue[0] == "Ids")
|
if (keyValue.Length != 2 || keyValue[0] != "Ids") continue;
|
||||||
{
|
|
||||||
var idString = Uri.UnescapeDataString(keyValue[1]);
|
string idString = Uri.UnescapeDataString(keyValue[1]);
|
||||||
ids = idString.Split(',').ToList();
|
ids = idString.Split(',').ToList();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var results = new List<object>();
|
List<object> results = [];
|
||||||
foreach (var platformId in ids)
|
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)
|
if (cachedLogin != null)
|
||||||
{
|
{
|
||||||
results.Add(new { accountId = cachedLogin.AccountId, platformId = platformId });
|
results.Add(new { accountId = cachedLogin.AccountId, platformId = platformId });
|
||||||
@@ -129,6 +118,10 @@ public class AuthController
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Results.Json(results);
|
return Results.Json(results);
|
||||||
});
|
}
|
||||||
|
|
||||||
|
[HttpGet("role/developer/{id}")]
|
||||||
|
public async Task<IResult> GetDeveloperRole(string id) {
|
||||||
|
return Results.Ok(RecNetResult.Ok());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user