2020-02-24 14:36:15 +01:00
|
|
|
using System.Linq;
|
2020-03-12 18:43:57 +01:00
|
|
|
using System.Security.Claims;
|
2020-02-24 14:36:15 +01:00
|
|
|
using System.Threading.Tasks;
|
2020-03-02 16:50:28 +01:00
|
|
|
using BTCPayServer.Client;
|
2020-02-24 14:36:15 +01:00
|
|
|
using BTCPayServer.Data;
|
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Security.APIKeys
|
|
|
|
{
|
|
|
|
public class APIKeyAuthorizationHandler : AuthorizationHandler<PolicyRequirement>
|
|
|
|
|
|
|
|
{
|
|
|
|
private readonly HttpContext _HttpContext;
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
private readonly StoreRepository _storeRepository;
|
|
|
|
|
|
|
|
public APIKeyAuthorizationHandler(IHttpContextAccessor httpContextAccessor,
|
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
StoreRepository storeRepository)
|
|
|
|
{
|
|
|
|
_HttpContext = httpContextAccessor.HttpContext;
|
|
|
|
_userManager = userManager;
|
|
|
|
_storeRepository = storeRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context,
|
|
|
|
PolicyRequirement requirement)
|
|
|
|
{
|
|
|
|
if (context.User.Identity.AuthenticationType != APIKeyConstants.AuthenticationType)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
switch (requirement.Policy)
|
|
|
|
{
|
2020-03-12 14:59:24 +01:00
|
|
|
case Policies.CanModifyProfile.Key:
|
|
|
|
success = context.HasPermissions(Permissions.ProfileManagement);
|
|
|
|
break;
|
2020-02-24 14:36:15 +01:00
|
|
|
case Policies.CanListStoreSettings.Key:
|
|
|
|
var selectiveStorePermissions =
|
2020-03-02 16:50:28 +01:00
|
|
|
Permissions.ExtractStorePermissionsIds(context.GetPermissions());
|
|
|
|
success = context.HasPermissions(Permissions.StoreManagement) ||
|
2020-02-24 14:36:15 +01:00
|
|
|
selectiveStorePermissions.Any();
|
|
|
|
break;
|
|
|
|
case Policies.CanModifyStoreSettings.Key:
|
|
|
|
string storeId = _HttpContext.GetImplicitStoreId();
|
2020-03-02 16:50:28 +01:00
|
|
|
if (!context.HasPermissions(Permissions.StoreManagement) &&
|
|
|
|
!context.HasPermissions(Permissions.GetStorePermission(storeId)))
|
2020-02-24 14:36:15 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (storeId == null)
|
|
|
|
{
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var userid = _userManager.GetUserId(context.User);
|
|
|
|
if (string.IsNullOrEmpty(userid))
|
|
|
|
break;
|
|
|
|
var store = await _storeRepository.FindStore((string)storeId, userid);
|
|
|
|
if (store == null)
|
|
|
|
break;
|
|
|
|
success = true;
|
|
|
|
_HttpContext.SetStoreData(store);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2020-03-13 11:47:22 +01:00
|
|
|
case Policies.CanCreateUser.Key:
|
2020-02-24 14:36:15 +01:00
|
|
|
case Policies.CanModifyServerSettings.Key:
|
2020-03-02 16:50:28 +01:00
|
|
|
if (!context.HasPermissions(Permissions.ServerManagement))
|
2020-02-24 14:36:15 +01:00
|
|
|
break;
|
2020-03-12 18:43:57 +01:00
|
|
|
// For this authorization, we still check in database because it is super sensitive.
|
|
|
|
success = await IsUserAdmin(context.User);
|
2020-02-24 14:36:15 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:43:57 +01:00
|
|
|
//if you do not have the specific permissions, BUT you have server management, we enable god mode
|
|
|
|
if (!success && context.HasPermissions(Permissions.ServerManagement) &&
|
|
|
|
requirement.Policy != Policies.CanModifyServerSettings.Key)
|
|
|
|
{
|
|
|
|
success = await IsUserAdmin(context.User);
|
|
|
|
}
|
|
|
|
|
2020-02-24 14:36:15 +01:00
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
context.Succeed(requirement);
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 18:43:57 +01:00
|
|
|
|
|
|
|
private async Task<bool> IsUserAdmin(ClaimsPrincipal contextUser)
|
|
|
|
{
|
|
|
|
var user = await _userManager.GetUserAsync(contextUser);
|
|
|
|
if (user == null)
|
|
|
|
return false;
|
|
|
|
if (!await _userManager.IsInRoleAsync(user, Roles.ServerAdmin))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-24 14:36:15 +01:00
|
|
|
}
|
|
|
|
}
|