1 Star 0 Fork 0

Yongbo/winauth

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
WinAuthConfig.cs 33.03 KB
一键复制 编辑 原始数据 按行查看 历史
WinAuth 提交于 8年前 . Fix build for .NET35
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
/*
* Copyright (C) 2013 Colin Mackie.
* This software is distributed under the terms of the GNU General Public License.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;
using WinAuth.Resources;
namespace WinAuth
{
/// <summary>
/// Delegate for ConfigChange event
/// </summary>
/// <param name="source"></param>
/// <param name="args"></param>
public delegate void ConfigChangedHandler(object source, ConfigChangedEventArgs args);
/// <summary>
/// Class holding configuration data for application
/// </summary>
[Serializable()]
public class WinAuthConfig : IList<WinAuthAuthenticator>, ICloneable, IWinAuthAuthenticatorChangedListener
{
public static decimal CURRENTVERSION = decimal.Parse(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(2), System.Globalization.CultureInfo.InvariantCulture);
/// <summary>
/// Default actions for double-click or click system tray
/// </summary>
public enum NotifyActions
{
Notification = 0,
CopyToClipboard = 1,
HotKey = 2
}
/// <summary>
/// Event handler fired when a config property is changed
/// </summary>
public event ConfigChangedHandler OnConfigChanged;
/// <summary>
/// Current file name
/// </summary>
private string _filename;
/// <summary>
/// Current version of this Config
/// </summary>
public decimal Version { get; private set; }
/// <summary>
/// Save password for re-saving and encrypting file
/// </summary>
public string Password { protected get; set; }
/// <summary>
/// Save Yubi key for re-saving and encrypting file
/// </summary>
public YubiKey Yubi { get; set; }
/// <summary>
/// If the config was upgraded
/// </summary>
public bool Upgraded { get; set; }
/// <summary>
/// Current encryption type
/// </summary>
private Authenticator.PasswordTypes _passwordType = Authenticator.PasswordTypes.None;
/// <summary>
/// Get/set the encryption type
/// </summary>
public Authenticator.PasswordTypes PasswordType
{
get
{
return _passwordType;
}
set
{
_passwordType = value;
if ((_passwordType & Authenticator.PasswordTypes.Explicit) == 0)
{
this.Password = null;
}
if ((_passwordType & (Authenticator.PasswordTypes.YubiKeySlot1 | Authenticator.PasswordTypes.YubiKeySlot2)) == 0)
{
this.Yubi = null;
}
}
}
/// <summary>
/// All authenticators
/// </summary>
private List<WinAuthAuthenticator> _authenticators = new List<WinAuthAuthenticator>();
/// <summary>
/// Current authenticator
/// </summary>
private WinAuthAuthenticator _authenticator;
/// <summary>
/// Flag for always on top
/// </summary>
private bool _alwaysOnTop;
/// <summary>
/// Flag to use tray icon
/// </summary>
private bool _useTrayIcon;
/// <summary>
/// Default action when click in system tray
/// </summary>
private NotifyActions _notifyAction;
/// <summary>
/// Flag to set start with Windows
/// </summary>
private bool _startWithWindows;
/// <summary>
/// Flag to size form based on numebr authenticators
/// </summary>
private bool _autoSize;
/// <summary>
/// Remember position
/// </summary>
private Point _position = Point.Empty;
/// <summary>
/// Width if not autosize
/// </summary>
private int _width;
/// <summary>
/// Height if not autosize
/// </summary>
private int _height;
/// <summary>
/// This config is readonly
/// </summary>
private bool _readOnly;
/// <summary>
/// MetroFormShadowType for main form
/// </summary>
private string _shadowType;
/// <summary>
/// User's own PGPKey for backups
/// </summary>
private string _pgpKey;
/// <summary>
/// Class used to serialize the settings inside the Xml config file
/// </summary>
[XmlRoot(ElementName="settings")]
public class setting
{
/// <summary>
/// Name of dictionary entry
/// </summary>
[XmlAttribute(AttributeName="key")]
public string Key;
/// <summary>
/// Value of dictionary entry
/// </summary>
[XmlAttribute(AttributeName = "value")]
public string Value;
}
/// <summary>
/// Inline settings for Portable mode
/// </summary>
private Dictionary<string, string> _settings = new Dictionary<string, string>();
#region System Settings
/// <summary>
/// Get/set file name of config data
/// </summary>
public string Filename
{
get
{
return _filename;
}
set
{
_filename = value;
}
}
/// <summary>
/// Get/set on top flag
/// </summary>
public bool AlwaysOnTop
{
get
{
return _alwaysOnTop;
}
set
{
_alwaysOnTop = value;
if (OnConfigChanged != null)
{
OnConfigChanged(this, new ConfigChangedEventArgs("AlwaysOnTop"));
}
}
}
/// <summary>
/// Get/set use tray icon top flag
/// </summary>
public bool UseTrayIcon
{
get
{
return _useTrayIcon;
}
set
{
_useTrayIcon = value;
if (OnConfigChanged != null)
{
OnConfigChanged(this, new ConfigChangedEventArgs("UseTrayIcon"));
}
}
}
/// <summary>
/// Default action when using the system tray or double-click
/// </summary>
public NotifyActions NotifyAction
{
get
{
return _notifyAction;
}
set
{
_notifyAction = value;
if (OnConfigChanged != null)
{
OnConfigChanged(this, new ConfigChangedEventArgs("NotifyAction"));
}
}
}
/// <summary>
/// Get/set start with windows flag
/// </summary>
public bool StartWithWindows
{
get
{
return _startWithWindows;
}
set
{
_startWithWindows = value;
if (OnConfigChanged != null)
{
OnConfigChanged(this, new ConfigChangedEventArgs("StartWithWindows"));
}
}
}
/// <summary>
/// Get/set start with windows flag
/// </summary>
public bool AutoSize
{
get
{
return _autoSize;
}
set
{
_autoSize = value;
if (OnConfigChanged != null)
{
OnConfigChanged(this, new ConfigChangedEventArgs("AutoSize"));
}
}
}
/// <summary>
/// Get/set the position
/// </summary>
public Point Position
{
get
{
return _position;
}
set
{
if (_position != value)
{
_position = value;
if (OnConfigChanged != null)
{
OnConfigChanged(this, new ConfigChangedEventArgs("Position"));
}
}
}
}
/// <summary>
/// Saved window width
/// </summary>
public int Width
{
get
{
return _width;
}
set
{
if (_width != value)
{
_width = value;
if (OnConfigChanged != null)
{
OnConfigChanged(this, new ConfigChangedEventArgs("Width"));
}
}
}
}
/// <summary>
/// Saved window height
/// </summary>
public int Height
{
get
{
return _height;
}
set
{
if (_height != value)
{
_height = value;
if (OnConfigChanged != null)
{
OnConfigChanged(this, new ConfigChangedEventArgs("Height"));
}
}
}
}
/// <summary>
/// Get/set shadow type for main form
/// </summary>
public string ShadowType
{
get
{
return _shadowType;
}
set
{
_shadowType = value;
if (OnConfigChanged != null)
{
OnConfigChanged(this, new ConfigChangedEventArgs("ShadowType"));
}
}
}
/// <summary>
/// Get user's own PGP key
/// </summary>
public string PGPKey
{
get
{
return _pgpKey;
}
}
/// <summary>
/// Return if we are in portable mode, which is when the config filename is in teh same directory as the exe
/// </summary>
public bool IsPortable
{
get
{
return (string.IsNullOrEmpty(this.Filename) == false
&& string.Compare(Path.GetDirectoryName(this.Filename), Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), true) == 0);
}
}
/// <summary>
/// Read a setting value.
/// </summary>
/// <param name="name">name of setting</param>
/// <param name="defaultValue">default value if setting doesn't exist</param>
/// <returns>setting value or default value</returns>
public string ReadSetting(string name, string defaultValue = null)
{
if (this.IsPortable == true)
{
// read setting from _settings
string value;
if (_settings.TryGetValue(name, out value) == true)
{
return value;
}
else
{
return defaultValue;
}
}
else
{
return WinAuthHelper.ReadRegistryValue(name, defaultValue) as string;
}
}
/// <summary>
/// Get all the settings keys beneath the specified key
/// </summary>
/// <param name="name">name of parent key</param>
/// <returns>string array of all child (recursively) setting names. Empty is none.</returns>
public string[] ReadSettingKeys(string name)
{
if (this.IsPortable == true)
{
List<string> keys = new List<string>();
foreach (var entry in _settings)
{
if (entry.Key.StartsWith(name) == true)
{
keys.Add(entry.Key);
}
}
return keys.ToArray();
}
else
{
return WinAuthHelper.ReadRegistryKeys(name);
}
}
/// <summary>
/// Write a setting value into the Config
/// </summary>
/// <param name="name">name of setting value</param>
/// <param name="value">setting value. If null, the setting is deleted.</param>
public void WriteSetting(string name, string value)
{
if (this.IsPortable == true)
{
if (value == null)
{
if (_settings.ContainsKey(name) == true)
{
_settings.Remove(name);
}
}
else
{
_settings[name] = value;
}
}
else
{
if (value == null)
{
WinAuthHelper.DeleteRegistryKey(name);
}
else
{
WinAuthHelper.WriteRegistryValue(name, value);
}
}
}
/// <summary>
/// Check if a given password is the config's password
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public bool IsPassword(string password)
{
return (string.Compare(password, this.Password) == 0);
}
#endregion
#region IList
/// <summary>
/// Rewrite the index values in the WinAuthAuthenticator objects to match our order
/// </summary>
private void SetIndexes()
{
int count = this.Count;
for (int i = 0; i < count; i++)
{
this[i].Index = i;
}
}
/// <summary>
/// Add a new authenticator
/// </summary>
/// <param name="authenticator">WinAuthAuthenticator instance</param>
public void Add(WinAuthAuthenticator authenticator)
{
authenticator.OnWinAuthAuthenticatorChanged += new WinAuthAuthenticatorChangedHandler(this.OnWinAuthAuthenticatorChanged);
_authenticators.Add(authenticator);
SetIndexes();
}
/// <summary>
/// Remove all the authenticators
/// </summary>
public void Clear()
{
foreach (WinAuthAuthenticator authenticator in this)
{
authenticator.Index = 0;
authenticator.OnWinAuthAuthenticatorChanged -= new WinAuthAuthenticatorChangedHandler(this.OnWinAuthAuthenticatorChanged);
}
_authenticators.Clear();
}
/// <summary>
/// Check if the config contains an authenticator
/// </summary>
/// <param name="authenticator"></param>
/// <returns></returns>
public bool Contains(WinAuthAuthenticator authenticator)
{
return _authenticators.Contains(authenticator);
}
/// <summary>
/// Copy elements from the list to an array
/// </summary>
/// <param name="index"></param>
/// <param name="array"></param>
/// <param name="arrayIndex"></param>
/// <param name="count"></param>
public void CopyTo(int index, WinAuthAuthenticator[] array, int arrayIndex, int count)
{
_authenticators.CopyTo(index, array, arrayIndex, count);
}
/// <summary>
/// Copy the list into an array
/// </summary>
/// <param name="array"></param>
/// <param name="index"></param>
public void CopyTo(WinAuthAuthenticator[] array, int index)
{
_authenticators.CopyTo(array, index);
}
/// <summary>
/// return the count of authenticators
/// </summary>
public int Count
{
get
{
return _authenticators.Count;
}
}
/// <summary>
/// Get the index of an authenticator
/// </summary>
/// <param name="authenticator"></param>
/// <returns></returns>
public int IndexOf(WinAuthAuthenticator authenticator)
{
return _authenticators.IndexOf(authenticator);
}
/// <summary>
/// Insert an authenticator at a specified position
/// </summary>
/// <param name="index"></param>
/// <param name="authenticator"></param>
public void Insert(int index, WinAuthAuthenticator authenticator)
{
authenticator.OnWinAuthAuthenticatorChanged += new WinAuthAuthenticatorChangedHandler(this.OnWinAuthAuthenticatorChanged);
_authenticators.Insert(index, authenticator);
SetIndexes();
}
/// <summary>
/// Return if this list is read only
/// </summary>
public bool IsReadOnly
{
get
{
return _readOnly;
}
set
{
_readOnly = value;
}
}
/// <summary>
/// Remove an authenticator
/// </summary>
/// <param name="authenticator"></param>
/// <returns></returns>
public bool Remove(WinAuthAuthenticator authenticator)
{
authenticator.OnWinAuthAuthenticatorChanged -= new WinAuthAuthenticatorChangedHandler(this.OnWinAuthAuthenticatorChanged);
bool result = _authenticators.Remove(authenticator);
SetIndexes();
return result;
}
/// <summary>
/// Remove an authenticator from a specific position
/// </summary>
/// <param name="index"></param>
public void RemoveAt(int index)
{
_authenticators[index].OnWinAuthAuthenticatorChanged -= new WinAuthAuthenticatorChangedHandler(this.OnWinAuthAuthenticatorChanged);
_authenticators.RemoveAt(index);
SetIndexes();
}
/// <summary>
/// Get an enumerator for the authenticators
/// </summary>
/// <returns></returns>
public IEnumerator<WinAuthAuthenticator> GetEnumerator()
{
return _authenticators.GetEnumerator();
}
/// <summary>
/// Get an enumerator for the authenticators
/// </summary>
/// <returns></returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
/// <summary>
/// Indexer to get an authenticator by postion
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public WinAuthAuthenticator this[int index]
{
get
{
return _authenticators[index];
}
set
{
throw new NotImplementedException();
}
}
/// <summary>
/// Sort the authenticator by their index value
/// </summary>
public void Sort()
{
_authenticators.Sort((a,b) => a.Index.CompareTo(b.Index));
}
#endregion
#region Authenticator Settings
/// <summary>
/// Current authenticator
/// </summary>
public WinAuthAuthenticator CurrentAuthenticator
{
get
{
return _authenticator;
}
set
{
_authenticator = value;
}
}
#endregion
/// <summary>
/// Create a default config object
/// </summary>
public WinAuthConfig()
{
Version = CURRENTVERSION;
AutoSize = true;
NotifyAction = NotifyActions.Notification;
}
public void OnWinAuthAuthenticatorChanged(WinAuthAuthenticator sender, WinAuthAuthenticatorChangedEventArgs e)
{
if (OnConfigChanged != null)
{
OnConfigChanged(this, new ConfigChangedEventArgs("Authenticator", sender, e));
}
}
#region ICloneable
/// <summary>
/// Clone return a new WinAuthConfig object
/// </summary>
/// <returns></returns>
public object Clone()
{
WinAuthConfig clone = (WinAuthConfig)this.MemberwiseClone();
// close the internal authenticator so the data is kept separate
clone.OnConfigChanged = null;
clone._authenticators = new List<WinAuthAuthenticator>();
foreach (var wa in _authenticators)
{
clone._authenticators.Add(wa.Clone() as WinAuthAuthenticator);
}
clone.CurrentAuthenticator = (this.CurrentAuthenticator != null ? clone._authenticators[this._authenticators.IndexOf(this.CurrentAuthenticator)] : null);
return clone;
}
public bool ReadXml(XmlReader reader, string password = null)
{
bool changed = false;
reader.Read();
while (reader.EOF == false && reader.IsEmptyElement == true)
{
reader.Read();
}
reader.MoveToContent();
while (reader.EOF == false)
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
// v2 file
case "WinAuth":
changed = ReadXmlInternal(reader, password);
break;
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
break;
}
}
return changed;
}
protected bool ReadXmlInternal(XmlReader reader, string password = null)
{
bool changed = false;
decimal version;
if (decimal.TryParse(reader.GetAttribute("version"), System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out version) == true)
{
Version = version;
if (version > WinAuthConfig.CURRENTVERSION)
{
// ensure we don't overwrite a newer config
throw new WinAuthInvalidNewerConfigException(string.Format(strings.ConfigIsNewer, version));
}
}
string encrypted = reader.GetAttribute("encrypted");
this.PasswordType = Authenticator.DecodePasswordTypes(encrypted);
if (this.PasswordType != Authenticator.PasswordTypes.None)
{
// read the encrypted text from the node
string data = reader.ReadElementContentAsString();
// decrypt
YubiKey yubi = null;
if ((this.PasswordType & (Authenticator.PasswordTypes.YubiKeySlot1 | Authenticator.PasswordTypes.YubiKeySlot2)) != 0 /* && this.Yubi == null */)
{
yubi = YubiKey.CreateInstance();
}
data = Authenticator.DecryptSequence(data, this.PasswordType, password, yubi);
using (MemoryStream ms = new MemoryStream(Authenticator.StringToByteArray(data)))
{
reader = XmlReader.Create(ms);
changed = ReadXml(reader, password);
}
this.PasswordType = Authenticator.DecodePasswordTypes(encrypted);
this.Password = password;
this.Yubi = yubi;
return changed;
}
reader.MoveToContent();
if (reader.IsEmptyElement)
{
reader.Read();
return changed;
}
bool defaultAutoRefresh = true;
bool defaultAllowCopy = false;
bool defaultCopyOnCode = false;
bool defaultHideSerial = true;
string defaultSkin = null;
reader.Read();
while (reader.EOF == false)
{
if (reader.IsStartElement())
{
switch (reader.Name)
{
case "config":
changed = ReadXmlInternal(reader, password) || changed;
break;
// 3.2 has new layout
case "data":
{
encrypted = reader.GetAttribute("encrypted");
this.PasswordType = Authenticator.DecodePasswordTypes(encrypted);
if (this.PasswordType != Authenticator.PasswordTypes.None)
{
HashAlgorithm hasher;
string hash = reader.GetAttribute("sha1");
if (string.IsNullOrEmpty(hash) == false)
{
hasher = Authenticator.SafeHasher("SHA1");
}
else
{
// old version has md5
hash = reader.GetAttribute("md5");
hasher = Authenticator.SafeHasher("MD5");
}
// read the encrypted text from the node
string data = reader.ReadElementContentAsString();
hasher.ComputeHash(Authenticator.StringToByteArray(data));
#if !NETFX_3
hasher.Dispose();
#endif
// decrypt
YubiKey yubi = null;
if ((this.PasswordType & (Authenticator.PasswordTypes.YubiKeySlot1 | Authenticator.PasswordTypes.YubiKeySlot2)) != 0 /* && this.Yubi == null */)
{
yubi = YubiKey.CreateInstance();
}
data = Authenticator.DecryptSequence(data, this.PasswordType, password, yubi);
byte[] plain = Authenticator.StringToByteArray(data);
using (MemoryStream ms = new MemoryStream(plain))
{
var datareader = XmlReader.Create(ms);
changed = ReadXmlInternal(datareader, password) || changed;
}
this.PasswordType = Authenticator.DecodePasswordTypes(encrypted);
this.Password = password;
this.Yubi = yubi;
}
}
break;
case "alwaysontop":
_alwaysOnTop = reader.ReadElementContentAsBoolean();
break;
case "usetrayicon":
_useTrayIcon = reader.ReadElementContentAsBoolean();
break;
case "notifyaction":
string s = reader.ReadElementContentAsString();
if (string.IsNullOrEmpty(s) == false)
{
try {
_notifyAction = (NotifyActions)Enum.Parse(typeof(NotifyActions), s, true);
}
catch (Exception) { }
}
break;
case "startwithwindows":
_startWithWindows = reader.ReadElementContentAsBoolean();
break;
case "autosize":
_autoSize = reader.ReadElementContentAsBoolean();
break;
case "left":
_position.X = reader.ReadElementContentAsInt();
break;
case "top":
_position.Y = reader.ReadElementContentAsInt();
break;
case "width":
_width = reader.ReadElementContentAsInt();
break;
case "height":
_height = reader.ReadElementContentAsInt();
break;
case "shadowtype":
_shadowType = reader.ReadElementContentAsString();
break;
case "pgpkey":
_pgpKey = reader.ReadElementContentAsString();
break;
case "settings":
XmlSerializer serializer = new XmlSerializer(typeof(setting[]), new XmlRootAttribute() { ElementName = "settings" });
_settings = ((setting[])serializer.Deserialize(reader)).ToDictionary(e => e.Key, e => e.Value);
break;
// previous setting used as defaults for new
case "autorefresh":
defaultAutoRefresh = reader.ReadElementContentAsBoolean();
break;
case "allowcopy":
defaultAllowCopy = reader.ReadElementContentAsBoolean();
break;
case "copyoncode":
defaultCopyOnCode = reader.ReadElementContentAsBoolean();
break;
case "hideserial":
defaultHideSerial = reader.ReadElementContentAsBoolean();
break;
case "skin":
defaultSkin = reader.ReadElementContentAsString();
break;
case "WinAuthAuthenticator":
var wa = new WinAuthAuthenticator();
changed = wa.ReadXml(reader, password) || changed;
this.Add(wa);
if (this.CurrentAuthenticator == null)
{
this.CurrentAuthenticator = wa;
}
break;
// for old 2.x configs
case "authenticator":
var waold = new WinAuthAuthenticator();
waold.AuthenticatorData = Authenticator.ReadXmlv2(reader, password);
if (waold.AuthenticatorData is BattleNetAuthenticator)
{
waold.Name = "Battle.net";
}
else if (waold.AuthenticatorData is GuildWarsAuthenticator)
{
waold.Name = "GuildWars 2";
}
else if (waold.AuthenticatorData is GuildWarsAuthenticator)
{
waold.Name = "Authenticator";
}
this.Add(waold);
this.CurrentAuthenticator = waold;
waold.AutoRefresh = defaultAutoRefresh;
waold.AllowCopy = defaultAllowCopy;
waold.CopyOnCode = defaultCopyOnCode;
waold.HideSerial = defaultHideSerial;
break;
// old 2.x auto login script
case "autologin":
var hks = new HoyKeySequence();
hks.ReadXml(reader, password);
if (hks.HotKey != 0)
{
if (this.CurrentAuthenticator.HotKey == null)
{
this.CurrentAuthenticator.HotKey = new HotKey();
}
HotKey hotkey = this.CurrentAuthenticator.HotKey;
hotkey.Action = HotKey.HotKeyActions.Inject;
hotkey.Key = hks.HotKey;
hotkey.Modifiers = hks.Modifiers;
if (hks.WindowTitleRegex == true && string.IsNullOrEmpty(hks.WindowTitle) == false)
{
hotkey.Window = "/" + Regex.Escape(hks.WindowTitle);
}
else if (string.IsNullOrEmpty(hks.WindowTitle) == false)
{
hotkey.Window = hks.WindowTitle;
}
else if (string.IsNullOrEmpty(hks.ProcessName) == false)
{
hotkey.Window = hks.ProcessName;
}
if (hks.Advanced == true)
{
hotkey.Action = HotKey.HotKeyActions.Advanced;
hotkey.Advanced = hks.AdvancedScript;
}
}
break;
default:
reader.Skip();
break;
}
}
else
{
reader.Read();
break;
}
}
return changed;
}
/// <summary>
/// Write the data as xml into an XmlWriter
/// </summary>
/// <param name="writer">XmlWriter to write config</param>
public void WriteXmlString(XmlWriter writer, bool includeFilename = false, bool includeSettings = true)
{
writer.WriteStartDocument(true);
//
if (includeFilename == true && string.IsNullOrEmpty(this.Filename) == false)
{
writer.WriteComment(this.Filename);
}
//
writer.WriteStartElement("WinAuth");
writer.WriteAttributeString("version", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(2));
//
writer.WriteStartElement("alwaysontop");
writer.WriteValue(this.AlwaysOnTop);
writer.WriteEndElement();
//
writer.WriteStartElement("usetrayicon");
writer.WriteValue(this.UseTrayIcon);
writer.WriteEndElement();
//
writer.WriteStartElement("notifyaction");
writer.WriteValue(Enum.GetName(typeof(NotifyActions), this.NotifyAction));
writer.WriteEndElement();
//
writer.WriteStartElement("startwithwindows");
writer.WriteValue(this.StartWithWindows);
writer.WriteEndElement();
//
writer.WriteStartElement("autosize");
writer.WriteValue(this.AutoSize);
writer.WriteEndElement();
//
if (this.Position.IsEmpty == false)
{
writer.WriteStartElement("left");
writer.WriteValue(this.Position.X);
writer.WriteEndElement();
writer.WriteStartElement("top");
writer.WriteValue(this.Position.Y);
writer.WriteEndElement();
}
//
writer.WriteStartElement("width");
writer.WriteValue(this.Width);
writer.WriteEndElement();
//
writer.WriteStartElement("height");
writer.WriteValue(this.Height);
writer.WriteEndElement();
//
if (string.IsNullOrEmpty(this.ShadowType) == false)
{
writer.WriteStartElement("shadowtype");
writer.WriteValue(this.ShadowType);
writer.WriteEndElement();
}
//
if (string.IsNullOrEmpty(this.PGPKey) == false)
{
writer.WriteStartElement("pgpkey");
writer.WriteCData(this.PGPKey);
writer.WriteEndElement();
}
if (PasswordType != Authenticator.PasswordTypes.None)
{
writer.WriteStartElement("data");
StringBuilder encryptedTypes = new StringBuilder();
if ((PasswordType & Authenticator.PasswordTypes.Explicit) != 0)
{
encryptedTypes.Append("y");
}
if ((PasswordType & Authenticator.PasswordTypes.User) != 0)
{
encryptedTypes.Append("u");
}
if ((PasswordType & Authenticator.PasswordTypes.Machine) != 0)
{
encryptedTypes.Append("m");
}
if ((PasswordType & Authenticator.PasswordTypes.YubiKeySlot1) != 0)
{
encryptedTypes.Append("a");
}
if ((PasswordType & Authenticator.PasswordTypes.YubiKeySlot2) != 0)
{
encryptedTypes.Append("b");
}
writer.WriteAttributeString("encrypted", encryptedTypes.ToString());
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.Encoding = Encoding.UTF8;
using (XmlWriter encryptedwriter = XmlWriter.Create(ms, settings))
{
encryptedwriter.WriteStartElement("config");
foreach (WinAuthAuthenticator wa in this)
{
wa.WriteXmlString(encryptedwriter);
}
encryptedwriter.WriteEndElement();
}
data = ms.ToArray();
}
using (var hasher = Authenticator.SafeHasher("SHA1"))
{
string encdata = Authenticator.EncryptSequence(Authenticator.ByteArrayToString(data), PasswordType, Password, this.Yubi);
string enchash = Authenticator.ByteArrayToString(hasher.ComputeHash(Authenticator.StringToByteArray(encdata)));
writer.WriteAttributeString("sha1", enchash);
writer.WriteString(encdata);
}
writer.WriteEndElement();
}
else
{
foreach (WinAuthAuthenticator wa in this)
{
wa.WriteXmlString(writer);
}
}
if (includeSettings == true && _settings.Count != 0)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
XmlSerializer serializer = new XmlSerializer(typeof(setting[]), new XmlRootAttribute() { ElementName = "settings" });
serializer.Serialize(writer, _settings.Select(e => new setting { Key = e.Key, Value = e.Value }).ToArray(), ns);
}
// close WinAuth
writer.WriteEndElement();
// end document
writer.WriteEndDocument();
}
#endregion
}
/// <summary>
/// Config change event arguments
/// </summary>
public class ConfigChangedEventArgs : EventArgs
{
public string PropertyName { get; private set; }
public WinAuthAuthenticator Authenticator { get; private set; }
public WinAuthAuthenticatorChangedEventArgs AuthenticatorChangedEventArgs { get; private set; }
/// <summary>
/// Default constructor
/// </summary>
public ConfigChangedEventArgs(string propertyName, WinAuthAuthenticator authenticator = null, WinAuthAuthenticatorChangedEventArgs acargs = null)
: base()
{
PropertyName = propertyName;
Authenticator = authenticator;
AuthenticatorChangedEventArgs = acargs;
}
}
public class WinAuthInvalidConfigException : ApplicationException
{
public WinAuthInvalidConfigException(string msg, Exception ex)
: base(msg, ex)
{
}
}
public class WinAuthConfigRequirePasswordException : ApplicationException
{
public WinAuthConfigRequirePasswordException()
: base()
{
}
}
public class WinAuthInvalidNewerConfigException : ApplicationException
{
public WinAuthInvalidNewerConfigException(string msg)
: base(msg)
{
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/YongboZhu/winauth.git
git@gitee.com:YongboZhu/winauth.git
YongboZhu
winauth
winauth
master

搜索帮助