/* ======================================================================== * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Xml; namespace Opc.Ua.Server { /// /// The well known roles in a server. /// https://reference.opcfoundation.org/Core/Part3/v105/docs/4.9.2 /// [DataContract(Namespace = Namespaces.Roles)] public class Role : IEquatable { /// /// The Role is allowed to browse and read non-security related Nodes only in the Server Object and all type Nodes. /// public static Role Anonymous { get; } = new Role(ObjectIds.WellKnownRole_Anonymous, BrowseNames.WellKnownRole_Anonymous); /// /// The Role is allowed to browse and read non-security related Nodes. /// public static Role AuthenticatedUser { get; } = new Role(ObjectIds.WellKnownRole_AuthenticatedUser, BrowseNames.WellKnownRole_AuthenticatedUser); /// /// The Role is allowed to browse, read live data, read historical data/events or subscribe to data/events. /// public static Role Observer { get; } = new Role(ObjectIds.WellKnownRole_Observer, BrowseNames.WellKnownRole_Observer); /// /// The Role is allowed to browse, read live data, read historical data/events or subscribe to data/events. /// In addition, the Session is allowed to write some live data and call some Methods. /// public static Role Operator { get; } = new Role(ObjectIds.WellKnownRole_Operator, BrowseNames.WellKnownRole_Operator); /// /// The Role is allowed to browse, read/write configuration data, read historical data/events, call Methods or subscribe to data/events. /// public static Role Engineer { get; } = new Role(ObjectIds.WellKnownRole_Engineer, BrowseNames.WellKnownRole_Engineer); /// /// The Role is allowed to browse, read live data, read historical data/events, call Methods or subscribe to data/events. /// public static Role Supervisor { get; } = new Role(ObjectIds.WellKnownRole_Supervisor, BrowseNames.WellKnownRole_Supervisor); /// /// The Role is allowed to change the non-security related configuration settings. /// public static Role ConfigureAdmin { get; } = new Role(ObjectIds.WellKnownRole_ConfigureAdmin, BrowseNames.WellKnownRole_ConfigureAdmin); /// /// The Role is allowed to change security related settings. /// public static Role SecurityAdmin { get; } = new Role(ObjectIds.WellKnownRole_SecurityAdmin, BrowseNames.WellKnownRole_SecurityAdmin); /// /// The Role is always assigned when a Session has been authenticated with a trusted /// ApplicationInstance Certificate and uses at least a signed communication channel. /// https://reference.opcfoundation.org/Core/Part3/v105/docs/4.9 /// public static Role TrustedApplication { get; } = new Role(ObjectIds.WellKnownRole_TrustedApplication, BrowseNames.WellKnownRole_TrustedApplication); /// /// Constructor for new Role /// /// NodeId of the Role, used for WellKnownRoles /// Name of the Role public Role(ExpandedNodeId roleId, string name) { RoleId = roleId; Name = name; } /// /// The name of the role. /// [DataMember(Name = "Name", IsRequired = true, Order = 10)] public string Name { get; private set; } /// /// The NodeId of the role. /// [DataMember(Name = "RoleId", IsRequired = true, Order = 20)] public ExpandedNodeId RoleId { get; set; } /// public bool Equals(Role? other) { if (other is null) { return false; } if (ReferenceEquals(this, other)) { return true; } if (GetType() != other.GetType()) { return false; } return (Name == other.Name) && (RoleId == other.RoleId); } /// public override bool Equals(object? obj) { return Equals(obj! as Role); } /// public override int GetHashCode() { return HashCode.Combine(Name, RoleId); } /// public static bool operator ==(Role lhs, Role rhs) { if (lhs is null) { if (rhs is null) { return true; } // Only the left side is null. return false; } // Equals handles case of null on right side. return lhs.Equals(rhs); } /// public static bool operator !=(Role lhs, Role rhs) { return !(lhs == rhs); } /// /// Returns the name of the role. /// public override string ToString() { return Name; } } /// /// The role based identity for a server. /// public class RoleBasedIdentity : IUserIdentity { /// /// Initialize the role based identity. /// public RoleBasedIdentity(IUserIdentity identity, IEnumerable roles, NamespaceTable namespaces) { InnerIdentity = identity; Roles = roles; if (identity is RoleBasedIdentity roleBasedIdentity) { Roles = roleBasedIdentity.Roles.Concat(roles); } GrantedRoleIds = identity.GrantedRoleIds .AddItems(roles .Where(role => role != null!) .Select(role => ExpandedNodeId.ToNodeId(role.RoleId, namespaces)) .Where(roleID => !identity.GrantedRoleIds.Contains(roleID)) .ToArrayOf()); } /// public ArrayOf GrantedRoleIds { get; } /// /// The role in the context of a server. /// public IEnumerable Roles { get; } /// /// Returns a new that extends this identity with /// merged in. Subclasses should override this /// method to preserve their concrete type (and any extra state such as /// ApplicationId) when extra roles are layered on top. /// /// Roles to add on top of the existing ones. /// Namespace table used to resolve role NodeIds. /// A new identity that carries the union of the current and additional roles. public virtual RoleBasedIdentity WithAdditionalRoles( IEnumerable additionalRoles, NamespaceTable namespaces) { return new RoleBasedIdentity(InnerIdentity, Roles.Concat(additionalRoles), namespaces); } /// /// The inner identity that this role-based identity wraps. /// protected IUserIdentity InnerIdentity { get; } /// public string DisplayName => InnerIdentity.DisplayName; /// public string PolicyId => InnerIdentity.PolicyId; /// public UserTokenType TokenType => InnerIdentity.TokenType; /// public XmlQualifiedName IssuedTokenType => InnerIdentity.IssuedTokenType; /// public bool SupportsSignatures => InnerIdentity.SupportsSignatures; /// public IUserIdentityTokenHandler TokenHandler => InnerIdentity.TokenHandler; } }