diff --git a/src/TcOpen.Hammer/HMI/App.xaml.cs b/src/TcOpen.Hammer/HMI/App.xaml.cs index 20162919e..952e455f5 100644 --- a/src/TcOpen.Hammer/HMI/App.xaml.cs +++ b/src/TcOpen.Hammer/HMI/App.xaml.cs @@ -13,6 +13,7 @@ using Serilog.Sinks; using TcOpen.Inxton.TcoCore.Wpf; using System.Windows.Media; +using TcOpen.Inxton.Local.Security.Readers; namespace HMI { @@ -71,6 +72,11 @@ public App() // Initialize logger Entry.PlcHammer.TECH_MAIN._app._logger.StartLoggingMessages(TcoCore.eMessageCategory.Info); + // Initialize external authentication + authenticationService.ExternalAuthorization = ExternalTokenAuthorization.CreatePlcTokenReader + (Entry.PlcHammer.TECH_MAIN._app._station001._externalToken, + Entry.PlcHammer.TECH_MAIN._app._station001._externalTokenPresence); + // Set up data exchange switch (answer) { diff --git a/src/TcOpen.Hammer/HMI/PlcHammer.Hmi.csproj b/src/TcOpen.Hammer/HMI/PlcHammer.Hmi.csproj index a08879b16..c114b316a 100644 --- a/src/TcOpen.Hammer/HMI/PlcHammer.Hmi.csproj +++ b/src/TcOpen.Hammer/HMI/PlcHammer.Hmi.csproj @@ -13,6 +13,7 @@ + diff --git a/src/TcOpen.Hammer/TcOpenHammer/TcOpenHammer/PlcHammer/POUs/Station001/Station001.TcPOU b/src/TcOpen.Hammer/TcOpenHammer/TcOpenHammer/PlcHammer/POUs/Station001/Station001.TcPOU index aadb247e6..55ff681f5 100644 --- a/src/TcOpen.Hammer/TcOpenHammer/TcOpenHammer/PlcHammer/POUs/Station001/Station001.TcPOU +++ b/src/TcOpen.Hammer/TcOpenHammer/TcOpenHammer/PlcHammer/POUs/Station001/Station001.TcPOU @@ -20,6 +20,9 @@ VAR _technologicalDataManager : TechnologicalDataManager(THIS^); _currentMode : enumModes := enumModes.Idle; _checkers : Checkers(THIS^); + + _externalToken : STRING; + _externalTokenPresence : BOOL; END_VAR]]> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/TcOpen.Inxton/src/Security/ExternalAuthorization.cs b/src/TcOpen.Inxton/src/Security/ExternalAuthorization.cs index 017d81724..0093b64cd 100644 --- a/src/TcOpen.Inxton/src/Security/ExternalAuthorization.cs +++ b/src/TcOpen.Inxton/src/Security/ExternalAuthorization.cs @@ -23,7 +23,7 @@ private void ChangeToken(string token) } public IUser RequestAuthorization(string token) - { + { AuthorizationErrorMessage = string.Empty; try { diff --git a/src/TcOpen.Inxton/src/TcOpen.Inxton.Local.Security.Externals/ExternalTokenAuthorization.cs b/src/TcOpen.Inxton/src/TcOpen.Inxton.Local.Security.Externals/ExternalTokenAuthorization.cs index 255010d00..6eab9e8e1 100644 --- a/src/TcOpen.Inxton/src/TcOpen.Inxton.Local.Security.Externals/ExternalTokenAuthorization.cs +++ b/src/TcOpen.Inxton/src/TcOpen.Inxton.Local.Security.Externals/ExternalTokenAuthorization.cs @@ -33,5 +33,18 @@ public static ExternalAuthorization CreateComReader(string portName, int baudRat { return new ExternalTokenAuthorization(new ComPortTokenProvider(portName, baudRate, dataBits, stopBits, parity)); } + + /// + /// Creates external authorization for token present in a string variable the PLC program. + /// + /// Onliner string containing the value of authentication token. + /// Onliner bool indicating presence of authentication token. + /// + public static ExternalAuthorization CreatePlcTokenReader(Vortex.Connector.ValueTypes.OnlinerString tokenValue, + Vortex.Connector.ValueTypes.OnlinerBool tokenPresence) + { + return new ExternalTokenAuthorization(new PlcTokenReader(tokenValue, tokenPresence)); + } + } } diff --git a/src/TcOpen.Inxton/src/TcOpen.Inxton.Local.Security.Externals/PlcTokenReader/PlcTokenReader.cs b/src/TcOpen.Inxton/src/TcOpen.Inxton.Local.Security.Externals/PlcTokenReader/PlcTokenReader.cs new file mode 100644 index 000000000..153e7b268 --- /dev/null +++ b/src/TcOpen.Inxton/src/TcOpen.Inxton.Local.Security.Externals/PlcTokenReader/PlcTokenReader.cs @@ -0,0 +1,74 @@ +using System; +using System.IO.Ports; +using TcOpen.Inxton.Security; +using Vortex.Connector; +using Vortex.Connector.ValueTypes; + +namespace TcOpen.Inxton.Local.Security +{ + /// + /// Provides access to authentication token data from the PLC. + /// + public class PlcTokenReader : ITokenProvider + { + + private readonly OnlinerString _valueToken; + private readonly OnlinerBool _tokenPresence; + + /// + /// Creates new instance of + /// + /// Onliner of the variable containing value of the token + /// Onliner indicating whether the authentication token is present (inserted/active) + public PlcTokenReader(OnlinerString valueToken, OnlinerBool tokenPresence) + { + _valueToken = valueToken; + _tokenPresence = tokenPresence; + + _valueToken?.Subscribe(TagDataChanged); + _tokenPresence?.Subscribe(TagDataPresence); + } + + public void SetTokenReceivedAction(Action tokenReceivedAction) + { + IncomingTokenAction = tokenReceivedAction; + } + + public Action IncomingTokenAction; + + + void TagDataChanged(IValueTag sender, ValueChangedEventArgs args) + { + try + { + if (_tokenPresence.Synchron) + { + IncomingTokenAction?.Invoke(_valueToken.Cyclic); + } + else + { + SecurityManager.Manager.Service.DeAuthenticateCurrentUser(); + } + } + catch (Exception) + { + throw; + } + } + + void TagDataPresence(IValueTag sender, ValueChangedEventArgs args) + { + try + { + if(_tokenPresence.Synchron == false) + { + SecurityManager.Manager.Service.DeAuthenticateCurrentUser(); + } + } + catch (Exception) + { + throw; + } + } + } +} diff --git a/src/TcOpen.Inxton/tests/TcOpen.Inxton.Local.Security/TcOpen.Inxton.Local.Security.ReadersTests/UnitTest1.cs b/src/TcOpen.Inxton/tests/TcOpen.Inxton.Local.Security/TcOpen.Inxton.Local.Security.ReadersTests/UnitTest1.cs index e16ed6ae3..02139ce24 100644 --- a/src/TcOpen.Inxton/tests/TcOpen.Inxton.Local.Security/TcOpen.Inxton.Local.Security.ReadersTests/UnitTest1.cs +++ b/src/TcOpen.Inxton/tests/TcOpen.Inxton.Local.Security/TcOpen.Inxton.Local.Security.ReadersTests/UnitTest1.cs @@ -1,4 +1,6 @@ using NUnit.Framework; +using System; +using TcOpen.Inxton.Local.Security.Readers; namespace TcOpen.Inxton.Local.Security.ReadersTests { @@ -10,9 +12,19 @@ public void Setup() } [Test] - public void Test1() + public void CreatePlcTokenReader() { - Assert.Pass(); + var tokenValueSource = new Vortex.Connector.ValueTypes.OnlinerString(); + var tokenPresence = new Vortex.Connector.ValueTypes.OnlinerBool(); + var reader = ExternalTokenAuthorization.CreatePlcTokenReader(tokenValueSource, tokenPresence); + } + + [Test] + public void CreateComTokenReader() + { + var tokenValueSource = new Vortex.Connector.ValueTypes.OnlinerString(); + var tokenPresence = new Vortex.Connector.ValueTypes.OnlinerBool(); + Assert.Throws(() => ExternalTokenAuthorization.CreateComReader("COM1")); } } } \ No newline at end of file