btcpayserver/BTCPayServer/Controllers/GreenField/StoreWebhooksController.cs

199 lines
8.0 KiB
C#
Raw Normal View History

2020-11-13 06:01:51 +01:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Runtime;
2020-11-19 04:40:07 +01:00
using BTCPayServer.Abstractions.Constants;
2020-11-13 06:01:51 +01:00
using BTCPayServer.Client;
using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.HostedServices;
using BTCPayServer.Security;
using BTCPayServer.Services.Stores;
using Google.Apis.Auth.OAuth2;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
namespace BTCPayServer.Controllers.GreenField
{
[ApiController]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield,
Policy = Policies.CanModifyStoreWebhooks)]
[EnableCors(CorsPolicies.All)]
public class StoreWebhooksController : ControllerBase
{
public StoreWebhooksController(StoreRepository storeRepository, WebhookNotificationManager webhookNotificationManager)
{
StoreRepository = storeRepository;
WebhookNotificationManager = webhookNotificationManager;
}
public StoreRepository StoreRepository { get; }
public WebhookNotificationManager WebhookNotificationManager { get; }
[HttpGet("~/api/v1/stores/{storeId}/webhooks/{webhookId?}")]
public async Task<IActionResult> ListWebhooks(string storeId, string webhookId)
2020-11-13 06:01:51 +01:00
{
if (webhookId is null)
{
return Ok((await StoreRepository.GetWebhooks(CurrentStoreId))
2020-11-13 06:01:51 +01:00
.Select(o => FromModel(o, false))
.ToList());
}
else
{
var w = await StoreRepository.GetWebhook(CurrentStoreId, webhookId);
2020-11-13 06:01:51 +01:00
if (w is null)
return WebhookNotFound();
2020-11-13 06:01:51 +01:00
return Ok(FromModel(w, false));
}
}
string CurrentStoreId
{
get
{
return this.HttpContext.GetStoreData()?.Id;
}
}
2020-11-13 06:01:51 +01:00
[HttpPost("~/api/v1/stores/{storeId}/webhooks")]
public async Task<IActionResult> CreateWebhook(string storeId, Client.Models.CreateStoreWebhookRequest create)
2020-11-13 06:01:51 +01:00
{
ValidateWebhookRequest(create);
if (!ModelState.IsValid)
return this.CreateValidationError(ModelState);
var webhookId = await StoreRepository.CreateWebhook(CurrentStoreId, ToModel(create));
var w = await StoreRepository.GetWebhook(CurrentStoreId, webhookId);
2020-11-13 06:01:51 +01:00
if (w is null)
return WebhookNotFound();
2020-11-13 06:01:51 +01:00
return Ok(FromModel(w, true));
}
private void ValidateWebhookRequest(StoreWebhookBaseData create)
{
if (!Uri.TryCreate(create?.Url, UriKind.Absolute, out var uri))
ModelState.AddModelError(nameof(Url), "Invalid Url");
}
[HttpPut("~/api/v1/stores/{storeId}/webhooks/{webhookId}")]
public async Task<IActionResult> UpdateWebhook(string storeId, string webhookId, Client.Models.UpdateStoreWebhookRequest update)
{
ValidateWebhookRequest(update);
if (!ModelState.IsValid)
return this.CreateValidationError(ModelState);
var w = await StoreRepository.GetWebhook(CurrentStoreId, webhookId);
2020-11-13 06:01:51 +01:00
if (w is null)
return WebhookNotFound();
2020-11-13 06:01:51 +01:00
await StoreRepository.UpdateWebhook(storeId, webhookId, ToModel(update));
return await ListWebhooks(storeId, webhookId);
2020-11-13 06:01:51 +01:00
}
[HttpDelete("~/api/v1/stores/{storeId}/webhooks/{webhookId}")]
public async Task<IActionResult> DeleteWebhook(string storeId, string webhookId)
2020-11-13 06:01:51 +01:00
{
var w = await StoreRepository.GetWebhook(CurrentStoreId, webhookId);
2020-11-13 06:01:51 +01:00
if (w is null)
return WebhookNotFound();
await StoreRepository.DeleteWebhook(CurrentStoreId, webhookId);
2020-11-13 06:01:51 +01:00
return Ok();
}
IActionResult WebhookNotFound()
{
return this.CreateAPIError(404, "webhook-not-found", "The webhook was not found");
}
IActionResult WebhookDeliveryNotFound()
{
return this.CreateAPIError(404, "webhookdelivery-not-found", "The webhook delivery was not found");
}
2020-11-13 06:01:51 +01:00
private WebhookBlob ToModel(StoreWebhookBaseData create)
{
return new WebhookBlob()
{
Active = create.Enabled,
Url = create.Url,
Secret = create.Secret,
AuthorizedEvents = create.AuthorizedEvents is Client.Models.StoreWebhookBaseData.AuthorizedEventsData aed ?
new AuthorizedWebhookEvents()
{
Everything = aed.Everything,
SpecificEvents = aed.SpecificEvents
}:
new AuthorizedWebhookEvents() { Everything = true },
AutomaticRedelivery = create.AutomaticRedelivery,
};
}
[HttpGet("~/api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries/{deliveryId?}")]
public async Task<IActionResult> ListDeliveries(string storeId, string webhookId, string deliveryId, int? count = null)
2020-11-13 06:01:51 +01:00
{
if (deliveryId is null)
{
return Ok((await StoreRepository.GetWebhookDeliveries(CurrentStoreId, webhookId, count))
2020-11-13 06:01:51 +01:00
.Select(o => FromModel(o))
.ToList());
}
else
{
var delivery = await StoreRepository.GetWebhookDelivery(CurrentStoreId, webhookId, deliveryId);
2020-11-13 06:01:51 +01:00
if (delivery is null)
return WebhookDeliveryNotFound();
2020-11-13 06:01:51 +01:00
return Ok(FromModel(delivery));
}
}
[HttpPost("~/api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}/redeliver")]
public async Task<IActionResult> RedeliverWebhook(string storeId, string webhookId, string deliveryId)
2020-11-13 06:01:51 +01:00
{
var delivery = await StoreRepository.GetWebhookDelivery(CurrentStoreId, webhookId, deliveryId);
2020-11-13 06:01:51 +01:00
if (delivery is null)
return WebhookDeliveryNotFound();
2020-11-13 06:01:51 +01:00
return this.Ok(new JValue(await WebhookNotificationManager.Redeliver(deliveryId)));
}
[HttpGet("~/api/v1/stores/{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}/request")]
public async Task<IActionResult> GetDeliveryRequest(string storeId, string webhookId, string deliveryId)
2020-11-13 06:01:51 +01:00
{
var delivery = await StoreRepository.GetWebhookDelivery(CurrentStoreId, webhookId, deliveryId);
2020-11-13 06:01:51 +01:00
if (delivery is null)
return WebhookDeliveryNotFound();
2020-11-13 06:01:51 +01:00
return File(delivery.GetBlob().Request, "application/json");
}
private Client.Models.WebhookDeliveryData FromModel(Data.WebhookDeliveryData data)
{
var b = data.GetBlob();
return new Client.Models.WebhookDeliveryData()
{
Id = data.Id,
Timestamp = data.Timestamp,
Status = b.Status,
ErrorMessage = b.ErrorMessage,
HttpCode = b.HttpCode
};
}
Client.Models.StoreWebhookData FromModel(Data.WebhookData data, bool includeSecret)
{
var b = data.GetBlob();
return new Client.Models.StoreWebhookData()
{
Id = data.Id,
Url = b.Url,
Enabled = b.Active,
Secret = includeSecret ? b.Secret : null,
AutomaticRedelivery = b.AutomaticRedelivery,
AuthorizedEvents = new Client.Models.StoreWebhookData.AuthorizedEventsData()
{
Everything = b.AuthorizedEvents.Everything,
SpecificEvents = b.AuthorizedEvents.SpecificEvents
}
};
}
}
}