Improve MediaTypeRouteFilterAttribute
This commit is contained in:
parent
cb48468627
commit
a4d787331b
3 changed files with 14 additions and 7 deletions
|
@ -11,8 +11,11 @@ namespace Iceshrimp.Backend.Controllers;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[MediaTypeRouteFilter("application/activity+json", "application/ld+json")]
|
[MediaTypeRouteFilter("application/activity+json", "application/ld+json")]
|
||||||
[Produces("application/activity+json", "application/ld+json")]
|
[Produces("application/activity+json", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"")]
|
||||||
public class ActivityPubController(ILogger<ActivityPubController> logger, DatabaseContext db, APUserRenderer userRenderer) : Controller {
|
public class ActivityPubController(
|
||||||
|
ILogger<ActivityPubController> logger,
|
||||||
|
DatabaseContext db,
|
||||||
|
APUserRenderer userRenderer) : Controller {
|
||||||
/*
|
/*
|
||||||
[HttpGet("/notes/{id}")]
|
[HttpGet("/notes/{id}")]
|
||||||
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Note))]
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Note))]
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using Microsoft.AspNetCore.Mvc.ActionConstraints;
|
using Microsoft.AspNetCore.Mvc.ActionConstraints;
|
||||||
|
|
||||||
namespace Iceshrimp.Backend.Controllers.Attributes;
|
namespace Iceshrimp.Backend.Controllers.Attributes;
|
||||||
|
@ -5,12 +6,14 @@ namespace Iceshrimp.Backend.Controllers.Attributes;
|
||||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
||||||
public class MediaTypeRouteFilterAttribute(params string[] mediaTypes) : Attribute, IActionConstraint {
|
public class MediaTypeRouteFilterAttribute(params string[] mediaTypes) : Attribute, IActionConstraint {
|
||||||
public bool Accept(ActionConstraintContext context) {
|
public bool Accept(ActionConstraintContext context) {
|
||||||
//TODO: this should parse the header properly, edge cases like profile=, charset=, q= are not currently handled.
|
|
||||||
//TODO: this should set the correct content type for the response as well
|
//TODO: this should set the correct content type for the response as well
|
||||||
return context.RouteContext.HttpContext.Request.Headers.ContainsKey("Accept") &&
|
if (!context.RouteContext.HttpContext.Request.Headers.ContainsKey("Accept")) return false;
|
||||||
mediaTypes.Any(p => context.RouteContext.HttpContext.Request.Headers.Accept.ToString() == p ||
|
|
||||||
context.RouteContext.HttpContext.Request.Headers.Accept.ToString()
|
var accept = context.RouteContext.HttpContext.Request.Headers.Accept.ToString().Split(',')
|
||||||
.StartsWith(p + ";"));
|
.Select(MediaTypeWithQualityHeaderValue.Parse)
|
||||||
|
.Select(p => p.MediaType);
|
||||||
|
|
||||||
|
return accept.Any(mediaTypes.Contains);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Order => HttpMethodActionConstraint.HttpMethodConstraintOrder + 1;
|
public int Order => HttpMethodActionConstraint.HttpMethodConstraintOrder + 1;
|
||||||
|
|
|
@ -32,6 +32,7 @@ public static class HttpSignature {
|
||||||
HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: make this share code with the the regular Verify function
|
||||||
public static bool VerifySign(this HttpRequestMessage request, string key) {
|
public static bool VerifySign(this HttpRequestMessage request, string key) {
|
||||||
var signatureHeader = request.Headers.GetValues("Signature").First();
|
var signatureHeader = request.Headers.GetValues("Signature").First();
|
||||||
var signature = Parse(signatureHeader);
|
var signature = Parse(signatureHeader);
|
||||||
|
|
Loading…
Add table
Reference in a new issue