mirror of
https://github.com/PluralKit/PluralKit.git
synced 2026-02-04 04:56:49 +00:00
refactor(api): move auth middleware to project root
This commit is contained in:
parent
1c9a68cb53
commit
ebcd623bd5
1 changed files with 0 additions and 0 deletions
32
PluralKit.API/AuthorizationTokenHandlerMiddleware.cs
Normal file
32
PluralKit.API/AuthorizationTokenHandlerMiddleware.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue