prework for implementing login

This commit is contained in:
WeeXnes 2023-11-21 18:13:39 +01:00
parent e3a34fa966
commit f6e3687ec2
8 changed files with 339 additions and 2 deletions

View file

@ -8,7 +8,7 @@ namespace WeeXnes.Core
{ {
public class Information public class Information
{ {
public const string Version = "4.4.6"; public const string Version = "4.4.7";
public const string EncryptionHash = "8zf5#RdyQ]$4x4_"; public const string EncryptionHash = "8zf5#RdyQ]$4x4_";
public const string ApiUrl = "https://api.github.com/repos/weexnes/weexnessuite/releases/latest"; public const string ApiUrl = "https://api.github.com/repos/weexnes/weexnessuite/releases/latest";
} }

133
WeeXnes/Core/LoginLib.cs Normal file
View file

@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Http;
using Newtonsoft.Json;
using WeeXnes.Views.Settings;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
namespace WeeXnes.Core
{
public class Auth
{
public BackgroundWorker _loginWorker { get; set; }
public UpdateVar<dynamic> _currentUserCache { get; private set; } = new UpdateVar<dynamic>();
private string _token { get; set; }
private string _userDataUrl { get; set; }
private string _loginUrl { get; set; }
public string _email { get; set; }
public string _password { private get; set; }
public UpdateVar<Exception> ExceptionCache { get; private set; } = new UpdateVar<Exception>();
public Auth(string loginUrl, string userDataUrl)
{
this._currentUserCache.Value = null;
this._loginWorker = new BackgroundWorker();
this._loginWorker.WorkerReportsProgress = true;
this._loginWorker.WorkerSupportsCancellation = true;
this._loginWorker.DoWork += (sender, args) =>
{
try
{
Login(this._email, this._password);
GetUserData();
}
catch (Exception ex)
{
this.ExceptionCache.Value = ex;
}
};
this._loginWorker.RunWorkerCompleted += (sender, args) =>
{
WeeXnes.Core.CustomConsole.WriteLine("LoginWorker complete");
};
this._loginUrl = loginUrl;
this._userDataUrl = userDataUrl;
}
public string Login(string email, string password)
{
if (String.IsNullOrEmpty(email))
throw new NullReferenceException();
if (String.IsNullOrEmpty(password))
throw new NullReferenceException();
using (HttpClient client = new HttpClient())
{
var postData = new Dictionary<string, string>
{
{ "email", email },
{ "password", password }
};
var content = new FormUrlEncodedContent(postData);
HttpResponseMessage response = client.PostAsync(_loginUrl, content).Result;
if (response.IsSuccessStatusCode)
{
string responseData = response.Content.ReadAsStringAsync().Result;
dynamic jsonData = Newtonsoft.Json.JsonConvert.DeserializeObject(responseData);
string token = jsonData.token;
_token = token;
return token;
//Console.WriteLine($"Token: {token}");
}
else
{
WeeXnes.Core.CustomConsole.WriteLine("Error: " + response.StatusCode);
return null;
}
}
}
public dynamic GetUserData(string token = null)
{
if(String.IsNullOrEmpty(token))
if (String.IsNullOrEmpty(_token))
{
return null;
}
else
{
token = _token;
}
// Create an instance of HttpClient
using (HttpClient client = new HttpClient())
{
// Add the token to the request headers
client.DefaultRequestHeaders.Add("token", token);
// Send the GET request
HttpResponseMessage response = client.GetAsync(_userDataUrl).Result;
// Check if the request was successful (status code 200)
if (response.IsSuccessStatusCode)
{
// Read the response content as a string
string responseData = response.Content.ReadAsStringAsync().Result;
//Console.WriteLine(responseData);
// Parse the JSON data into a dynamic object
dynamic user = JsonConvert.DeserializeObject(responseData);
user = user.user;
// Now you can access the user object properties dynamically
WeeXnes.Core.CustomConsole.WriteLine("authenticated user: " + user.name);
//Console.WriteLine($"Email: {user.email}");
// Access other properties as needed
_currentUserCache.Value = user;
return user;
}
else
{
// Handle the error, e.g., print the status code
_currentUserCache.Value = null;
WeeXnes.Core.CustomConsole.WriteLine("Error: " + response.StatusCode);
return null;
}
}
}
}
}

View file

@ -9,6 +9,7 @@
xmlns:settings="clr-namespace:WeeXnes.Views.Settings" xmlns:settings="clr-namespace:WeeXnes.Views.Settings"
xmlns:discordrpc="clr-namespace:WeeXnes.Views.DiscordRPC" xmlns:discordrpc="clr-namespace:WeeXnes.Views.DiscordRPC"
xmlns:passwordGenerator="clr-namespace:WeeXnes.Views.PasswordGenerator" xmlns:passwordGenerator="clr-namespace:WeeXnes.Views.PasswordGenerator"
xmlns:profile="clr-namespace:WeeXnes.Views.ProfileView"
mc:Ignorable="d" mc:Ignorable="d"
Height="400" Height="400"
Width="500" Width="500"
@ -90,6 +91,13 @@
Name="ButtonPwGen" Name="ButtonPwGen"
PageTag="Gen" PageTag="Gen"
PageType="{x:Type passwordGenerator:PasswordGenView}"/> PageType="{x:Type passwordGenerator:PasswordGenView}"/>
<ui:NavigationItem
Visibility="Collapsed"
Content="Profile"
Icon="InprivateAccount24"
Name="ButtonProfile"
PageTag="Profile"
PageType="{x:Type profile:ProfileView}"/>
</ui:NavigationStore.Items> </ui:NavigationStore.Items>
<ui:NavigationStore.Footer> <ui:NavigationStore.Footer>
<ui:NavigationItem <ui:NavigationItem

View file

@ -0,0 +1,12 @@
<Page x:Class="WeeXnes.Views.ProfileView.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WeeXnes.Views.ProfileView"
mc:Ignorable="d"
Title="LoginView" Height="450" Width="800">
<Grid>
</Grid>
</Page>

View file

@ -0,0 +1,12 @@
using System.Windows.Controls;
namespace WeeXnes.Views.ProfileView
{
public partial class LoginView : Page
{
public LoginView()
{
InitializeComponent();
}
}
}

View file

@ -0,0 +1,78 @@
<Page x:Class="WeeXnes.Views.ProfileView.ProfileView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WeeXnes.Views.ProfileView"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
mc:Ignorable="d"
Title="ProfileView" Height="Auto" Width="Auto"
Loaded="ProfileView_OnLoaded"
Unloaded="ProfileView_OnUnloaded">
<Grid>
<Grid Name="LoadingScreen" Visibility="Collapsed">
<ui:LoadingScreen Background="Transparent"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
<StackPanel Orientation="Vertical" Name="ProfileContentPanel" Visibility="Collapsed">
<Border Height="40px" CornerRadius="5">
<Border.Background>
<ImageBrush
Stretch="UniformToFill"
x:Name="BannerBackground"/>
</Border.Background>
<Label Content="TestName" HorizontalAlignment="Center" VerticalAlignment="Center"
Foreground="White"
FontSize="20px"
Name="UsernameLabel">
<Label.Effect>
<DropShadowEffect
ShadowDepth="0"
Direction="0"
Color="Black"
Opacity="1"
BlurRadius="3"/>
</Label.Effect>
</Label>
</Border>
<Label Name="ProfileDesc" HorizontalAlignment="Center" Foreground="White" Padding="0,10,0,10"/>
<Separator/>
<Label Content="" HorizontalAlignment="Center" Padding="0,0,0,0"/>
<!-- Options for Account -->
<ui:CardAction Icon="MailInbox24"
Name="InboxButton">
<StackPanel>
<TextBlock
Margin="0,0,0,4"
FontWeight="Medium"
Text="Inbox" />
<TextBlock
FontSize="11"
Foreground="{DynamicResource TextFillColorTertiaryBrush}"
Text="Check your messages" />
</StackPanel>
</ui:CardAction>
<ui:CardAction Icon="Key24"
Name="LicenseButton" Margin="0,5,0,0">
<StackPanel>
<TextBlock
Margin="0,0,0,4"
FontWeight="Medium"
Text="Licenses" />
<TextBlock
FontSize="11"
Foreground="{DynamicResource TextFillColorTertiaryBrush}"
Text="See your currently active licenses for other Apps of mine (These are no purchasable licenses, these are access licenses only given to specific people for testing)"
TextTrimming="CharacterEllipsis"/>
</StackPanel>
</ui:CardAction>
</StackPanel>
</Grid>
</Page>

View file

@ -0,0 +1,83 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using WeeXnes.Core;
using WeeXnes.Views.Settings;
namespace WeeXnes.Views.ProfileView
{
public partial class ProfileView : Page
{
public static Auth auth = new Auth(
"https://weexnes.dev:4000/login",
"https://weexnes.dev:4000/user"
);
public ProfileView()
{
InitializeComponent();
auth._email = "";
auth._password = "";
}
private void ProfileView_OnLoaded(object sender, RoutedEventArgs e)
{
auth.ExceptionCache.ValueChanged += LoginWorkerException;
auth._currentUserCache.ValueChanged += userCacheChanged;
WeeXnes.Core.CustomConsole.WriteLine("Event hooks loaded");
if (auth._currentUserCache.Value == null)
{
LoadingScreen.Visibility = Visibility.Visible;
auth._loginWorker.RunWorkerAsync();
}
else
{
ProfileContentPanel.Visibility = Visibility.Visible;
}
}
private void userCacheChanged()
{
if (auth._currentUserCache.Value != null)
{
this.Dispatcher.Invoke(() =>
{
LoadProfileToGui();
ProfileContentPanel.Visibility = Visibility.Visible;
LoadingScreen.Visibility = Visibility.Collapsed;
});
}
else
{
ProfileContentPanel.Visibility = Visibility.Collapsed;
LoadingScreen.Visibility = Visibility.Visible;
}
}
private void LoginWorkerException()
{
this.Dispatcher.Invoke(() =>
{
new FluentMessageBox(auth.ExceptionCache.Value.Message).ShowDialog();
});
}
private void LoadProfileToGui()
{
//Load Profile
UsernameLabel.Content = Convert.ToString(auth._currentUserCache.Value.name);
ProfileDesc.Content = Convert.ToString(auth._currentUserCache.Value.profileInfo.bio);
BannerBackground.ImageSource =
new BitmapImage(new Uri(Convert.ToString(auth._currentUserCache.Value.profileInfo.bannerUrl)));
}
private void ProfileView_OnUnloaded(object sender, RoutedEventArgs e)
{
auth.ExceptionCache.ValueChanged -= LoginWorkerException;
auth._currentUserCache.ValueChanged -= userCacheChanged;
WeeXnes.Core.CustomConsole.WriteLine("Event hooks unloaded");
}
}
}

View file

@ -4,7 +4,7 @@
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<Version>4.4.6</Version> <Version>4.4.7</Version>
<ProjectGuid>{4B33CEE7-C74D-43B9-B99A-8B273D5195BC}</ProjectGuid> <ProjectGuid>{4B33CEE7-C74D-43B9-B99A-8B273D5195BC}</ProjectGuid>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<RootNamespace>WeeXnes</RootNamespace> <RootNamespace>WeeXnes</RootNamespace>
@ -41,6 +41,7 @@
<Reference Include="DiscordRPC, Version=1.0.175.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="DiscordRPC, Version=1.0.175.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\DiscordRichPresence.1.0.175\lib\net35\DiscordRPC.dll</HintPath> <HintPath>..\packages\DiscordRichPresence.1.0.175\lib\net35\DiscordRPC.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
@ -51,6 +52,7 @@
<Reference Include="System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"> <Reference Include="System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Drawing.Common.6.0.0\lib\net461\System.Drawing.Common.dll</HintPath> <HintPath>..\packages\System.Drawing.Common.6.0.0\lib\net461\System.Drawing.Common.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="System.Xaml"> <Reference Include="System.Xaml">
@ -71,6 +73,7 @@
<Compile Include="Core\CustomConsole.cs" /> <Compile Include="Core\CustomConsole.cs" />
<Compile Include="Core\EncryptorLibrary.cs" /> <Compile Include="Core\EncryptorLibrary.cs" />
<Compile Include="Core\HandleLaunchArguments.cs" /> <Compile Include="Core\HandleLaunchArguments.cs" />
<Compile Include="Core\LoginLib.cs" />
<Compile Include="Core\RpcLogEvents.cs" /> <Compile Include="Core\RpcLogEvents.cs" />
<Compile Include="Core\SaveSettingsHandler.cs" /> <Compile Include="Core\SaveSettingsHandler.cs" />
<Compile Include="Core\WXFile.cs" /> <Compile Include="Core\WXFile.cs" />
@ -103,6 +106,12 @@
<Compile Include="Views\PasswordGenerator\SaveToKeyManagerView.xaml.cs"> <Compile Include="Views\PasswordGenerator\SaveToKeyManagerView.xaml.cs">
<DependentUpon>SaveToKeyManagerView.xaml</DependentUpon> <DependentUpon>SaveToKeyManagerView.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Views\ProfileView\LoginView.xaml.cs">
<DependentUpon>LoginView.xaml</DependentUpon>
</Compile>
<Compile Include="Views\ProfileView\ProfileView.xaml.cs">
<DependentUpon>ProfileView.xaml</DependentUpon>
</Compile>
<Compile Include="Views\Settings\ApiResponse.cs" /> <Compile Include="Views\Settings\ApiResponse.cs" />
<Compile Include="Views\Settings\ChangePathsView.xaml.cs"> <Compile Include="Views\Settings\ChangePathsView.xaml.cs">
<DependentUpon>ChangePathsView.xaml</DependentUpon> <DependentUpon>ChangePathsView.xaml</DependentUpon>
@ -141,6 +150,8 @@
<Page Include="Views\PasswordGenerator\PasswordGenView.xaml" /> <Page Include="Views\PasswordGenerator\PasswordGenView.xaml" />
<Page Include="Views\PasswordGenerator\SavePasswordView.xaml" /> <Page Include="Views\PasswordGenerator\SavePasswordView.xaml" />
<Page Include="Views\PasswordGenerator\SaveToKeyManagerView.xaml" /> <Page Include="Views\PasswordGenerator\SaveToKeyManagerView.xaml" />
<Page Include="Views\ProfileView\LoginView.xaml" />
<Page Include="Views\ProfileView\ProfileView.xaml" />
<Page Include="Views\Settings\ChangePathsView.xaml" /> <Page Include="Views\Settings\ChangePathsView.xaml" />
<Page Include="Views\Settings\FluentMessageBox.xaml" /> <Page Include="Views\Settings\FluentMessageBox.xaml" />
<Page Include="Views\Settings\SettingsView.xaml" /> <Page Include="Views\Settings\SettingsView.xaml" />