PasswordFile generation changed, token no longer outside of WEB service visible

This commit is contained in:
Thomas Wiedebusch
2023-02-23 16:35:25 +01:00
parent 3dd36f7a9a
commit 165ea0b719
10 changed files with 182 additions and 90 deletions
@@ -65,11 +65,22 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MeterPwdHandlerDb.cs" />
<Compile Include="MeterPwdDb.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\CommonCore.Configuration\CommonCore.Configuration.csproj">
<Project>{9EB1F659-6F73-4F65-BCAB-4114FD94F5FC}</Project>
<Name>CommonCore.Configuration</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\SoftwareAccessHelper\SoftwareAccessHelper.csproj">
<Project>{A33E3C6D-EAAE-4A20-A0EE-F9E6922FD029}</Project>
<Name>SoftwareAccessHelper</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
@@ -37,6 +37,10 @@ namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisPwd
/// </summary>
public String Skeleton = "";
/// <summary>
/// Password, which is the Skeleton, if password file not installed, or the PwdLevel8, if the password file is installed .
/// </summary>
public String Password = "";
/// <summary>
/// List of hashes.
/// </summary>
public List<Byte[]> ListOfHashes = new List<Byte[]>();
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Xylem.Common.CommonCore.Configuration;
using Xylem.Common.Logic.SoftwareAccessHelper;
namespace Xylem.Common.Hardware.WaterMeter.Genesis.GenesisPwd
{
/// <summary>
/// Class for meter password handler DB interfaces.
/// </summary>
[Serializable]
public static class MeterPwdHandlerDb
{
/// <summary>
/// Acquires the actual password from DB. Initially, this is the Skeleton-Key, if the password file is
/// written it is the Lvl8Pwd.
/// </summary>
/// <param name="pcbId">input the PcbId</param>
/// <param name="password">output of password</param>
/// <returns>true if successfully executed</returns>
/// <remarks date="2023-Feb-23" author="Roland Drabesch/Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static Boolean GetPasswordFromDb(String pcbId, out String password)
{
var url = ServiceUrls.GenesisGetPasswordServiceUrl();
password = "";
try
{
var http = (HttpWebRequest)WebRequest.Create(url + pcbId);
var response = (HttpWebResponse)http.GetResponse();
var responseStream = response.GetResponseStream();
if (responseStream == null)
{
return false;
}
using (var sr = new StreamReader(responseStream))
{
var responseJson = sr.ReadToEnd();
password = responseJson.Trim('"');
}
}
catch (Exception )
{
return false;
}
return true;
}
/// <summary>
/// Collect order number, radio address, skeleton key, password hashes and passwords from DB.
/// </summary>
/// <param name="pcbId">input the PcbId</param>
/// <param name="passwordContainer">output of passwordContainer</param>
/// <returns>true if successfully executed</returns>
/// <remarks date="2023-Feb-23" author="Roland Drabesch/Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static Boolean SetPwdFromSkeletonToLvl8inDd(String pcbId, MeterPwdDb passwordContainer)
{
try
{
var newPwd = Encoding.UTF8.GetString(passwordContainer.ListOfPasswords.Last());
var passwordSetServiceUrl = ServiceUrls.GenesisSetPasswordServiceUrl();
var responseJson = LocalWebRequest.GetRequest(passwordSetServiceUrl + pcbId + "&Password=" + newPwd, 8000);
if (responseJson.ToLower().Contains("store"))
{
return true;
}
return false;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// Collect order number, radio address, skeleton key, password hashes and passwords from DB.
/// </summary>
/// <param name="pcbId">input the PcbId</param>
/// <param name="passwordContainer">output of passwordContainer</param>
/// <returns>true if successfully executed</returns>
/// <remarks date="2023-Feb-23" author="Roland Drabesch/Thomas Wiedebusch">
/// - Initial.
/// </remarks>
public static Boolean RequestPwdFileFromDb(String pcbId, out MeterPwdDb passwordContainer)
{
passwordContainer = new MeterPwdDb();
try
{
var url = ServiceUrls.GenesisGetPasswordContainerServiceUrl();
var requestResponse = LocalWebRequest.GetRequestWithError(url + pcbId, out var errorCode, 20000, isRetry: true);
if (errorCode == HttpStatusCode.OK.GetHashCode())
{
passwordContainer = JsonConvert.DeserializeObject<MeterPwdDb>(requestResponse);
return true;
}
return false;
}
catch (Exception)
{
return false;
}
}
}
}
@@ -1,21 +1,19 @@
using Newtonsoft.Json;
using ProductionUiCordonel.ProductionProcesses.Enum;
using ProductionUiCordonel.ProductionProcesses.Enum;
using ProductionUiCordonel.UserControls.FinalTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Controls;
using Xylem.Common.CommonCore.Configuration;
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisFile;
using Xylem.Common.Logic.SoftwareAccessHelper;
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisPwd;
namespace ProductionUiCordonel.ProductionProcesses.Actions
{
public class ProductionProcessPasswordFile : BaseProductionProcess
{
private UcPasswordFile UserControl;
private UcPasswordFile _userControl;
private MeterPwdDb _pwdContainer = new MeterPwdDb();
private MeterPwdFile _meterPwdFile;
public ProductionProcessPasswordFile()
{
@@ -25,16 +23,16 @@ namespace ProductionUiCordonel.ProductionProcesses.Actions
public override void InitUserControl()
{
UserControl = new UcPasswordFile();
_userControl = new UcPasswordFile();
}
public override UserControl GetControl()
{
if (UserControl == null)
if (_userControl == null)
{
throw new Exception($"UserControl {GetType()} not Ready");
}
return UserControl;
return _userControl;
}
private void updateImage(Image ctlImage, Boolean? success = null)
@@ -49,15 +47,15 @@ namespace ProductionUiCordonel.ProductionProcesses.Actions
public override void StartProcess()
{
Boolean ret = RequestPassword();
updateImage(UserControl.RequestPassword, ret);
var ret = MeterPwdHandlerDb.RequestPwdFileFromDb(Meter.PcbId, out _pwdContainer);
updateImage(_userControl.RequestPassword, ret);
Progress += 16;
//If the password file is written, the "Password" from pwContainer is overwritten from Skeleton-key
//If the password file is written, the "Password" from _pwdContainer is overwritten from Skeleton-key
//to the "level 8 password" form the LuDB server. The DB password column is the current login password
//for production. Initially the "Password" is the Skeleton-key which can not be part of the list of
//Passwords. The level 8 password is the last in the pwContainer list of passwords.
if (Encoding.UTF8.GetString(pwContainer.listOfPasswords.Last()) == pwContainer.Password)
//Passwords. The level 8 password is the last in the _pwdContainer list of passwords.
if (Encoding.UTF8.GetString(_pwdContainer.ListOfPasswords.Last()) == _pwdContainer.Password)
{
NewStatus("Passwörter schon geschrieben. Prozess wird übersprungen");
currentProcessState = ProductionProcessState.Done;
@@ -70,7 +68,7 @@ namespace ProductionUiCordonel.ProductionProcesses.Actions
ret = CreatePasswordFile();
}
updateImage(UserControl.CreateFile, ret);
updateImage(_userControl.CreateFile, ret);
Progress += 16;
//Write the password file to the Cordonel, read it back and make a binary verification
@@ -79,14 +77,14 @@ namespace ProductionUiCordonel.ProductionProcesses.Actions
ret = WriteAndVerifyPasswordFileToMeter();
}
updateImage(UserControl.WriteToMeter, ret);
updateImage(_userControl.WriteToMeter, ret);
Progress += 16;
//Check if new password is in the meter ... even if the file write wasn't successful
if (ret)
{
Meter.Logout();
ret = Meter.Login(Encoding.UTF8.GetString(pwContainer.listOfPasswords.Last()));
ret = Meter.Login(Encoding.UTF8.GetString(_pwdContainer.ListOfPasswords.Last()));
}
//Password in Production DB can be updated (overwrite Skeleton-key in Password column with
@@ -94,16 +92,16 @@ namespace ProductionUiCordonel.ProductionProcesses.Actions
//this is the indicator, that the password file is valid.
if (ret)
{
ret = UpdatePasswordinDb();
ret = MeterPwdHandlerDb.SetPwdFromSkeletonToLvl8inDd(Meter.PcbId, _pwdContainer);
}
updateImage(UserControl.UpdatePassword, ret);
updateImage(_userControl.UpdatePassword, ret);
Progress += 16;
if (ret)
{
ret = Meter.Logout();
}
updateImage(UserControl.Logout, ret);
updateImage(_userControl.Logout, ret);
Progress += 16;
//test the production database password
@@ -112,7 +110,7 @@ namespace ProductionUiCordonel.ProductionProcesses.Actions
Meter.ClearPassword();
ret = Meter.Login();
}
updateImage(UserControl.CheckNewPassword, ret);
updateImage(_userControl.CheckNewPassword, ret);
Progress = 100;
if (!ret)
@@ -130,73 +128,22 @@ namespace ProductionUiCordonel.ProductionProcesses.Actions
}
#region realWork
public class MeterPasswordContainer
{
public Int64? orderNumber = null;
public Int64? radioAdress = null;
public String Skeleton = "";
public String Password = "";
public List<Byte[]> listOfHashes = new List<Byte[]>();
public List<Byte[]> listOfPasswords = new List<Byte[]>();
}
private MeterPasswordContainer pwContainer = new MeterPasswordContainer();
private MeterPwdFile meterPwdFile;
public Boolean RequestPassword()
{
NewStatus("Request Passwords");
var url = ServiceUrls.GenesisGetPasswordContainerServiceUrl();
//url = "http://localhost:56011/api/Password/GetPasswordHashFileContent?PcbId=";
url = ($"{url}{Meter.PcbId}");
var header = new Dictionary<String, String>
{
{ "Cache-Control", "no-cache" },
{ "X-Node-Token", "d5b8c0b2-81f6-4421-80d0-44eb2093e616" }
};
var resp = LocalWebRequest.GetRequestWithError(url, out var errorCode, 20000, header, true);
if (errorCode == HttpStatusCode.OK.GetHashCode())
{
pwContainer = JsonConvert.DeserializeObject<MeterPasswordContainer>(resp);
NewStatus("Passwörter verfügbar");
return true;
}
return false;
}
public Boolean CreatePasswordFile()
{
meterPwdFile = new MeterPwdFile(Meter);
return meterPwdFile.BuildPwdFile(pwContainer.listOfPasswords, 1, pwContainer.Skeleton);
_meterPwdFile = new MeterPwdFile(Meter);
return _meterPwdFile.BuildPwdFile(_pwdContainer.ListOfPasswords, 1, _pwdContainer.Skeleton);
}
public Boolean WriteAndVerifyPasswordFileToMeter()
{
if (meterPwdFile.UnlockEraseWriteMeterPwdFile())
if (_meterPwdFile.UnlockEraseWriteMeterPwdFile())
{
if (meterPwdFile.WriteMeterPwdFile())
if (_meterPwdFile.WriteMeterPwdFile())
{
return meterPwdFile.VerifyMeterPwdFile();
return _meterPwdFile.VerifyMeterPwdFile();
}
}
return false;
}
public Boolean UpdatePasswordinDb()
{
var newPs = Encoding.UTF8.GetString(pwContainer.listOfPasswords.Last());
var passwordSetServiceUrl = ServiceUrls.GenesisSetPasswordServiceUrl();
var responseJson = LocalWebRequest.GetRequest(passwordSetServiceUrl + Meter.PcbId + "&Password=" + newPs, 8000);
if (responseJson.ToLower().Contains("store"))
{
return true;
}
return false;
}
#endregion
}
@@ -335,6 +335,10 @@
<Project>{2C6378C6-6BA1-4CEE-8992-8404D8369A90}</Project>
<Name>GenesisFile</Name>
</ProjectReference>
<ProjectReference Include="..\Hardware\WaterMeter\Genesis\GenesisPwd\GenesisPwd.csproj">
<Project>{AC434B18-4D7B-49BA-9AD5-0527C79E8297}</Project>
<Name>GenesisPwd</Name>
</ProjectReference>
<ProjectReference Include="..\Hardware\WaterMeter\Genesis\Registers\Registers.csproj">
<Project>{F4AAF7E6-7333-4284-B735-E32DEB076C64}</Project>
<Name>Registers</Name>