93 lines
4.9 KiB
C#
93 lines
4.9 KiB
C#
using Opc.Ua;
|
|
using Opc.Ua.Server;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
|
|
|
|
/// <summary>
|
|
/// An SDK identity that additionally carries the application roles resolved for an API key
|
|
/// (<see cref="OpcUaUserAuthResult.Roles"/>) for the authenticated session. The SDK may augment identities with mandatory and dynamically mapped roles. This subtype
|
|
/// preserves the application identity when those roles are added, so a method
|
|
/// handler can later recover the roles via
|
|
/// <c>(context as ISessionOperationContext)?.UserIdentity as RoleCarryingUserIdentity</c> — the
|
|
/// seam the inbound alarm ack/shelve <c>AlarmAck</c> gate (T18) reads. This type only carries the
|
|
/// roles; it makes no auth decision.
|
|
/// </summary>
|
|
public sealed class RoleCarryingUserIdentity : RoleBasedIdentity
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance, building the base identity from the same UserName token the
|
|
/// SDK decrypted and attaching the authenticator's resolved roles.
|
|
/// </summary>
|
|
/// <param name="token">The user identity token the SDK handed to the impersonation handler.</param>
|
|
/// <param name="roles">The roles resolved by the authenticator (e.g. <c>ReadOnly</c>, <c>AlarmAck</c>).</param>
|
|
public RoleCarryingUserIdentity(UserIdentityToken token, IReadOnlyList<string> roles)
|
|
: this(token, roles, principal: string.Empty, apiKeyId: null)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance carrying the ACL principal and the authenticating key id alongside
|
|
/// the roles.
|
|
/// </summary>
|
|
/// <param name="token">The user identity token the SDK handed to the impersonation handler.</param>
|
|
/// <param name="roles">The roles resolved by the authenticator.</param>
|
|
/// <param name="principal">The ACL principal this session authenticates as.</param>
|
|
/// <param name="apiKeyId">The public id of the key that authenticated, or null for anonymous.</param>
|
|
public RoleCarryingUserIdentity(
|
|
UserIdentityToken token, IReadOnlyList<string> roles, string principal, string? apiKeyId,
|
|
string? credentialVersion = null, DateTimeOffset? credentialExpiresUtc = null)
|
|
: this(new UserIdentity(token), roles, principal, apiKeyId, credentialVersion, credentialExpiresUtc, [], new NamespaceTable())
|
|
{
|
|
}
|
|
|
|
private RoleCarryingUserIdentity(
|
|
IUserIdentity identity, IReadOnlyList<string> roles, string principal, string? apiKeyId,
|
|
string? credentialVersion, DateTimeOffset? credentialExpiresUtc, IEnumerable<Role> sdkRoles, NamespaceTable namespaces)
|
|
: base(identity, sdkRoles, namespaces)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(roles);
|
|
// Freeze to a defensive copy so a caller mutating the source list can never retroactively
|
|
// alter this session's granted roles, and so the gate's per-ack `.Contains(...)` runs over a
|
|
// known-small frozen array.
|
|
Roles = [.. roles];
|
|
Principal = principal ?? string.Empty;
|
|
ApiKeyId = apiKeyId;
|
|
CredentialVersion = credentialVersion;
|
|
CredentialExpiresUtc = credentialExpiresUtc;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override RoleBasedIdentity WithAdditionalRoles(IEnumerable<Role> additionalRoles, NamespaceTable namespaces) =>
|
|
new RoleCarryingUserIdentity(InnerIdentity, Roles, Principal, ApiKeyId, CredentialVersion, CredentialExpiresUtc,
|
|
base.Roles.Concat(additionalRoles), namespaces);
|
|
|
|
/// <summary>The roles the authenticator granted this session, used by downstream permission gates.</summary>
|
|
public new IReadOnlyList<string> Roles { get; }
|
|
|
|
/// <summary>
|
|
/// The ACL principal this session authenticates as — the name <c>NodeAcl</c> rows key on, and what
|
|
/// the permission trie matches. Empty when the authenticator predates the principal model, which
|
|
/// the ACL gate treats as unauthorized.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// ⚠️ <b>This is NOT <see cref="UserIdentity.DisplayName"/>.</b> The base class derives that from
|
|
/// the presented token, so for a key-bearing client it is the static username <c>apikey</c> —
|
|
/// identical for every key in the fleet and useless as an identity. See
|
|
/// <see cref="OpcUaUserAuthResult.Principal"/>.
|
|
/// </remarks>
|
|
public string Principal { get; }
|
|
|
|
/// <summary>
|
|
/// Public id of the API key that authenticated this session, or null for anonymous. Lets an open
|
|
/// session be re-checked against a later deployment that revoked the key, instead of the key
|
|
/// remaining usable until the client happens to disconnect.
|
|
/// </summary>
|
|
public string? ApiKeyId { get; }
|
|
|
|
/// <summary>Authenticated credential version captured at activation, never the secret.</summary>
|
|
public string? CredentialVersion { get; }
|
|
|
|
/// <summary>Original credential expiry captured at activation.</summary>
|
|
public DateTimeOffset? CredentialExpiresUtc { get; }
|
|
}
|