mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-01-11 19:44:44 +00:00
Work in progress, built PasswordManager and PasswordClient classes
This commit is contained in:
@@ -4,7 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
|
||||
namespace PepperDash.Core.PasswordManager
|
||||
namespace PepperDash.Core.PasswordManagement
|
||||
{
|
||||
// Example JSON password configuration object
|
||||
//{
|
||||
@@ -13,7 +13,7 @@ namespace PepperDash.Core.PasswordManager
|
||||
// {
|
||||
// "key": "Password01",
|
||||
// "name": "Technician Password",
|
||||
// "enable": true,
|
||||
// "enabled": true,
|
||||
// "password": "1988"
|
||||
// }
|
||||
// ]
|
||||
@@ -36,18 +36,38 @@ namespace PepperDash.Core.PasswordManager
|
||||
/// <summary>
|
||||
/// Password object enabled
|
||||
/// </summary>
|
||||
public bool enable { get; set; }
|
||||
public bool enabled { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public ushort simplEnabled
|
||||
{
|
||||
get { return (ushort)(enabled ? 1 : 0); }
|
||||
set { enabled = Convert.ToBoolean(value); }
|
||||
}
|
||||
/// <summary>
|
||||
/// Password object configured password
|
||||
/// </summary>
|
||||
public string password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Password type
|
||||
/// </summary>
|
||||
private int type { get; set; }
|
||||
/// <summary>
|
||||
/// Password Type for S+
|
||||
/// </summary>
|
||||
public ushort simplType
|
||||
{
|
||||
get { return Convert.ToUInt16(type); }
|
||||
set { type = value; }
|
||||
}
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public PasswordConfig()
|
||||
{
|
||||
|
||||
simplEnabled = 0;
|
||||
simplType = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,7 +83,7 @@ namespace PepperDash.Core.PasswordManager
|
||||
/// </summary>
|
||||
public GlobalConfig()
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
|
||||
namespace PepperDash.Core.PasswordManagement
|
||||
{
|
||||
/// <summary>
|
||||
/// Constants
|
||||
/// </summary>
|
||||
public class PasswordManagementConstants
|
||||
{
|
||||
/// <summary>
|
||||
/// Generic boolean value change constant
|
||||
/// </summary>
|
||||
public const ushort BoolValueChange = 1;
|
||||
/// <summary>
|
||||
/// Evaluated boolean change constant
|
||||
/// </summary>
|
||||
public const ushort BoolEvaluatedChange = 2;
|
||||
/// <summary>
|
||||
/// Password is valid change constant
|
||||
/// </summary>
|
||||
public const ushort PasswordIsValid = 3;
|
||||
/// <summary>
|
||||
/// Password is invalid change constant
|
||||
/// </summary>
|
||||
public const ushort PasswordIsInvalid = 4;
|
||||
/// <summary>
|
||||
/// Password LED change constant
|
||||
/// </summary>
|
||||
public const ushort PasswordLedChange = 5;
|
||||
|
||||
/// <summary>
|
||||
/// Generic ushort value change constant
|
||||
/// </summary>
|
||||
public const ushort UshrtValueChange = 101;
|
||||
/// <summary>
|
||||
/// Password count
|
||||
/// </summary>
|
||||
public const ushort PasswordListCount = 102;
|
||||
/// <summary>
|
||||
/// Password length
|
||||
/// </summary>
|
||||
public const ushort SelectedPasswordLength = 103;
|
||||
/// <summary>
|
||||
/// Password to validate length
|
||||
/// </summary>
|
||||
public const ushort UserEnteredPasswordLength = 104;
|
||||
|
||||
/// <summary>
|
||||
/// Generic string value change constant
|
||||
/// </summary>
|
||||
public const ushort StringValueChange = 201;
|
||||
/// <summary>
|
||||
/// Password key change constant
|
||||
/// </summary>
|
||||
public const ushort PasswordKey = 202;
|
||||
/// <summary>
|
||||
/// Password selected key change constant
|
||||
/// </summary>
|
||||
public const ushort PasswordKeySelected = 203;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
|
||||
namespace PepperDash.Core.PasswordManagement
|
||||
{
|
||||
public class PasswordClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Password Client
|
||||
/// </summary>
|
||||
public PasswordConfig Client { get; set; }
|
||||
/// <summary>
|
||||
/// Used to build the password entered by the user
|
||||
/// </summary>
|
||||
public string PasswordToValidate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Boolean event
|
||||
/// </summary>
|
||||
public event EventHandler<BoolChangeEventArgs> BoolChange;
|
||||
/// <summary>
|
||||
/// Ushort event
|
||||
/// </summary>
|
||||
public event EventHandler<UshrtChangeEventArgs> UshrtChange;
|
||||
/// <summary>
|
||||
/// String event
|
||||
/// </summary>
|
||||
public event EventHandler<StringChangeEventArgs> StringChange;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public PasswordClient()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize method
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
public void Initialize(string key)
|
||||
{
|
||||
OnBoolChange(false, 0, PasswordManagementConstants.BoolEvaluatedChange);
|
||||
|
||||
Client = new PasswordConfig();
|
||||
PasswordToValidate = "";
|
||||
|
||||
// there has to be a better way to get the index of the current index of password
|
||||
ushort i = 0;
|
||||
foreach (var password in PasswordManager.Passwords)
|
||||
{
|
||||
i++;
|
||||
OnStringChange(password.key, i, PasswordManagementConstants.PasswordKey);
|
||||
}
|
||||
|
||||
OnBoolChange(true, 0, PasswordManagementConstants.BoolEvaluatedChange);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves password by key
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
public void GetPasswordByKey(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
Debug.Console(1, "PassowrdClient.GetPasswordByKey failed:\rKey {0} is null or empty", key);
|
||||
return;
|
||||
}
|
||||
|
||||
PasswordConfig password = PasswordManager.Passwords.FirstOrDefault(p => p.key.Equals(key));
|
||||
if (password == null)
|
||||
{
|
||||
OnUshrtChange(0, 0, PasswordManagementConstants.SelectedPasswordLength);
|
||||
return;
|
||||
}
|
||||
|
||||
Client = password;
|
||||
OnUshrtChange((ushort)Client.password.Length, 0, PasswordManagementConstants.SelectedPasswordLength);
|
||||
OnStringChange(Client.key, 0, PasswordManagementConstants.PasswordKeySelected);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Password validation method
|
||||
/// </summary>
|
||||
/// <param name="password"></param>
|
||||
public void ValidatePassword(string password)
|
||||
{
|
||||
if (string.IsNullOrEmpty(password))
|
||||
return;
|
||||
|
||||
if (string.Equals(Client.password, password))
|
||||
{
|
||||
OnBoolChange(true, 0, PasswordManagementConstants.PasswordIsValid);
|
||||
}
|
||||
else
|
||||
{
|
||||
OnBoolChange(true, 0, PasswordManagementConstants.PasswordIsInvalid);
|
||||
}
|
||||
|
||||
|
||||
OnBoolChange(false, 0, PasswordManagementConstants.PasswordIsValid);
|
||||
OnBoolChange(false, 0, PasswordManagementConstants.PasswordIsInvalid);
|
||||
|
||||
ClearPassword();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds the user entered passwrod string, will attempt to validate the user entered
|
||||
/// password against the selected password when the length of the 2 are equal
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
public void BuildPassword(string data)
|
||||
{
|
||||
PasswordToValidate = String.Concat(PasswordToValidate, data);
|
||||
OnBoolChange(true, (ushort)PasswordToValidate.Length, PasswordManagementConstants.PasswordLedChange);
|
||||
|
||||
if (PasswordToValidate.Length == Client.password.Length)
|
||||
ValidatePassword(PasswordToValidate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the user entered password and resets the LEDs
|
||||
/// </summary>
|
||||
public void ClearPassword()
|
||||
{
|
||||
PasswordToValidate = "";
|
||||
OnBoolChange(true, (ushort)PasswordToValidate.Length, PasswordManagementConstants.PasswordLedChange);
|
||||
|
||||
for(var i = 1; i <= Client.password.Length; i++)
|
||||
OnBoolChange(false, (ushort)i, PasswordManagementConstants.PasswordLedChange);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Protected boolean change event handler
|
||||
/// </summary>
|
||||
/// <param name="state"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <param name="type"></param>
|
||||
protected void OnBoolChange(bool state, ushort index, ushort type)
|
||||
{
|
||||
var handler = BoolChange;
|
||||
if (handler != null)
|
||||
{
|
||||
var args = new BoolChangeEventArgs(state, type);
|
||||
args.Index = index;
|
||||
BoolChange(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Protected ushort change event handler
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <param name="type"></param>
|
||||
protected void OnUshrtChange(ushort value, ushort index, ushort type)
|
||||
{
|
||||
var handler = UshrtChange;
|
||||
if (handler != null)
|
||||
{
|
||||
var args = new UshrtChangeEventArgs(value, type);
|
||||
args.Index = index;
|
||||
UshrtChange(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Protected string change event handler
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="index"></param>
|
||||
/// <param name="type"></param>
|
||||
protected void OnStringChange(string value, ushort index, ushort type)
|
||||
{
|
||||
var handler = StringChange;
|
||||
if (handler != null)
|
||||
{
|
||||
var args = new StringChangeEventArgs(value, type);
|
||||
args.Index = index;
|
||||
StringChange(this, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,14 +7,14 @@ using Newtonsoft.Json.Linq;
|
||||
using Crestron.SimplSharp;
|
||||
using PepperDash.Core.JsonToSimpl;
|
||||
|
||||
namespace PepperDash.Core.PasswordManager
|
||||
namespace PepperDash.Core.PasswordManagement
|
||||
{
|
||||
public class PasswordManager : IKeyed
|
||||
public class PasswordManager
|
||||
{
|
||||
public string Key { get; set; }
|
||||
|
||||
public List<PasswordConfig> Passwords { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// List of passwords configured
|
||||
/// </summary>
|
||||
public static List<PasswordConfig> Passwords = new List<PasswordConfig>();
|
||||
/// <summary>
|
||||
/// Boolean event
|
||||
/// </summary>
|
||||
@@ -33,7 +33,7 @@ namespace PepperDash.Core.PasswordManager
|
||||
/// </summary>
|
||||
public PasswordManager()
|
||||
{
|
||||
|
||||
Passwords.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -43,7 +43,7 @@ namespace PepperDash.Core.PasswordManager
|
||||
/// <param name="uniqueId"></param>
|
||||
public void Initialize(string uniqueId, string key)
|
||||
{
|
||||
OnBoolChange(false, 0, PasswordManagerConstants.BoolEvaluatedChange);
|
||||
OnBoolChange(false, 0, PasswordManagementConstants.BoolEvaluatedChange);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -53,8 +53,6 @@ namespace PepperDash.Core.PasswordManager
|
||||
return;
|
||||
}
|
||||
|
||||
Key = key;
|
||||
|
||||
JsonToSimplMaster master = J2SGlobal.GetMasterByFile(uniqueId);
|
||||
if(master == null)
|
||||
{
|
||||
@@ -64,25 +62,31 @@ namespace PepperDash.Core.PasswordManager
|
||||
|
||||
var passwords = master.JsonObject.ToObject<RootObject>().global.passwords;
|
||||
if(passwords == null)
|
||||
{
|
||||
{
|
||||
Debug.Console(1, "PasswordManager.Initialize failed:\rCould not find password object");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach(var password in passwords)
|
||||
{
|
||||
AddPassword(password);
|
||||
if (password != null)
|
||||
{
|
||||
Debug.Console(1, "PasswordManager.Initialize: {0}, {1}, {2}, {3}, {4}", password.key, password.name, password.simplEnabled, password.simplType, password.password);
|
||||
AddPassword(password);
|
||||
}
|
||||
}
|
||||
|
||||
OnUshrtChange(Convert.ToUInt16(Passwords.Count), 0, PasswordManagementConstants.PasswordListCount);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
var msg = string.Format("PasswordManager.Initialize({0}, {1}) failed:\r{2}", uniqueId, Key, e.Message);
|
||||
var msg = string.Format("PasswordManager.Initialize({0}, {1}) failed:\r{2}", uniqueId, key, e.Message);
|
||||
CrestronConsole.PrintLine(msg);
|
||||
ErrorLog.Error(msg);
|
||||
}
|
||||
finally
|
||||
{
|
||||
OnBoolChange(true, 0, PasswordManagerConstants.BoolEvaluatedChange);
|
||||
OnBoolChange(true, 0, PasswordManagementConstants.BoolEvaluatedChange);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +99,9 @@ namespace PepperDash.Core.PasswordManager
|
||||
if (password == null)
|
||||
return;
|
||||
|
||||
RemovePassword(password);
|
||||
var item = Passwords.FirstOrDefault(i => i.key.Equals(password.key));
|
||||
if (item != null)
|
||||
Passwords.Remove(item);
|
||||
Passwords.Add(password);
|
||||
}
|
||||
|
||||
@@ -108,7 +114,7 @@ namespace PepperDash.Core.PasswordManager
|
||||
if (password == null)
|
||||
return;
|
||||
|
||||
var item = Passwords.FirstOrDefault(p => p.key.Equals(password.key));
|
||||
var item = Passwords.FirstOrDefault(i => i.key.Equals(password.key));
|
||||
if (item != null)
|
||||
Passwords.Remove(item);
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
|
||||
namespace PepperDash.Core.PasswordManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Constants
|
||||
/// </summary>
|
||||
public class PasswordManagerConstants
|
||||
{
|
||||
/// <summary>
|
||||
/// Generic boolean value change constant
|
||||
/// </summary>
|
||||
public const ushort BoolValueChange = 1;
|
||||
/// <summary>
|
||||
/// Evaluated boolean change constant
|
||||
/// </summary>
|
||||
public const ushort BoolEvaluatedChange = 2;
|
||||
|
||||
/// <summary>
|
||||
/// Generic ushort value change constant
|
||||
/// </summary>
|
||||
public const ushort UshrtValueChange = 101;
|
||||
|
||||
/// <summary>
|
||||
/// Generic string value change constant
|
||||
/// </summary>
|
||||
public const ushort StringValueChange = 201;
|
||||
}
|
||||
}
|
||||
@@ -110,9 +110,10 @@
|
||||
<Compile Include="JsonToSimpl\JsonToSimplGenericMaster.cs" />
|
||||
<Compile Include="JsonToSimpl\JsonToSimplMaster.cs" />
|
||||
<Compile Include="Network\DiscoveryThings.cs" />
|
||||
<Compile Include="PasswordManager\Config.cs" />
|
||||
<Compile Include="PasswordManager\Constants.cs" />
|
||||
<Compile Include="PasswordManager\PasswordManager.cs" />
|
||||
<Compile Include="PasswordManagement\Config.cs" />
|
||||
<Compile Include="PasswordManagement\Constants.cs" />
|
||||
<Compile Include="PasswordManagement\PasswordClient.cs" />
|
||||
<Compile Include="PasswordManagement\PasswordManager.cs" />
|
||||
<Compile Include="SystemInfo\EventArgs and Constants.cs" />
|
||||
<Compile Include="SystemInfo\SystemInfoConfig.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
Reference in New Issue
Block a user