Skip to content

Commit

Permalink
Сервер: AES, авторизация, регестрация, токены
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkRen committed May 18, 2024
1 parent 8810093 commit 0acb2bb
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 38 deletions.
2 changes: 1 addition & 1 deletion LauncherDM/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace LauncherDM
/// </summary>
public partial class App : Application
{
private static Mutex mutex = new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name);
private static Mutex mutex = new Mutex(true, "fjoasdkjff8139darkmilk");
[STAThread] // Это означает, что все потоки в этой программе выполняются в рамках одного процесса, а управление программой осуществляется одним главным потоком
protected override void OnStartup(StartupEventArgs e)
{
Expand Down
27 changes: 22 additions & 5 deletions LauncherDM/Services/AuthorizationService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using LauncherDM.Services.Interfaces;
using System;
using System.Security.Cryptography;
using System.Text;
using LauncherDM.Properties;
using LauncherDM.Services.Interfaces;
using ServerTCP;
using ServerTCP.Сryptographies;

namespace LauncherDM.Services
{
Expand All @@ -9,15 +12,29 @@ internal class AuthorizationService : IAuthorizationService
private IServerRequestService _serverRequest;
public bool Authorization(string login, string password)
{
if (string.IsNullOrEmpty(login))
throw new ArgumentNullException(login);

if (string.IsNullOrEmpty(password))
throw new ArgumentNullException(password);


var hash = SHA256.HashData(Encoding.UTF8.GetBytes(password));
var convertHash = Convert.ToHexString(hash);
//var requestPublicKey = _serverRequest.SendMessageRequest(MessageHeader.MessageType.PublicKey);
//var publicKey = requestPublicKey.Message.ToString();

var data = string.Concat(login, ",", password);
var data = string.Concat(login, ",", convertHash);
//var cryptMessage = CryptoRsa.Encrypt(publicKey, data);
//var requestToken = _serverRequest.SendMessageRequest(cryptMessage, MessageHeader.MessageType.Login);

var requestToken = _serverRequest.SendMessageRequest(data, MessageHeader.MessageType.Login);
return false;

if (requestToken?.Message.ToString() == "0")
return false;

SettingsApp.Default.Token = requestToken?.Message.ToString();
SettingsApp.Default.Save();
return true;
}

public AuthorizationService()
Expand Down
4 changes: 2 additions & 2 deletions LauncherDM/Services/DialogWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public void OpenWindow(object viewModel)
else if (viewModel is AccountUserControlViewModel or AuthorizationWindowViewModel)
{
var regAndLogWindow = new RegAndLogWindow();
regAndLogWindow.DataContext = new RegAndLogWindowViewModel(new ToolbarToWindowViewModel(new WindowService(regAndLogWindow)),
regAndLogWindow.DataContext = new RegAndLogWindowViewModel(regAndLogWindow.Close, new ToolbarToWindowViewModel(new WindowService(regAndLogWindow)),
new ResourcesHelperService());
regAndLogWindow.Show();
regAndLogWindow.ShowDialog();
return;
}
else if (viewModel is ToolbarToWindowViewModel)
Expand Down
7 changes: 7 additions & 0 deletions LauncherDM/Services/Interfaces/ISignUpService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace LauncherDM.Services.Interfaces
{
internal interface ISignUpService
{
bool SignUp(string login, string email, string password);
}
}
42 changes: 42 additions & 0 deletions LauncherDM/Services/SignUpService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using LauncherDM.Properties;
using LauncherDM.Services.Interfaces;
using ServerTCP;
using System.Text;
using System;
using System.Security.Cryptography;

namespace LauncherDM.Services
{
internal class SignUpService : ISignUpService
{
private IServerRequestService _serverRequest;
public bool SignUp(string login, string email, string password)
{
if (string.IsNullOrEmpty(login))
throw new ArgumentNullException(login);

if (string.IsNullOrEmpty(email))
throw new ArgumentNullException(email);

if (string.IsNullOrEmpty(password))
throw new ArgumentNullException(password);

var hash = SHA256.HashData(Encoding.UTF8.GetBytes(password));
var convertHash = Convert.ToHexString(hash);
var data = string.Concat(login, ",", convertHash, ",", email);
var requestToken = _serverRequest.SendMessageRequest(data, MessageHeader.MessageType.Registration);

if (requestToken?.Message.ToString() == "0")
return false;

SettingsApp.Default.Token = requestToken?.Message.ToString();
SettingsApp.Default.Save();
return true;
}

public SignUpService()
{
_serverRequest = new ServerRequestService();
}
}
}
24 changes: 22 additions & 2 deletions LauncherDM/ViewModels/RegAndLogWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using LauncherDM.Infastructure.Commands.Base;
using LauncherDM.Infastructure.Commands;
using LauncherDM.Models;
using System;

namespace LauncherDM.ViewModels
{
Expand All @@ -20,6 +21,12 @@ class RegAndLogWindowViewModel : ViewModel.Base.ViewModel

#endregion

#region Fields

private Action _closeAction;

#endregion

#region Bindings

public string Title => _resourcesHelper.LocalizationGet("Authorization");
Expand Down Expand Up @@ -124,7 +131,9 @@ private void OnLoginCommandExecuted(object p)
IAuthorizationService authorization = new AuthorizationService();
if (authorization.Authorization(Login, Password))
{

_dialogWindow.OpenWindow(this);
_dialogWindow.CloseAction = _closeAction;
_dialogWindow.CloseWindow();
}
else
{
Expand All @@ -141,16 +150,27 @@ private void OnLoginCommandExecuted(object p)
private bool CanSignUpCommandExecute(object p) => true;
private void OnSignUpCommandExecuted(object p)
{
ISignUpService signUpService = new SignUpService();
if (signUpService.SignUp(RegLogin, Email, RegPassword))
{

}
else
{
IDialogMessageBoxService dialogMessageBox = new DialogMessageBoxService();
dialogMessageBox.DialogShow("Error Server Reques", "Error Server Reques");
}

}

#endregion

#endregion

public RegAndLogWindowViewModel(ToolbarToWindowViewModel toolbarViewModel, ResourcesHelperService resourcesHelper)
public RegAndLogWindowViewModel(Action closeWindow ,ToolbarToWindowViewModel toolbarViewModel, ResourcesHelperService resourcesHelper)
{
_resourcesHelper = resourcesHelper;
_closeAction = closeWindow;
ToolbarVM = toolbarViewModel;
_dialogWindow = new DialogWindowService();
_regAndLogWindowService = new RegAndLogWindowServiceService();
Expand Down
14 changes: 7 additions & 7 deletions LauncherDM/Views/Windows/RegAndLogWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,9 @@
</TransformGroup>
</Button.RenderTransform>
<Button.Triggers>
<EventTrigger RoutedEvent="PreviewMouseUp">
<!--<EventTrigger RoutedEvent="PreviewMouseUp">
<BeginStoryboard Storyboard="{StaticResource StoryboardReg}"/>
</EventTrigger>
</EventTrigger>-->
</Button.Triggers>
</Button>
<Button x:Name="ButtonDown" Command="{Binding LoginCommand}" Style="{StaticResource ButtonSingUp}" HorizontalAlignment="Left" Margin="255,185,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5">
Expand All @@ -438,9 +438,9 @@
</TransformGroup>
</Button.RenderTransform>
<Button.Triggers>
<EventTrigger RoutedEvent="PreviewMouseUp">
<!--<EventTrigger RoutedEvent="PreviewMouseUp">
<BeginStoryboard Storyboard="{StaticResource StoryboardReg}"/>
</EventTrigger>
</EventTrigger>-->
</Button.Triggers>
</Button>
<Grid x:Name="SingUp" Margin="-10,-354,10,-272" RenderTransformOrigin="0.5,0.5">
Expand All @@ -466,7 +466,7 @@
</Style>
</TextBlock.Style>
</TextBlock>
<TextBox x:Name="LoginRegTB" MaxLength="12" HorizontalAlignment="Left" Height="40" Margin="17,595,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="282" Background="{x:Null}" Foreground="Black" SelectionBrush="{x:Null}" BorderBrush="{x:Null}" VerticalContentAlignment="Center" FontFamily="Cascadia Mono Light"/>
<TextBox Text="{Binding RegLogin}" x:Name="LoginRegTB" MaxLength="12" HorizontalAlignment="Left" Height="40" Margin="17,595,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="282" Background="{x:Null}" Foreground="Black" SelectionBrush="{x:Null}" BorderBrush="{x:Null}" VerticalContentAlignment="Center" FontFamily="Cascadia Mono Light"/>
<Path Data="M30,105 L417,105" Fill="Black" HorizontalAlignment="Left" Height="1" Margin="20,635,0,0" Stroke="Black" Stretch="Fill" VerticalAlignment="Top" Width="280"/>

<TextBlock Text="{Binding EmailText}" Foreground="#FF3E3E3E" Height="20" FontFamily="Cascadia Mono Light" Margin="20,270,0,0" >
Expand All @@ -481,7 +481,7 @@
</Style>
</TextBlock.Style>
</TextBlock>
<TextBox x:Name="EmailRegTB" MaxLength="255" HorizontalAlignment="Left" Height="40" Margin="17,641,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="282" Background="{x:Null}" Foreground="Black" SelectionBrush="{x:Null}" BorderBrush="{x:Null}" VerticalContentAlignment="Center" FontFamily="Cascadia Mono Light"/>
<TextBox Text="{Binding Email}" x:Name="EmailRegTB" MaxLength="255" HorizontalAlignment="Left" Height="40" Margin="17,641,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="282" Background="{x:Null}" Foreground="Black" SelectionBrush="{x:Null}" BorderBrush="{x:Null}" VerticalContentAlignment="Center" FontFamily="Cascadia Mono Light"/>
<Path Data="M30,105 L417,105" Fill="Black" HorizontalAlignment="Left" Height="1" Margin="20,681,0,0" Stroke="Black" Stretch="Fill" VerticalAlignment="Top" Width="280"/>

<TextBlock Text="{Binding PasswordText}" Foreground="#FF3E3E3E" Height="20" FontFamily="Cascadia Mono Light" Margin="20,362,0,0" >
Expand All @@ -496,7 +496,7 @@
</Style>
</TextBlock.Style>
</TextBlock>
<TextBox x:Name="PasswordRegTB" MaxLength="64" HorizontalAlignment="Left" Height="40" Margin="17,687,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="282" Background="{x:Null}" Foreground="Black" SelectionBrush="{x:Null}" BorderBrush="{x:Null}" VerticalContentAlignment="Center" FontFamily="Cascadia Mono Light"/>
<TextBox Text="{Binding RegPassword}" x:Name="PasswordRegTB" MaxLength="64" HorizontalAlignment="Left" Height="40" Margin="17,687,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="282" Background="{x:Null}" Foreground="Black" SelectionBrush="{x:Null}" BorderBrush="{x:Null}" VerticalContentAlignment="Center" FontFamily="Cascadia Mono Light"/>
<Path Data="M30,105 L417,105" Fill="Black" HorizontalAlignment="Left" Height="1" Margin="20,727,0,0" Stroke="Black" Stretch="Fill" VerticalAlignment="Top" Width="280"/>
<CheckBox Content="{Binding RememberMeText}" HorizontalAlignment="Left" Height="24" Margin="20,733,0,0" VerticalAlignment="Top" Width="220" Foreground="Black" VerticalContentAlignment="Center" Background="{x:Null}" FontFamily="Cascadia Mono Light" FontSize="10"/>
</Grid>
Expand Down
17 changes: 12 additions & 5 deletions ServerTCP/DataBase/DataBaseCommands.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System.Threading.Channels;
using ServerTCP.Models;
using ServerTCP.Models;
using ServerTCP.Models.Data;

namespace ServerTCP.DataBase
{
internal class DataBaseCommands
{
public static void Insert(object table, MessageHeader.MessageType messageType)

public const string SaltPassword = "1asdjh192jd1286sdarkmilk";
public const string PaperPassword = "jf10hdsj12fbzd12darkmilk";

public static bool Insert(object table, MessageHeader.MessageType messageType)
{
using (var db = new ApplicationContext())
{
Expand All @@ -19,11 +22,11 @@ public static void Insert(object table, MessageHeader.MessageType messageType)
db.users.Add(user);
break;
}
db.SaveChanges();
return db.SaveChanges() == 1;
}
}

public static object Select(MessageHeader.MessageType messageType)
public static object Select(MessageHeader.MessageType messageType, params string[] data )
{
using (var db = new ApplicationContext())
{
Expand All @@ -35,6 +38,10 @@ public static object Select(MessageHeader.MessageType messageType)
var appWPF = db.apps.Where(x => x.parametername == "wpfDMVersion").ToArray();
result = appWPF[0].parametervalue;
break;
case MessageHeader.MessageType.Login:
var user = db.users.Where(x => x.username == data[0] && x.password == data[1]).ToArray();
result = user.Length > 0;
break;
default:
result = null;
break;
Expand Down
16 changes: 7 additions & 9 deletions ServerTCP/MessageHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public byte[] MessageToArray(bool loadToken = false)

switch (Type)
{
case MessageType.Login:
//message = (byte[])Message;
Encoding.UTF8.GetBytes(Message.ToString());
case MessageType.Login:
case MessageType.Registration:
message = Encoding.UTF8.GetBytes(Message.ToString());
break;
}

Expand All @@ -112,6 +112,7 @@ public byte[] MessageToArray(bool loadToken = false)
switch (Type)
{
case MessageType.Login:
case MessageType.Registration:
Array.Copy(bytes, 0, result, 3, bytes.Length);
Array.Copy(message ?? [0], 0, result, LengthAndDataType, messageLength);
break;
Expand All @@ -132,6 +133,8 @@ internal byte[] MessageServerToArray(bool loadToken = false)
switch (Type)
{
case MessageType.Token:
case MessageType.Login:
case MessageType.Registration:
case MessageType.TitleLoading:
case MessageType.PublicKey:
case MessageType.Version:
Expand Down Expand Up @@ -169,6 +172,7 @@ public static MessageHeader FromArray(ReadOnlySpan<byte> buffer)
case MessageHeader.MessageType.Version:
case MessageHeader.MessageType.PublicKey:
case MessageHeader.MessageType.Login:
case MessageHeader.MessageType.Registration:
case MessageHeader.MessageType.TitleLoading:
case MessageHeader.MessageType.Check:
return new MessageHeader(Encoding.UTF8.GetString(buffer.ToArray(), LengthAndDataType, buffer.Length - LengthAndDataType), (MessageType)buffer[0], (MessageLanguages.Languages)buffer[1]);
Expand All @@ -188,12 +192,6 @@ public static MessageHeader FromArray(ReadOnlySpan<byte> buffer)
// return new MessageHeader(result.ToArray(),
// (MessageType)buffer[0]);
// break;
case MessageHeader.MessageType.Registration:
case MessageHeader.MessageType.Log:
case MessageHeader.MessageType.File:
case MessageHeader.MessageType.Photo:
case MessageHeader.MessageType.Data:
case MessageHeader.MessageType.Title:
default:
return null;
break;
Expand Down
6 changes: 5 additions & 1 deletion ServerTCP/Models/User.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace ServerTCP.Models
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace ServerTCP.Models
{
internal class User
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int id { get; set; }
public string username { get; set; }
public string login { get; set; }
Expand Down
Loading

0 comments on commit 0acb2bb

Please sign in to comment.