/* ======================================================================== * Copyright (c) 2005-2026 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 Opc.Ua.Identity; using Opc.Ua.Server.UserDatabase; using Opc.Ua.Server.UserManagement; namespace Opc.Ua.Server { /// /// Validates username/password identity tokens against an . /// public sealed class UserNamePasswordAuthenticator : IUserTokenAuthenticator { private readonly IUserDatabase m_userDatabase; private readonly IUserManagement m_userManagement; private readonly Func>? m_verify; /// /// Creates a username/password authenticator. /// public UserNamePasswordAuthenticator( IUserDatabase userDatabase, IUserManagement userManagement, ITelemetryContext telemetry) { m_userDatabase = userDatabase ?? throw new ArgumentNullException(nameof(userDatabase)); m_userManagement = userManagement ?? throw new ArgumentNullException(nameof(userManagement)); _ = telemetry ?? throw new ArgumentNullException(nameof(telemetry)); } /// /// Creates a delegate-backed username/password authenticator. /// public UserNamePasswordAuthenticator( Func> verify) { m_userDatabase = null!; m_userManagement = null!; m_verify = verify ?? throw new ArgumentNullException(nameof(verify)); } /// public UserTokenType TokenType => UserTokenType.UserName; /// public string? IssuedTokenProfileUri => null; /// public ValueTask AuthenticateAsync( AuthenticationContext context, CancellationToken ct = default) { if (context.TokenHandler is not UserNameIdentityTokenHandler userTokenHandler) { return new ValueTask(AuthenticationResult.NotHandled); } if (m_verify != null) { return AuthenticateWithVerifierAsync(userTokenHandler, ct); } string userName = userTokenHandler.UserName; byte[]? password = userTokenHandler.DecryptedPassword; if (string.IsNullOrEmpty(userName)) { return Reject( StatusCodes.BadIdentityTokenInvalid, "Security token is not a valid username token. An empty username is not accepted."); } if (Utils.Utf8IsNullOrEmpty(password)) { return Reject( StatusCodes.BadIdentityTokenInvalid, "Security token is not a valid username token. An empty password is not accepted."); } if (!m_userManagement.IsUserActive(userName) || !m_userDatabase.CheckCredentials(userName, password)) { return Reject( StatusCodes.BadUserAccessDenied, "Invalid username or password."); } return new ValueTask( AuthenticationResult.Accept(new UserIdentity(userTokenHandler))); } private async ValueTask AuthenticateWithVerifierAsync( UserNameIdentityTokenHandler userTokenHandler, CancellationToken ct) { ct.ThrowIfCancellationRequested(); try { IUserIdentity? identity = await m_verify!(userTokenHandler, ct).ConfigureAwait(false); return identity == null ? RejectResult( StatusCodes.BadIdentityTokenRejected, "Username token verifier did not return an identity.") : AuthenticationResult.Accept(identity); } catch (ServiceResultException ex) { return AuthenticationResult.Reject(ex.Result); } } private static ValueTask Reject(StatusCode statusCode, string message) { return new ValueTask(RejectResult(statusCode, message)); } private static AuthenticationResult RejectResult(StatusCode statusCode, string message) { return AuthenticationResult.Reject(new ServiceResult(statusCode, new LocalizedText(message))); } } }