using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel.Design; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Drawing.Text; using System.Drawing; using NeomtechERP.Auth.Core.Global; using NeomtechERP.Auth.Core; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; using MTWorkHR.Core.Global; using MTWorkHR.Core.Email; using MTWorkHR.Core.UnitOfWork; using MTWorkHR.Application.Services.Interfaces; using MTWorkHR.Core.Entities; namespace MTWorkHR.Application.Services { public class OTPService : IOTPService { private readonly AppSettingsConfiguration appSettings; private readonly IMailSender mailSender; private readonly IUnitOfWork unitOfWork; private readonly IWebHostEnvironment env; public OTPService(AppSettingsConfiguration appSettings, IUnitOfWork unitOfWork, IMailSender mailSender, IWebHostEnvironment env) { this.appSettings = appSettings; this.mailSender = mailSender; this.unitOfWork = unitOfWork; this.env = env; } public async Task RandomOneTimePassword(string userId) { string oneTimePassword = default; for (var index = 0; index < appSettings.OTPSettings.Length; index++) { oneTimePassword += $"{new Random().Next(0, 10)}"; } var loginOTP = new LoginOTP() { OTP = oneTimePassword, UserId = userId, CreateDate = DateTime.Now, ExpireDate = DateTime.Now.AddMinutes(appSettings.OTPSettings.ExpirePeriodInMinutes) }; await unitOfWork.LoginOTP.AddAsync(loginOTP); await unitOfWork.CompleteAsync(); return oneTimePassword; } public async Task VerifyOTP(string userId, string oneTimePassword) { if (appSettings.OTPSettings.AllowZeros) { var dummyOTP = ""; for (var index = 0; index < appSettings.OTPSettings.Length; index++) { dummyOTP += "1"; } return oneTimePassword == dummyOTP; } else return await unitOfWork.LoginOTP.VerifyOTP(userId, oneTimePassword); } //public async Task SentOTPBySMS(string phoneNumber, string oneTimePassword, SMSMethod method) //{ // if (appSettings.OTPSettings.SendSMS) // { // string message = string.Format(appSettings.OTPSettings.MessageBody, oneTimePassword); // var smsModel = new SMSDTO // { // PhoneNumber = phoneNumber, // MessageBody = message, // Method = method.ToString(), // Module = "Auth", // }; // var smsOutput = await smsProvider.SendSmsBySMSProviderSettings(smsModel); // } // } public async Task SentOTPByMail(string userId, string email, string oneTimePassword) { if (appSettings.OTPSettings.SendEmail) { string subject = appSettings.OTPSettings.MessageSubject; string message = string.Format(appSettings.OTPSettings.MessageBody , oneTimePassword); await mailSender.SendEmail(new EmailMessage { Subject = subject, To = email, Body = message, url = "", userId = userId }); } } } }