More reworking of the login code

This commit is contained in:
Timothy Baldridge 2022-01-01 16:28:57 -07:00
parent c34a78bb4f
commit 92e1e3e393
7 changed files with 54 additions and 19 deletions

View File

@ -1,12 +1,16 @@
using System;
using System.Drawing;
using System.Reactive.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Extensions.Logging;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Common;
using Wabbajack.DTOs.Interventions;
using Wabbajack.DTOs.Logins;
using Wabbajack.Messages;
@ -14,7 +18,7 @@ using Wabbajack.Networking.Http.Interfaces;
namespace Wabbajack.LoginManagers;
public class NexusLoginManager : INeedsLogin
public class NexusLoginManager : ViewModel, INeedsLogin
{
private readonly ILogger<NexusLoginManager> _logger;
private readonly ITokenProvider<NexusApiState> _token;
@ -26,23 +30,35 @@ public class NexusLoginManager : INeedsLogin
public ImageSource Icon { get; set; }
[Reactive]
public bool HaveLogin { get; set; }
public NexusLoginManager(ILogger<NexusLoginManager> logger, ITokenProvider<NexusApiState> token)
{
_logger = logger;
_token = token;
ClearLogin = ReactiveCommand.Create(() =>
RefreshTokenState();
ClearLogin = ReactiveCommand.CreateFromTask(async () =>
{
_logger.LogInformation("Deleting Login information for {SiteName}", SiteName);
_token.Delete();
});
await _token.Delete();
RefreshTokenState();
}, this.WhenAnyValue(v => v.HaveLogin));
Icon = BitmapFrame.Create(
typeof(NexusLoginManager).Assembly.GetManifestResourceStream("Wabbajack.LoginManagers.Icons.nexus.png")!);
TriggerLogin = ReactiveCommand.Create(() =>
TriggerLogin = ReactiveCommand.CreateFromTask(async () =>
{
_logger.LogInformation("Logging into {SiteName}", SiteName);
NexusLogin.Send();
});
await NexusLogin.Send();
RefreshTokenState();
}, this.WhenAnyValue(v => v.HaveLogin).Select(v => !v));
}
private void RefreshTokenState()
{
HaveLogin = _token.HaveToken();
}
}

View File

@ -1,3 +1,4 @@
using System.Threading.Tasks;
using ReactiveUI;
using Wabbajack.Networking.Http.Interfaces;
@ -5,13 +6,17 @@ namespace Wabbajack.Messages;
public class NexusLogin
{
private TaskCompletionSource CompletionSource { get; }
public NexusLogin()
{
CompletionSource = new TaskCompletionSource();
}
public static void Send()
public static Task Send()
{
MessageBus.Current.SendMessage(new NexusLogin());
var msg = new NexusLogin();
MessageBus.Current.SendMessage(msg);
return msg.CompletionSource.Task;
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Reactive.Disposables;
using System.Reactive.Subjects;
@ -102,6 +103,7 @@ public class LoggerProvider : ILoggerProvider
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
Func<TState, Exception?, string> formatter)
{
Debug.WriteLine(formatter(state, exception));
_provider._messages.OnNext(new LogMessage<TState>(DateTime.UtcNow, _provider.NextMessageId(), logLevel,
eventId, state, exception, formatter));
}

View File

@ -1,5 +1,6 @@
using System;
using System.Reactive.Disposables;
using System.Windows.Forms;
using ReactiveUI;
namespace Wabbajack
@ -11,18 +12,22 @@ namespace Wabbajack
InitializeComponent();
this.WhenActivated(disposable =>
{
this.WhenAny(x => x.ViewModel.Login.Icon)
ViewModel.WhenAny(x => x.Login.Icon)
.BindToStrict(this, view => view.Favicon.Source)
.DisposeWith(disposable);
this.OneWayBindStrict(ViewModel, x => x.Login.SiteName, x => x.SiteNameText.Text)
.DisposeWith(disposable);
this.OneWayBindStrict(ViewModel, x => x.Login.TriggerLogin, x => x.LoginButton.Command)
ViewModel.WhenAnyValue(vm => vm.Login.SiteName)
.BindToStrict(this, view => view.SiteNameText.Text)
.DisposeWith(disposable);
this.OneWayBindStrict(ViewModel, x => x.Login.ClearLogin, x => x.LogoutButton.Command)
ViewModel.WhenAnyValue(vm => vm.Login.TriggerLogin)
.BindToStrict(this, view => view.LoginButton.Command)
.DisposeWith(disposable);
ViewModel.WhenAnyValue(vm => vm.Login.ClearLogin)
.BindToStrict(this, view => view.LogoutButton.Command)
.DisposeWith(disposable);
});
}
}

View File

@ -12,4 +12,6 @@ public interface ITokenProvider<T>
public ValueTask SetToken(T val);
public ValueTask<bool> Delete();
public bool HaveToken();
}

View File

@ -50,7 +50,7 @@ public class EncryptedJsonTokenProvider<T> : ITokenProvider<T>
if (value == default)
{
_logger.LogCritical("No login data for {key}", _key);
throw new Exception("No login data for {_key}");
throw new Exception($"No login data for {_key}");
}
return _dtos.Deserialize<T>(EnvValue!);

View File

@ -48,6 +48,11 @@ public class WabbajackApiTokenProvider : ITokenProvider<WabbajackApiState>
throw new NotImplementedException();
}
public bool HaveToken()
{
return true;
}
private async Task CreateMetricsKey()
{
var key = MakeRandomKey();