mirror of
https://github.com/OPCFoundation/UA-.NETStandard.git
synced 2026-10-18 22:55:08 +08:00
0000513cde
# Description Introduces a pluggable user-identity provider model covering all OPC UA Part 6 §6.5 mechanisms (Anonymous, UserName, X509, IssuedToken/JWT) on both client and server, with full `Microsoft.Extensions.DependencyInjection` integration, plus the Part 12 §9 `AuthorizationService` two-phase token-request flow **including refresh-token rotation**, the Part 12 §8 KeyCredential Push model, and a clean provider model for OPC 10000-12 §7.2 `ApplicationSelfAdmin`. The work is split across phase-by-phase commits so it can be reviewed independently; nothing here changes wire-level behaviour or forces existing applications to migrate. ### Documentation All under `Docs/`: - `IdentityProviders.md` — full developer guide: three-layer model, recipes, configuration reference, "what gets auto-wired" matrix, JWS algorithm table, identity-augmenters section, **"Implementing your own provider"** how-to subsections for Entra / OIDC / Windows / AspNetCore (sibling files merged in per review feedback). - `DependencyInjection.md` — `Identity` sections with `appsettings.json` examples, identity-extension matrix, `WithAuthorizationService` + `WithKeyCredentialPush` + `AddIdentityAugmenter` extensions documented. - `RoleBasedUserManagement.md` — claim-criteria callout describing `IIdentityClaims` probe surface. - `AuthorizationService.md` — Architecture diagram + `IAccessTokenProvider` conceptual narrative restored from master; modern `StartRequestToken` / `FinishRequestToken` flow leads; refresh-token recipe added. - `KeyCredentialService.md` — Push model + Pull vs Push vs Bridge decision matrix + Hybrid (Push + Pull) section; explicit experimental warning for the bridge. - `GDS.md` — KeyCredential Push, `StartRequestToken`, `GdsApplicationSelfAdminProvider`, **shipped Refresh-token support with rotation policy and audit semantics** all documented. - `MigrationGuide.md` — User Identity Providers section listing `[Obsolete]` markers + mechanical recipes; SelfAdmin migration via `IIdentityAugmenter`; `EnableRefreshTokens` default note. - `README.md` index entries. ## Checklist - [x] I have signed the [CLA](https://opcfoundation.org/license/cla/ContributorLicenseAgreementv1.0.pdf) and read the [CONTRIBUTING](https://github.com/OPCFoundation/UA-.NETStandard/blob/master/CONTRIBUTING.md) doc. - [x] I have added tests that prove my fix is effective or that my feature works and increased code coverage. - [x] I have added all necessary documentation. - [x] I have verified that my changes do not introduce (new) build or analyzer warnings. - [x] I ran **all** tests locally using the **UA.slnx** solution against at least .net **framework** and .net **10**, and all passed. - [x] I fixed **all** failing and flaky tests in the CI pipelines and **all** CodeQL warnings. - [x] I have addressed **all** PR feedback received.
163 lines
7.5 KiB
C#
163 lines
7.5 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using NUnit.Framework;
|
|
using Opc.Ua.Server.TestFramework;
|
|
using Quickstarts.ReferenceServer;
|
|
|
|
namespace Opc.Ua.Server.Tests
|
|
{
|
|
[TestFixture]
|
|
[Category("ConfigurationNodeManager")]
|
|
[Parallelizable(ParallelScope.None)]
|
|
public class ConfigurationNodeManagerTests
|
|
{
|
|
[Test]
|
|
public async Task ConfigurationNodeManager_NamespaceMetadata_LifecycleAsync()
|
|
{
|
|
// Arrange
|
|
var fixture = new ServerFixture<StandardServer>(t => new ReferenceServer(t));
|
|
StandardServer server = await fixture.StartAsync().ConfigureAwait(false);
|
|
|
|
// Get ConfigurationNodeManager
|
|
IServerInternal serverInternal = server.CurrentInstance;
|
|
var configManager = serverInternal.ConfigurationNodeManager as ConfigurationNodeManager;
|
|
Assert.That(configManager, Is.Not.Null, "ConfigurationNodeManager not found or not of expected type");
|
|
|
|
const string namespaceUri = "http://test.org/UA/NamespaceMetadataTest";
|
|
|
|
// Act 1: CreateNamespaceMetadataState
|
|
NamespaceMetadataState metadata = await configManager.CreateNamespaceMetadataStateAsync(namespaceUri).ConfigureAwait(false);
|
|
Assert.That(metadata, Is.Not.Null);
|
|
Assert.That(metadata.NamespaceUri.Value, Is.EqualTo(namespaceUri));
|
|
|
|
// Act 2: GetNamespaceMetadataState
|
|
NamespaceMetadataState metadataGet = await configManager.GetNamespaceMetadataStateAsync(namespaceUri).ConfigureAwait(false);
|
|
Assert.That(metadataGet, Is.SameAs(metadata));
|
|
|
|
// Act 3: Verify Event Subscription
|
|
bool eventRaised = false;
|
|
configManager.DefaultPermissionsChanged += (s, e) => eventRaised = true;
|
|
|
|
// Trigger change
|
|
// Use array initialization
|
|
metadata.DefaultRolePermissions.Value =
|
|
[
|
|
new RolePermissionType { RoleId = ObjectIds.WellKnownRole_Observer, Permissions = (uint)PermissionType.Read }
|
|
];
|
|
metadata.ClearChangeMasks(serverInternal.DefaultSystemContext, true);
|
|
|
|
Assert.That(eventRaised, Is.True, "DefaultPermissionsChanged event should be raised when DefaultRolePermissions changes");
|
|
|
|
// Act 4: Dispose Behavior
|
|
eventRaised = false;
|
|
|
|
// We call Dispose() on the manager directly to verify it unsubscribes.
|
|
configManager.Dispose();
|
|
|
|
// Trigger change again
|
|
metadata.DefaultRolePermissions.Value =
|
|
[
|
|
new() { RoleId = ObjectIds.WellKnownRole_Observer, Permissions = (uint)PermissionType.Read | (uint)PermissionType.Browse }
|
|
];
|
|
|
|
metadata.ClearChangeMasks(serverInternal.DefaultSystemContext, true);
|
|
|
|
Assert.That(eventRaised, Is.False, "DefaultPermissionsChanged event should NOT be raised after Dispose");
|
|
|
|
// Cleanup
|
|
await fixture.StopAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ConfigurationNodeManager_ServerNamespaces_Changed_TriggersSubscriptionAsync()
|
|
{
|
|
// Arrange
|
|
var fixture = new ServerFixture<StandardServer>(t => new ReferenceServer(t));
|
|
StandardServer server = await fixture.StartAsync().ConfigureAwait(false);
|
|
|
|
// Get ConfigurationNodeManager
|
|
IServerInternal serverInternal = server.CurrentInstance;
|
|
var configManager = serverInternal.ConfigurationNodeManager as ConfigurationNodeManager;
|
|
Assert.That(configManager, Is.Not.Null, "ConfigurationNodeManager not found");
|
|
|
|
// Find ServerNamespaces node
|
|
// Note: We need to access the node directly to modify it.
|
|
// Since we are in the same process and using StandardServer, we can try to find it via NodeManager.
|
|
// StandardServer.NodeManager is IMasterNodeManager which has FindNodeInAddressSpaceAsync.
|
|
NodeState result = await serverInternal.NodeManager
|
|
.FindNodeInAddressSpaceAsync(ObjectIds.Server_Namespaces)
|
|
.ConfigureAwait(false);
|
|
var serverNamespacesNode = result as NamespacesState;
|
|
Assert.That(serverNamespacesNode, Is.Not.Null, "ServerNamespaces node not found in address space");
|
|
|
|
bool eventRaised = false;
|
|
configManager.DefaultPermissionsChanged += (s, e) => eventRaised = true;
|
|
|
|
const string manualNamespaceUri = "http://manual.org/UA/Test";
|
|
|
|
// Act: Manually add a new metadata node to ServerNamespaces
|
|
// This bypasses CreateNamespaceMetadataState to ensure the event handler picks it up
|
|
var manualMetadata = new NamespaceMetadataState(serverNamespacesNode)
|
|
{
|
|
// Assign a NodeId
|
|
NodeId = new NodeId(Guid.NewGuid(), 1),
|
|
BrowseName = new QualifiedName("ManualMetadata", 1)
|
|
};
|
|
// NamespaceUri property is required for it to be useful, though strictly not for the subscription
|
|
manualMetadata.NamespaceUri = new PropertyState<string>.Implementation<VariantBuilder>(manualMetadata)
|
|
{
|
|
Value = manualNamespaceUri
|
|
};
|
|
|
|
// Create DefaultRolePermissions property
|
|
manualMetadata.DefaultRolePermissions = new PropertyState<ArrayOf<RolePermissionType>>
|
|
.Implementation<StructureBuilder<RolePermissionType>>(manualMetadata)
|
|
{
|
|
NodeId = new NodeId(Guid.NewGuid(), 1),
|
|
BrowseName = new QualifiedName(BrowseNames.DefaultRolePermissions, manualMetadata.NodeId.NamespaceIndex),
|
|
Value = [] // Empty initially
|
|
};
|
|
manualMetadata.AddChild(manualMetadata.DefaultRolePermissions);
|
|
|
|
// Add to ServerNamespaces
|
|
serverNamespacesNode.AddChild(manualMetadata);
|
|
|
|
// Trigger StateChanged on ServerNamespaces (simulating dynamic addition)
|
|
// This is crucial because AddChild alone might not trigger the event depending on implementation
|
|
serverNamespacesNode.ClearChangeMasks(serverInternal.DefaultSystemContext, true);
|
|
|
|
// Verify subscription is active by modifying the protected property
|
|
// We expect the event to fire
|
|
|
|
manualMetadata.DefaultRolePermissions.Value =
|
|
[
|
|
new RolePermissionType { RoleId = ObjectIds.WellKnownRole_Observer, Permissions = (uint)PermissionType.Read }
|
|
];
|
|
|
|
manualMetadata.ClearChangeMasks(serverInternal.DefaultSystemContext, true);
|
|
|
|
Assert.That(eventRaised, Is.True, "DefaultPermissionsChanged event should be raised for manually added metadata node");
|
|
|
|
// Cleanup
|
|
await fixture.StopAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
[Test]
|
|
public async Task ConfigurationNodeManager_NoUserManagement_RemovesUserManagementNodeAsync()
|
|
{
|
|
var fixture = new ServerFixture<StandardServer>(t => new StandardServer(t));
|
|
StandardServer server = await fixture.StartAsync().ConfigureAwait(false);
|
|
|
|
IServerInternal serverInternal = server.CurrentInstance;
|
|
Assert.That(serverInternal.UserManagement, Is.Null);
|
|
|
|
NodeState userManagementNode = await serverInternal.NodeManager
|
|
.FindNodeInAddressSpaceAsync(ObjectIds.UserManagement)
|
|
.ConfigureAwait(false);
|
|
Assert.That(userManagementNode, Is.Null);
|
|
|
|
await fixture.StopAsync().ConfigureAwait(false);
|
|
}
|
|
}
|
|
}
|