Files
Marc Schier 0000513cde Pluggable user-identity provider model (#3807)
# 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.
2026-06-02 01:14:54 +02:00

484 lines
22 KiB
C#

/* ========================================================================
* 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.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Opc.Ua.Client.TestFramework;
namespace Opc.Ua.InformationModel.Tests
{
/// <summary>
/// compliance tests for Attribute Write Values.
/// </summary>
[TestFixture]
[Category("Conformance")]
[Category("AttributeServices")]
public class AttributeWriteValuesTests : TestFixture
{
[Description("Write to the Value attribute of a Variable, where the AccessLevel == CurrentWriteService.*/")]
[Test]
public async Task WriteValueWithCurrentWriteAccessSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write the minimum value for each supported data-type. Some items may fail with BadOutOfRange. */")]
[Test]
public async Task WriteMinimumValueForSupportedDataTypesSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write the MAXIMUM value for each supported data-type. */")]
[Test]
public async Task WriteMaximumValueForSupportedDataTypesSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write to a localizedText passing in all params, no params, and some params. */")]
[Test]
public async Task WriteLocalizedTextWithVariousParametersSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("write to a node of type INTEGER where the data-type of the value is specified as SByte, Int16, Int32, and Int64; where the value is: - Data-type max - Data-type min */")]
[Test]
public async Task WriteIntegerWithSignedDataTypeVariantsSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("write to a node of type UINTEGER where the data-type of the value is specified as Byte, UInt16, UInt32, and UInt64; where the value is: - Data-type max - Data-type min */")]
[Test]
public async Task WriteUIntegerWithUnsignedDataTypeVariantsSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write() a value of NaN to all configured floating point numbers (Float, Double &amp; Duration). */")]
[Test]
public async Task WriteNaNToFloatingPointTypesSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write to a string variable; specify a value within the extended code-page")]
[Test]
public async Task WriteStringWithExtendedCodePageSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write to the Value attribute (without statusCode, sourceTimestamp, or serverTimestamp) of several nodes that has a ValueRank of array. Write the entire array. This test should be d")]
[Test]
public async Task WriteEntireArrayValueSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write to the Value attribute (without statusCode, sourceTimestamp, or serverTimestamp) of nodes which have a ValueRank of a multi-dimensional array. Write the entire multi-dimensio")]
[Test]
public async Task WriteEntireMultiDimensionalArraySucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("Write to the Value attribute (without statusCode, sourceTimestamp, or serverTimestamp) of several nodes which have aValueRank of a multi-dimensional array. Write the entire multi-d")]
[Test]
public async Task WriteEntireMultiDimensionalArrayToMultipleNodesSucceedsAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsGood(response.Results[0]), Is.True);
}
[Description("nodesToWrite array empty; expected service result = BadNothingToDo. */")]
[Test]
public async Task WriteEmptyArrayReturnsBadNothingToDoAsync()
{
ArrayOf<WriteValue> wv = Array.Empty<WriteValue>().ToArrayOf();
WriteResponse response;
try
{
response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
}
catch (ServiceResultException sre)
{
Assert.That(sre.StatusCode, Is.EqualTo(StatusCodes.BadNothingToDo));
return;
}
// BadNothingToDo may be reported as the service result or per-operation result.
StatusCode serviceResult = response.ResponseHeader.ServiceResult;
if (serviceResult != StatusCodes.Good)
{
Assert.That((uint)serviceResult, Is.EqualTo((uint)StatusCodes.BadNothingToDo));
Assert.That(response.Results, Is.Null.Or.Empty);
}
else
{
Assert.That(response.Results, Is.Not.Null);
Assert.That(response.Results.Count, Is.Zero);
}
}
[Description("Write to valid attributes of multiple unknown nodes, in a single call. */")]
[Test]
public async Task WriteToMultipleUnknownNodesReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = new NodeId("NonExistent_InvalidNode_1", 2),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(1))
},
new() {
NodeId = new NodeId("NonExistent_InvalidNode_2", 2),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(2))
},
new() {
NodeId = new NodeId("NonExistent_InvalidNode_3", 2),
AttributeId = Attributes.DisplayName,
Value = new DataValue(new Variant(new LocalizedText("en", "Test3")))
},
new() {
NodeId = new NodeId("NonExistent_InvalidNode_4", 2),
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(4))
},
new() {
NodeId = new NodeId(Guid.NewGuid(), 2),
AttributeId = Attributes.DisplayName,
Value = new DataValue(new Variant(new LocalizedText("en", "Test5")))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(StatusCode.IsGood(response.ResponseHeader.ServiceResult), Is.True);
Assert.That(response.Results.Count, Is.EqualTo(wv.Count));
for (int i = 0; i < response.Results.Count; i++)
{
Assert.That(StatusCode.IsBad(response.Results[i]), Is.True,
$"Expected Bad status for unknown node at index {i}, got {response.Results[i]}");
}
}
[Description("Write to an invalid (non-writable) attribute of a valid node. */")]
[Test]
public async Task WriteToNonWritableAttributeReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = ToNodeId(Constants.ScalarStaticInt32),
AttributeId = Attributes.NodeClass,
Value = new DataValue(new Variant((int)NodeClass.Variable))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(StatusCode.IsGood(response.ResponseHeader.ServiceResult), Is.True);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True,
$"Expected Bad status when writing non-writable attribute, got {response.Results[0]}");
uint code = response.Results[0].Code;
Assert.That(code,
Is.EqualTo(StatusCodes.BadNotWritable)
.Or.EqualTo(StatusCodes.BadAttributeIdInvalid)
.Or.EqualTo(StatusCodes.BadWriteNotSupported)
.Or.EqualTo(StatusCodes.BadUserAccessDenied),
$"Unexpected status code 0x{code:X8}");
}
[Description("Write to invalid (non-writable) attributes of a valid node, multiple times in the same call. */")]
[Test]
public async Task WriteToMultipleNonWritableAttributesReturnsBadStatusAsync()
{
NodeId target = ToNodeId(Constants.ScalarStaticInt32);
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = target,
AttributeId = Attributes.NodeClass,
Value = new DataValue(new Variant((int)NodeClass.Variable))
},
new() {
NodeId = target,
AttributeId = Attributes.BrowseName,
Value = new DataValue(new Variant(new QualifiedName("Renamed", 2)))
},
new() {
NodeId = target,
AttributeId = Attributes.NodeId,
Value = new DataValue(new Variant(new NodeId(99u, 2)))
},
new() {
NodeId = target,
AttributeId = Attributes.DataType,
Value = new DataValue(new Variant(DataTypeIds.String))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(StatusCode.IsGood(response.ResponseHeader.ServiceResult), Is.True);
Assert.That(response.Results.Count, Is.EqualTo(wv.Count));
for (int i = 0; i < response.Results.Count; i++)
{
Assert.That(StatusCode.IsBad(response.Results[i]), Is.True,
$"Expected Bad status at index {i}, got {response.Results[i]}");
}
}
[Description("Write to a node whose AccessLevel does not contain write capabilities. */")]
[Test]
public async Task WriteToReadOnlyNodeReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = VariableIds.Server_ServerStatus_State,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant((int)ServerState.Running))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(StatusCode.IsGood(response.ResponseHeader.ServiceResult), Is.True);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True,
$"Expected Bad status when writing read-only node, got {response.Results[0]}");
uint code = response.Results[0].Code;
Assert.That(code,
Is.EqualTo(StatusCodes.BadNotWritable)
.Or.EqualTo(StatusCodes.BadUserAccessDenied)
.Or.EqualTo(StatusCodes.BadWriteNotSupported),
$"Unexpected status code 0x{code:X8}");
}
[Description("Write a NULL value for each supported data-type. Expect a Bad_TypeMismatch for each operation level result. */")]
[Test]
public async Task WriteNullValueReturnsBadTypeMismatchAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = Constants.InvalidNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True);
}
[Description("write to a node of type UINTEGER where the data-type of the value is specified as SByte, Int16, Int32, and Int64 */")]
[Test]
public async Task WriteSignedTypeToUIntegerNodeReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = Constants.InvalidNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True);
}
[Description("write to a node of type INTEGER where the data-type of the value is specified as SByte, UInt16, UInt32, and UInt64 */")]
[Test]
public async Task WriteUnsignedTypeToIntegerNodeReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = Constants.InvalidNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True);
}
[Description("Write to a node of type INTEGER where the data-type of the value is specified as float. */")]
[Test]
public async Task WriteFloatToIntegerNodeReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = Constants.InvalidNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True);
}
[Description("Write to a LocalizedText with a valid value, but specify a localeId that is known to not be supported, e g. aardvark. */")]
[Test]
public async Task WriteLocalizedTextWithUnsupportedLocaleReturnsBadStatusAsync()
{
ArrayOf<WriteValue> wv = new WriteValue[]
{
new() {
NodeId = Constants.InvalidNodeId,
AttributeId = Attributes.Value,
Value = new DataValue(new Variant(42))
}
}.ToArrayOf();
WriteResponse response = await Session.WriteAsync(null, wv, CancellationToken.None).ConfigureAwait(false);
Assert.That(response.Results.Count, Is.EqualTo(1));
Assert.That(StatusCode.IsBad(response.Results[0]), Is.True);
}
}
}