mirror of
https://github.com/PepperDash/PepperDashCore.git
synced 2026-01-11 19:44:44 +00:00
Work in progress, PDC-23-PasswordManager
This commit is contained in:
77
Pepperdash Core/Pepperdash Core/PasswordManager/Config.cs
Normal file
77
Pepperdash Core/Pepperdash Core/PasswordManager/Config.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
|
||||
namespace PepperDash.Core.PasswordManager
|
||||
{
|
||||
// Example JSON password configuration object
|
||||
//{
|
||||
// "global":{
|
||||
// "passwords":[
|
||||
// {
|
||||
// "key": "Password01",
|
||||
// "name": "Technician Password",
|
||||
// "enable": true,
|
||||
// "password": "1988"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// Passwrod manager JSON configuration
|
||||
/// </summary>
|
||||
public class PasswordConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// Key used to search for object in JSON array
|
||||
/// </summary>
|
||||
public string key { get; set; }
|
||||
/// <summary>
|
||||
/// Friendly name of password object
|
||||
/// </summary>
|
||||
public string name { get; set; }
|
||||
/// <summary>
|
||||
/// Password object enabled
|
||||
/// </summary>
|
||||
public bool enable { get; set; }
|
||||
/// <summary>
|
||||
/// Password object configured password
|
||||
/// </summary>
|
||||
public string password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public PasswordConfig()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Global JSON object
|
||||
/// </summary>
|
||||
public class GlobalConfig
|
||||
{
|
||||
public List<PasswordConfig> passwords { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public GlobalConfig()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Root JSON object
|
||||
/// </summary>
|
||||
public class RootObject
|
||||
{
|
||||
public GlobalConfig global { get; set; }
|
||||
}
|
||||
}
|
||||
33
Pepperdash Core/Pepperdash Core/PasswordManager/Constants.cs
Normal file
33
Pepperdash Core/Pepperdash Core/PasswordManager/Constants.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Crestron.SimplSharp;
|
||||
using PepperDash.Core.JsonToSimpl;
|
||||
|
||||
namespace PepperDash.Core.PasswordManager
|
||||
{
|
||||
public class PasswordManager : IKeyed
|
||||
{
|
||||
public string Key { get; set; }
|
||||
|
||||
public List<PasswordConfig> Passwords { 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 PasswordManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize method
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="uniqueId"></param>
|
||||
public void Initialize(string uniqueId, string key)
|
||||
{
|
||||
OnBoolChange(false, 0, PasswordManagerConstants.BoolEvaluatedChange);
|
||||
|
||||
try
|
||||
{
|
||||
if(string.IsNullOrEmpty(uniqueId) || string.IsNullOrEmpty(key))
|
||||
{
|
||||
Debug.Console(1, "PasswordManager.Initialize({0}, {1}) null or empty parameters", uniqueId, key);
|
||||
return;
|
||||
}
|
||||
|
||||
Key = key;
|
||||
|
||||
JsonToSimplMaster master = J2SGlobal.GetMasterByFile(uniqueId);
|
||||
if(master == null)
|
||||
{
|
||||
Debug.Console(1, "PassowrdManager.Initialize failed:\rCould not find JSON file with uniqueID {0}", uniqueId);
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds password to the list
|
||||
/// </summary>
|
||||
/// <param name="password"></param>
|
||||
private void AddPassword(PasswordConfig password)
|
||||
{
|
||||
if (password == null)
|
||||
return;
|
||||
|
||||
RemovePassword(password);
|
||||
Passwords.Add(password);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes password from the list
|
||||
/// </summary>
|
||||
/// <param name="password"></param>
|
||||
private void RemovePassword(PasswordConfig password)
|
||||
{
|
||||
if (password == null)
|
||||
return;
|
||||
|
||||
var item = Passwords.FirstOrDefault(p => p.key.Equals(password.key));
|
||||
if (item != null)
|
||||
Passwords.Remove(item);
|
||||
}
|
||||
|
||||
/// <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="state"></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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,6 +110,9 @@
|
||||
<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="SystemInfo\EventArgs and Constants.cs" />
|
||||
<Compile Include="SystemInfo\SystemInfoConfig.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
||||
Reference in New Issue
Block a user