using Microsoft.AspNetCore.Authorization;
using ZB.MOM.WW.Auth.AspNetCore;
namespace ZB.MOM.WW.Secrets.Ui;
///
/// Authorization policies and the roles that back them for the secrets-management UI.
///
///
/// Two policies gate the UI, layered by privilege:
///
/// -
///
/// — view and administer secret metadata (the list
/// page, create/rename/delete affordances). Satisfied by ,
/// , or .
///
///
/// -
///
/// — the strictly more privileged right to reveal a secret's
/// plaintext value. Satisfied only by or .
/// Because both reveal-granting roles also satisfy , holding
/// reveal always implies manage.
///
///
///
/// Both policies require an authenticated user. The requirements are expressed as role checks
/// against the canonical claim, so the host maps its LDAP
/// groups to these role names (via its IGroupRoleMapper / Security:Ldap:GroupToRole
/// configuration) exactly as it does for every other ZB.MOM.WW role. The role-name
/// constants are deliberately minimal and can be overridden by the host by registering its own
/// policies under the same policy names instead of calling .
///
public static class SecretsAuthorization
{
/// Policy name for administering secret metadata (the list page and management actions).
public const string ManagePolicy = "secrets:manage";
/// Policy name for revealing a secret's plaintext value — strictly more privileged than .
public const string RevealPolicy = "secrets:reveal";
/// Role that grants secret metadata management (satisfies ).
public const string ManageRole = "secrets-manager";
/// Role that grants secret value reveal (satisfies both and ).
public const string RevealRole = "secrets-reveal";
/// Administrator role that satisfies every secrets policy.
public const string AdminRole = "administrator";
///
/// Registers the and policies on the
/// supplied . Each policy requires an authenticated user;
/// is a strict superset-privilege of .
///
/// The authorization options to add the policies to.
/// The same instance, to allow chaining.
/// is null.
public static AuthorizationOptions AddSecretsAuthorization(this AuthorizationOptions options)
{
ArgumentNullException.ThrowIfNull(options);
options.AddPolicy(ManagePolicy, policy => policy
.RequireAuthenticatedUser()
.RequireRole(ManageRole, RevealRole, AdminRole));
options.AddPolicy(RevealPolicy, policy => policy
.RequireAuthenticatedUser()
.RequireRole(RevealRole, AdminRole));
return options;
}
}