2021-03-11 13:34:52 +01:00
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Threading.Tasks;
|
2021-03-11 21:46:32 +09:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2021-03-11 13:34:52 +01:00
|
|
|
using BTCPayServer.Client;
|
|
|
|
using BTCPayServer.Security.Bitpay;
|
2022-01-14 13:05:23 +09:00
|
|
|
using BTCPayServer.Security.Greenfield;
|
2021-03-11 13:34:52 +01:00
|
|
|
using BTCPayServer.Services;
|
2021-03-11 21:46:32 +09:00
|
|
|
using CsvHelper.Configuration.Attributes;
|
2021-03-11 13:34:52 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
|
|
namespace BTCPayServer
|
|
|
|
{
|
|
|
|
public static class AuthorizationExtensions
|
|
|
|
{
|
|
|
|
public static async Task<(bool HotWallet, bool RPCImport)> CanUseHotWallet(
|
|
|
|
this IAuthorizationService authorizationService,
|
|
|
|
PoliciesSettings policiesSettings,
|
|
|
|
ClaimsPrincipal user)
|
|
|
|
{
|
2021-03-11 21:46:32 +09:00
|
|
|
if (!user.Identity.IsAuthenticated)
|
|
|
|
return (false, false);
|
|
|
|
var claimUser = user.Identity as ClaimsIdentity;
|
|
|
|
if (claimUser is null)
|
|
|
|
return (false, false);
|
|
|
|
|
|
|
|
bool isAdmin = false;
|
|
|
|
if (claimUser.AuthenticationType == AuthenticationSchemes.Cookie)
|
|
|
|
isAdmin = user.IsInRole(Roles.ServerAdmin);
|
2022-01-14 13:05:23 +09:00
|
|
|
else if (claimUser.AuthenticationType == GreenfieldConstants.AuthenticationType)
|
2021-03-11 21:46:32 +09:00
|
|
|
isAdmin = (await authorizationService.AuthorizeAsync(user, Policies.CanModifyServerSettings)).Succeeded;
|
2021-12-31 16:59:02 +09:00
|
|
|
return isAdmin ? (true, true) :
|
2021-03-11 21:46:32 +09:00
|
|
|
(policiesSettings?.AllowHotWalletForAll is true, policiesSettings?.AllowHotWalletRPCImportForAll is true);
|
2021-03-11 13:34:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|