feat(apiv2): group stubs, authentication middleware, /systems/:id endpoint

This commit is contained in:
spiral
2021-10-01 21:50:01 -04:00
parent 8a88b23021
commit 57722e035b
6 changed files with 227 additions and 6 deletions

View File

@@ -0,0 +1,36 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Dapper;
using PluralKit.Core;
namespace PluralKit.API
{
public class AuthorizationTokenHandlerMiddleware
{
private readonly RequestDelegate _next;
public AuthorizationTokenHandlerMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext ctx, IDatabase db)
{
ctx.Request.Headers.TryGetValue("authorization", out var authHeaders);
if (authHeaders.Count > 0)
{
var systemId = await db.Execute(conn => conn.QuerySingleOrDefaultAsync<SystemId?>(
"select id from systems where token = @token",
new { token = authHeaders[0] }
));
if (systemId != null)
ctx.Items.Add("SystemId", systemId);
}
await _next.Invoke(ctx);
}
}
}