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
|
|
|
{
|
2025-08-17 02:47:01 -07:00
|
|
|
if (cfg.TrustAuth)
|
|
|
|
|
{
|
|
|
|
|
if (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));
|
|
|
|
|
|
|
|
|
|
if (ctx.Request.Headers.TryGetValue("X-PluralKit-PrivacyLevel", out var levelHeaders)
|
|
|
|
|
&& levelHeaders.Count > 0)
|
|
|
|
|
ctx.Items.Add("LookupContext",
|
|
|
|
|
levelHeaders[0].ToLower().Trim() == "private" ? LookupContext.ByOwner : LookupContext.ByNonOwner);
|
|
|
|
|
}
|
2021-11-26 21:10:56 -05:00
|
|
|
|
2025-04-26 12:03:00 +00:00
|
|
|
if (cfg.TrustAuth
|
|
|
|
|
&& ctx.Request.Headers.TryGetValue("X-PluralKit-AppId", out var aidHeaders)
|
|
|
|
|
&& aidHeaders.Count > 0
|
|
|
|
|
&& int.TryParse(aidHeaders[0], out var appId))
|
|
|
|
|
ctx.Items.Add("AppId", appId);
|
|
|
|
|
|
2021-11-26 21:10:56 -05:00
|
|
|
await _next.Invoke(ctx);
|
2021-10-01 21:50:01 -04:00
|
|
|
}
|
|
|
|
|
}
|