2021-10-01 21:50:01 -04:00
|
|
|
using Dapper;
|
|
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
namespace PluralKit.API;
|
|
|
|
|
|
|
|
|
|
public class AuthorizationTokenHandlerMiddleware
|
2021-10-01 21:50:01 -04:00
|
|
|
{
|
2021-11-26 21:10:56 -05:00
|
|
|
private readonly RequestDelegate _next;
|
|
|
|
|
|
|
|
|
|
public AuthorizationTokenHandlerMiddleware(RequestDelegate next)
|
2021-10-01 21:50:01 -04:00
|
|
|
{
|
2021-11-26 21:10:56 -05:00
|
|
|
_next = next;
|
|
|
|
|
}
|
2021-10-01 21:50:01 -04:00
|
|
|
|
2024-08-04 07:29:57 +09:00
|
|
|
public async Task Invoke(HttpContext ctx, IDatabase db, ApiConfig cfg)
|
2021-11-26 21:10:56 -05:00
|
|
|
{
|
2024-08-04 07:29:57 +09:00
|
|
|
if (cfg.TrustAuth
|
|
|
|
|
&& ctx.Request.Headers.TryGetValue("X-PluralKit-SystemId", out var sidHeaders)
|
|
|
|
|
&& sidHeaders.Count > 0
|
|
|
|
|
&& int.TryParse(sidHeaders[0], out var systemId))
|
|
|
|
|
ctx.Items.Add("SystemId", new SystemId(systemId));
|
2021-11-26 21:10:56 -05:00
|
|
|
|
|
|
|
|
await _next.Invoke(ctx);
|
2021-10-01 21:50:01 -04:00
|
|
|
}
|
|
|
|
|
}
|