Config: account tweaks, UTF-8 fix, update server url

This commit is contained in:
Liza 2023-02-15 10:20:25 +01:00
parent 6412afbfbb
commit 4f8deea8e0
5 changed files with 43 additions and 19 deletions

View File

@ -32,9 +32,11 @@ namespace Pal.Client.Configuration
throw new InvalidOperationException("invalid account id format");
}
public string EncryptedId { get; init; } = null!;
[JsonPropertyName("Id")]
[JsonInclude]
public string EncryptedId { get; private set; } = null!;
public string Server { get; set; } = null!;
public string Server { get; init; } = null!;
[JsonIgnore] public bool IsUsable => DecryptAccountId(EncryptedId) != null;
@ -56,7 +58,7 @@ namespace Pal.Client.Configuration
ConfigurationData.Entropy, DataProtectionScope.CurrentUser);
return new Guid(guidBytes);
}
catch (CryptographicException e)
catch (Exception e)
{
PluginLog.Verbose(e, $"Could not load account id {id}");
return null;
@ -71,10 +73,21 @@ namespace Pal.Client.Configuration
DataProtectionScope.CurrentUser);
return $"s:{Convert.ToBase64String(guidBytes)}";
}
catch (CryptographicException)
catch (Exception)
{
return g.ToString();
}
}
public bool EncryptIfNeeded()
{
if (Guid.TryParse(EncryptedId, out Guid g))
{
string oldId = EncryptedId;
EncryptedId = EncryptAccountId(g);
return oldId != EncryptedId;
}
return false;
}
}
}

View File

@ -1,13 +1,12 @@
using System;
using System.IO;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using Dalamud.Logging;
using Dalamud.Plugin;
using ImGuiNET;
using Newtonsoft.Json;
using JsonSerializer = System.Text.Json.JsonSerializer;
using NJson = Newtonsoft.Json;
namespace Pal.Client.Configuration
{
@ -31,7 +30,7 @@ namespace Pal.Client.Configuration
PluginLog.Information("Migrating config file from v1-v6 format");
ConfigurationV1 configurationV1 =
JsonConvert.DeserializeObject<ConfigurationV1>(
NJson.JsonConvert.DeserializeObject<ConfigurationV1>(
File.ReadAllText(_pluginInterface.ConfigFile.FullName)) ?? new ConfigurationV1();
configurationV1.Migrate();
configurationV1.Save();
@ -52,7 +51,7 @@ namespace Pal.Client.Configuration
public void Save(IConfigurationInConfigDirectory config)
{
File.WriteAllText(ConfigPath,
JsonSerializer.Serialize(config, config.GetType(), new JsonSerializerOptions { WriteIndented = true }),
JsonSerializer.Serialize(config, config.GetType(), new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping }),
Encoding.UTF8);
}
@ -94,7 +93,8 @@ namespace Pal.Client.Configuration
if (string.IsNullOrEmpty(accountId))
continue;
IAccountConfiguration newAccount = v7.CreateAccount(server, accountId);
string serverName = server.Replace(".μ.tv", ".liza.sh");
IAccountConfiguration newAccount = v7.CreateAccount(serverName, accountId);
newAccount.CachedRoles = oldAccount.CachedRoles.ToList();
}

View File

@ -72,9 +72,9 @@ namespace Pal.Client.Configuration
public interface IAccountConfiguration
{
public bool IsUsable { get; }
public string Server { get; }
public Guid AccountId { get; }
bool IsUsable { get; }
string Server { get; }
Guid AccountId { get; }
/// <summary>
/// This is taken from the JWT, and is only refreshed on a successful login.
@ -84,6 +84,8 @@ namespace Pal.Client.Configuration
/// This has no impact on what roles the JWT actually contains, but is just to make it
/// easier to draw a consistent UI. The server will still reject unauthorized calls.
/// </summary>
public List<string> CachedRoles { get; set; }
List<string> CachedRoles { get; set; }
bool EncryptIfNeeded();
}
}

View File

@ -54,7 +54,7 @@ namespace Pal.Client.Net
var createAccountReply = await accountClient.CreateAccountAsync(new CreateAccountRequest(), headers: UnauthorizedHeaders(), deadline: DateTime.UtcNow.AddSeconds(10), cancellationToken: cancellationToken);
if (createAccountReply.Success)
{
if (Guid.TryParse(createAccountReply.AccountId, out Guid accountId))
if (!Guid.TryParse(createAccountReply.AccountId, out Guid accountId))
throw new InvalidOperationException("invalid account id returned");
configuredAccount = Service.Configuration.CreateAccount(RemoteUrl, accountId);
@ -92,7 +92,16 @@ namespace Pal.Client.Net
PluginLog.Information($"TryConnect: Login successful with account id: {configuredAccount.AccountId.ToPartialId()}");
_loginInfo = new LoginInfo(loginReply.AuthToken);
configuredAccount.CachedRoles = _loginInfo.Claims?.Roles.ToList() ?? new List<string>();
bool save = configuredAccount.EncryptIfNeeded();
List<string> newRoles = _loginInfo.Claims?.Roles.ToList() ?? new();
if (!newRoles.SequenceEqual(configuredAccount.CachedRoles))
{
configuredAccount.CachedRoles = newRoles;
save = true;
}
if (save)
Service.ConfigurationManager.Save(Service.Configuration);
}
else

View File

@ -14,7 +14,7 @@ namespace Pal.Client.Net
#if DEBUG
public const string RemoteUrl = "http://localhost:5145";
#else
public const string RemoteUrl = "https://pal.μ.tv";
public const string RemoteUrl = "https://pal.liza.sh";
#endif
private readonly string _userAgent = $"{typeof(RemoteApi).Assembly.GetName().Name?.Replace(" ", "")}/{typeof(RemoteApi).Assembly.GetName().Version?.ToString(2)}";