Foxchat.NET/Foxchat.Core/Extensions/HttpContextExtensions.cs

40 lines
1.4 KiB
C#
Raw Permalink Normal View History

2024-05-21 20:14:52 +02:00
using System.Diagnostics.CodeAnalysis;
2024-05-21 17:45:35 +02:00
using Foxchat.Core.Federation;
using Microsoft.AspNetCore.Http;
namespace Foxchat.Core.Extensions;
public static class HttpContextExtensions
{
2024-05-21 20:14:52 +02:00
public static bool ExtractRequestData(this HttpContext ctx, [NotNullWhen(true)] out string? signature,
[NotNullWhen(true)] out string? domain, [NotNullWhen(true)] out SignatureData? data)
2024-05-21 17:45:35 +02:00
{
2024-05-21 20:14:52 +02:00
signature = null;
domain = null;
data = null;
2024-05-21 17:45:35 +02:00
if (!ctx.Request.Headers.TryGetValue(RequestSigningService.SIGNATURE_HEADER, out var encodedSignature))
return false;
if (!ctx.Request.Headers.TryGetValue(RequestSigningService.DATE_HEADER, out var date))
return false;
if (!ctx.Request.Headers.TryGetValue(RequestSigningService.SERVER_HEADER, out var server))
return false;
var time = RequestSigningService.ParseTime(date.ToString());
string? userId = null;
if (ctx.Request.Headers.TryGetValue(RequestSigningService.USER_HEADER, out var userIdHeader))
userId = userIdHeader;
var host = ctx.Request.Headers.Host.ToString();
signature = encodedSignature.ToString();
domain = server.ToString();
data = new SignatureData(
time,
host,
ctx.Request.Path,
(int?)ctx.Request.Headers.ContentLength,
userId
);
return true;
}
}