Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
[NEW-FEATURE] External Authorization to work with cyclic PLC data (#513)
Browse files Browse the repository at this point in the history
* Create draft PR for #512

* external authentication added possibility to use plc variable as a source of authentication token

* added basic tests for the token providers

Co-authored-by: PTKu <[email protected]>
Co-authored-by: Peter <[email protected]>
  • Loading branch information
3 people authored Sep 9, 2022
1 parent 68c612e commit 6be92d8
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 37 deletions.
6 changes: 6 additions & 0 deletions src/TcOpen.Hammer/HMI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Serilog.Sinks;
using TcOpen.Inxton.TcoCore.Wpf;
using System.Windows.Media;
using TcOpen.Inxton.Local.Security.Readers;

namespace HMI
{
Expand Down Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions src/TcOpen.Hammer/HMI/PlcHammer.Hmi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ItemGroup>
<ProjectReference Include="..\..\Serilog.Sinks.MQTT\src\Serilog.Sinks.MQTT\Serilog.Sinks.MQTT.csproj" />
<ProjectReference Include="..\..\TcoInspectors\src\Wpf\TcOpen.Inxton.TcoInspectors.Wpf\TcOpen.Inxton.TcoInspectors.Wpf.csproj" />
<ProjectReference Include="..\..\TcOpen.Inxton\src\TcOpen.Inxton.Local.Security.Externals\TcOpen.Inxton.Local.Security.Readers.csproj" />
<ProjectReference Include="..\..\_packaging\TcOpen.Group.Wpf\TcOpen.Group.Wpf.csproj" />
<ProjectReference Include="..\PlcHammerConnector\PlcHammerConnector.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ VAR
_technologicalDataManager : TechnologicalDataManager(THIS^);
_currentMode : enumModes := enumModes.Idle;
_checkers : Checkers(THIS^);
_externalToken : STRING;
_externalTokenPresence : BOOL;
END_VAR]]></Declaration>
<Implementation>
<ST><![CDATA[_components();
Expand Down Expand Up @@ -166,39 +169,5 @@ END_VAR
</Implementation>
</Get>
</Property>
<LineIds Name="Station001">
<LineId Id="3" Count="22" />
<LineId Id="2" Count="0" />
</LineIds>
<LineIds Name="Station001.AutomatMode">
<LineId Id="3" Count="6" />
<LineId Id="2" Count="0" />
</LineIds>
<LineIds Name="Station001.Checkers.Get">
<LineId Id="2" Count="0" />
</LineIds>
<LineIds Name="Station001.Components.Get">
<LineId Id="2" Count="0" />
</LineIds>
<LineIds Name="Station001.GroundMode">
<LineId Id="3" Count="10" />
<LineId Id="2" Count="0" />
</LineIds>
<LineIds Name="Station001.ProcessRecepie.Get">
<LineId Id="2" Count="0" />
</LineIds>
<LineIds Name="Station001.ProcessTraceabilty.Get">
<LineId Id="2" Count="0" />
</LineIds>
<LineIds Name="Station001.ProductionData.Get">
<LineId Id="2" Count="0" />
</LineIds>
<LineIds Name="Station001.ServiceMode">
<LineId Id="3" Count="7" />
<LineId Id="2" Count="0" />
</LineIds>
<LineIds Name="Station001.TechnologicalDataManager.Get">
<LineId Id="2" Count="0" />
</LineIds>
</POU>
</TcPlcObject>
2 changes: 1 addition & 1 deletion src/TcOpen.Inxton/src/Security/ExternalAuthorization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private void ChangeToken(string token)
}

public IUser RequestAuthorization(string token)
{
{
AuthorizationErrorMessage = string.Empty;
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,18 @@ public static ExternalAuthorization CreateComReader(string portName, int baudRat
{
return new ExternalTokenAuthorization(new ComPortTokenProvider(portName, baudRate, dataBits, stopBits, parity));
}

/// <summary>
/// Creates external authorization for token present in a string variable the PLC program.
/// </summary>
/// <param name="tokenValue">Onliner string containing the value of authentication token.</param>
/// <param name="tokenPresence">Onliner bool indicating presence of authentication token.</param>
/// <returns></returns>
public static ExternalAuthorization CreatePlcTokenReader(Vortex.Connector.ValueTypes.OnlinerString tokenValue,
Vortex.Connector.ValueTypes.OnlinerBool tokenPresence)
{
return new ExternalTokenAuthorization(new PlcTokenReader(tokenValue, tokenPresence));
}

}
}
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Provides access to authentication token data from the PLC.
/// </summary>
public class PlcTokenReader : ITokenProvider
{

private readonly OnlinerString _valueToken;
private readonly OnlinerBool _tokenPresence;

/// <summary>
/// Creates new instance of <see cref="PlcTokenReader"/>
/// </summary>
/// <param name="valueToken">Onliner of the variable containing value of the token</param>
/// <param name="tokenPresence">Onliner indicating whether the authentication token is present (inserted/active)</param>
public PlcTokenReader(OnlinerString valueToken, OnlinerBool tokenPresence)
{
_valueToken = valueToken;
_tokenPresence = tokenPresence;

_valueToken?.Subscribe(TagDataChanged);
_tokenPresence?.Subscribe(TagDataPresence);
}

public void SetTokenReceivedAction(Action<string> tokenReceivedAction)
{
IncomingTokenAction = tokenReceivedAction;
}

public Action<string> 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;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NUnit.Framework;
using System;
using TcOpen.Inxton.Local.Security.Readers;

namespace TcOpen.Inxton.Local.Security.ReadersTests
{
Expand All @@ -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<System.IO.IOException>(() => ExternalTokenAuthorization.CreateComReader("COM1"));
}
}
}

0 comments on commit 6be92d8

Please sign in to comment.