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;
|
2023-12-01 10:50:05 +01:00
|
|
|
using BTCPayServer.HostedServices.Webhooks;
|
2020-11-06 12:42:26 +01:00
|
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Data
|
|
|
|
{
|
|
|
|
public class AuthorizedWebhookEvents
|
|
|
|
{
|
|
|
|
public bool Everything { get; set; }
|
|
|
|
|
2023-12-01 10:50:05 +01:00
|
|
|
public string[] SpecificEvents { get; set; } = Array.Empty<string>();
|
|
|
|
public bool Match(string evt)
|
2020-11-06 12:42:26 +01:00
|
|
|
{
|
|
|
|
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>()
|
|
|
|
{
|
2023-12-01 10:50:05 +01:00
|
|
|
return JsonConvert.DeserializeObject<T>(UTF8Encoding.UTF8.GetString(Request), 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)
|
|
|
|
{
|
2023-02-21 07:06:34 +01:00
|
|
|
return webhook.HasTypedBlob<WebhookBlob>().GetBlob();
|
2020-11-06 12:42:26 +01:00
|
|
|
}
|
|
|
|
public static void SetBlob(this WebhookData webhook, WebhookBlob blob)
|
|
|
|
{
|
2023-02-21 07:06:34 +01:00
|
|
|
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
|
2023-12-01 10:50:05 +01:00
|
|
|
return JsonConvert.DeserializeObject<WebhookDeliveryBlob>(webhook.Blob, 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
|
2023-12-01 10:50:05 +01:00
|
|
|
webhook.Blob = JsonConvert.SerializeObject(blob, WebhookSender.DefaultSerializerSettings);
|
2020-11-06 12:42:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|