2020-03-02 16:50:28 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Client
|
|
|
|
|
{
|
2020-03-20 05:41:47 +01:00
|
|
|
|
public class Policies
|
2020-03-02 16:50:28 +01:00
|
|
|
|
{
|
2020-05-29 02:00:13 +02:00
|
|
|
|
public const string CanCreateLightningInvoiceInternalNode = "btcpay.server.cancreatelightninginvoiceinternalnode";
|
|
|
|
|
public const string CanCreateLightningInvoiceInStore = "btcpay.store.cancreatelightninginvoice";
|
|
|
|
|
public const string CanUseInternalLightningNode = "btcpay.server.canuseinternallightningnode";
|
|
|
|
|
public const string CanUseLightningNodeInStore = "btcpay.store.canuselightningnode";
|
2020-03-19 11:11:15 +01:00
|
|
|
|
public const string CanModifyServerSettings = "btcpay.server.canmodifyserversettings";
|
|
|
|
|
public const string CanModifyStoreSettings = "btcpay.store.canmodifystoresettings";
|
2020-06-27 08:34:03 +02:00
|
|
|
|
public const string CanModifyStoreSettingsUnscoped = "btcpay.store.canmodifystoresettings:";
|
2020-03-19 11:11:15 +01:00
|
|
|
|
public const string CanViewStoreSettings = "btcpay.store.canviewstoresettings";
|
|
|
|
|
public const string CanCreateInvoice = "btcpay.store.cancreateinvoice";
|
2020-05-19 19:59:23 +02:00
|
|
|
|
public const string CanViewPaymentRequests = "btcpay.store.canviewpaymentrequests";
|
|
|
|
|
public const string CanModifyPaymentRequests = "btcpay.store.canmodifypaymentrequests";
|
2020-03-19 11:11:15 +01:00
|
|
|
|
public const string CanModifyProfile = "btcpay.user.canmodifyprofile";
|
|
|
|
|
public const string CanViewProfile = "btcpay.user.canviewprofile";
|
|
|
|
|
public const string CanCreateUser = "btcpay.server.cancreateuser";
|
2020-06-24 03:34:09 +02:00
|
|
|
|
public const string CanManagePullPayments = "btcpay.store.canmanagepullpayments";
|
2020-03-19 11:11:15 +01:00
|
|
|
|
public const string Unrestricted = "unrestricted";
|
|
|
|
|
public static IEnumerable<string> AllPolicies
|
2020-03-12 14:59:24 +01:00
|
|
|
|
{
|
2020-03-19 11:11:15 +01:00
|
|
|
|
get
|
2020-03-12 14:59:24 +01:00
|
|
|
|
{
|
2020-03-19 11:11:15 +01:00
|
|
|
|
yield return CanCreateInvoice;
|
|
|
|
|
yield return CanModifyServerSettings;
|
|
|
|
|
yield return CanModifyStoreSettings;
|
|
|
|
|
yield return CanViewStoreSettings;
|
2020-05-19 19:59:23 +02:00
|
|
|
|
yield return CanViewPaymentRequests;
|
|
|
|
|
yield return CanModifyPaymentRequests;
|
2020-03-19 11:11:15 +01:00
|
|
|
|
yield return CanModifyProfile;
|
|
|
|
|
yield return CanViewProfile;
|
|
|
|
|
yield return CanCreateUser;
|
|
|
|
|
yield return Unrestricted;
|
2020-05-29 02:00:13 +02:00
|
|
|
|
yield return CanUseInternalLightningNode;
|
|
|
|
|
yield return CanCreateLightningInvoiceInternalNode;
|
|
|
|
|
yield return CanUseLightningNodeInStore;
|
|
|
|
|
yield return CanCreateLightningInvoiceInStore;
|
2020-06-24 03:34:09 +02:00
|
|
|
|
yield return CanManagePullPayments;
|
2020-03-19 11:11:15 +01:00
|
|
|
|
}
|
2020-03-12 14:59:24 +01:00
|
|
|
|
}
|
2020-03-20 05:41:47 +01:00
|
|
|
|
public static bool IsValidPolicy(string policy)
|
|
|
|
|
{
|
|
|
|
|
return AllPolicies.Any(p => p.Equals(policy, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
}
|
2020-03-02 16:50:28 +01:00
|
|
|
|
|
2020-03-20 05:41:47 +01:00
|
|
|
|
public static bool IsStorePolicy(string policy)
|
|
|
|
|
{
|
|
|
|
|
return policy.StartsWith("btcpay.store", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}
|
2020-06-12 11:26:20 +02:00
|
|
|
|
public static bool IsStoreModifyPolicy(string policy)
|
|
|
|
|
{
|
|
|
|
|
return policy.StartsWith("btcpay.store.canmodify", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}
|
2020-03-23 14:17:17 +01:00
|
|
|
|
public static bool IsServerPolicy(string policy)
|
|
|
|
|
{
|
|
|
|
|
return policy.StartsWith("btcpay.server", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
}
|
2020-03-20 05:41:47 +01:00
|
|
|
|
}
|
|
|
|
|
public class Permission
|
|
|
|
|
{
|
2020-06-07 16:17:48 +02:00
|
|
|
|
public static Permission Create(string policy, string scope = null)
|
2020-03-19 11:11:15 +01:00
|
|
|
|
{
|
2020-06-07 16:17:48 +02:00
|
|
|
|
if (TryCreatePermission(policy, scope, out var r))
|
2020-03-19 11:11:15 +01:00
|
|
|
|
return r;
|
|
|
|
|
throw new ArgumentException("Invalid Permission");
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 16:17:48 +02:00
|
|
|
|
public static bool TryCreatePermission(string policy, string scope, out Permission permission)
|
2020-03-19 11:11:15 +01:00
|
|
|
|
{
|
|
|
|
|
permission = null;
|
|
|
|
|
if (policy == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(policy));
|
|
|
|
|
policy = policy.Trim().ToLowerInvariant();
|
2020-03-20 05:41:47 +01:00
|
|
|
|
if (!Policies.IsValidPolicy(policy))
|
2020-03-19 11:11:15 +01:00
|
|
|
|
return false;
|
2020-06-07 16:17:48 +02:00
|
|
|
|
if (scope != null && !Policies.IsStorePolicy(policy))
|
2020-03-19 11:11:15 +01:00
|
|
|
|
return false;
|
2020-06-07 16:17:48 +02:00
|
|
|
|
permission = new Permission(policy, scope);
|
2020-03-19 11:11:15 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool TryParse(string str, out Permission permission)
|
|
|
|
|
{
|
|
|
|
|
permission = null;
|
|
|
|
|
if (str == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(str));
|
|
|
|
|
str = str.Trim();
|
|
|
|
|
var separator = str.IndexOf(':');
|
|
|
|
|
if (separator == -1)
|
|
|
|
|
{
|
|
|
|
|
str = str.ToLowerInvariant();
|
2020-03-20 05:41:47 +01:00
|
|
|
|
if (!Policies.IsValidPolicy(str))
|
2020-03-19 11:11:15 +01:00
|
|
|
|
return false;
|
|
|
|
|
permission = new Permission(str, null);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var policy = str.Substring(0, separator).ToLowerInvariant();
|
2020-03-20 05:41:47 +01:00
|
|
|
|
if (!Policies.IsValidPolicy(policy))
|
2020-03-19 11:11:15 +01:00
|
|
|
|
return false;
|
2020-03-20 05:41:47 +01:00
|
|
|
|
if (!Policies.IsStorePolicy(policy))
|
2020-03-19 11:11:15 +01:00
|
|
|
|
return false;
|
|
|
|
|
var storeId = str.Substring(separator + 1);
|
|
|
|
|
if (storeId.Length == 0)
|
|
|
|
|
return false;
|
|
|
|
|
permission = new Permission(policy, storeId);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 16:17:48 +02:00
|
|
|
|
internal Permission(string policy, string scope)
|
2020-03-19 11:11:15 +01:00
|
|
|
|
{
|
|
|
|
|
Policy = policy;
|
2020-06-07 16:17:48 +02:00
|
|
|
|
Scope = scope;
|
2020-03-19 11:11:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Contains(Permission subpermission)
|
|
|
|
|
{
|
|
|
|
|
if (subpermission is null)
|
|
|
|
|
throw new ArgumentNullException(nameof(subpermission));
|
|
|
|
|
|
|
|
|
|
if (!ContainsPolicy(subpermission.Policy))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-03-20 05:41:47 +01:00
|
|
|
|
if (!Policies.IsStorePolicy(subpermission.Policy))
|
2020-03-19 11:11:15 +01:00
|
|
|
|
return true;
|
2020-06-07 16:17:48 +02:00
|
|
|
|
return Scope == null || subpermission.Scope == this.Scope;
|
2020-03-19 11:11:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<Permission> ToPermissions(string[] permissions)
|
|
|
|
|
{
|
|
|
|
|
if (permissions == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(permissions));
|
|
|
|
|
foreach (var p in permissions)
|
|
|
|
|
{
|
|
|
|
|
if (TryParse(p, out var pp))
|
|
|
|
|
yield return pp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool ContainsPolicy(string subpolicy)
|
|
|
|
|
{
|
2020-03-20 05:41:47 +01:00
|
|
|
|
if (this.Policy == Policies.Unrestricted)
|
2020-03-19 11:11:15 +01:00
|
|
|
|
return true;
|
|
|
|
|
if (this.Policy == subpolicy)
|
|
|
|
|
return true;
|
2020-05-19 19:59:23 +02:00
|
|
|
|
switch (subpolicy)
|
2020-06-28 10:55:27 +02:00
|
|
|
|
{
|
2020-05-19 19:59:23 +02:00
|
|
|
|
case Policies.CanViewStoreSettings when this.Policy == Policies.CanModifyStoreSettings:
|
|
|
|
|
case Policies.CanCreateInvoice when this.Policy == Policies.CanModifyStoreSettings:
|
|
|
|
|
case Policies.CanViewProfile when this.Policy == Policies.CanModifyProfile:
|
|
|
|
|
case Policies.CanModifyPaymentRequests when this.Policy == Policies.CanModifyStoreSettings:
|
|
|
|
|
case Policies.CanViewPaymentRequests when this.Policy == Policies.CanModifyStoreSettings:
|
|
|
|
|
case Policies.CanViewPaymentRequests when this.Policy == Policies.CanViewStoreSettings:
|
2020-05-29 02:00:13 +02:00
|
|
|
|
case Policies.CanCreateLightningInvoiceInternalNode when this.Policy == Policies.CanUseInternalLightningNode:
|
|
|
|
|
case Policies.CanCreateLightningInvoiceInStore when this.Policy == Policies.CanUseLightningNodeInStore:
|
2020-05-19 19:59:23 +02:00
|
|
|
|
return true;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-03-19 11:11:15 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 16:17:48 +02:00
|
|
|
|
public string Scope { get; }
|
2020-03-19 11:11:15 +01:00
|
|
|
|
public string Policy { get; }
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2020-06-07 16:17:48 +02:00
|
|
|
|
if (Scope != null)
|
2020-03-19 11:11:15 +01:00
|
|
|
|
{
|
2020-06-07 16:17:48 +02:00
|
|
|
|
return $"{Policy}:{Scope}";
|
2020-03-19 11:11:15 +01:00
|
|
|
|
}
|
|
|
|
|
return Policy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
Permission item = obj as Permission;
|
|
|
|
|
if (item == null)
|
|
|
|
|
return false;
|
|
|
|
|
return ToString().Equals(item.ToString());
|
|
|
|
|
}
|
|
|
|
|
public static bool operator ==(Permission a, Permission b)
|
|
|
|
|
{
|
|
|
|
|
if (System.Object.ReferenceEquals(a, b))
|
|
|
|
|
return true;
|
|
|
|
|
if (((object)a == null) || ((object)b == null))
|
|
|
|
|
return false;
|
|
|
|
|
return a.ToString() == b.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool operator !=(Permission a, Permission b)
|
|
|
|
|
{
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return ToString().GetHashCode();
|
|
|
|
|
}
|
2020-03-02 16:50:28 +01:00
|
|
|
|
}
|
|
|
|
|
}
|