diff --git a/OpenLyricsClient/Backend/Core.cs b/OpenLyricsClient/Backend/Core.cs index 8ef7181..3aed65b 100644 --- a/OpenLyricsClient/Backend/Core.cs +++ b/OpenLyricsClient/Backend/Core.cs @@ -34,6 +34,7 @@ class Core private Debugger _debugger; + private Sealing _sealing; private SettingsHandler _settingsHandler; private ServiceHandler _serviceHandler; @@ -83,6 +84,8 @@ public Core() this._windowLogger = new WindowLogger(); + this._sealing = new Sealing(); + string workingDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + string.Format("{0}OpenLyricsClient{0}", System.IO.Path.DirectorySeparatorChar); @@ -106,7 +109,7 @@ public Core() _loaded = true; - SettingsHandler.TriggerGlobal(); + Task.Factory.StartNew(async () => await SettingsHandler.TriggerGlobal()); } private async Task TickTask() @@ -153,6 +156,11 @@ protected virtual void SlowTickEvent() slowTickEventHandler?.Invoke(this); } + public Sealing Sealing + { + get => _sealing; + } + public SettingsHandler SettingsHandler { get => this._settingsHandler; diff --git a/OpenLyricsClient/Backend/Helper/Sealing.cs b/OpenLyricsClient/Backend/Helper/Sealing.cs new file mode 100644 index 0000000..882917d --- /dev/null +++ b/OpenLyricsClient/Backend/Helper/Sealing.cs @@ -0,0 +1,39 @@ +using System; +using System.Diagnostics; +using DevBase.Cryptography.BouncyCastle.AES; + +namespace OpenLyricsClient.Backend.Helper; + +public class Sealing +{ + private string _serverPublicKey; + private string _simpleEncryptionKey; + + private DevBase.Cryptography.BouncyCastle.Sealing.Sealing _sealing; + private AESBuilderEngine _aesBuilder; + + public Sealing() + { + this._serverPublicKey = "MIIBSzCCAQMGByqGSM49AgEwgfcCAQEwLAYHKoZIzj0BAQIhAP////8AAAABAAAAAAAAAAAAAAAA////////////////MFsEIP////8AAAABAAAAAAAAAAAAAAAA///////////////8BCBaxjXYqjqT57PrvVV2mIa8ZR0GsMxTsPY7zjw+J9JgSwMVAMSdNgiG5wSTamZ44ROdJreBn36QBEEEaxfR8uEsQkf4vOblY6RA8ncDfYEt6zOg9KE5RdiYwpZP40Li/hp/m47n60p8D54WK84zV2sxXs7LtkBoN79R9QIhAP////8AAAAA//////////+85vqtpxeehPO5ysL8YyVRAgEBA0IABBqSdbiWAMxcEig+rX1FlApI7pE/kPNUmejo5PXvElsf6pjHuDlBN4fYvpmaX6lncddAuNPnQmZ89Ogb95xwPnA="; + this._sealing = new DevBase.Cryptography.BouncyCastle.Sealing.Sealing(this._serverPublicKey); + + this._simpleEncryptionKey = "g0o6Z9cInEYPJlLTNcm8iZND5AWeRfepa6xQhjpXd/k="; + + this._aesBuilder = new AESBuilderEngine() + .SetKey(this._simpleEncryptionKey) + .SetRandomSeed(); + + } + public string SimpleEncrypt(string data) => this._aesBuilder.EncryptString(data); + + public string SimpleDecrypt(string encryptedData) => this._aesBuilder.DecryptString(encryptedData); + + public string Seal(string data) => this._sealing.Seal(data); + + public string UnSeal(string sealedData) => this._sealing.UnSeal(sealedData); + + public string ServerPublicKey + { + get => _serverPublicKey; + } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Settings/Sections/Account/AccountSection.cs b/OpenLyricsClient/Backend/Settings/Sections/Account/AccountSection.cs new file mode 100644 index 0000000..1dcb992 --- /dev/null +++ b/OpenLyricsClient/Backend/Settings/Sections/Account/AccountSection.cs @@ -0,0 +1,79 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using DevBase.Api.Apis.OpenLyricsClient.Structure.Json; +using Newtonsoft.Json.Linq; +using OpenLyricsClient.Backend; +using OpenLyricsClient.Backend.Settings; +using OpenLyricsClient.Backend.Settings.Sections.Account; +using OpenLyricsClient.Backend.Structure; +using OpenLyricsClient.Backend.Utils; +using OpenLyricsClient = DevBase.Api.Apis.OpenLyricsClient.OpenLyricsClient; + +public class AccountSection : ISettingSection +{ + private FileInfo _file; + private JObject _data; + + public AccountSection(string filePath) + { + this._file = new FileInfo(filePath); + } + + public async Task WriteToDisk() + { + string encoded = Core.INSTANCE.Sealing.SimpleEncrypt(this._data.ToString()); + await File.WriteAllTextAsync(this._file.FullName, encoded); + } + + public async Task ReadFromDisk() + { + if (!this._file.Exists) + { + this._data = Defaults(); + await WriteToDisk(); + return; + } + + await using FileStream stream = this._file.OpenRead(); + using StreamReader reader = new StreamReader(stream); + + string decoded = Core.INSTANCE.Sealing.SimpleDecrypt(reader.ReadToEnd()); + + this._data = JObject.Parse(decoded); + + await stream.FlushAsync(); + + stream.Close(); + reader.Close(); + } + + public T GetValue(string field) + { + return (T)this._data[field].ToObject(); + } + + public async Task SetValue(string field, T value) + { + this._data[field] = JToken.FromObject(value); + await WriteToDisk(); + } + + public JObject Defaults() + { + JsonOpenLyricsClientSubscription account = + new DevBase.Api.Apis.OpenLyricsClient.OpenLyricsClient(Core.INSTANCE.Sealing.ServerPublicKey) + .CreateSubscription() + .GetAwaiter() + .GetResult(); + + Structure structure = new Structure + { + UserID = account.UserID, + UserSecret = account.UserSecret + }; + + return new JsonDeserializer().Serialize(structure); + } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Settings/Sections/Account/Structure.cs b/OpenLyricsClient/Backend/Settings/Sections/Account/Structure.cs new file mode 100644 index 0000000..ec35bdd --- /dev/null +++ b/OpenLyricsClient/Backend/Settings/Sections/Account/Structure.cs @@ -0,0 +1,7 @@ +namespace OpenLyricsClient.Backend.Settings.Sections.Account; + +public class Structure +{ + public string UserID { get; set; } + public string UserSecret { get; set; } +} \ No newline at end of file diff --git a/OpenLyricsClient/Backend/Settings/Sections/Connection/Spotify/SpotifySection.cs b/OpenLyricsClient/Backend/Settings/Sections/Connection/Spotify/SpotifySection.cs index 6016087..b15f93e 100644 --- a/OpenLyricsClient/Backend/Settings/Sections/Connection/Spotify/SpotifySection.cs +++ b/OpenLyricsClient/Backend/Settings/Sections/Connection/Spotify/SpotifySection.cs @@ -25,7 +25,8 @@ public SpotifySection(string filePath) public async Task WriteToDisk() { - await File.WriteAllTextAsync(this._file.FullName, this._data.ToString()); + string encoded = Core.INSTANCE.Sealing.SimpleEncrypt(this._data.ToString()); + await File.WriteAllTextAsync(this._file.FullName, encoded); } public async Task ReadFromDisk() @@ -40,7 +41,9 @@ public async Task ReadFromDisk() await using FileStream stream = this._file.OpenRead(); using StreamReader reader = new StreamReader(stream); - this._data = JObject.Parse(reader.ReadToEnd()); + string decoded = Core.INSTANCE.Sealing.SimpleDecrypt(reader.ReadToEnd()); + + this._data = JObject.Parse(decoded); await stream.FlushAsync(); diff --git a/OpenLyricsClient/Backend/Settings/Sections/Tokens/TokenSection.cs b/OpenLyricsClient/Backend/Settings/Sections/Tokens/TokenSection.cs index 68e715e..0123588 100644 --- a/OpenLyricsClient/Backend/Settings/Sections/Tokens/TokenSection.cs +++ b/OpenLyricsClient/Backend/Settings/Sections/Tokens/TokenSection.cs @@ -67,7 +67,7 @@ public async Task RemoveToken(MusixMatchToken token) public async Task RemoveUsage(MusixMatchToken token) { - List tokens = GetValue>("Tokens"); + /*List tokens = GetValue>("Tokens"); for (int i = 0; i < tokens.Capacity; i++) { @@ -84,7 +84,7 @@ public async Task RemoveUsage(MusixMatchToken token) await RemoveToken(token); await AddToken(newToken); } - } + }*/ } public T GetValue(string field) diff --git a/OpenLyricsClient/Backend/Settings/SettingsHandler.cs b/OpenLyricsClient/Backend/Settings/SettingsHandler.cs index e0ccd08..f8a028e 100644 --- a/OpenLyricsClient/Backend/Settings/SettingsHandler.cs +++ b/OpenLyricsClient/Backend/Settings/SettingsHandler.cs @@ -33,6 +33,9 @@ public SettingsHandler(string workingDirectory) this._sections.Add(new RomanizationSection(string.Format("{0}{1}", workingDirectory, "Romanization.json"))); + + this._sections.Add(new AccountSection(string.Format("{0}{1}", + workingDirectory, "Account.json"))); Task.Factory.StartNew(Initialize).GetAwaiter().GetResult(); }