/* ========================================================================
* 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.IO;
namespace Opc.Ua.Client
{
///
/// A session configuration stores all the information
/// needed to reconnect a session with a new secure channel.
///
[DataType(Namespace = Namespaces.OpcUaXsd)]
public partial record class SessionOptions
{
///
/// The session name used by the client.
///
[DataTypeField(Order = 1)]
public partial string? SessionName { get; init; }
///
/// The serialized user identity token. Use
/// for the high-level wrapper.
///
[DataTypeField(Order = 2, StructureHandling = StructureHandling.ExtensionObject)]
public UserIdentityToken? IdentityToken { get; set; }
///
/// The identity used to create the session.
/// This is a convenience property that wraps .
///
public IUserIdentity? Identity
{
get => IdentityToken != null ? new UserIdentity(IdentityToken) : null;
set => IdentityToken = value?.TokenHandler?.Token;
}
///
/// The configured endpoint for the secure channel.
///
[DataTypeField(Order = 3, StructureHandling = StructureHandling.Inline)]
public partial ConfiguredEndpoint? ConfiguredEndpoint { get; init; }
///
/// If the client is configured to check the certificate domain.
///
[DataTypeField(Order = 4)]
public partial bool CheckDomain { get; init; }
}
///
/// A session state stores not just configuration but
/// also the subscription states
///
[DataType(Namespace = Namespaces.OpcUaXsd)]
public partial record class SessionState : SessionOptions
{
///
/// Default constructor
///
public SessionState()
{
}
///
/// Creates a session state
///
public SessionState(SessionOptions options)
: base(options)
{
}
///
/// When the session configuration was created.
///
[DataTypeField(Order = 10)]
public DateTimeUtc Timestamp { get; set; } = DateTimeUtc.Now;
///
/// The session id assigned by the server.
///
[DataTypeField(Order = 11)]
public partial NodeId SessionId { get; init; }
///
/// The authentication token used by the server to identify the session.
///
[DataTypeField(Order = 12)]
public partial NodeId AuthenticationToken { get; init; }
///
/// The raw bytes of the last server nonce received.
/// Persisting bytes avoids object-serialization ambiguity for Nonce internals.
///
[DataTypeField(Order = 13)]
public partial ByteString ServerNonce { get; init; }
///
/// The raw bytes of the client nonce used when the session was created.
/// Required for enhanced-policy activate signatures during reconnect.
///
[DataTypeField(Order = 14)]
public partial ByteString ClientNonce { get; init; }
///
/// The user identity token policy which was used to create the session.
///
[DataTypeField(Order = 15)]
public partial string? UserIdentityTokenPolicy { get; init; }
///
/// The raw bytes of the last server ECC ephemeral key received.
///
[DataTypeField(Order = 16)]
public partial ByteString ServerEccEphemeralKey { get; init; }
///
/// Allows the list of subscriptions to be saved/restored
/// when the object is serialized.
///
[DataTypeField(Order = 20, StructureHandling = StructureHandling.Inline)]
public partial ArrayOf Subscriptions { get; init; }
}
///
/// A session configuration stores all the information
/// needed to reconnect a session with a new secure channel.
///
[DataType(Namespace = Namespaces.OpcUaXsd)]
public partial record class SessionConfiguration : SessionState
{
///
/// Default constructor
///
public SessionConfiguration()
{
}
///
/// Creates a session configuration
///
public SessionConfiguration(SessionState state)
: base(state)
{
}
///
/// Creates the session configuration from a stream.
///
public static SessionConfiguration? Create(Stream stream, ITelemetryContext telemetry)
{
var context = ServiceMessageContext.Create(telemetry);
context.Factory.Builder.AddOpcUaClientDataTypes();
using var decoder = new BinaryDecoder(stream, context, true);
ArrayOf nsUris = decoder.ReadStringArray(null);
ArrayOf serverUris = decoder.ReadStringArray(null);
// Namespace and server URI tables on the wire are encoded as nullable string
// arrays, but the runtime contract requires non-null entries; the bangs reflect
// that the decoder yields no null elements for these fields.
context.NamespaceUris = new NamespaceTable(nsUris.Memory.ToArray()!);
context.ServerUris = new StringTable(serverUris.Memory.ToArray()!);
var config = new SessionConfiguration();
config.Decode(decoder);
return config;
}
}
}