using System.Diagnostics.CodeAnalysis; using Foxchat.Core.Federation; using Microsoft.AspNetCore.Http; namespace Foxchat.Core.Extensions; public static class HttpContextExtensions { public static bool ExtractRequestData(this HttpContext ctx, [NotNullWhen(true)] out string? signature, [NotNullWhen(true)] out string? domain, [NotNullWhen(true)] out SignatureData? data) { signature = null; domain = null; data = null; 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; } }