Skip to content

Commit

Permalink
feat: rename plugin namespace and use wox storage to save config
Browse files Browse the repository at this point in the history
  • Loading branch information
KawaiiZapic committed Dec 30, 2023
1 parent 297b0e3 commit 70209aa
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Configurations>Debug;Release</Configurations>
<Authors>Zapic</Authors>
<Version>1.2.1</Version>
<Version>$([System.IO.File]::ReadAllText('$(MSBuildThisFileDirectory)plugin.json').Split(',')[5].Split(':')[1].Trim().Trim('"'))</Version>
<Title>Powertoys Run TOTP</Title>
<Product>Powertoys Run TOTP Plugin</Product>
<RepositoryUrl>https://github.com/KawaiiZapic/PowertoysRunTOTP</RepositoryUrl>
Expand Down
98 changes: 50 additions & 48 deletions Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
using System.Text.Json;
using System.Security.Cryptography;
using System.Text;
using Wox.Infrastructure.Storage;

namespace PowertoysRunTOTP {
public class ConfigStruct {
namespace Community.PowerToys.Run.Plugin.TOTP {
public class OTPList {
public class KeyEntry {
public string Name { get; set; }
public string Key { get; set; }
Expand All @@ -18,63 +19,29 @@ public class KeyEntry {


public static class Config {
private static readonly string DataDirectory = Environment.ExpandEnvironmentVariables("%LOCALAPPDATA%") + "\\Microsoft\\PowerToys\\PowerToys Run\\Settings\\Plugins\\TOTP\\";
private static readonly string ConfigPath = DataDirectory + "Config.json";
private static readonly int Version = 1;

public static ConfigStruct LoadConfig() {
var fileInfo = new FileInfo(ConfigPath);
if (!fileInfo.Exists) {
if (!fileInfo.Directory!.Exists) {
Directory.CreateDirectory(fileInfo.Directory!.FullName);
}
var newFile = File.Create(ConfigPath);
var options = new JsonSerializerOptions { WriteIndented = true };
var EmptyConfig = new ConfigStruct {
Entries = new List<ConfigStruct.KeyEntry>(),
Version = Version,
};
JsonSerializer.Serialize(newFile, EmptyConfig, options);
newFile.Dispose();
}
var file = File.OpenRead(ConfigPath);
try {
var result = JsonSerializer.Deserialize<ConfigStruct>(file) ?? throw new Exception("Failed to load config: Result is null");
return result;
} finally {
file.Dispose();
}

}

public static List<ConfigStruct.KeyEntry> LoadKeyList() {
return LoadConfig().Entries;
}

public static void SaveConfig(ConfigStruct config) {
var fileInfo = new FileInfo(ConfigPath);
if (!fileInfo.Directory!.Exists) {
Directory.CreateDirectory(fileInfo.Directory!.FullName);
}
var file = File.Open(ConfigPath, FileMode.Create);
var options = new JsonSerializerOptions { WriteIndented = true };
try {
JsonSerializer.Serialize(file, config, options);
} finally {
file.Dispose();
public static List<OTPList.KeyEntry> LoadKeyList() {
var nc = new PluginJsonStorage<OTPList>();
var config = nc.Load();
if (config.Entries == null) {
config.Version = 2;
config.Entries = new List<OTPList.KeyEntry>();
nc.Save();
}
return config.Entries;
}

public static void SaveKeyList(List<ConfigStruct.KeyEntry> list) {
var config = LoadConfig();
public static void SaveKeyList(List<OTPList.KeyEntry> list) {
var nc = new PluginJsonStorage<OTPList>();
var config = nc.Load();
foreach (var entry in list) {
if (entry.IsEncrypted != true) {
entry.Key = EncryptKey(entry.Key);
entry.IsEncrypted = true;
}
}
config.Entries = list;
SaveConfig(config);
nc.Save();
}

public static string DecryptKey(string encrypted) {
Expand Down Expand Up @@ -136,4 +103,39 @@ public static void Migrate() {
}

}
public static class ConfigMigratorV1 {
private static readonly string DataDirectoryV1 = Environment.ExpandEnvironmentVariables("%LOCALAPPDATA%") + "\\Microsoft\\PowerToys\\PowerToys Run\\Settings\\Plugins\\TOTP\\";
private static readonly string ConfigPathV1 = DataDirectoryV1 + "Config.json";

public class OTPList {
public class KeyEntry {
public string Name { get; set; }
public string Key { get; set; }
public bool IsEncrypted { get; set; }

}

public int Version { get; set; }
public List<KeyEntry> Entries { get; set; }
}

public static void Migrate() {
if (!new FileInfo(ConfigPathV1).Exists)
return;

var FileV1 = File.Open(ConfigPathV1, FileMode.Open);
var ConfigV1 = JsonSerializer.Deserialize<OTPList>(FileV1) ?? throw new Exception("Config should not be null");

var ConfigV2 = new PluginJsonStorage<OTPList>();
var config = ConfigV2.Load();
config.Version = 2;
config.Entries = ConfigV1.Entries;
ConfigV2.Save();

FileV1.Close();
File.Delete(ConfigPathV1);
Directory.Delete(DataDirectoryV1);
}

}
}
16 changes: 9 additions & 7 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
using System.Web;
using System.Windows;
using OtpNet;
using PowertoysRunTOTP;

namespace PowerToysRunTOTP {
namespace Community.PowerToys.Run.Plugin.TOTP {

public class Main: IPlugin {
public static string PluginID => "2FC51DBA9F0F42108E26602486C186C1";
Expand Down Expand Up @@ -34,14 +33,14 @@ public List<Result> Query(Query query) {
Action = (e) => {
try {
var list = Config.LoadKeyList();
list.Add(new ConfigStruct.KeyEntry {
list.Add(new OTPList.KeyEntry {
Key = sercet,
Name = name,
IsEncrypted = false
});
Config.SaveKeyList(list);
} catch (Exception ex) {
MessageBox.Show(ex.Message + ex.StackTrace, "PowerToys TOTP Ran into error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "PowerToys TOTP Ran into error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
return true;
}
Expand Down Expand Up @@ -83,7 +82,7 @@ public List<Result> Query(Query query) {
} else {
name = item.Issuer + ": " + item.Name;
}
list.Add(new ConfigStruct.KeyEntry{
list.Add(new OTPList.KeyEntry{
Name = name,
Key = key,
IsEncrypted = false
Expand All @@ -92,7 +91,7 @@ public List<Result> Query(Query query) {
try {
Config.SaveKeyList(list);
} catch(Exception ex) {
MessageBox.Show(ex.Message + ex.StackTrace, "PowerToys TOTP Ran into error", MessageBoxButton.OK, MessageBoxImage.Exclamation);}
MessageBox.Show(ex.Message + "\n" + ex.StackTrace, "PowerToys TOTP Ran into error", MessageBoxButton.OK, MessageBoxImage.Exclamation);}
return true;
}
}
Expand All @@ -110,7 +109,7 @@ public List<Result> Query(Query query) {
};
}
}
List<ConfigStruct.KeyEntry> totpList;
List<OTPList.KeyEntry> totpList;
try {
totpList = Config.LoadKeyList();
} catch (Exception ex) {
Expand Down Expand Up @@ -174,6 +173,9 @@ public void Init(PluginInitContext context) {
try {
ConfigMigratorV0.Migrate();
} catch (Exception) { }
try {
ConfigMigratorV1.Migrate();
} catch (Exception) { }
}

private void UpdateIconPath(Theme theme) {
Expand Down
Loading

0 comments on commit 70209aa

Please sign in to comment.