Skip to content

Commit

Permalink
Spotify Access is now encrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDotH committed Apr 27, 2023
1 parent 9194516 commit 60b097d
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 5 deletions.
10 changes: 9 additions & 1 deletion OpenLyricsClient/Backend/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Core

private Debugger<Core> _debugger;

private Sealing _sealing;
private SettingsHandler _settingsHandler;

private ServiceHandler _serviceHandler;
Expand Down Expand Up @@ -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);
Expand All @@ -106,7 +109,7 @@ public Core()

_loaded = true;

SettingsHandler.TriggerGlobal();
Task.Factory.StartNew(async () => await SettingsHandler.TriggerGlobal());
}

private async Task TickTask()
Expand Down Expand Up @@ -153,6 +156,11 @@ protected virtual void SlowTickEvent()
slowTickEventHandler?.Invoke(this);
}

public Sealing Sealing
{
get => _sealing;
}

public SettingsHandler SettingsHandler
{
get => this._settingsHandler;
Expand Down
39 changes: 39 additions & 0 deletions OpenLyricsClient/Backend/Helper/Sealing.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<T>(string field)
{
return (T)this._data[field].ToObject<T>();
}

public async Task SetValue<T>(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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace OpenLyricsClient.Backend.Settings.Sections.Account;

public class Structure
{
public string UserID { get; set; }
public string UserSecret { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task RemoveToken(MusixMatchToken token)

public async Task RemoveUsage(MusixMatchToken token)
{
List<MusixMatchToken> tokens = GetValue<List<MusixMatchToken>>("Tokens");
/*List<MusixMatchToken> tokens = GetValue<List<MusixMatchToken>>("Tokens");
for (int i = 0; i < tokens.Capacity; i++)
{
Expand All @@ -84,7 +84,7 @@ public async Task RemoveUsage(MusixMatchToken token)
await RemoveToken(token);
await AddToken(newToken);
}
}
}*/
}

public T GetValue<T>(string field)
Expand Down
3 changes: 3 additions & 0 deletions OpenLyricsClient/Backend/Settings/SettingsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 60b097d

Please sign in to comment.