/* ========================================================================
* 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.Threading;
using System.Threading.Tasks;
using Opc.Ua.Security.Certificates;
namespace Opc.Ua.Server
{
///
/// Allows application components to receive notifications when changes to sessions occur.
///
///
/// Sinks that receive these events must not block the thread.
///
public interface ISessionManager : IDisposable
{
///
/// Raised after a new session is created.
///
event SessionEventHandler SessionCreated;
///
/// Raised whenever a session is activated and the user identity or preferred locales changed.
///
event SessionEventHandler SessionActivated;
///
/// Raised before a session is closed.
///
event SessionEventHandler SessionClosing;
///
/// Raised after diagnostics of an existing session were changed.
///
event SessionEventHandler SessionDiagnosticsChanged;
///
/// Raised to signal a channel that the session is still alive.
///
event SessionEventHandler SessionChannelKeepAlive;
///
/// Raised before the user identity for a session is changed.
///
[Obsolete(
"Replaced by IUserTokenAuthenticator + IServerIdentityRegistry. " +
"Register authenticators via services.AddIdentityAuthenticator() or " +
"server.CurrentInstance.IdentityRegistry.Register(...). See Docs/IdentityProviders.md.")]
event ImpersonateEventHandler ImpersonateUser;
///
/// Raised to validate a session-less request.
///
event EventHandler ValidateSessionLessRequest;
///
/// Starts the session manager.
///
ValueTask StartupAsync(CancellationToken cancellationToken = default);
///
/// Stops the session manager and closes all sessions.
///
void Shutdown();
///
/// Clears all tracked failed authentication attempts and lockouts.
/// Intended for diagnostic and test scenarios.
///
void ClearAuthenticationLockouts();
///
/// Returns all of the sessions known to the session manager.
///
/// A list of the sessions.
IList GetSessions();
///
/// Find and return a session specified by authentication token
///
/// The requested session.
ISession? GetSession(NodeId authenticationToken);
///
/// Creates a new session.
///
ValueTask CreateSessionAsync(
OperationContext context,
Certificate serverCertificate,
string? sessionName,
ByteString clientNonce,
ApplicationDescription? clientDescription,
string? endpointUrl,
Certificate? clientCertificate,
CertificateCollection? clientCertificateChain,
double requestedSessionTimeout,
uint maxResponseMessageSize,
CancellationToken cancellationToken = default);
///
/// Activates an existing session
///
ValueTask<(bool IdentityContextChanged, ByteString ServerNonce, ServiceResult ActivationStatus)> ActivateSessionAsync(
OperationContext context,
NodeId authenticationToken,
SignatureData? clientSignature,
ExtensionObject userIdentityToken,
SignatureData? userTokenSignature,
ArrayOf localeIds,
CancellationToken cancellationToken = default);
///
/// Closes the specified session.
///
///
/// This method should not throw an exception if the session no longer exists.
///
ValueTask CloseSessionAsync(NodeId sessionId, CancellationToken cancellationToken = default);
///
/// Validates request header and returns a request context.
///
///
/// This method verifies that the session id is valid and that it uses secure channel id
/// associated with current thread. It also verifies that the timestamp is not too
/// and that the sequence number is not out of order (update requests only).
///
ValueTask ValidateRequestAsync(
RequestHeader? requestHeader,
SecureChannelContext secureChannelContext,
RequestType requestType,
RequestLifetime requestLifetime);
///
/// Triggers the event so subscribers can react.
///
void RaiseSessionDiagnosticsChangedEvent(ISession session);
}
///
/// The result of a call to .
///
public class CreateSessionResult
{
///
/// The created Session.
///
public required ISession Session { get; init; }
///
/// The SessionID assigned to the session.
///
public required NodeId SessionId { get; init; }
///
/// The authentication token used to identify and authorize the client.
///
public required NodeId AuthenticationToken { get; init; }
///
/// The server nonce of the session.
///
public required ByteString ServerNonce { get; init; }
///
/// The revised session timeout.
///
public required double RevisedSessionTimeout { get; init; }
}
///
/// The possible reasons for a session related event.
///
public enum SessionEventReason
{
///
/// A new session was created.
///
Created,
///
/// A session is being activated with a new user identity.
///
Impersonating,
///
/// A session was activated and the user identity or preferred locales changed.
///
Activated,
///
/// The diagnostics of an existing session were changed.
///
DiagnosticsChanged,
///
/// A session is about to be closed.
///
Closing,
///
/// A keep alive to signal a channel that the session is still active.
/// Triggered by the session manager based on .
///
ChannelKeepAlive
}
///
/// The delegate for functions used to receive session related events.
///
public delegate void SessionEventHandler(ISession session, SessionEventReason reason);
///
/// A class which provides the event arguments for session related event.
///
public class ImpersonateEventArgs : EventArgs
{
///
/// Creates a new instance.
///
public ImpersonateEventArgs(
IUserIdentityTokenHandler? newIdentityHandler,
UserTokenPolicy? userTokenPolicy,
EndpointDescription? endpointDescription = null)
{
UserIdentityTokenHandler = newIdentityHandler!;
UserTokenPolicy = userTokenPolicy!;
EndpointDescription = endpointDescription!;
}
///
/// The new user identity for the session.
///
public IUserIdentityTokenHandler UserIdentityTokenHandler { get; }
///
/// The new user identity for the session.
///
[Obsolete("Use UserIdentityTokenHandler to process the token.")]
public UserIdentityToken NewIdentity => UserIdentityTokenHandler.Token;
///
/// The user token policy selected by the client.
///
public UserTokenPolicy UserTokenPolicy { get; }
///
/// An application defined handle that can be used for access control operations.
///
public IUserIdentity Identity { get; set; } = null!;
///
/// An application defined handle that can be used for access control operations.
///
public IUserIdentity EffectiveIdentity { get; set; } = null!;
///
/// Set to indicate that an error occurred validating the identity and that it should be rejected.
///
public ServiceResult IdentityValidationError { get; set; } = null!;
///
/// Get the EndpointDescription
///
public EndpointDescription EndpointDescription { get; }
}
///
/// The delegate for functions used to receive impersonation events.
///
public delegate void ImpersonateEventHandler(ISession session, ImpersonateEventArgs args);
///
/// A class which provides the event arguments for session related event.
///
public class ValidateSessionLessRequestEventArgs : EventArgs
{
///
/// Creates a new instance.
///
public ValidateSessionLessRequestEventArgs(
NodeId authenticationToken,
RequestType requestType)
{
AuthenticationToken = authenticationToken;
RequestType = requestType;
}
///
/// The request type for the request.
///
public RequestType RequestType { get; }
///
/// The new user identity for the session.
///
public NodeId AuthenticationToken { get; }
///
/// The identity to associate with the session-less request.
///
public IUserIdentity Identity { get; set; } = null!;
///
/// Set to indicate that an error occurred validating the session-less request and that it should be rejected.
///
public ServiceResult Error { get; set; } = null!;
}
}