mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 22:14:26 +01:00
Properly limit CORS to bitpay api
This commit is contained in:
@@ -11,25 +11,43 @@ using System.Linq;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BTCPayServer.Data;
|
using BTCPayServer.Data;
|
||||||
using BTCPayServer.Servcices.Invoices;
|
using BTCPayServer.Servcices.Invoices;
|
||||||
|
using Microsoft.AspNetCore.Cors;
|
||||||
|
using BTCPayServer.Services.Stores;
|
||||||
|
|
||||||
namespace BTCPayServer.Controllers
|
namespace BTCPayServer.Controllers
|
||||||
{
|
{
|
||||||
public partial class InvoiceController
|
[EnableCors("BitpayAPI")]
|
||||||
|
[BitpayAPIConstraint]
|
||||||
|
public class InvoiceControllerAPI : Controller
|
||||||
{
|
{
|
||||||
|
private InvoiceController _InvoiceController;
|
||||||
|
private InvoiceRepository _InvoiceRepository;
|
||||||
|
private TokenRepository _TokenRepository;
|
||||||
|
private StoreRepository _StoreRepository;
|
||||||
|
|
||||||
|
public InvoiceControllerAPI(InvoiceController invoiceController,
|
||||||
|
InvoiceRepository invoceRepository,
|
||||||
|
TokenRepository tokenRepository,
|
||||||
|
StoreRepository storeRepository)
|
||||||
|
{
|
||||||
|
this._InvoiceController = invoiceController;
|
||||||
|
this._InvoiceRepository = invoceRepository;
|
||||||
|
this._TokenRepository = tokenRepository;
|
||||||
|
this._StoreRepository = storeRepository;
|
||||||
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Route("invoices")]
|
[Route("invoices")]
|
||||||
[MediaTypeConstraint("application/json")]
|
[MediaTypeConstraint("application/json")]
|
||||||
[BitpayAPIConstraint]
|
|
||||||
public async Task<DataWrapper<InvoiceResponse>> CreateInvoice([FromBody] Invoice invoice)
|
public async Task<DataWrapper<InvoiceResponse>> CreateInvoice([FromBody] Invoice invoice)
|
||||||
{
|
{
|
||||||
var bitToken = await CheckTokenPermissionAsync(Facade.Merchant, invoice.Token);
|
var bitToken = await CheckTokenPermissionAsync(Facade.Merchant, invoice.Token);
|
||||||
var store = await FindStore(bitToken);
|
var store = await FindStore(bitToken);
|
||||||
return await CreateInvoiceCore(invoice, store);
|
return await _InvoiceController.CreateInvoiceCore(invoice, store, HttpContext.Request.GetAbsoluteRoot());
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("invoices/{id}")]
|
[Route("invoices/{id}")]
|
||||||
[BitpayAPIConstraint]
|
|
||||||
public async Task<DataWrapper<InvoiceResponse>> GetInvoice(string id, string token)
|
public async Task<DataWrapper<InvoiceResponse>> GetInvoice(string id, string token)
|
||||||
{
|
{
|
||||||
var bitToken = await CheckTokenPermissionAsync(Facade.Merchant, token);
|
var bitToken = await CheckTokenPermissionAsync(Facade.Merchant, token);
|
||||||
@@ -44,7 +62,6 @@ namespace BTCPayServer.Controllers
|
|||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("invoices")]
|
[Route("invoices")]
|
||||||
[BitpayAPIConstraint]
|
|
||||||
public async Task<DataWrapper<InvoiceResponse[]>> GetInvoices(
|
public async Task<DataWrapper<InvoiceResponse[]>> GetInvoices(
|
||||||
string token,
|
string token,
|
||||||
DateTimeOffset? dateStart = null,
|
DateTimeOffset? dateStart = null,
|
||||||
|
|||||||
@@ -150,7 +150,6 @@ namespace BTCPayServer.Controllers
|
|||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
[Route("i/{invoiceId}/status")]
|
[Route("i/{invoiceId}/status")]
|
||||||
[DisableCors]
|
|
||||||
public async Task<IActionResult> GetStatus(string invoiceId)
|
public async Task<IActionResult> GetStatus(string invoiceId)
|
||||||
{
|
{
|
||||||
var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);
|
var invoice = await _InvoiceRepository.GetInvoice(null, invoiceId);
|
||||||
@@ -161,7 +160,6 @@ namespace BTCPayServer.Controllers
|
|||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Route("i/{invoiceId}/UpdateCustomer")]
|
[Route("i/{invoiceId}/UpdateCustomer")]
|
||||||
[DisableCors]
|
|
||||||
public async Task<IActionResult> UpdateCustomer(string invoiceId, [FromBody]UpdateCustomerModel data)
|
public async Task<IActionResult> UpdateCustomer(string invoiceId, [FromBody]UpdateCustomerModel data)
|
||||||
{
|
{
|
||||||
if(!ModelState.IsValid)
|
if(!ModelState.IsValid)
|
||||||
@@ -248,7 +246,7 @@ namespace BTCPayServer.Controllers
|
|||||||
ItemDesc = model.ItemDesc,
|
ItemDesc = model.ItemDesc,
|
||||||
FullNotifications = true,
|
FullNotifications = true,
|
||||||
BuyerEmail = model.BuyerEmail,
|
BuyerEmail = model.BuyerEmail,
|
||||||
}, store);
|
}, store, HttpContext.Request.GetAbsoluteRoot());
|
||||||
|
|
||||||
StatusMessage = $"Invoice {result.Data.Id} just created!";
|
StatusMessage = $"Invoice {result.Data.Id} just created!";
|
||||||
return RedirectToAction(nameof(ListInvoices));
|
return RedirectToAction(nameof(ListInvoices));
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ namespace BTCPayServer.Controllers
|
|||||||
{
|
{
|
||||||
public partial class InvoiceController : Controller
|
public partial class InvoiceController : Controller
|
||||||
{
|
{
|
||||||
TokenRepository _TokenRepository;
|
|
||||||
InvoiceRepository _InvoiceRepository;
|
InvoiceRepository _InvoiceRepository;
|
||||||
BTCPayWallet _Wallet;
|
BTCPayWallet _Wallet;
|
||||||
IRateProvider _RateProvider;
|
IRateProvider _RateProvider;
|
||||||
@@ -58,7 +57,6 @@ namespace BTCPayServer.Controllers
|
|||||||
Network network,
|
Network network,
|
||||||
InvoiceRepository invoiceRepository,
|
InvoiceRepository invoiceRepository,
|
||||||
UserManager<ApplicationUser> userManager,
|
UserManager<ApplicationUser> userManager,
|
||||||
TokenRepository tokenRepository,
|
|
||||||
BTCPayWallet wallet,
|
BTCPayWallet wallet,
|
||||||
IRateProvider rateProvider,
|
IRateProvider rateProvider,
|
||||||
StoreRepository storeRepository,
|
StoreRepository storeRepository,
|
||||||
@@ -69,7 +67,6 @@ namespace BTCPayServer.Controllers
|
|||||||
_Explorer = explorerClient ?? throw new ArgumentNullException(nameof(explorerClient));
|
_Explorer = explorerClient ?? throw new ArgumentNullException(nameof(explorerClient));
|
||||||
_StoreRepository = storeRepository ?? throw new ArgumentNullException(nameof(storeRepository));
|
_StoreRepository = storeRepository ?? throw new ArgumentNullException(nameof(storeRepository));
|
||||||
_Network = network ?? throw new ArgumentNullException(nameof(network));
|
_Network = network ?? throw new ArgumentNullException(nameof(network));
|
||||||
_TokenRepository = tokenRepository ?? throw new ArgumentNullException(nameof(tokenRepository));
|
|
||||||
_InvoiceRepository = invoiceRepository ?? throw new ArgumentNullException(nameof(invoiceRepository));
|
_InvoiceRepository = invoiceRepository ?? throw new ArgumentNullException(nameof(invoiceRepository));
|
||||||
_Wallet = wallet ?? throw new ArgumentNullException(nameof(wallet));
|
_Wallet = wallet ?? throw new ArgumentNullException(nameof(wallet));
|
||||||
_RateProvider = rateProvider ?? throw new ArgumentNullException(nameof(rateProvider));
|
_RateProvider = rateProvider ?? throw new ArgumentNullException(nameof(rateProvider));
|
||||||
@@ -78,7 +75,7 @@ namespace BTCPayServer.Controllers
|
|||||||
_FeeProvider = feeProvider ?? throw new ArgumentNullException(nameof(feeProvider));
|
_FeeProvider = feeProvider ?? throw new ArgumentNullException(nameof(feeProvider));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<DataWrapper<InvoiceResponse>> CreateInvoiceCore(Invoice invoice, StoreData store)
|
internal async Task<DataWrapper<InvoiceResponse>> CreateInvoiceCore(Invoice invoice, StoreData store, string serverUrl)
|
||||||
{
|
{
|
||||||
var derivationStrategy = store.DerivationStrategy;
|
var derivationStrategy = store.DerivationStrategy;
|
||||||
var entity = new InvoiceEntity
|
var entity = new InvoiceEntity
|
||||||
@@ -91,7 +88,7 @@ namespace BTCPayServer.Controllers
|
|||||||
notificationUri = null;
|
notificationUri = null;
|
||||||
EmailAddressAttribute emailValidator = new EmailAddressAttribute();
|
EmailAddressAttribute emailValidator = new EmailAddressAttribute();
|
||||||
entity.ExpirationTime = entity.InvoiceTime + TimeSpan.FromMinutes(15.0);
|
entity.ExpirationTime = entity.InvoiceTime + TimeSpan.FromMinutes(15.0);
|
||||||
entity.ServerUrl = HttpContext.Request.GetAbsoluteRoot();
|
entity.ServerUrl = serverUrl;
|
||||||
entity.FullNotifications = invoice.FullNotifications;
|
entity.FullNotifications = invoice.FullNotifications;
|
||||||
entity.NotificationURL = notificationUri?.AbsoluteUri;
|
entity.NotificationURL = notificationUri?.AbsoluteUri;
|
||||||
entity.BuyerInformation = Map<Invoice, BuyerInformation>(invoice);
|
entity.BuyerInformation = Map<Invoice, BuyerInformation>(invoice);
|
||||||
|
|||||||
@@ -141,6 +141,7 @@ namespace BTCPayServer.Hosting
|
|||||||
services.TryAddSingleton<IAuthorizationHandler, OwnStoreHandler>();
|
services.TryAddSingleton<IAuthorizationHandler, OwnStoreHandler>();
|
||||||
services.AddTransient<AccessTokenController>();
|
services.AddTransient<AccessTokenController>();
|
||||||
services.AddTransient<CallbackController>();
|
services.AddTransient<CallbackController>();
|
||||||
|
services.AddTransient<InvoiceController>();
|
||||||
// Add application services.
|
// Add application services.
|
||||||
services.AddTransient<IEmailSender, EmailSender>();
|
services.AddTransient<IEmailSender, EmailSender>();
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
|
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
|
||||||
|
using Microsoft.AspNetCore.Mvc.Cors.Internal;
|
||||||
|
|
||||||
namespace BTCPayServer.Hosting
|
namespace BTCPayServer.Hosting
|
||||||
{
|
{
|
||||||
@@ -106,7 +107,13 @@ namespace BTCPayServer.Hosting
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
services.AddHangfire(configuration);
|
services.AddHangfire(configuration);
|
||||||
services.AddCors();
|
services.AddCors(o =>
|
||||||
|
{
|
||||||
|
o.AddPolicy("BitpayAPI", b =>
|
||||||
|
{
|
||||||
|
b.AllowAnyMethod().AllowAnyHeader().AllowAnyOrigin();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
services.Configure<IOptions<ApplicationInsightsServiceOptions>>(o =>
|
services.Configure<IOptions<ApplicationInsightsServiceOptions>>(o =>
|
||||||
{
|
{
|
||||||
@@ -135,10 +142,6 @@ namespace BTCPayServer.Hosting
|
|||||||
app.UsePayServer();
|
app.UsePayServer();
|
||||||
app.UseStaticFiles();
|
app.UseStaticFiles();
|
||||||
app.UseAuthentication();
|
app.UseAuthentication();
|
||||||
app.UseCors(b =>
|
|
||||||
{
|
|
||||||
b.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
|
|
||||||
});
|
|
||||||
app.UseHangfireServer();
|
app.UseHangfireServer();
|
||||||
app.UseHangfireDashboard("/hangfire", new DashboardOptions() { Authorization = new[] { new NeedRole(Roles.ServerAdmin) } });
|
app.UseHangfireDashboard("/hangfire", new DashboardOptions() { Authorization = new[] { new NeedRole(Roles.ServerAdmin) } });
|
||||||
app.UseMvc(routes =>
|
app.UseMvc(routes =>
|
||||||
|
|||||||
Reference in New Issue
Block a user