Skip to content

Commit

Permalink
add password feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Somai authored and Vlad Somai committed Jul 20, 2022
1 parent e77026e commit 4f90814
Show file tree
Hide file tree
Showing 8 changed files with 923 additions and 20 deletions.
1 change: 1 addition & 0 deletions Necta/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 25 additions & 4 deletions Necta/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using PuppeteerSharp;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Threading;

namespace Necta
{
Expand Down Expand Up @@ -37,6 +38,7 @@ public Necta()

try
{
PasswordModal.CreatePasswordModal(this);
NectaConfigService.Initialize();
SetDataFromConfig();
SaveAndValidateURI(true);
Expand All @@ -50,6 +52,8 @@ public Necta()
{
NectaLogService.WriteLog(ex.Message, LogLevels.ERROR);
}

CheckPassword();
}

private void SaveButton_Click(object sender, EventArgs e)
Expand All @@ -59,7 +63,7 @@ private void SaveButton_Click(object sender, EventArgs e)

private void SetDataFromConfig()
{
ConfigType config = NectaConfigService.ReadConfig();
ConfigType config = ConfigContent<ConfigType>.ReadConfig(NectaConfigService.nectaConfigFile);

ApiGetUri_textBox.Text = config.API_GET_URI;
ApiUpdateUri_textBox.Text = config.API_UPDATE_URI;
Expand Down Expand Up @@ -152,9 +156,7 @@ public static async void PrintReceipt(Receipt receipt)

private void NectaNotifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
this.WindowState = FormWindowState.Normal;
NectaNotifyIcon1.Visible = false;
CheckPassword();
}

private void onResize(object sender, EventArgs e)
Expand Down Expand Up @@ -219,5 +221,24 @@ private static void ProcessPrint()

p.WaitForInputIdle();
}

private void CheckPassword()
{
PasswordModal.mInstance.showPasswordFrom();
}

public void hideMainForm()
{
Hide();
this.WindowState = FormWindowState.Minimized;
NectaNotifyIcon1.Visible = false;
}

public void showMainFrom()
{
Show();
this.WindowState = FormWindowState.Normal;
NectaNotifyIcon1.Visible = false;
}
}
}
9 changes: 9 additions & 0 deletions Necta/Necta.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="API\API.cs" />
<Compile Include="PasswordModal.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="PasswordModal.Designer.cs">
<DependentUpon>PasswordModal.cs</DependentUpon>
</Compile>
<Compile Include="NectaServices\NectaConfigService.cs" />
<Compile Include="NectaServices\NectaLogService.cs" />
<Compile Include="NectaServices\NectaService.cs" />
Expand All @@ -161,6 +167,9 @@
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="PasswordModal.resx">
<DependentUpon>PasswordModal.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
Expand Down
73 changes: 58 additions & 15 deletions Necta/NectaServices/NectaConfigService.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
using Necta.API;
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;

