From 696f02934936a8640f598430743b5ea1af409247 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Tue, 24 Nov 2026 09:18:07 -0500 Subject: [PATCH] =?UTF-8?q?feat(grpc-data):=20A1=20=E2=80=94=20ImportedCon?= =?UTF-8?q?ditionIds:=20c1.=20condition=20identifier,=20child=20ids,=20map?= =?UTF-8?q?ped/conditions=20namespace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataConnections/ImportedConditionIds.cs | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/ImportedConditionIds.cs diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/ImportedConditionIds.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/ImportedConditionIds.cs new file mode 100644 index 000000000..4bd41f5fd --- /dev/null +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/DataConnections/ImportedConditionIds.cs @@ -0,0 +1,131 @@ +using System.Text.Json; + +namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; + +/// +/// A1 (§5.2 "Imported condition NodeId", N4): the consumer-local identifier of an imported condition, in a third mapped +/// namespace beside / . +/// +/// The identifier is keyed on (DataConnectionId, condition_key) only — never the mapping, the home placement +/// or stream order — so disabling, adding or re-ordering mappings never changes it. It is +/// c1. + base64url(JSON{ConnectionId, ConditionKey}); a condition child (#629) is +/// <identifier>/<browse path>, split at the FIRST / (base64url has none). +/// +/// +/// ⚠️ This namespace is NOT one of 's and has no +/// : anything that classifies a NodeId (authorizer, history, read, call) must dispatch +/// BEFORE folding a namespace into a realm (the node manager's RealmOf folds +/// every index other than 0/2 into Uns). Decoding never establishes authorization. +/// +/// +public static class ImportedConditionIds +{ + /// The imported-condition namespace (registered by the consumer beside the two mapped namespaces). + public const string NamespaceUri = "https://zb.com/otopcua/mapped/conditions"; + + /// Separates a condition identifier from a child browse path. + public const char ChildSeparator = '/'; + + /// A well-formed condition_key is base64url (unpadded) of a 32-byte HMAC-SHA256: 43 characters. + public const int ConditionKeyLength = 43; + + /// Upper bound of a connection id inside the identifier (the ConfigDb key is far shorter). + public const int MaximumConnectionIdLength = 256; + + /// Upper bound of a whole identifier, child path included. + public const int MaximumIdentifierLength = 4_096; + + private const string Prefix = "c1."; + private sealed record Identity(string ConnectionId, string ConditionKey); + + public static bool IsConditionNamespace(string? namespaceUri) => + string.Equals(namespaceUri, NamespaceUri, StringComparison.Ordinal); + + /// True for an opaque key of the shape the origin mints (43 base64url characters decoding to 32 bytes). + public static bool IsWellFormedConditionKey(string? conditionKey) + { + if (conditionKey is null || conditionKey.Length != ConditionKeyLength || !IsBase64Url(conditionKey)) return false; + // 43 chars carry 258 bits; the last character's low 2 bits must be zero for a canonical 32-byte encoding. + return (Base64UrlValue(conditionKey[^1]) & 0b11) == 0; + } + + /// The condition identifier for and an origin . + public static string Identifier(string connectionId, string conditionKey) + { + ArgumentException.ThrowIfNullOrWhiteSpace(connectionId); + if (connectionId.Length > MaximumConnectionIdLength || connectionId.Any(char.IsControl)) + throw new ArgumentOutOfRangeException(nameof(connectionId)); + if (!IsWellFormedConditionKey(conditionKey)) + throw new ArgumentException("A condition key is 43 base64url characters.", nameof(conditionKey)); + var payload = JsonSerializer.SerializeToUtf8Bytes(new Identity(connectionId, conditionKey)); + return Prefix + Convert.ToBase64String(payload).TrimEnd('=').Replace('+', '-').Replace('/', '_'); + } + + /// A child (#629) of a condition: <identifier>/<browse path>. + public static string ChildIdentifier(string conditionIdentifier, string browsePath) + { + if (!TryDecodeIdentifier(conditionIdentifier, out _, out _)) + throw new ArgumentException("Invalid imported condition identifier.", nameof(conditionIdentifier)); + if (!IsValidBrowsePath(browsePath)) throw new ArgumentException("Invalid child browse path.", nameof(browsePath)); + var child = conditionIdentifier + ChildSeparator + browsePath; + if (child.Length > MaximumIdentifierLength) throw new ArgumentOutOfRangeException(nameof(browsePath)); + return child; + } + + /// Decodes a bare condition identifier (no child path). Canonical encodings only. + public static bool TryDecodeIdentifier(string? identifier, out string connectionId, out string conditionKey) + { + connectionId = string.Empty; conditionKey = string.Empty; + if (identifier is null || identifier.Length > MaximumIdentifierLength || !identifier.StartsWith(Prefix, StringComparison.Ordinal)) + return false; + var encoded = identifier[Prefix.Length..]; + if (encoded.Length == 0 || !IsBase64Url(encoded)) return false; + try + { + var padded = encoded.Replace('-', '+').Replace('_', '/'); + padded = padded.PadRight((padded.Length + 3) / 4 * 4, '='); + var parsed = JsonSerializer.Deserialize(Convert.FromBase64String(padded)); + if (parsed?.ConnectionId is null || parsed.ConditionKey is null + || !string.Equals(Identifier(parsed.ConnectionId, parsed.ConditionKey), identifier, StringComparison.Ordinal)) + return false; + connectionId = parsed.ConnectionId; conditionKey = parsed.ConditionKey; + return true; + } + catch (Exception error) when (error is JsonException or FormatException or ArgumentException) + { return false; } + } + + /// + /// Parses a condition identifier or a child of one. is the bare condition part, + /// null for the condition itself. + /// + public static bool TryParse(string? identifier, out string conditionIdentifier, out string connectionId, + out string conditionKey, out string? browsePath) + { + conditionIdentifier = string.Empty; connectionId = string.Empty; conditionKey = string.Empty; browsePath = null; + if (identifier is null || identifier.Length > MaximumIdentifierLength) return false; + var split = identifier.IndexOf(ChildSeparator); + var head = split < 0 ? identifier : identifier[..split]; + string? tail = split < 0 ? null : identifier[(split + 1)..]; + if (tail is not null && !IsValidBrowsePath(tail)) return false; + if (!TryDecodeIdentifier(head, out connectionId, out conditionKey)) return false; + conditionIdentifier = head; browsePath = tail; + return true; + } + + /// A non-empty /-joined path of non-empty segments with no control characters. + public static bool IsValidBrowsePath(string? browsePath) => + !string.IsNullOrEmpty(browsePath) && browsePath.Length < MaximumIdentifierLength + && !browsePath.Split(ChildSeparator).Any(string.IsNullOrEmpty) && !browsePath.Any(char.IsControl); + + private static bool IsBase64Url(string value) => value.All(c => char.IsAsciiLetterOrDigit(c) || c is '-' or '_'); + + private static int Base64UrlValue(char c) => c switch + { + >= 'A' and <= 'Z' => c - 'A', + >= 'a' and <= 'z' => c - 'a' + 26, + >= '0' and <= '9' => c - '0' + 52, + '-' => 62, + _ => 63 + }; +}