147 lines
6.5 KiB
C#
147 lines
6.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data.SqlClient;
|
||
using System.Net.Http;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using SvetoforVKBot.Models.Updates;
|
||
using VkNet;
|
||
using VkNet.Enums.SafetyEnums;
|
||
using VkNet.Model.Keyboard;
|
||
using VkNet.Model.RequestParams;
|
||
using System.Linq;
|
||
using SvetoforVKBot.Services;
|
||
using SvetoforVKBot.Models.Commands.LK;
|
||
|
||
namespace SvetoforVKBot.Models.Commands.Registration
|
||
{
|
||
public class GetLoginCommand
|
||
{
|
||
public void Execute(RootObject update, VkApi client, SvetoforVKBot.Data.SvetoforVKBotEntities db)
|
||
{
|
||
var chatId = update.@object.message.from_id;
|
||
MessagesSendParams @params = new MessagesSendParams();
|
||
var keyboardBuilder = new KeyboardBuilder().Clear();
|
||
|
||
try
|
||
{
|
||
if (update.@object.message.text.Length > 0 && update.@object.message.text.Length <= 50)
|
||
{
|
||
var ids = new long[] { chatId };
|
||
string login = update.@object.message.text.Trim();
|
||
string message = "";
|
||
var user = db.Users.Single(usr => usr.chatId == chatId);
|
||
if (checkAuthStatus(login))
|
||
{
|
||
user.isAuthorized = 1;
|
||
user.tag = "Подписчик - Регистрация - Пол";
|
||
db.SaveChanges();
|
||
|
||
if (user.reg == 1)
|
||
{
|
||
ShowLKCommand showLK = new ShowLKCommand();
|
||
showLK.Execute(update, client, db);
|
||
}
|
||
else
|
||
{
|
||
keyboardBuilder
|
||
.AddButton("М", "selectGender-1", KeyboardButtonColor.Primary)
|
||
.AddButton("Ж", "selectGender-2", KeyboardButtonColor.Primary)
|
||
.AddLine()
|
||
.AddButton("Отменить", "startPL", KeyboardButtonColor.Default);
|
||
|
||
@params.Keyboard = keyboardBuilder.Build();
|
||
@params.UserId = chatId;
|
||
@params.Message = //1 шаг из 4.
|
||
"✅ Авторизация прошла успешно.\n" +
|
||
"Выберите на клавиатуре внизу или напишите пол в формате \"М\" или \"Ж\"";
|
||
@params.RandomId = GetRandomId();
|
||
client.Messages.SendAsync(@params);
|
||
}
|
||
return;
|
||
}
|
||
string pincode = PasswordService.GeneratePassword();
|
||
string hash = PasswordService.CreateMD5(pincode + chatId.ToString());
|
||
message = $"Ваш код подтверждения: {pincode}";
|
||
|
||
|
||
keyboardBuilder
|
||
.AddButton("Отменить", "startPL", KeyboardButtonColor.Default);
|
||
@params.Keyboard = keyboardBuilder.Build();
|
||
@params.Message = "На Вашу корпоративную почту отправлено письмо с кодом подтверждения.\n" +
|
||
"Введите код в ответ на данное сообщение, чтобы завершить авторизацию.";
|
||
@params.UserId = chatId;
|
||
@params.RandomId = GetRandomId();
|
||
client.Messages.SendAsync(@params);
|
||
|
||
user.login = login;
|
||
user.password = hash;
|
||
user.tag = "Подписчик - Регистрация - Код";
|
||
db.SaveChanges();
|
||
|
||
EmailService emailService = new EmailService();
|
||
emailService.SendEmailAsync($"{login}@vyatsu.ru", "Код подтверждения", message);
|
||
|
||
}
|
||
else
|
||
{
|
||
@params.Message = "Данные введены некорректно 😕 Напишите, пожалуйста, текст длиной до 50 символов.";
|
||
@params.UserId = chatId;
|
||
@params.RandomId = GetRandomId();
|
||
client.Messages.SendAsync(@params);
|
||
}
|
||
}
|
||
catch (Exception ee)
|
||
{
|
||
@params.Message = "‼Ошибка в GetFIOCommand: " + ee.Message;
|
||
@params.Attachments = null;
|
||
@params.Keyboard = null;
|
||
@params.UserId = 59111081;
|
||
@params.RandomId = GetRandomId();
|
||
client.Messages.SendAsync(@params);
|
||
}
|
||
}
|
||
|
||
private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create();
|
||
public int GetRandomId()
|
||
{
|
||
var intBytes = new byte[4];
|
||
|
||
Rng.GetBytes(intBytes);
|
||
|
||
return BitConverter.ToInt32(intBytes, 0);
|
||
}
|
||
|
||
public static string StringSha256Hash(string text) =>
|
||
string.IsNullOrEmpty(text) ? string.Empty : BitConverter.ToString(new System.Security.Cryptography.SHA256Managed().ComputeHash(System.Text.Encoding.UTF8.GetBytes(text))).
|
||
Replace("-", string.Empty);
|
||
|
||
private bool checkAuthStatus(string login)
|
||
{
|
||
HttpClient client = new HttpClient();
|
||
string sault = @"{ YDB * mhB * *0SuQ([WU.TSuJt.j47}UW{";
|
||
string uri = String.Format("https://new.vyatsu.ru/api/user_tmp_id/get/?user={0}&hash={1}", login, StringSha256Hash(login + sault));
|
||
var result = client.GetAsync(uri);
|
||
result.Wait();
|
||
HttpResponseMessage resp = result.Result;
|
||
if (resp.StatusCode == System.Net.HttpStatusCode.OK)
|
||
{
|
||
var bodyreader = resp.Content.ReadAsStringAsync();
|
||
bodyreader.Wait();
|
||
string body = bodyreader.Result;
|
||
if (body.Contains(login))
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
private void getAuthStatus(string login)
|
||
{
|
||
HttpClient client = new HttpClient();
|
||
string sault = @"{ YDB * mhB * *0SuQ([WU.TSuJt.j47}UW{";
|
||
string uri = String.Format("https://new.vyatsu.ru/api/user_tmp_id/get/?user={0}&hash={1}", login, StringSha256Hash(login + sault));
|
||
var result = client.GetAsync(uri);
|
||
}
|
||
|
||
}
|
||
} |