merge preliminare modificehe F.Sodano

This commit is contained in:
Samuele E. Locatelli
2020-11-02 15:36:05 +01:00
parent aa8f776659
commit bd49fbb743
2 changed files with 194 additions and 95 deletions
+180 -92
View File
@@ -7,105 +7,193 @@ using System.Collections.Generic;
namespace OpcUaServer.Server
{
public class Server : StandardServer
{
private ServerNodeManager _serverNodeManager;
private readonly IPrinter _printer;
private readonly string _pathXml;
private readonly ServerAuthenticationService _serverAuthenticationService;
/// <summary>
/// Evento richiesta refresh invio dellos tato attuale delel conditions
/// </summary>
public event EventHandler eh_reqRefreshCondition;
public Server(IPrinter printer, string pathXml)
public class Server : StandardServer
{
_printer = printer;
_pathXml = pathXml;
_serverAuthenticationService = new ServerAuthenticationService(this);
}
#region Private Fields
public void SetNodeValue(string nodeName, object value)
{
_serverNodeManager.SetNodeValue(nodeName, value);
}
private readonly string _pathXml;
private readonly IPrinter _printer;
private readonly ServerAuthenticationService _serverAuthenticationService;
private ServerNodeManager _serverNodeManager;
public void ReportEvent(string nodeName, string eventMessage, string severity, string value, bool active)
{
_serverNodeManager.ReportEvent(nodeName, eventMessage, severity, value, active);
}
#endregion Private Fields
protected override MasterNodeManager CreateMasterNodeManager(IServerInternal server, ApplicationConfiguration configuration)
{
_printer.Print(">>>>> Server: Creating node manager");
#region Public Constructors
_serverNodeManager = new ServerNodeManager(server, configuration, _printer, new XmlNodeParser(), _pathXml);
var nodeManagers = new List<INodeManager>
public Server(IPrinter printer, string pathXml)
{
_printer = printer;
_pathXml = pathXml;
_serverAuthenticationService = new ServerAuthenticationService(this);
}
#endregion Public Constructors
#region Public Events
/// <summary>
/// Evento richiesta refresh invio dellos tato attuale delel conditions
/// </summary>
public event EventHandler eh_reqRefreshCondition;
#endregion Public Events
#region Private Methods
private void SessionManager_ImpersonateUser(Session session, ImpersonateEventArgs args)
{
// check for a user name token.
_printer.Print(">>>>> Server: Authentication for session starting");
switch (args.NewIdentity)
{
case UserNameIdentityToken userNameToken:
args.Identity =
_serverAuthenticationService.VerifyPassword(userNameToken,
LoadServerProperties().ProductUri);
_printer.Print(">>>>> Server: Authentication for session userNameToken Accepted: " + args.Identity.DisplayName);
break;
case X509IdentityToken x509Token:
_serverAuthenticationService.VerifyUserTokenCertificate(x509Token.Certificate,
LoadServerProperties().ProductUri);
args.Identity = new UserIdentity(x509Token);
_printer.Print(">>>>> Server: Authentication for session X509 Token Accepted: " +
args.Identity.DisplayName);
break;
default:
_printer.Print(">>>>> Server: Authentication for session Anonymous: ");
break;
}
// 2019.04.08: aggiunta task x forzare il refresh/reinvio di TUTTE le conditions attive al momento in cui si è connesso il NUOVO client...
sendCurrCond();
}
#endregion Private Methods
#region Protected Methods
protected override MasterNodeManager CreateMasterNodeManager(IServerInternal server, ApplicationConfiguration configuration)
{
_printer.Print(">>>>> Server: Creating node manager");
_serverNodeManager = new ServerNodeManager(server, configuration, _printer, new XmlNodeParser(), _pathXml);
var nodeManagers = new List<INodeManager>
{
_serverNodeManager
};
return new MasterNodeManager(server, configuration, null, nodeManagers.ToArray());
return new MasterNodeManager(server, configuration, null, nodeManagers.ToArray());
}
protected override ServerProperties LoadServerProperties()
{
var properties = new ServerProperties
{
ManufacturerName = "Steamware",
ProductName = "Server",
ProductUri = "http://opcfoundation.org/Quickstart/ReferenceServer/v1.03",
SoftwareVersion = Utils.GetAssemblySoftwareVersion(),
BuildNumber = Utils.GetAssemblyBuildNumber(),
BuildDate = Utils.GetAssemblyTimestamp()
};
return properties;
}
protected override void OnServerStarted(IServerInternal server)
{
base.OnServerStarted(server);
server.SessionManager.ImpersonateUser += SessionManager_ImpersonateUser;
}
#endregion Protected Methods
#region Public Methods
public override ResponseHeader Browse(
RequestHeader requestHeader,
ViewDescription view,
uint requestedMaxReferencesPerNode,
BrowseDescriptionCollection nodesToBrowse,
out BrowseResultCollection results,
out DiagnosticInfoCollection diagnosticInfos)
{
results = null;
diagnosticInfos = null;
OperationContext context = ValidateRequest(requestHeader, RequestType.Browse);
try
{
if (nodesToBrowse == null || nodesToBrowse.Count == 0)
{
throw new ServiceResultException(StatusCodes.BadNothingToDo);
}
bool filter = false;
// return empty browse results for Anonymous users
// This logic should be further extended....
if (context.UserIdentity.TokenType == UserTokenType.Anonymous)
filter = true;
ServerInternal.NodeManager.Browse(
context,
view,
requestedMaxReferencesPerNode,
nodesToBrowse,
out results,
out diagnosticInfos);
if (filter) foreach (var res in results)
{
res.References.RemoveAll(x => x.BrowseName.Name.StartsWith("Machine/") && x.BrowseName.Name != "Machine/Status");
}
return CreateResponse(requestHeader, context.StringTable);
}
catch (ServiceResultException e)
{
lock (ServerInternal.DiagnosticsWriteLock)
{
ServerInternal.ServerDiagnostics.RejectedRequestsCount++;
if (IsSecurityError(e.StatusCode))
{
ServerInternal.ServerDiagnostics.SecurityRejectedRequestsCount++;
}
}
throw TranslateException(context, e);
}
finally
{
OnRequestComplete(context);
}
}
public void ReportEvent(string nodeName, string eventMessage, string severity, string value, bool active)
{
_serverNodeManager.ReportEvent(nodeName, eventMessage, severity, value, active);
}
public void sendCurrCond()
{
if (eh_reqRefreshCondition != null)
{
eh_reqRefreshCondition(this, new EventArgs());
}
}
public void SetNodeValue(string nodeName, object value)
{
_serverNodeManager.SetNodeValue(nodeName, value);
}
#endregion Public Methods
}
protected override ServerProperties LoadServerProperties()
{
var properties = new ServerProperties
{
ManufacturerName = "Steamware",
ProductName = "Server",
ProductUri = "http://opcfoundation.org/Quickstart/ReferenceServer/v1.03",
SoftwareVersion = Utils.GetAssemblySoftwareVersion(),
BuildNumber = Utils.GetAssemblyBuildNumber(),
BuildDate = Utils.GetAssemblyTimestamp()
};
return properties;
}
protected override void OnServerStarted(IServerInternal server)
{
base.OnServerStarted(server);
server.SessionManager.ImpersonateUser += SessionManager_ImpersonateUser;
}
private void SessionManager_ImpersonateUser(Session session, ImpersonateEventArgs args)
{
// check for a user name token.
_printer.Print(">>>>> Server: Authentication for session starting");
switch (args.NewIdentity)
{
case UserNameIdentityToken userNameToken:
args.Identity =
_serverAuthenticationService.VerifyPassword(userNameToken,
LoadServerProperties().ProductUri);
_printer.Print(">>>>> Server: Authentication for session userNameToken Accepted: " + args.Identity.DisplayName);
break;
case X509IdentityToken x509Token:
_serverAuthenticationService.VerifyUserTokenCertificate(x509Token.Certificate,
LoadServerProperties().ProductUri);
args.Identity = new UserIdentity(x509Token);
_printer.Print(">>>>> Server: Authentication for session X509 Token Accepted: " +
args.Identity.DisplayName);
break;
default:
_printer.Print(">>>>> Server: Authentication for session Anonymous: ");
break;
}
// 2019.04.08: aggiunta task x forzare il refresh/reinvio di TUTTE le conditions attive al momento in cui si è connesso il NUOVO client...
sendCurrCond();
}
public void sendCurrCond()
{
if(eh_reqRefreshCondition!=null)
{
eh_reqRefreshCondition(this, new EventArgs());
}
}
}
}
}
@@ -6,13 +6,23 @@ namespace OpcUaServer.Server.Services
{
public class ServerAuthenticationService
{
#region Private Fields
private readonly Server _server;
#endregion Private Fields
#region Public Constructors
public ServerAuthenticationService(Server server)
{
_server = server;
}
#endregion Public Constructors
#region Public Methods
public IUserIdentity VerifyPassword(UserNameIdentityToken userNameToken, string productUri)
{
var userName = userNameToken.UserName;
@@ -38,8 +48,9 @@ namespace OpcUaServer.Server.Services
//}
// standard users for CTT verification
if ((userName == "user1" && password == "password") ||
(userName == "user2" && password == "password1")) return new UserIdentity(userNameToken);
if ((userName == "scmAdmin" && password == "password123"))
return new UserIdentity(userNameToken);
// construct translation object with default text.
var info = new TranslationInfo(
"InvalidPassword",
@@ -53,7 +64,6 @@ namespace OpcUaServer.Server.Services
"InvalidPassword",
productUri,
new LocalizedText(info)));
}
public void VerifyUserTokenCertificate(X509Certificate2 certificate, string productUri)
@@ -104,5 +114,6 @@ namespace OpcUaServer.Server.Services
}
}
#endregion Public Methods
}
}