Can log into the Nexus via the UI

This commit is contained in:
Timothy Baldridge 2022-01-29 22:47:49 -07:00
parent c26d8e4d98
commit fea3a81cf3
10 changed files with 204 additions and 13 deletions

View File

@ -10,6 +10,7 @@ using Wabbajack.App.Blazor.Utility;
using Wabbajack.Services.OSIntegrated;
using Blazored.Modal;
using Blazored.Toast;
using Wabbajack.App.Blazor.Browser.ViewModels;
namespace Wabbajack.App.Blazor;
@ -72,6 +73,7 @@ public partial class App
services.AddBlazoredModal();
services.AddBlazoredToast();
services.AddTransient<MainWindow>();
services.AddTransient<NexusLogin>();
services.AddSingleton<SystemParametersConstructor>();
services.AddSingleton<IStateContainer, StateContainer>();
return services;

View File

@ -4,6 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Wabbajack.App.Blazor.Browser"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<TabItem.Style>
@ -12,7 +13,6 @@
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="16" x:Name="HeaderText">_</TextBlock>
<Button Click="ButtonBase_OnClick">X</Button>
</StackPanel>
</TabItem.Header>
<Grid>

View File

@ -1,8 +1,11 @@
using System;
using System.Reactive.Disposables;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using ReactiveUI;
using Wabbajack.Common;
namespace Wabbajack.App.Blazor.Browser;
@ -15,19 +18,30 @@ public partial class BrowserTabView : IDisposable
_compositeDisposable = new CompositeDisposable();
InitializeComponent();
Browser.Browser.Source = new Uri("http://www.google.com");
vm.Browser = Browser;
DataContext = vm;
vm.WhenAnyValue(vm => vm.HeaderText)
.BindTo(this, view => view.HeaderText.Text)
.DisposeWith(_compositeDisposable);
Start().FireAndForget();
}
private async Task Start()
{
await ((BrowserTabViewModel) DataContext).RunWrapper(CancellationToken.None);
ClickClose(this, new RoutedEventArgs());
}
public void Dispose()
{
_compositeDisposable.Dispose();
var vm = (BrowserTabViewModel) DataContext;
vm.Browser = null;
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
private void ClickClose(object sender, RoutedEventArgs e)
{
var tc = (TabControl) this.Parent;
tc.Items.Remove(this);

View File

@ -1,10 +1,84 @@
using System;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using HtmlAgilityPack;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using ReactiveUI.Fody.Helpers;
using Wabbajack.DTOs.Logins;
namespace Wabbajack.App.Blazor.Browser;
public class BrowserTabViewModel : ViewModel
public abstract class BrowserTabViewModel : ViewModel
{
[Reactive]
public string HeaderText { get; set; }
[Reactive]
public string Instructions { get; set; }
public BrowserView? Browser { get; set; }
private WebView2 _browser => Browser!.Browser;
public async Task RunWrapper(CancellationToken token)
{
await Run(token);
}
protected abstract Task Run(CancellationToken token);
public async Task NavigateTo(Uri uri)
{
var tcs = new TaskCompletionSource();
void Completed(object? o, CoreWebView2NavigationCompletedEventArgs a)
{
if (a.IsSuccess)
{
tcs.TrySetResult();
}
else
{
tcs.TrySetException(new Exception($"Navigation error to {uri}"));
}
}
_browser.NavigationCompleted += Completed;
_browser.Source = uri;
await tcs.Task;
_browser.NavigationCompleted -= Completed;
}
public async Task<Cookie[]> GetCookies(string domainEnding, CancellationToken token)
{
var cookies = (await _browser.CoreWebView2.CookieManager.GetCookiesAsync(""))
.Where(c => c.Domain.EndsWith(domainEnding));
return cookies.Select(c => new Cookie
{
Domain = c.Domain,
Name = c.Name,
Path = c.Path,
Value = c.Value
}).ToArray();
}
public async Task<string> EvaluateJavaScript(string js)
{
return await _browser.ExecuteScriptAsync(js);
}
public async Task<HtmlDocument> GetDom(CancellationToken token)
{
var v = HttpUtility.UrlDecode("\u003D");
var source = await EvaluateJavaScript("document.body.outerHTML");
var decoded = JsonSerializer.Deserialize<string>(source);
var doc = new HtmlDocument();
doc.LoadHtml(decoded);
return doc;
}
}

View File

@ -7,10 +7,28 @@
xmlns:local="clr-namespace:Wabbajack.App.Blazor.Browser"
xmlns:wpf="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
xmlns:reactiveUi="http://reactiveui.net"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<wpf:WebView2 x:Name="Browser"></wpf:WebView2>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"></ColumnDefinition>
<ColumnDefinition Width="30"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="0">
<iconPacks:PackIconModern Kind="NavigatePrevious"></iconPacks:PackIconModern>
</Button>
<Button Grid.Row="0" Grid.Column="1">
<iconPacks:PackIconModern Kind="Home"></iconPacks:PackIconModern>
</Button>
<TextBox Grid.Row="0" Grid.Column="3" VerticalContentAlignment="Center"></TextBox>
<wpf:WebView2 Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" x:Name="Browser"></wpf:WebView2>
</Grid>
</reactiveUi:ReactiveUserControl>

View File

@ -1,11 +1,94 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Fizzler.Systems.HtmlAgilityPack;
using Wabbajack.DTOs.Logins;
using Wabbajack.Services.OSIntegrated;
namespace Wabbajack.App.Blazor.Browser.ViewModels;
public class NexusLogin : BrowserTabViewModel
{
public NexusLogin()
private readonly EncryptedJsonTokenProvider<NexusApiState> _tokenProvider;
public NexusLogin(EncryptedJsonTokenProvider<NexusApiState> tokenProvider)
{
HeaderText = "Nexus Login";
_tokenProvider = tokenProvider;
}
protected override async Task Run(CancellationToken token)
{
token.ThrowIfCancellationRequested();
Instructions = "Please log into the Nexus";
await NavigateTo(new Uri(
"https://users.nexusmods.com/auth/continue?client_id=nexus&redirect_uri=https://www.nexusmods.com/oauth/callback&response_type=code&referrer=//www.nexusmods.com"));
Cookie[] cookies = { };
while (true)
{
cookies = await GetCookies("nexusmods.com", token);
if (cookies.Any(c => c.Name == "member_id"))
break;
token.ThrowIfCancellationRequested();
await Task.Delay(500, token);
}
Instructions = "Getting API Key...";
await NavigateTo(new Uri("https://www.nexusmods.com/users/myaccount?tab=api"));
var key = "";
while (true)
{
try
{
key = (await GetDom(token))
.DocumentNode
.QuerySelectorAll("input[value=wabbajack]")
.SelectMany(p => p.ParentNode.ParentNode.QuerySelectorAll("textarea.application-key"))
.Select(node => node.InnerHtml)
.FirstOrDefault() ?? "";
}
catch (Exception)
{
// ignored
}
if (!string.IsNullOrEmpty(key))
break;
try
{
await EvaluateJavaScript(
"var found = document.querySelector(\"input[value=wabbajack]\").parentElement.parentElement.querySelector(\"form button[type=submit]\");" +
"found.onclick= function() {return true;};" +
"found.class = \" \"; " +
"found.click();" +
"found.remove(); found = undefined;"
);
Instructions = "Generating API Key, Please Wait...";
}
catch (Exception)
{
// ignored
}
token.ThrowIfCancellationRequested();
await Task.Delay(500, token);
}
Instructions = "Success, saving information...";
await _tokenProvider.SetToken(new NexusApiState
{
Cookies = cookies,
ApiKey = key
});
}
}

View File

@ -21,11 +21,6 @@
</blazor:BlazorWebView.RootComponents>
</blazor:BlazorWebView>
</TabItem>
<TabItem>
<TabItem.Header>
<TextBlock FontSize="16">Manual Download</TextBlock>
</TabItem.Header>
</TabItem>
</TabControl>
</Grid>
<Window.TaskbarItemInfo>

View File

@ -2,8 +2,11 @@
@using ReactiveUI
@using Wabbajack.App.Blazor.Browser.ViewModels
@using Wabbajack.App.Blazor.Messages
@using Microsoft.Extensions.DependencyInjection
@namespace Wabbajack.App.Blazor.Pages
@inject IServiceProvider _serviceProvider;
<div id="content">
<div class="resources">
@ -16,6 +19,6 @@
public void LoginToNexus()
{
MessageBus.Current.SendMessage(new OpenBrowserTab(new NexusLogin()));
MessageBus.Current.SendMessage(new OpenBrowserTab(_serviceProvider.GetRequiredService<NexusLogin>()));
}
}

View File

@ -17,8 +17,10 @@
<PackageReference Include="Blazored.Modal" Version="6.0.1" />
<PackageReference Include="Blazored.Toast" Version="3.2.2" />
<PackageReference Include="DynamicData" Version="7.4.9" />
<PackageReference Include="Fizzler.Systems.HtmlAgilityPack" Version="1.2.1" />
<PackageReference Include="GitInfo" Version="2.2.0" />
<PackageReference Include="MahApps.Metro" Version="2.4.9" />
<PackageReference Include="MahApps.Metro.IconPacks" Version="4.11.0" />
<PackageReference Include="Microsoft-WindowsAPICodePack-Shell" Version="1.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Wpf" Version="6.0.101-preview.11.2349" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.0" />

View File

@ -113,7 +113,7 @@ public static class ServiceExtensions
service.AddSingleton<WriteOnlyClient>();
// Token Providers
service.AddAllSingleton<ITokenProvider<NexusApiState>, NexusApiTokenProvider>();
service.AddAllSingleton<ITokenProvider<NexusApiState>, EncryptedJsonTokenProvider<NexusApiState>, NexusApiTokenProvider>();
service
.AddAllSingleton<ITokenProvider<LoversLabLoginState>, EncryptedJsonTokenProvider<LoversLabLoginState>,
LoversLabTokenProvider>();