HISP/HorseIsleServer/MPN00BS/ResetWindow.axaml.cs
2022-07-25 19:52:05 +12:00

144 lines
4.6 KiB
C#
Executable file

using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using HISP.Security;
using HISP.Server;
using System;
using System.Text.RegularExpressions;
namespace MPN00BS
{
public partial class ResetWindow : Window
{
public ResetWindow()
{
InitializeComponent();
}
private void ValidateInput()
{
if (ValidateUsername() && ValidatePassword())
resetPasswordButton.IsEnabled = true;
else
resetPasswordButton.IsEnabled = false;
}
private bool ValidatePassword()
{
if (passwordBox == null)
return false;
if (passwordBox.Text == null)
return false;
int selStart = passwordBox.SelectionStart;
int selEnd = passwordBox.SelectionEnd;
passwordBox.Text = Regex.Replace(passwordBox.Text, "[^A-Za-z0-9]", "");
passwordBox.SelectionStart = selStart;
passwordBox.SelectionEnd = selEnd;
if (passwordBox.Text.Length < 6)
{
passwordValidationFailReason.Content = "- Password must be more than 6 characters.";
return false;
}
if (passwordBox.Text.Length >= 16)
{
passwordValidationFailReason.Content = "- Password must be less than 16 characters.";
return false;
}
passwordValidationFailReason.Content = "";
return true;
}
private bool ValidateUsername()
{
if (usernameBox == null)
return false;
if (usernameBox.Text == null)
return false;
int selStart = usernameBox.SelectionStart;
int selEnd = usernameBox.SelectionEnd;
usernameBox.Text = Regex.Replace(usernameBox.Text, "[^A-Za-z]", "");
usernameBox.SelectionStart = selStart;
usernameBox.SelectionEnd = selEnd;
if (usernameBox.Text.Length < 3)
{
usernameValidationFailReason.Content = "- Username must be more than 3 characters.";
return false;
}
if (usernameBox.Text.Length >= 16)
{
usernameValidationFailReason.Content = "- Username must be less than 16 characters.";
return false;
}
if (Regex.IsMatch(usernameBox.Text, "[A-Z]{2,}"))
{
usernameValidationFailReason.Content = "- Username have the first letter of each word capitalized.";
return false;
}
if (usernameBox.Text.ToUpper()[0] != usernameBox.Text[0])
{
usernameValidationFailReason.Content = "- Username have the first letter of each word capitalized.";
return false;
}
if (!Database.CheckUserExist(usernameBox.Text))
{
usernameValidationFailReason.Content = "- Username not found.";
return false;
}
usernameValidationFailReason.Content = "";
return true;
}
private void usernameChanged(object sender, KeyEventArgs e)
{
if (usernameBox == null)
return;
ValidateInput();
}
private void passwordChanged(object sender, KeyEventArgs e)
{
if (passwordBox == null)
return;
ValidateInput();
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
resetPasswordButton = this.FindControl<Button>("resetPasswordButton");
usernameValidationFailReason = this.FindControl<Label>("usernameValidationFailReason");
passwordValidationFailReason = this.FindControl<Label>("passwordValidationFailReason");
usernameBox = this.FindControl<TextBox>("usernameBox");
passwordBox = this.FindControl<TextBox>("passwordBox");
}
private void ResetPassword(object sender, RoutedEventArgs e)
{
// Get salt
byte[] salt = Database.GetPasswordSalt(usernameBox.Text);
// Hash password
string hashsalt = BitConverter.ToString(Authentication.HashAndSalt(passwordBox.Text, salt)).Replace("-", "");
Database.SetPasswordHash(usernameBox.Text, hashsalt);
this.Close();
}
}
}