2020-02-24 14:36:15 +01:00
|
|
|
using System.Linq;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
// For this authorization, we stil check in database because it is super sensitive.
|
|
|
|
var user = await _userManager.GetUserAsync(context.User);
|
|
|
|
if (user == null)
|
|
|
|
break;
|
|
|
|
if (!await _userManager.IsInRoleAsync(user, Roles.ServerAdmin))
|
|
|
|
break;
|
|
|
|
success = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
context.Succeed(requirement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|