Fix Verification User Senario ...
This commit is contained in:
@@ -13,6 +13,7 @@ namespace xBotService.Providers.GraphQL
|
||||
Field(x => x.BotUserName);
|
||||
Field(x => x.BotLastname);
|
||||
Field(x => x.BotFirstname);
|
||||
Field(x => x.VerificationCode);
|
||||
Field(x => x.LastVerificationSent);
|
||||
Field(x => x.ShownVirificationMenu);
|
||||
}
|
||||
|
||||
+161
-141
@@ -17,6 +17,7 @@ using xExceptions.Constants;
|
||||
using System;
|
||||
using xBotService.Models.Dtos;
|
||||
using xIdentityService.Security;
|
||||
using xBotService.Interfaces.Dtos;
|
||||
|
||||
namespace xBotService.Providers
|
||||
{
|
||||
@@ -28,21 +29,24 @@ namespace xBotService.Providers
|
||||
private readonly IXIdentityHttpProvider httpProvider;
|
||||
private readonly XValidationProvider validationProvider;
|
||||
private readonly XBotServiceConfiguration configuration;
|
||||
private readonly IXBotUserServiceProvider botUserServiceProvider;
|
||||
|
||||
public XBotProvider(
|
||||
ILogger<XBotProvider> logger,
|
||||
IXSecurityProvider securityProvider,
|
||||
IXIdentityHttpProvider httpProvider,
|
||||
XValidationProvider validationProvider,
|
||||
XBotServiceConfiguration configuration
|
||||
XBotServiceConfiguration configuration,
|
||||
IXBotUserServiceProvider botUserServiceProvider
|
||||
)
|
||||
{
|
||||
//
|
||||
this.logger = logger;
|
||||
this.securityProvider = securityProvider;
|
||||
this.httpProvider = httpProvider;
|
||||
this.configuration = configuration;
|
||||
this.securityProvider = securityProvider;
|
||||
this.validationProvider = validationProvider;
|
||||
this.botUserServiceProvider = botUserServiceProvider;
|
||||
}
|
||||
|
||||
//
|
||||
@@ -253,6 +257,13 @@ namespace xBotService.Providers
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Normalize ...
|
||||
if (label.IsNullOrEmpty())
|
||||
{
|
||||
label = XBotMessageIds.XBotRequestVerificationCodeLabel;
|
||||
}
|
||||
|
||||
//
|
||||
// Markup for Keyboards ...
|
||||
var markup = XBotReplyMarkup
|
||||
@@ -289,16 +300,29 @@ namespace xBotService.Providers
|
||||
/// <summary>
|
||||
/// Send Verification Code to Requests ...
|
||||
/// </summary>
|
||||
/// <param name="botUser"></param>
|
||||
/// <param name="botUserId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public virtual async Task<XBotMessage> SendVerificationCode(
|
||||
XBotUserDto botUser,
|
||||
long botUserId,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (botUserId <= 0)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Check Bot User Exists ...
|
||||
var botUser = await botUserServiceProvider.FindOneAsync(
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken,
|
||||
predicate: x => x.BotUserId.Equals(botUserId)
|
||||
);
|
||||
if (
|
||||
botUser.IsNullOrDefault() ||
|
||||
botUser.BotUserId <= 0)
|
||||
@@ -314,20 +338,22 @@ namespace xBotService.Providers
|
||||
}
|
||||
|
||||
//
|
||||
var code = securityProvider
|
||||
.Encrypt(botUser.BotUserId.ToString())
|
||||
.ToMd5String();
|
||||
var verifiedTime = DateTime.UtcNow;
|
||||
var code = CalculateVerificationCode(
|
||||
user: botUser,
|
||||
verifiedTime: verifiedTime
|
||||
);
|
||||
|
||||
//
|
||||
var message = XBotMessageIds.XBotVerificationCodeMessageText
|
||||
.ApplyTemplates(new Dictionary<string, string>
|
||||
{
|
||||
{ XBotParams.XVerificationTokenParam, code },
|
||||
{ XBotParams.XVerificationTokenParam, code },
|
||||
{ XBotParams.XTTLInMinuteParam, configuration.VerificationCodeTTL.ToString() },
|
||||
});
|
||||
|
||||
|
||||
//
|
||||
var response = await SendMessage(
|
||||
var result = await SendMessage(
|
||||
cancellationToken: cancellationToken,
|
||||
requestModel: new XBotSendMessageRequest
|
||||
{
|
||||
@@ -335,158 +361,152 @@ namespace xBotService.Providers
|
||||
ChatId = botUser.BotUserId,
|
||||
}
|
||||
);
|
||||
if (!result.IsNullOrDefault())
|
||||
{
|
||||
//
|
||||
// Update Bot User ...
|
||||
botUser.VerificationCode = code;
|
||||
botUser.LastVerificationSent = verifiedTime;
|
||||
botUser = await botUserServiceProvider.UpdateAsync(
|
||||
item: botUser,
|
||||
id: botUser.Id,
|
||||
saveChanges: true,
|
||||
connectionId: null,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return response;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// an Accept for Exit Loading on Bot ...
|
||||
/// Verify User Subscription ...
|
||||
/// </summary>
|
||||
/// <param name="callbackQueryId"></param>
|
||||
/// <param name="text"></param>
|
||||
/// <param name="showAlert"></param>
|
||||
/// <param name="token"></param>
|
||||
/// <param name="userInfo"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// public virtual async Task<XBotMessageResponse> AnswerCallbackQuery(
|
||||
// string callbackQueryId,
|
||||
// string text = null,
|
||||
// bool showAlert = false,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// // Validation ...
|
||||
// validationProvider.GroupValidationBuilder()
|
||||
// .AddNotEmpty(callbackQueryId)
|
||||
// .ValidateGroup();
|
||||
public virtual async Task<XBotMessage> VerifyUser(
|
||||
string token,
|
||||
XUserClaimsInfoDto userInfo = null,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
//
|
||||
// Validation ...
|
||||
if (!token.IsMD5String() ||
|
||||
token.IsNullOrEmpty() ||
|
||||
userInfo.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
// //
|
||||
// var client = GetRestClient();
|
||||
// var request = GetRestRequest(
|
||||
// endpoint: XBotEndpoints.AnswerCallbackQuery,
|
||||
// @params: new Dictionary<string, string>
|
||||
// {
|
||||
// { XBotParams.XTokenParam, configuration.Token }
|
||||
// }
|
||||
// );
|
||||
//
|
||||
// Detect Verified User ...
|
||||
var botUser = await botUserServiceProvider.FindOneAsync(
|
||||
includeBuilder: null,
|
||||
ignoreSoftDeleteds: true,
|
||||
cancellationToken: cancellationToken,
|
||||
predicate: x =>
|
||||
x.LastVerificationSent.HasValue &&
|
||||
x.VerificationCode.Equals(token)
|
||||
);
|
||||
if (botUser.IsNullOrDefault())
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
|
||||
// //
|
||||
// request.AddJsonBody(new
|
||||
// {
|
||||
// text = text,
|
||||
// show_alert = showAlert,
|
||||
// callback_query_id = callbackQueryId,
|
||||
// });
|
||||
//
|
||||
// Check Verification Code is Valid ...
|
||||
var userCode = CalculateVerificationCode(
|
||||
user: botUser,
|
||||
verifiedTime: botUser.LastVerificationSent.Value
|
||||
);
|
||||
if (!token.Equals(userCode))
|
||||
{
|
||||
XException.NotAllowed.Throw();
|
||||
}
|
||||
|
||||
// //
|
||||
// var response = await httpProvider.RunRestRequest<XBotMessageResponse>(
|
||||
// client: client,
|
||||
// request: request,
|
||||
// method: XHttpMethod.POST,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
//
|
||||
// Validate Toke Life Time ...
|
||||
var isExpired = botUser.LastVerificationSent.Value
|
||||
.IsExpiredTTL(configuration.VerificationCodeTTL * 60);
|
||||
if (isExpired)
|
||||
{
|
||||
//
|
||||
// Cleanup User Verification Info ...
|
||||
botUser.VerificationCode = null;
|
||||
botUser.LastVerificationSent = null;
|
||||
botUser.ShownVirificationMenu = false;
|
||||
|
||||
// //
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
botUser = await botUserServiceProvider.UpdateAsync(
|
||||
item: botUser,
|
||||
id: botUser.Id,
|
||||
saveChanges: true,
|
||||
connectionId: null,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
/// Remove Inline Keyboard After Click (Optional) ...
|
||||
/// </summary>
|
||||
/// <param name="chatId"></param>
|
||||
/// <param name="messageId"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// public virtual async Task<XBotMessageResponse> RemoveInlineKeyboard(
|
||||
// long chatId,
|
||||
// long messageId,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// // Validation ...
|
||||
// if (chatId <= 0 || messageId <= 0)
|
||||
// {
|
||||
// XException.InvalidArgs.Throw();
|
||||
// }
|
||||
//
|
||||
XException.ExpiredDate.Throw();
|
||||
}
|
||||
|
||||
// //
|
||||
// var client = GetRestClient();
|
||||
// var request = GetRestRequest(
|
||||
// endpoint: XBotEndpoints.EditMessageReplyMarkup,
|
||||
// @params: new Dictionary<string, string>
|
||||
// {
|
||||
// { XBotParams.XTokenParam, configuration.Token }
|
||||
// }
|
||||
// );
|
||||
//
|
||||
// Make User Valid ...
|
||||
botUser.VerificationCode = null;
|
||||
botUser.UserId = userInfo.UserId;
|
||||
botUser.LastVerificationSent = null;
|
||||
botUser.VerifiedOn = DateTime.UtcNow;
|
||||
botUser.ShownVirificationMenu = false;
|
||||
|
||||
// //
|
||||
// request.AddJsonBody(new
|
||||
// {
|
||||
// chat_id = chatId,
|
||||
// message_id = messageId,
|
||||
// reply_markup = new { inline_keyboard = new List<List<object>>() }
|
||||
// });
|
||||
//
|
||||
botUser = await botUserServiceProvider.UpdateAsync(
|
||||
item: botUser,
|
||||
id: botUser.Id,
|
||||
saveChanges: true,
|
||||
connectionId: null,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
// //
|
||||
// var response = await httpProvider.RunRestRequest<XBotMessageResponse>(
|
||||
// client: client,
|
||||
// request: request,
|
||||
// method: XHttpMethod.POST,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
//
|
||||
// Send User Verification Message ...
|
||||
var result = await SendMessage(
|
||||
cancellationToken: cancellationToken,
|
||||
requestModel: new XBotSendMessageRequest
|
||||
{
|
||||
ChatId = botUser.BotUserId,
|
||||
Text = XBotMessageIds.XBotUserVerificationSucceedMessageText
|
||||
}
|
||||
);
|
||||
|
||||
// //
|
||||
// return response;
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// Set Webhook for Retrieve Messages on Recieved ...
|
||||
/// </summary>
|
||||
/// <param name="webhookUrl"></param>
|
||||
/// <param name="secretToken"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
// public virtual async Task<bool> SetWebhook(
|
||||
// string webhookUrl,
|
||||
// string secretToken = null,
|
||||
// CancellationToken cancellationToken = default
|
||||
// )
|
||||
// {
|
||||
// //
|
||||
// var client = GetRestClient();
|
||||
// var request = GetRestRequest(
|
||||
// endpoint: XBotEndpoints.SetWebhook,
|
||||
// @params: new Dictionary<string, string>
|
||||
// {
|
||||
// { XBotParams.XTokenParam, configuration.Token }
|
||||
// }
|
||||
// );
|
||||
|
||||
// //
|
||||
// request.AddJsonBody(new
|
||||
// {
|
||||
// url = webhookUrl,
|
||||
// secret_token = secretToken
|
||||
// });
|
||||
|
||||
// //
|
||||
// var iResponse = await httpProvider.RunRestRequest<XBotMessageResponse>(
|
||||
// client: client,
|
||||
// request: request,
|
||||
// method: XHttpMethod.POST,
|
||||
// cancellationToken: cancellationToken
|
||||
// );
|
||||
// var response = iResponse?.Ok ?? false;
|
||||
|
||||
// //
|
||||
// return response;
|
||||
// }
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//
|
||||
#region Private ...
|
||||
/// <summary>
|
||||
/// Calculate Verification Code ...
|
||||
/// </summary>
|
||||
/// <param name="user"></param>
|
||||
/// <param name="verifiedTime"></param>
|
||||
/// <returns></returns>
|
||||
private string CalculateVerificationCode(
|
||||
XBotUserDto user,
|
||||
DateTime verifiedTime
|
||||
)
|
||||
{
|
||||
//
|
||||
var result = securityProvider
|
||||
.Encrypt($"{user.BotUserId}{verifiedTime}")
|
||||
.ToMd5String();
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user