add basic suppport for client_credentials oauth grant

This commit is contained in:
sam 2024-05-20 17:00:21 +02:00
parent 049f4a56de
commit 8995213d26
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
20 changed files with 627 additions and 58 deletions

View file

@ -30,12 +30,12 @@ public partial class RequestSigningService
if (!resp.IsSuccessStatusCode)
{
var error = await resp.Content.ReadAsStringAsync();
throw new FoxchatError.OutgoingFederationError($"Request to {domain}{requestPath} returned an error", DeserializeObject<Models.ApiError>(error));
throw new ApiError.OutgoingFederationError($"Request to {domain}{requestPath} returned an error", DeserializeObject<Models.ApiError>(error));
}
var bodyString = await resp.Content.ReadAsStringAsync();
return DeserializeObject<T>(bodyString)
?? throw new FoxchatError.OutgoingFederationError($"Request to {domain}{requestPath} returned invalid response body");
?? throw new ApiError.OutgoingFederationError($"Request to {domain}{requestPath} returned invalid response body");
}
private HttpRequestMessage BuildHttpRequest(HttpMethod method, string domain, string requestPath, string? userId = null, object? bodyData = null)

View file

@ -5,16 +5,15 @@ using Foxchat.Core.Database;
using Foxchat.Core.Utils;
using NodaTime;
using NodaTime.Text;
using Serilog;
namespace Foxchat.Core.Federation;
public partial class RequestSigningService(ILogger logger, IClock clock, IDatabaseContext context, CoreConfig config)
public partial class RequestSigningService(ILogger logger, IClock clock, IDatabaseContext db, CoreConfig config)
{
private readonly ILogger _logger = logger.ForContext<RequestSigningService>();
private readonly IClock _clock = clock;
private readonly CoreConfig _config = config;
private readonly RSA _rsa = context.GetInstanceKeysAsync().GetAwaiter().GetResult();
private readonly RSA _rsa = db.GetInstanceKeysAsync().GetAwaiter().GetResult();
private readonly HttpClient _httpClient = new();
public string GenerateSignature(SignatureData data)
@ -41,11 +40,11 @@ public partial class RequestSigningService(ILogger logger, IClock clock, IDataba
var time = ParseTime(dateHeader);
if ((now + Duration.FromMinutes(1)) < time)
{
throw new FoxchatError.IncomingFederationError("Request was made in the future");
throw new ApiError.IncomingFederationError("Request was made in the future");
}
else if ((now - Duration.FromMinutes(1)) > time)
{
throw new FoxchatError.IncomingFederationError("Request was made too long ago");
throw new ApiError.IncomingFederationError("Request was made too long ago");
}
var plaintext = GeneratePlaintext(new SignatureData(time, host, requestPath, contentLength, userId));
@ -54,7 +53,7 @@ public partial class RequestSigningService(ILogger logger, IClock clock, IDataba
if (!CryptoUtils.TryFromBase64String(encodedSignature, out var signature))
{
throw new FoxchatError.IncomingFederationError("Invalid base64 signature");
throw new ApiError.IncomingFederationError("Invalid base64 signature");
}
var deformatter = new RSAPKCS1SignatureDeformatter(rsa);