Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 6be92d8

Browse files
[NEW-FEATURE] External Authorization to work with cyclic PLC data (#513)
* 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 <PTKu@users.noreply.github.com> Co-authored-by: Peter <61538034+PTKu@users.noreply.github.com>
1 parent 68c612e commit 6be92d8

File tree

7 files changed

+112
-37
lines changed

7 files changed

+112
-37
lines changed

src/TcOpen.Hammer/HMI/App.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Serilog.Sinks;
1414
using TcOpen.Inxton.TcoCore.Wpf;
1515
using System.Windows.Media;
16+
using TcOpen.Inxton.Local.Security.Readers;
1617

1718
namespace HMI
1819
{
@@ -71,6 +72,11 @@ public App()
7172
// Initialize logger
7273
Entry.PlcHammer.TECH_MAIN._app._logger.StartLoggingMessages(TcoCore.eMessageCategory.Info);
7374

75+
// Initialize external authentication
76+
authenticationService.ExternalAuthorization = ExternalTokenAuthorization.CreatePlcTokenReader
77+
(Entry.PlcHammer.TECH_MAIN._app._station001._externalToken,
78+
Entry.PlcHammer.TECH_MAIN._app._station001._externalTokenPresence);
79+
7480
// Set up data exchange
7581
switch (answer)
7682
{

src/TcOpen.Hammer/HMI/PlcHammer.Hmi.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<ItemGroup>
1414
<ProjectReference Include="..\..\Serilog.Sinks.MQTT\src\Serilog.Sinks.MQTT\Serilog.Sinks.MQTT.csproj" />
1515
<ProjectReference Include="..\..\TcoInspectors\src\Wpf\TcOpen.Inxton.TcoInspectors.Wpf\TcOpen.Inxton.TcoInspectors.Wpf.csproj" />
16+
<ProjectReference Include="..\..\TcOpen.Inxton\src\TcOpen.Inxton.Local.Security.Externals\TcOpen.Inxton.Local.Security.Readers.csproj" />
1617
<ProjectReference Include="..\..\_packaging\TcOpen.Group.Wpf\TcOpen.Group.Wpf.csproj" />
1718
<ProjectReference Include="..\PlcHammerConnector\PlcHammerConnector.csproj" />
1819
</ItemGroup>

src/TcOpen.Hammer/TcOpenHammer/TcOpenHammer/PlcHammer/POUs/Station001/Station001.TcPOU

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ VAR
2020
_technologicalDataManager : TechnologicalDataManager(THIS^);
2121
_currentMode : enumModes := enumModes.Idle;
2222
_checkers : Checkers(THIS^);
23+
24+
_externalToken : STRING;
25+
_externalTokenPresence : BOOL;
2326
END_VAR]]></Declaration>
2427
<Implementation>
2528
<ST><![CDATA[_components();
@@ -166,39 +169,5 @@ END_VAR
166169
</Implementation>
167170
</Get>
168171
</Property>
169-
<LineIds Name="Station001">
170-
<LineId Id="3" Count="22" />
171-
<LineId Id="2" Count="0" />
172-
</LineIds>
173-
<LineIds Name="Station001.AutomatMode">
174-
<LineId Id="3" Count="6" />
175-
<LineId Id="2" Count="0" />
176-
</LineIds>
177-
<LineIds Name="Station001.Checkers.Get">
178-
<LineId Id="2" Count="0" />
179-
</LineIds>
180-
<LineIds Name="Station001.Components.Get">
181-
<LineId Id="2" Count="0" />
182-
</LineIds>
183-
<LineIds Name="Station001.GroundMode">
184-
<LineId Id="3" Count="10" />
185-
<LineId Id="2" Count="0" />
186-
</LineIds>
187-
<LineIds Name="Station001.ProcessRecepie.Get">
188-
<LineId Id="2" Count="0" />
189-
</LineIds>
190-
<LineIds Name="Station001.ProcessTraceabilty.Get">
191-
<LineId Id="2" Count="0" />
192-
</LineIds>
193-
<LineIds Name="Station001.ProductionData.Get">
194-
<LineId Id="2" Count="0" />
195-
</LineIds>
196-
<LineIds Name="Station001.ServiceMode">
197-
<LineId Id="3" Count="7" />
198-
<LineId Id="2" Count="0" />
199-
</LineIds>
200-
<LineIds Name="Station001.TechnologicalDataManager.Get">
201-
<LineId Id="2" Count="0" />
202-
</LineIds>
203172
</POU>
204173
</TcPlcObject>

src/TcOpen.Inxton/src/Security/ExternalAuthorization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private void ChangeToken(string token)
2323
}
2424

2525
public IUser RequestAuthorization(string token)
26-
{
26+
{
2727
AuthorizationErrorMessage = string.Empty;
2828
try
2929
{

src/TcOpen.Inxton/src/TcOpen.Inxton.Local.Security.Externals/ExternalTokenAuthorization.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,18 @@ public static ExternalAuthorization CreateComReader(string portName, int baudRat
3333
{
3434
return new ExternalTokenAuthorization(new ComPortTokenProvider(portName, baudRate, dataBits, stopBits, parity));
3535
}
36+
37+
/// <summary>
38+
/// Creates external authorization for token present in a string variable the PLC program.
39+
/// </summary>
40+
/// <param name="tokenValue">Onliner string containing the value of authentication token.</param>
41+
/// <param name="tokenPresence">Onliner bool indicating presence of authentication token.</param>
42+
/// <returns></returns>
43+
public static ExternalAuthorization CreatePlcTokenReader(Vortex.Connector.ValueTypes.OnlinerString tokenValue,
44+
Vortex.Connector.ValueTypes.OnlinerBool tokenPresence)
45+
{
46+
return new ExternalTokenAuthorization(new PlcTokenReader(tokenValue, tokenPresence));
47+
}
48+
3649
}
3750
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.IO.Ports;
3+
using TcOpen.Inxton.Security;
4+
using Vortex.Connector;
5+
using Vortex.Connector.ValueTypes;
6+
7+
namespace TcOpen.Inxton.Local.Security
8+
{
9+
/// <summary>
10+
/// Provides access to authentication token data from the PLC.
11+
/// </summary>
12+
public class PlcTokenReader : ITokenProvider
13+
{
14+
15+
private readonly OnlinerString _valueToken;
16+
private readonly OnlinerBool _tokenPresence;
17+
18+
/// <summary>
19+
/// Creates new instance of <see cref="PlcTokenReader"/>
20+
/// </summary>
21+
/// <param name="valueToken">Onliner of the variable containing value of the token</param>
22+
/// <param name="tokenPresence">Onliner indicating whether the authentication token is present (inserted/active)</param>
23+
public PlcTokenReader(OnlinerString valueToken, OnlinerBool tokenPresence)
24+
{
25+
_valueToken = valueToken;
26+
_tokenPresence = tokenPresence;
27+
28+
_valueToken?.Subscribe(TagDataChanged);
29+
_tokenPresence?.Subscribe(TagDataPresence);
30+
}
31+
32+
public void SetTokenReceivedAction(Action<string> tokenReceivedAction)
33+
{
34+
IncomingTokenAction = tokenReceivedAction;
35+
}
36+
37+
public Action<string> IncomingTokenAction;
38+
39+
40+
void TagDataChanged(IValueTag sender, ValueChangedEventArgs args)
41+
{
42+
try
43+
{
44+
if (_tokenPresence.Synchron)
45+
{
46+
IncomingTokenAction?.Invoke(_valueToken.Cyclic);
47+
}
48+
else
49+
{
50+
SecurityManager.Manager.Service.DeAuthenticateCurrentUser();
51+
}
52+
}
53+
catch (Exception)
54+
{
55+
throw;
56+
}
57+
}
58+
59+
void TagDataPresence(IValueTag sender, ValueChangedEventArgs args)
60+
{
61+
try
62+
{
63+
if(_tokenPresence.Synchron == false)
64+
{
65+
SecurityManager.Manager.Service.DeAuthenticateCurrentUser();
66+
}
67+
}
68+
catch (Exception)
69+
{
70+
throw;
71+
}
72+
}
73+
}
74+
}

src/TcOpen.Inxton/tests/TcOpen.Inxton.Local.Security/TcOpen.Inxton.Local.Security.ReadersTests/UnitTest1.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using NUnit.Framework;
2+
using System;
3+
using TcOpen.Inxton.Local.Security.Readers;
24

35
namespace TcOpen.Inxton.Local.Security.ReadersTests
46
{
@@ -10,9 +12,19 @@ public void Setup()
1012
}
1113

1214
[Test]
13-
public void Test1()
15+
public void CreatePlcTokenReader()
1416
{
15-
Assert.Pass();
17+
var tokenValueSource = new Vortex.Connector.ValueTypes.OnlinerString();
18+
var tokenPresence = new Vortex.Connector.ValueTypes.OnlinerBool();
19+
var reader = ExternalTokenAuthorization.CreatePlcTokenReader(tokenValueSource, tokenPresence);
20+
}
21+
22+
[Test]
23+
public void CreateComTokenReader()
24+
{
25+
var tokenValueSource = new Vortex.Connector.ValueTypes.OnlinerString();
26+
var tokenPresence = new Vortex.Connector.ValueTypes.OnlinerBool();
27+
Assert.Throws<System.IO.IOException>(() => ExternalTokenAuthorization.CreateComReader("COM1"));
1628
}
1729
}
1830
}

0 commit comments

Comments
 (0)