chat: add database types and auth middleware

This commit is contained in:
sam 2024-05-21 16:41:01 +02:00
parent 656eec81d8
commit 6f6e19bbb5
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
24 changed files with 1165 additions and 15 deletions

View file

@ -31,23 +31,22 @@ public partial class RequestSigningService(ILogger logger, IClock clock, IDataba
}
public bool VerifySignature(
string publicKey, string encodedSignature, string dateHeader, string host, string requestPath, int? contentLength, string? userId)
string publicKey, string encodedSignature, SignatureData data)
{
var rsa = RSA.Create();
rsa.ImportFromPem(publicKey);
var now = _clock.GetCurrentInstant();
var time = ParseTime(dateHeader);
if ((now + Duration.FromMinutes(1)) < time)
if ((now + Duration.FromMinutes(1)) < data.Time)
{
throw new ApiError.IncomingFederationError("Request was made in the future");
}
else if ((now - Duration.FromMinutes(1)) > time)
else if ((now - Duration.FromMinutes(1)) > data.Time)
{
throw new ApiError.IncomingFederationError("Request was made too long ago");
}
var plaintext = GeneratePlaintext(new SignatureData(time, host, requestPath, contentLength, userId));
var plaintext = GeneratePlaintext(data);
var plaintextBytes = Encoding.UTF8.GetBytes(plaintext);
var hash = SHA256.HashData(plaintextBytes);
@ -73,5 +72,5 @@ public partial class RequestSigningService(ILogger logger, IClock clock, IDataba
private static readonly InstantPattern _pattern = InstantPattern.Create("ddd, dd MMM yyyy HH:mm:ss 'GMT'", CultureInfo.GetCultureInfo("en-US"));
private static string FormatTime(Instant time) => _pattern.Format(time);
private static Instant ParseTime(string header) => _pattern.Parse(header).GetValueOrThrow();
public static Instant ParseTime(string header) => _pattern.Parse(header).GetValueOrThrow();
}