Skip to content

Commit

Permalink
Allow credentials to be backed up (Closes #525) (#648)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMacocian committed Apr 16, 2024
1 parent 1191e99 commit e7c9111
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Daybreak/Configuration/Options/CredentialManagerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Daybreak.Configuration.Options;

[OptionsIgnore]
[OptionsSynchronizationIgnore]
[OptionsName(Name = "Credentials")]
internal sealed class CredentialManagerOptions
{
[JsonProperty(nameof(ProtectedLoginCredentials))]
Expand Down
1 change: 1 addition & 0 deletions Daybreak/Configuration/ProjectConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ public override void RegisterStartupActions(IStartupActionProducer startupAction
startupActionProducer.RegisterAction<FixSymbolicLinkStartupAction>();
startupActionProducer.RegisterAction<UpdateUModAction>();
startupActionProducer.RegisterAction<FixPriceHistoryEntries>();
startupActionProducer.RegisterAction<CredentialsOptionsMigrator>();
}

public override void RegisterPostUpdateActions(IPostUpdateActionProducer postUpdateActionProducer)
Expand Down
2 changes: 0 additions & 2 deletions Daybreak/Models/ProtectedLoginCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ public sealed class ProtectedLoginCredentials
public string? ProtectedUsername { get; set; }
[JsonProperty(nameof(ProtectedPassword))]
public string? ProtectedPassword { get; set; }
[JsonProperty(nameof(CharacterName))]
public string? CharacterName { get; set; }
}
1 change: 1 addition & 0 deletions Daybreak/Services/Options/IOptionsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Daybreak.Services.Options;

public interface IOptionsProvider
{
JObject? TryGetKeyedOptions(string key);
IEnumerable<object> GetRegisteredOptions();
IEnumerable<Type> GetRegisteredOptionsTypes();
void SaveRegisteredOptions(object options);
Expand Down
10 changes: 10 additions & 0 deletions Daybreak/Services/Options/OptionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ public void SaveRegisteredOptions(string name, JObject options)
this.SaveOptions(registeredType, options);
}

public JObject? TryGetKeyedOptions(string key)
{
if (!this.optionsCache.TryGetValue(key, out var value))
{
return default;
}

return JsonConvert.DeserializeObject<JObject>(value);
}

private void SaveOptions(Type type, object value)
{
var optionsName = GetOptionsName(type);
Expand Down
42 changes: 42 additions & 0 deletions Daybreak/Services/Startup/Actions/CredentialsOptionsMigrator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Daybreak.Attributes;
using Daybreak.Configuration.Options;
using Daybreak.Services.Options;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Core.Extensions;
using System.Reflection;

namespace Daybreak.Services.Startup.Actions;
internal sealed class CredentialsOptionsMigrator : StartupActionBase
{
private const string OldOptionsKey = "CredentialManagerOptions";

private readonly IOptionsProvider optionsProvider;
private readonly ILogger<CredentialsOptionsMigrator> logger;

public CredentialsOptionsMigrator(
IOptionsProvider optionsProvider,
ILogger<CredentialsOptionsMigrator> logger)
{
this.optionsProvider = optionsProvider.ThrowIfNull();
this.logger = logger.ThrowIfNull();
}

public override void ExecuteOnStartup()
{
if (this.optionsProvider.TryGetKeyedOptions(OldOptionsKey) is not JObject options)
{
return;
}

var newOptionsKey = GetNewOptionsKey();
this.logger.LogInformation($"Found [{OldOptionsKey}]. Migrating options to [{newOptionsKey}]");
this.optionsProvider.SaveRegisteredOptions(newOptionsKey, options);
}

private static string GetNewOptionsKey()
{
return typeof(CredentialManagerOptions).GetCustomAttribute<OptionsNameAttribute>()?.Name ?? throw new InvalidOperationException("Unable to retrieve new credentials options key");
}
}

0 comments on commit e7c9111

Please sign in to comment.