33 lines
1.5 KiB
C#
33 lines
1.5 KiB
C#
using Fleet.Core;
|
|
using Fleet.Identity;
|
|
|
|
namespace MoShiro.Backend.Application;
|
|
|
|
/// <summary>
|
|
/// 节点的属主:创建加入码时选定的 passkey 凭证 id,写进安装命令,节点注册时固定在本地。
|
|
/// 属主必须是操作员集合里的凭证;<see cref="BackendOptions.RequireOwner"/> 打开时(默认)必须已经有信任根,并且至少选一把——
|
|
/// 不选属主的节点没有人能授权它的敏感操作,也就不该存在。没有明确指定时默认是操作员集合里的全部 passkey。
|
|
/// </summary>
|
|
public static class OwnerPolicy
|
|
{
|
|
public static Result<string[]> Resolve(bool required, OperatorSet? set, IReadOnlyList<string>? requested)
|
|
{
|
|
if (set is null)
|
|
{
|
|
return required
|
|
? ValidationError.Fail<string[]>("controllers", "set up the trust root first (System, Trust): every node needs an owner passkey")
|
|
: Result<string[]>.Ok([]);
|
|
}
|
|
|
|
string[] chosen = requested is null ? [.. set.Keys.Select(k => k.CredentialId)] : [.. requested.Distinct(StringComparer.Ordinal)];
|
|
if (chosen.Any(c => set.Keys.All(k => k.CredentialId != c)))
|
|
{
|
|
return ValidationError.Fail<string[]>("controllers", "must be passkeys of the operator set");
|
|
}
|
|
|
|
return required && chosen.Length == 0
|
|
? ValidationError.Fail<string[]>("controllers", "pick at least one owner passkey: a node's owner is who its sensitive sessions and data are bound to")
|
|
: Result<string[]>.Ok(chosen);
|
|
}
|
|
}
|