btcpayserver/BTCPayServer/Data/WebhookDataExtensions.cs

92 lines
3.2 KiB
C#
Raw Normal View History

2020-11-06 12:42:26 +01:00
using System;
using System.Collections.Generic;
2023-05-28 16:44:10 +02:00
using System.ComponentModel;
2020-11-06 12:42:26 +01:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SshNet.Security.Cryptography;
namespace BTCPayServer.Data
{
public class AuthorizedWebhookEvents
{
public bool Everything { get; set; }
[JsonProperty(ItemConverterType = typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public WebhookEventType[] SpecificEvents { get; set; } = Array.Empty<WebhookEventType>();
public bool Match(WebhookEventType evt)
{
return Everything || SpecificEvents.Contains(evt);
}
}
2021-12-31 08:59:02 +01:00
2020-11-06 12:42:26 +01:00
public class WebhookDeliveryBlob
{
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public WebhookDeliveryStatus Status { get; set; }
public int? HttpCode { get; set; }
2023-05-28 16:44:10 +02:00
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-11-06 12:42:26 +01:00
public string ErrorMessage { get; set; }
2023-05-28 16:44:10 +02:00
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-11-06 12:42:26 +01:00
public byte[] Request { get; set; }
2023-05-28 16:44:10 +02:00
public void Prune()
{
var request = JObject.Parse(UTF8Encoding.UTF8.GetString(Request));
foreach (var prop in request.Properties().ToList())
{
if (prop.Name == "type")
continue;
prop.Remove();
}
Request = UTF8Encoding.UTF8.GetBytes(request.ToString(Formatting.None));
}
2020-11-06 12:42:26 +01:00
public T ReadRequestAs<T>()
{
return JsonConvert.DeserializeObject<T>(UTF8Encoding.UTF8.GetString(Request), HostedServices.WebhookSender.DefaultSerializerSettings);
2020-11-06 12:42:26 +01:00
}
2023-05-28 16:44:10 +02:00
public bool IsPruned()
{
return ReadRequestAs<WebhookEvent>().IsPruned();
}
2020-11-06 12:42:26 +01:00
}
public class WebhookBlob
{
public string Url { get; set; }
public bool Active { get; set; } = true;
public string Secret { get; set; }
public bool AutomaticRedelivery { get; set; }
public AuthorizedWebhookEvents AuthorizedEvents { get; set; }
}
public static class WebhookDataExtensions
{
public static WebhookBlob GetBlob(this WebhookData webhook)
{
return webhook.HasTypedBlob<WebhookBlob>().GetBlob();
2020-11-06 12:42:26 +01:00
}
public static void SetBlob(this WebhookData webhook, WebhookBlob blob)
{
webhook.HasTypedBlob<WebhookBlob>().SetBlob(blob);
2020-11-06 12:42:26 +01:00
}
public static WebhookDeliveryBlob GetBlob(this WebhookDeliveryData webhook)
{
2023-05-28 16:44:10 +02:00
if (webhook.Blob is null)
return null;
else
return JsonConvert.DeserializeObject<WebhookDeliveryBlob>(webhook.Blob, HostedServices.WebhookSender.DefaultSerializerSettings);
2020-11-06 12:42:26 +01:00
}
public static void SetBlob(this WebhookDeliveryData webhook, WebhookDeliveryBlob blob)
{
2023-05-28 16:44:10 +02:00
if (blob is null)
webhook.Blob = null;
else
webhook.Blob = JsonConvert.SerializeObject(blob, HostedServices.WebhookSender.DefaultSerializerSettings);
2020-11-06 12:42:26 +01:00
}
}
}