btcpayserver/BTCPayServer/Controllers/UIHomeController.cs

209 lines
7.7 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2017-09-13 08:47:34 +02:00
using System.Diagnostics;
2020-06-28 10:55:27 +02:00
using System.IO;
using System.Linq;
2020-06-28 10:55:27 +02:00
using System.Net.Http;
2017-09-13 08:47:34 +02:00
using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Abstractions.Contracts;
using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Client;
2020-06-28 10:55:27 +02:00
using BTCPayServer.Data;
2021-03-30 04:41:44 +02:00
using BTCPayServer.Filters;
2017-09-13 08:47:34 +02:00
using BTCPayServer.Models;
using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Services;
using BTCPayServer.Services.Stores;
2020-06-28 10:55:27 +02:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
2020-06-28 10:55:27 +02:00
using Microsoft.AspNetCore.Mvc;
2020-03-18 12:08:09 +01:00
using Microsoft.Extensions.FileProviders;
2020-06-28 10:55:27 +02:00
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
2017-09-13 08:47:34 +02:00
namespace BTCPayServer.Controllers
{
2022-01-07 04:32:00 +01:00
public class UIHomeController : Controller
2017-09-13 08:47:34 +02:00
{
private readonly ThemeSettings _theme;
private readonly StoreRepository _storeRepository;
private readonly BTCPayNetworkProvider _networkProvider;
private IHttpClientFactory HttpClientFactory { get; }
private SignInManager<ApplicationUser> SignInManager { get; }
2023-03-01 07:49:21 +01:00
private IFileProvider _WebRootFileProvider;
public LanguageService LanguageService { get; }
2018-12-03 15:57:43 +01:00
2022-01-07 04:32:00 +01:00
public UIHomeController(IHttpClientFactory httpClientFactory,
ThemeSettings theme,
LanguageService languageService,
StoreRepository storeRepository,
BTCPayNetworkProvider networkProvider,
2023-03-01 07:49:21 +01:00
IWebHostEnvironment environment,
SignInManager<ApplicationUser> signInManager)
2018-12-03 15:57:43 +01:00
{
_theme = theme;
2018-12-03 15:57:43 +01:00
HttpClientFactory = httpClientFactory;
LanguageService = languageService;
_networkProvider = networkProvider;
_storeRepository = storeRepository;
SignInManager = signInManager;
2023-03-01 07:49:21 +01:00
_WebRootFileProvider = environment.WebRootFileProvider;
2018-12-03 15:57:43 +01:00
}
[HttpGet("home")]
public Task<IActionResult> Home()
{
return Index();
}
2021-03-30 04:41:44 +02:00
[Route("")]
[DomainMappingConstraint]
public async Task<IActionResult> Index()
{
if (_theme.FirstRun)
{
2022-01-07 04:32:00 +01:00
return RedirectToAction(nameof(UIAccountController.Register), "UIAccount");
}
if (SignInManager.IsSignedIn(User))
{
var userId = SignInManager.UserManager.GetUserId(HttpContext.User);
var storeId = HttpContext.GetUserPrefsCookie()?.CurrentStoreId;
if (storeId != null)
{
// verify store exists and redirect to it
var store = await _storeRepository.FindStore(storeId, userId);
if (store != null)
{
2023-05-26 16:49:32 +02:00
return RedirectToStore(userId, store);
}
}
var stores = await _storeRepository.GetStoresByUserId(userId);
return stores.Any()
2023-05-26 16:49:32 +02:00
? RedirectToStore(userId, stores.First())
: RedirectToAction(nameof(UIUserStoresController.CreateStore), "UIUserStores");
}
2021-12-31 08:59:02 +01:00
return Challenge();
2017-09-13 08:47:34 +02:00
}
[Route("misc/lang")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie + "," + AuthenticationSchemes.Greenfield)]
public IActionResult Languages()
{
return Json(LanguageService.GetLanguages(), new JsonSerializerSettings { Formatting = Formatting.Indented });
}
2018-12-03 15:57:43 +01:00
[Route("misc/permissions")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie + "," + AuthenticationSchemes.Greenfield)]
public IActionResult Permissions()
{
return Json(Client.Models.PermissionMetadata.PermissionNodes, new JsonSerializerSettings { Formatting = Formatting.Indented });
}
2023-03-01 07:49:21 +01:00
[Route("misc/translations/{resource}/{lang}")]
[AllowAnonymous]
public IActionResult GetTranslations(string resource, string lang)
{
string path;
if (resource == "checkout-v1")
path = "locales";
else if (resource == "checkout-v2")
path = "locales/checkout";
else
return NotFound();
var enLang = Lang(path + "/en.json");
var en = (enLang as JsonResult)?.Value as JObject;
if (en is null || lang == "en" || lang == "en-US")
return enLang;
lang = LanguageService.FindLanguage(lang)?.Code;
if (lang is null)
return enLang;
var oLang = Lang(path + $"/{lang}.json");
var o = (oLang as JsonResult)?.Value as JObject;
if (o is null)
return enLang;
en.Merge(o);
return Json(en);
}
private IActionResult Lang(string path)
{
var fi = _WebRootFileProvider.GetFileInfo(path);
try
{
using var fs = fi.CreateReadStream();
return Json(JObject.Load(new JsonTextReader(new StreamReader(fs, leaveOpen: true))));
}
catch
{
return NotFound();
}
}
2020-03-18 12:08:09 +01:00
[Route("swagger/v1/swagger.json")]
2020-06-28 10:55:27 +02:00
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie + "," + AuthenticationSchemes.Greenfield)]
public async Task<IActionResult> Swagger([FromServices] IEnumerable<ISwaggerProvider> swaggerProviders)
2020-03-18 12:08:09 +01:00
{
JObject json = new();
var res = await Task.WhenAll(swaggerProviders.Select(provider => provider.Fetch()));
foreach (JObject jObject in res)
{
json.Merge(jObject);
}
2020-03-18 12:08:09 +01:00
var servers = new JArray();
servers.Add(new JObject(new JProperty("url", HttpContext.Request.GetAbsoluteRoot())));
json["servers"] = servers;
2022-04-21 05:30:49 +02:00
var tags = (JArray)json["tags"];
json["tags"] = new JArray(tags
.Select(o => (name: ((JObject)o)["name"].Value<string>(), o))
.OrderBy(o => o.name)
.Select(o => o.o)
.ToArray());
2020-03-18 12:08:09 +01:00
return Json(json);
}
Exchange api no kraken (#3679) * WIP New APIs for dealing with custodians/exchanges * Simplified things * More API refinements + index.html file for quick viewing * Finishing touches on spec * Switched cryptoCode to paymentMethod as this allows us to differentiate between onchain and lightning * Moved draft API docs to "/docs-draft" * WIP baby steps * Added DB migration for CustodianAccountData * Rough but working POST /v1/api/custodian-account + GET /v1/api/custodian * WIP + early Kraken API client * Moved service registration to proper location * Working create + list custodian accounts + permissions + WIP Kraken client * Kraken API Balances call is working * Added asset balances to response * List Custodian Accounts call does not load assetBalances by default, because it can fail. Can be requested when needed. * Call to get the details of 1 specific custodian account * Added permissions to swagger * Added "tradableAssetPairs" to Kraken custodian response + cache the tradable pairs in memory for 24 hours * Removed unused file * WIP + Moved files to better locations * Updated docs * Working API endpoint to get info on a trade (same response as creating a new trade) * Working API endpoints for Deposit + Trade + untested Withdraw * Delete custodian account * Trading works, better error handling, cleanup * Working withdrawals + New endpoint for getting bid/ask prices * Completed withdrawals + new endpoint for getting info on a past withdrawal to simplify testing, Enums are output as strings, * Better error handling when withdrawing to a wrong destination * WithdrawalAddressName in config is now a string per currency (dictionary) * Added TODOs * Only show the custodian account "config" to users who are allowed * Added the new permissions to the API Keys UI * Renamed KrakenClient to KrakenExchange * WIP Kraken Config Form * Removed files for UI again, will make separate PR later * Fixed docs + Refactored to use PaymentMethod more + Added "name" to custodian account + Using cancelationToken everywhere * Updated withdrawal info docs * First unit test * Complete tests for /api/v1/custodians and /api/v1/custodian-accounts endpoints + Various improvements and fixes * Mock custodian and more exceptions * Many more tests + cleanup, moved files to better locations * More tests * WIP more tests * Greenfield API tests complete * Added missing "Name" column * Cleanup, TODOs and beginning of Kraken Tests * Added Kraken tests using public endpoints + handling of "SATS" currency * Added 1st mocked Kraken API call: GetAssetBalancesAsync * Added assert for bad config * Mocked more Kraken API responses + added CreationDate to withdrawal response * pr review club changes * Make Kraken Custodian a plugin * Re-added User-Agent header as it is required * Fixed bug in market trade on Kraken using a percentage as qty * A short delay so Kraken has the time to execute the market order and we don't fetch the details too quickly. * Merged the draft swagger into the main swagger since it didn't work anymore * Fixed API permissions test * Removed 2 TODOs * Fixed unit test * Remove Kraken Api as it should be separate opt-in plugin * Flatten namespace hierarchy and use InnerExeption instead of OriginalException * Remove useless line * Make sure account is from a specific store * Proper error if custodian code not found * Remove various warnings * Remove various warnings * Handle CustodianApiException through an exception filter * Store custodian-account blob directly * Remove duplications, transform methods into property * Improve docs tags * Make sure the custodianCode saved is canonical * Fix test Co-authored-by: Wouter Samaey <wouter.samaey@storefront.be> Co-authored-by: nicolas.dorier <nicolas.dorier@gmail.com>
2022-05-18 07:59:56 +02:00
2020-03-18 12:08:09 +01:00
[Route("docs")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
2020-03-18 12:08:09 +01:00
public IActionResult SwaggerDocs()
{
return View();
}
[Route("recovery-seed-backup")]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie, Policy = Policies.CanModifyStoreSettings)]
public IActionResult RecoverySeedBackup(RecoverySeedBackupViewModel vm)
{
return View("RecoverySeedBackup", vm);
}
[HttpPost]
[Route("postredirect-callback-test")]
public ActionResult PostRedirectCallbackTestpage(IFormCollection data)
{
var list = data.Keys.Aggregate(new Dictionary<string, string>(), (res, key) =>
{
res.Add(key, data[key]);
return res;
});
return Json(list);
}
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
2023-05-26 16:49:32 +02:00
public RedirectToActionResult RedirectToStore(string userId, StoreData store)
{
2023-05-26 16:49:32 +02:00
return store.HasPermission(userId, Policies.CanModifyStoreSettings)
? RedirectToAction("Dashboard", "UIStores", new { storeId = store.Id })
: RedirectToAction("ListInvoices", "UIInvoice", new { storeId = store.Id });
}
2017-09-13 08:47:34 +02:00
}
}