diff --git a/BTCPayServer/Controllers/StoresController.cs b/BTCPayServer/Controllers/StoresController.cs index 99bec985b..dda5a65ee 100644 --- a/BTCPayServer/Controllers/StoresController.cs +++ b/BTCPayServer/Controllers/StoresController.cs @@ -647,6 +647,7 @@ namespace BTCPayServer.Controllers return View(model); } + [HttpGet] [Route("{storeId}/tokens/{tokenId}/revoke")] public async Task RevokeToken(string tokenId) @@ -780,13 +781,22 @@ namespace BTCPayServer.Controllers [HttpPost] [Route("{storeId}/tokens/apikey")] - public async Task GenerateAPIKey(string storeId) + public async Task GenerateAPIKey(string storeId, string command="") { var store = HttpContext.GetStoreData(); if (store == null) return NotFound(); - await _TokenRepository.GenerateLegacyAPIKey(CurrentStore.Id); - TempData[WellKnownTempData.SuccessMessage] = "API Key re-generated"; + if (command == "revoke") + { + await _TokenRepository.RevokeLegacyAPIKeys(CurrentStore.Id); + TempData[WellKnownTempData.SuccessMessage] = "API Key revoked"; + } + else + { + await _TokenRepository.GenerateLegacyAPIKey(CurrentStore.Id); + TempData[WellKnownTempData.SuccessMessage] = "API Key re-generated"; + } + return RedirectToAction(nameof(ListTokens), new { storeId diff --git a/BTCPayServer/Security/Bitpay/TokenRepository.cs b/BTCPayServer/Security/Bitpay/TokenRepository.cs index c52acbf34..cb0289a52 100644 --- a/BTCPayServer/Security/Bitpay/TokenRepository.cs +++ b/BTCPayServer/Security/Bitpay/TokenRepository.cs @@ -76,6 +76,21 @@ namespace BTCPayServer.Security.Bitpay } } + public async Task RevokeLegacyAPIKeys(string storeId) + { + var keys = await GetLegacyAPIKeys(storeId); + if (!keys.Any()) + { + return; + } + + using (var ctx = _Factory.CreateContext()) + { + ctx.ApiKeys.RemoveRange(keys.Select(s => new APIKeyData() {Id = s})); + await ctx.SaveChangesAsync(); + } + } + public async Task GetLegacyAPIKeys(string storeId) { using (var ctx = _Factory.CreateContext()) diff --git a/BTCPayServer/Views/Stores/ListTokens.cshtml b/BTCPayServer/Views/Stores/ListTokens.cshtml index 2af62ceb4..d7ce9d040 100644 --- a/BTCPayServer/Views/Stores/ListTokens.cshtml +++ b/BTCPayServer/Views/Stores/ListTokens.cshtml @@ -55,9 +55,25 @@
- +
+ + @if (string.IsNullOrEmpty(Model.ApiKey)) + { +
+ +
+ } + else + { +
+ +
+
+ +
+ } +
-