fix issues with uid selection and in case of a bad/unparsable token

This commit is contained in:
Stanley Dimant
2024-10-29 15:09:58 +01:00
parent 4f8292e2bf
commit 1ef6f50246
3 changed files with 42 additions and 24 deletions

View File

@@ -194,9 +194,19 @@ public class ServerConfigurationManager
{ {
JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler(); JwtSecurityTokenHandler handler = new JwtSecurityTokenHandler();
if (server.OAuthToken == null) return string.Empty; if (server.OAuthToken == null) return string.Empty;
try
{
var token = handler.ReadJwtToken(server.OAuthToken); var token = handler.ReadJwtToken(server.OAuthToken);
return token.Claims.First(f => string.Equals(f.Type, "discord_user", StringComparison.Ordinal)).Value!; return token.Claims.First(f => string.Equals(f.Type, "discord_user", StringComparison.Ordinal)).Value!;
} }
catch (Exception ex)
{
_logger.LogWarning(ex, "Could not read jwt, resetting it");
server.OAuthToken = null;
Save();
return string.Empty;
}
}
public string[] GetServerNames() public string[] GetServerNames()
{ {

View File

@@ -1315,7 +1315,7 @@ public class SettingsUi : WindowMediatorSubscriberBase
} }
else else
{ {
friendlyName = item.UID; friendlyName = item.UID ?? "-";
friendlyNameTranslation = "UID"; friendlyNameTranslation = "UID";
} }
@@ -1346,7 +1346,7 @@ public class SettingsUi : WindowMediatorSubscriberBase
if (!useOauth) if (!useOauth)
{ {
_uiShared.DrawCombo("Secret Key##" + item.CharacterName + i, keys, (w) => w.Value.FriendlyName, _uiShared.DrawCombo("Secret Key###" + item.CharacterName + i, keys, (w) => w.Value.FriendlyName,
(w) => (w) =>
{ {
if (w.Key != item.SecretKeyIdx) if (w.Key != item.SecretKeyIdx)
@@ -1358,7 +1358,7 @@ public class SettingsUi : WindowMediatorSubscriberBase
} }
else else
{ {
_uiShared.DrawUIDComboForAuthentication(i, item, selectedServer.ServerUri); _uiShared.DrawUIDComboForAuthentication(i, item, selectedServer.ServerUri, _logger);
} }
if (_uiShared.IconTextButton(FontAwesomeIcon.Trash, "Delete Character") && UiSharedService.CtrlPressed()) if (_uiShared.IconTextButton(FontAwesomeIcon.Trash, "Delete Character") && UiSharedService.CtrlPressed())
_serverConfigurationManager.RemoveCharacterFromServer(idx, item); _serverConfigurationManager.RemoveCharacterFromServer(idx, item);

View File

@@ -54,7 +54,7 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase
private readonly Dalamud.Localization _localization; private readonly Dalamud.Localization _localization;
private readonly IDalamudPluginInterface _pluginInterface; private readonly IDalamudPluginInterface _pluginInterface;
private readonly ITextureProvider _textureProvider; private readonly ITextureProvider _textureProvider;
private readonly Dictionary<string, object> _selectedComboItems = new(StringComparer.Ordinal); private readonly Dictionary<string, object?> _selectedComboItems = new(StringComparer.Ordinal);
private readonly ServerConfigurationManager _serverConfigurationManager; private readonly ServerConfigurationManager _serverConfigurationManager;
private bool _cacheDirectoryHasOtherFilesThanCache = false; private bool _cacheDirectoryHasOtherFilesThanCache = false;
@@ -552,14 +552,14 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase
DrawHelpText("The storage is automatically governed by Mare. It will clear itself automatically once it reaches the set capacity by removing the oldest unused files. You typically do not need to clear it yourself."); DrawHelpText("The storage is automatically governed by Mare. It will clear itself automatically once it reaches the set capacity by removing the oldest unused files. You typically do not need to clear it yourself.");
} }
public T? DrawCombo<T>(string comboName, IEnumerable<T> comboItems, Func<T, string> toName, public T? DrawCombo<T>(string comboName, IEnumerable<T> comboItems, Func<T?, string> toName,
Action<T?>? onSelected = null, T? initialSelectedItem = default) Action<T?>? onSelected = null, T? initialSelectedItem = default)
{ {
if (!comboItems.Any()) return default; if (!comboItems.Any()) return default;
if (!_selectedComboItems.TryGetValue(comboName, out var selectedItem) && selectedItem == null) if (!_selectedComboItems.TryGetValue(comboName, out var selectedItem) && selectedItem == null)
{ {
if (!EqualityComparer<T>.Default.Equals(initialSelectedItem, default)) if (!EqualityComparer<T>.Default.Equals(initialSelectedItem, default(T)))
{ {
selectedItem = initialSelectedItem; selectedItem = initialSelectedItem;
_selectedComboItems[comboName] = selectedItem!; _selectedComboItems[comboName] = selectedItem!;
@@ -568,12 +568,12 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase
} }
else else
{ {
selectedItem = comboItems.First(); selectedItem = null;
_selectedComboItems[comboName] = selectedItem!; _selectedComboItems[comboName] = selectedItem;
} }
} }
if (ImGui.BeginCombo(comboName, toName((T)selectedItem!))) if (ImGui.BeginCombo(comboName, toName((T?)selectedItem)))
{ {
foreach (var item in comboItems) foreach (var item in comboItems)
{ {
@@ -588,7 +588,7 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase
ImGui.EndCombo(); ImGui.EndCombo();
} }
return (T)_selectedComboItems[comboName]; return (T?)_selectedComboItems[comboName];
} }
public void DrawFileScanState() public void DrawFileScanState()
@@ -879,32 +879,40 @@ public partial class UiSharedService : DisposableMediatorSubscriberBase
} }
} }
public void DrawUIDComboForAuthentication(int indexOffset, Authentication item, string serverUri) private record UIDAliasPair(string? UID, string? Alias);
public void DrawUIDComboForAuthentication(int indexOffset, Authentication item, string serverUri, ILogger? logger = null)
{ {
using (ImRaii.Disabled(_discordOAuthUIDs == null)) using (ImRaii.Disabled(_discordOAuthUIDs == null))
{ {
DrawCombo("UID##" + item.CharacterName + serverUri + indexOffset, _discordOAuthUIDs?.Result ?? new(StringComparer.Ordinal) { { item.UID ?? string.Empty, string.Empty } }, var aliasPairs = _discordOAuthUIDs?.Result?.Select(t => new UIDAliasPair(t.Key, t.Value)).ToList() ?? [new UIDAliasPair(item.UID ?? null, null)];
var uidComboName = "UID###" + item.CharacterName + item.WorldId + serverUri + indexOffset;
logger?.LogInformation("Drawing Combo with name {name}", uidComboName);
DrawCombo(uidComboName, aliasPairs,
(v) => (v) =>
{ {
if (!string.IsNullOrEmpty(v.Value)) if (v is null)
{
return $"{v.Key} ({v.Value})";
}
if (string.IsNullOrEmpty(v.Key))
return "No UID set"; return "No UID set";
return $"{v.Key}"; if (!string.IsNullOrEmpty(v.Alias))
{
return $"{v.UID} ({v.Alias})";
}
if (string.IsNullOrEmpty(v.UID))
return "No UID set";
return $"{v.UID}";
}, },
(v) => (v) =>
{ {
if (!string.Equals(v.Key, item.UID, StringComparison.Ordinal)) if (!string.Equals(v.UID, item.UID, StringComparison.Ordinal))
{ {
item.UID = v.Key; item.UID = v.UID;
_serverConfigurationManager.Save(); _serverConfigurationManager.Save();
} }
}, },
_discordOAuthUIDs?.Result?.FirstOrDefault(f => string.Equals(f.Key, item.UID, StringComparison.Ordinal)) ?? default); aliasPairs.Find(f => string.Equals(f.UID, item.UID, StringComparison.Ordinal)) ?? default);
} }
if (_discordOAuthUIDs == null) if (_discordOAuthUIDs == null)
{ {