Improve HTTP signature handling, proper logging
This commit is contained in:
parent
3bea6254b3
commit
a3a330f559
4 changed files with 16 additions and 27 deletions
|
@ -17,27 +17,12 @@ namespace Iceshrimp.Backend.Controllers;
|
||||||
public class SignatureTestController : Controller {
|
public class SignatureTestController : Controller {
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Consumes(MediaTypeNames.Application.Json)]
|
[Consumes(MediaTypeNames.Application.Json)]
|
||||||
public async Task<IActionResult> Inbox() {
|
public async Task<IActionResult> Inbox([FromServices] ILogger<SignatureTestController> logger,
|
||||||
var sig = new HttpSignature(Request, ["(request-target)", "digest", "host", "date"]);
|
[FromServices] DatabaseContext db) {
|
||||||
|
var sig = new HttpSignature(Request, ["(request-target)", "digest", "host", "date"]);
|
||||||
//TODO: fetch key from db (duh)
|
var key = await db.UserPublickeys.SingleOrDefaultAsync(p => p.KeyId == sig.KeyId);
|
||||||
|
var verified = key != null && sig.Verify(key.KeyPem);
|
||||||
const string key = """
|
logger.LogInformation("sig.Verify returned {result} for key {keyId}", verified, sig.KeyId);
|
||||||
-----BEGIN PUBLIC KEY-----
|
return verified ? Ok() : StatusCode(StatusCodes.Status403Forbidden);
|
||||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtCFuaufkSCpsDZ2twSrH
|
|
||||||
GAFcJTGQ7ZspaFekVM7gBP1GQ/jfjwO3qT9fMgbsCuQXNTIw0U9zlsTIPB91yNPw
|
|
||||||
w5UpbqQ3dnpnYnwXg1BsqfX7EOLR1Dlnw6dk+5yeginJsNno15SRQ7CDqbEXj7Nc
|
|
||||||
lhNOGgU+LaXHhN59Paye3sfsvUHu4fmTp/rALWGPl/Rvx7RVRcR76CcTfTaHPYdb
|
|
||||||
OQAtqPJBfWgHpPLAUjRypzZoN/ExMgiCbFuxI7UFNNXxU3te8GNZaaob8bSwyUB6
|
|
||||||
Xuq7Rw+Me3eYiDxrYHQ99ZytsgoHBNVrVh/X7wIl0AlpjyWeGug3uIUjXR0twuGj
|
|
||||||
wwIDAQAB
|
|
||||||
-----END PUBLIC KEY-----
|
|
||||||
""";
|
|
||||||
|
|
||||||
return Ok(new ErrorResponse {
|
|
||||||
StatusCode = 200,
|
|
||||||
Error = "null",
|
|
||||||
Message = sig.Verify(key).ToString()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@ using System.Text;
|
||||||
namespace Iceshrimp.Backend.Core.Federation.Cryptography;
|
namespace Iceshrimp.Backend.Core.Federation.Cryptography;
|
||||||
|
|
||||||
public class HttpSignature {
|
public class HttpSignature {
|
||||||
private readonly string _keyId;
|
public readonly string KeyId;
|
||||||
private readonly string _algo;
|
private readonly string _algo;
|
||||||
private readonly byte[] _signature;
|
private readonly byte[] _signature;
|
||||||
private readonly byte[] _signatureData;
|
private readonly byte[] _signatureData;
|
||||||
|
@ -23,7 +23,7 @@ public class HttpSignature {
|
||||||
|
|
||||||
var signatureBase64 = sig["signature"] ?? throw new ConstraintException("Signature string is missing the signature field");
|
var signatureBase64 = sig["signature"] ?? throw new ConstraintException("Signature string is missing the signature field");
|
||||||
|
|
||||||
_keyId = sig["keyId"] ?? throw new ConstraintException("Signature string is missing the keyId field");
|
KeyId = sig["keyId"] ?? throw new ConstraintException("Signature string is missing the keyId field");
|
||||||
_algo = sig["algorithm"] ?? throw new ConstraintException("Signature string is missing the algorithm field");
|
_algo = sig["algorithm"] ?? throw new ConstraintException("Signature string is missing the algorithm field");
|
||||||
_signature = Convert.FromBase64String(signatureBase64);
|
_signature = Convert.FromBase64String(signatureBase64);
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ public class Fetch {
|
||||||
|
|
||||||
//TODO: required attribute doesn't work with Newtonsoft.Json it appears
|
//TODO: required attribute doesn't work with Newtonsoft.Json it appears
|
||||||
//TODO: enforce @type values
|
//TODO: enforce @type values
|
||||||
|
//TODO: firstordefault -> singleordefault
|
||||||
|
|
||||||
public static void Test2() {
|
public static void Test2() {
|
||||||
var thing = FetchActivity("https://staging.e2net.social/users/9esresfwle/outbox?page=true");
|
var thing = FetchActivity("https://staging.e2net.social/users/9esresfwle/outbox?page=true");
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
using Asp.Versioning;
|
using Asp.Versioning;
|
||||||
|
using Iceshrimp.Backend.Core.Database;
|
||||||
using Vite.AspNetCore.Extensions;
|
using Vite.AspNetCore.Extensions;
|
||||||
|
|
||||||
//TODO: Add proper logger
|
|
||||||
Console.WriteLine("-- Iceshrimp.NET (alpha) --");
|
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
builder.Services.AddControllers().AddNewtonsoftJson();
|
builder.Services.AddControllers().AddNewtonsoftJson();
|
||||||
|
@ -22,10 +20,15 @@ builder.Services.AddViteServices(options => {
|
||||||
options.Server.UseFullDevUrl = true;
|
options.Server.UseFullDevUrl = true;
|
||||||
options.Base = "frontend"; // relative to wwwroot
|
options.Base = "frontend"; // relative to wwwroot
|
||||||
});
|
});
|
||||||
|
builder.Services.AddLogging(logging => logging.AddSimpleConsole(options => {
|
||||||
|
options.SingleLine = true;
|
||||||
|
}));
|
||||||
|
builder.Services.AddDbContext<DatabaseContext>();
|
||||||
|
|
||||||
//TODO: load built assets in production
|
//TODO: load built assets in production
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
app.Logger.LogInformation("Initializing, please wait...");
|
||||||
|
|
||||||
app.UseSwagger();
|
app.UseSwagger();
|
||||||
app.UseSwaggerUI(options => { options.DocumentTitle = "Iceshrimp API documentation"; });
|
app.UseSwaggerUI(options => { options.DocumentTitle = "Iceshrimp API documentation"; });
|
||||||
|
|
Loading…
Add table
Reference in a new issue