38 lines
1.3 KiB
C#
38 lines
1.3 KiB
C#
|
using Foxchat.Core.Federation;
|
||
|
using Microsoft.AspNetCore.Http;
|
||
|
|
||
|
namespace Foxchat.Core.Extensions;
|
||
|
|
||
|
public static class HttpContextExtensions
|
||
|
{
|
||
|
public static bool ExtractRequestData(this HttpContext ctx, out string signature, out string domain, out SignatureData data)
|
||
|
{
|
||
|
signature = string.Empty;
|
||
|
domain = string.Empty;
|
||
|
data = SignatureData.Empty;
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|