namespace Necta.NectaServices
{
class NectaConfigService
{
private const string nectaConfigFile = @"C:\Necta\Config\Config.json";
private const string nectaConfigPath = @"C:\Necta\Config\";
public const string nectaConfigFile = @"C:\Necta\Config\Config.json";
public const string nectaPasswordFile = @"C:\Necta\Config\Password.json";
private const string defaultPassword = "meals";

public static void Initialize()
{
if (!Directory.Exists(nectaConfigPath))
Directory.CreateDirectory(nectaConfigPath);

if (File.Exists(nectaConfigFile))
if (File.Exists(nectaConfigFile) && File.Exists(nectaPasswordFile))
return;

using (StreamWriter file = new StreamWriter(nectaConfigFile, append: true))
using (StreamWriter file = new StreamWriter(nectaConfigFile))
{
file.WriteLine("{");
file.WriteLine(" \"API_GET_URI\": \"https://develop.meals.lv/other/printer/?method=queue&key=rest4\",");
Expand All @@ -27,27 +33,21 @@ public static void Initialize()
file.WriteLine(" \"API_REQUEST_INTERVAL\": 3000");
file.WriteLine("}");
}
}
public static ConfigType ReadConfig()
{
string configContent = "";

using (StreamReader configFile = new StreamReader(nectaConfigFile))
using (StreamWriter file = new StreamWriter(nectaPasswordFile))
{
configContent = configFile.ReadToEnd();
file.WriteLine("{");
file.WriteLine(" \"Password\": \"{0}\"", Hasher.GetSha256Hash(defaultPassword));
file.WriteLine("}");
}

var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var config = JsonSerializer.Deserialize<ConfigType>(configContent, options);

return config;
File.SetAttributes(nectaPasswordFile, FileAttributes.Hidden);
}
public static void SaveNewConfig()
{
if (!Directory.Exists(nectaConfigPath))
Directory.CreateDirectory(nectaConfigPath);

var currentConfig = ReadConfig();
var currentConfig = ConfigContent<ConfigType>.ReadConfig(nectaConfigFile);

using (StreamWriter file = new StreamWriter(nectaConfigFile))
{
Expand All @@ -62,6 +62,44 @@ public static void SaveNewConfig()
}
}

//generic class that will read any json document and return the c# object
class ConfigContent<T>
{
public static T ReadConfig(string pathToFile)
{
string configContent = "";

using (StreamReader configFile = new StreamReader(pathToFile))
{
configContent = configFile.ReadToEnd();
}

var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
var config = JsonSerializer.Deserialize<T>(configContent, options);

return config;
}
}

public static class Hasher
{
public static string GetSha256Hash(string value)
{
StringBuilder Sb = new StringBuilder();

using (SHA256 hash = SHA256.Create())
{
Encoding enc = Encoding.UTF8;
byte[] result = hash.ComputeHash(enc.GetBytes(value));

foreach (byte b in result)
Sb.Append(b.ToString("x2", CultureInfo.InvariantCulture));
}

return Sb.ToString();
}
}

class ConfigType
{
public string API_GET_URI { get; set; }
Expand All @@ -70,4 +108,9 @@ class ConfigType
public string CHROME_PATH { get; set; }
public int API_REQUEST_INTERVAL { get; set; }
}

class PasswordType
{
public string Password { get; set; }
}
}
94 changes: 94 additions & 0 deletions Necta/PasswordModal.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions Necta/PasswordModal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using MaterialSkin;
using MaterialSkin.Controls;
using Necta.NectaServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Necta
{
public partial class PasswordModal : MaterialForm
{
public static PasswordModal mInstance = null;
private Necta MainInstance = null;
private static bool passwordOK = false;

public static void CreatePasswordModal(Necta instance)
{
if(mInstance==null)
mInstance = new PasswordModal(instance);
}
private PasswordModal(Necta instance)
{
MainInstance = instance;

InitializeComponent();
var materialSkinManager = MaterialSkinManager.Instance;
materialSkinManager.AddFormToManage(this);
materialSkinManager.Theme = MaterialSkinManager.Themes.LIGHT;
materialSkinManager.ColorScheme = new ColorScheme(Primary.Red300, Primary.Red300, Primary.BlueGrey500, Accent.LightBlue200, TextShade.BLACK);
instance.hideMainForm();
}

private void PasswordButton_Click(object sender, EventArgs e)
{
passwordOK = false;

var currentPassword = ConfigContent<PasswordType>.ReadConfig(NectaConfigService.nectaPasswordFile);
if (currentPassword.Password == Hasher.GetSha256Hash(PasswordTextbox.Text))
{
passwordOK = true;
hidePasswordForm();
MainInstance.showMainFrom();
}
else
MessageBox.Show(this, "Invalid password, please try again.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
}

private void PasswordForm_Closing(object e, FormClosingEventArgs ev)
{
if(!passwordOK)
Application.Exit();
}

private void hidePasswordForm()
{
Hide();
this.WindowState = FormWindowState.Minimized;
}

public void showPasswordFrom()
{
PasswordTextbox.Text = "";
passwordOK = false;
Show();
this.WindowState = FormWindowState.Normal;
}
}
}
Loading

0 comments on commit 4f90814

Please sign in to comment.