From e90414bded16a5502347fe3da94562bd3d3a817a Mon Sep 17 00:00:00 2001 From: Andrew Camilleri Date: Sat, 6 Jan 2024 08:46:19 +0100 Subject: [PATCH] Hide LN Balance when using internal node and not server admin (#5639) * Hide LN Balance when using internal node and not server admin * Minor updates --------- Co-authored-by: Dennis Reimann --- .../StoreLightningBalance/Default.cshtml | 5 ++- .../StoreLightningBalance.cs | 33 ++++++++++++++----- BTCPayServer/Views/UIStores/Dashboard.cshtml | 3 +- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/BTCPayServer/Components/StoreLightningBalance/Default.cshtml b/BTCPayServer/Components/StoreLightningBalance/Default.cshtml index c2cdbd902..8ab1c3d52 100644 --- a/BTCPayServer/Components/StoreLightningBalance/Default.cshtml +++ b/BTCPayServer/Components/StoreLightningBalance/Default.cshtml @@ -1,5 +1,8 @@ @model BTCPayServer.Components.StoreLightningBalance.StoreLightningBalanceViewModel - +@if(!Model.InitialRendering && Model.Balance == null) +{ + return; +}
Lightning Balance
diff --git a/BTCPayServer/Components/StoreLightningBalance/StoreLightningBalance.cs b/BTCPayServer/Components/StoreLightningBalance/StoreLightningBalance.cs index ec9c1515b..cf21bd5e4 100644 --- a/BTCPayServer/Components/StoreLightningBalance/StoreLightningBalance.cs +++ b/BTCPayServer/Components/StoreLightningBalance/StoreLightningBalance.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using System.Threading.Tasks; using BTCPayServer.Abstractions.Extensions; +using BTCPayServer.Client; using BTCPayServer.Configuration; using BTCPayServer.Data; using BTCPayServer.Lightning; @@ -9,9 +10,11 @@ using BTCPayServer.Models; using BTCPayServer.Models.StoreViewModels; using BTCPayServer.Payments; using BTCPayServer.Payments.Lightning; +using BTCPayServer.Security; using BTCPayServer.Services; using BTCPayServer.Services.Rates; using BTCPayServer.Services.Stores; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; @@ -26,6 +29,7 @@ public class StoreLightningBalance : ViewComponent private readonly LightningClientFactoryService _lightningClientFactory; private readonly IOptions _lightningNetworkOptions; private readonly IOptions _externalServiceOptions; + private readonly IAuthorizationService _authorizationService; public StoreLightningBalance( StoreRepository storeRepo, @@ -34,13 +38,15 @@ public class StoreLightningBalance : ViewComponent BTCPayServerOptions btcpayServerOptions, LightningClientFactoryService lightningClientFactory, IOptions lightningNetworkOptions, - IOptions externalServiceOptions) + IOptions externalServiceOptions, + IAuthorizationService authorizationService) { _storeRepo = storeRepo; _currencies = currencies; _networkProvider = networkProvider; _btcpayServerOptions = btcpayServerOptions; _externalServiceOptions = externalServiceOptions; + _authorizationService = authorizationService; _lightningClientFactory = lightningClientFactory; _lightningNetworkOptions = lightningNetworkOptions; } @@ -54,13 +60,19 @@ public class StoreLightningBalance : ViewComponent vm.DefaultCurrency = vm.Store.GetStoreBlob().DefaultCurrency; vm.CurrencyData = _currencies.GetCurrencyData(vm.DefaultCurrency, true); - - if (vm.InitialRendering) - return View(vm); - + try { - var lightningClient = GetLightningClient(vm.Store, vm.CryptoCode); + var lightningClient = await GetLightningClient(vm.Store, vm.CryptoCode); + if (lightningClient == null) + { + vm.InitialRendering = false; + return View(vm); + } + + if (vm.InitialRendering) + return View(vm); + var balance = await lightningClient.GetBalance(); vm.Balance = balance; vm.TotalOnchain = balance.OnchainBalance != null @@ -72,7 +84,8 @@ public class StoreLightningBalance : ViewComponent (balance.OffchainBalance.Closing ?? 0) : null; } - catch (NotSupportedException) + + catch (Exception ex) when (ex is NotImplementedException or NotSupportedException) { // not all implementations support balance fetching vm.ProblemDescription = "Your node does not support balance fetching."; @@ -85,7 +98,7 @@ public class StoreLightningBalance : ViewComponent return View(vm); } - private ILightningClient GetLightningClient(StoreData store, string cryptoCode) + private async Task GetLightningClient(StoreData store, string cryptoCode ) { var network = _networkProvider.GetNetwork(cryptoCode); var id = new PaymentMethodId(cryptoCode, PaymentTypes.LightningLike); @@ -101,7 +114,9 @@ public class StoreLightningBalance : ViewComponent } if (existing.IsInternalNode && _lightningNetworkOptions.Value.InternalLightningByCryptoCode.TryGetValue(cryptoCode, out var internalLightningNode)) { - return internalLightningNode; + var result = await _authorizationService.AuthorizeAsync(HttpContext.User, null, + new PolicyRequirement(Policies.CanUseInternalLightningNode)); + return result.Succeeded ? internalLightningNode : null; } return null; diff --git a/BTCPayServer/Views/UIStores/Dashboard.cshtml b/BTCPayServer/Views/UIStores/Dashboard.cshtml index ea35813ae..94d555d75 100644 --- a/BTCPayServer/Views/UIStores/Dashboard.cshtml +++ b/BTCPayServer/Views/UIStores/Dashboard.cshtml @@ -7,6 +7,7 @@ @using BTCPayServer.Components.AppSales @using BTCPayServer.Components.AppTopItems @using BTCPayServer.Services.Apps +@using BTCPayServer.Client @model StoreDashboardViewModel @{ ViewData.SetActivePage(StoreNavPages.Dashboard, Model.StoreName, Model.StoreId); @@ -82,7 +83,7 @@ @if (Model.LightningEnabled) { - + } @if (Model.WalletEnabled) {