OTPService.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Microsoft.AspNetCore.Mvc;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel.Design;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Drawing.Drawing2D;
  12. using System.Drawing.Imaging;
  13. using System.Drawing.Text;
  14. using System.Drawing;
  15. using NeomtechERP.Auth.Core.Global;
  16. using NeomtechERP.Auth.Core;
  17. using Microsoft.AspNetCore.Hosting;
  18. using Microsoft.Extensions.Hosting;
  19. using MTWorkHR.Core.Global;
  20. using MTWorkHR.Core.Email;
  21. using MTWorkHR.Core.UnitOfWork;
  22. using MTWorkHR.Application.Services.Interfaces;
  23. using MTWorkHR.Core.Entities;
  24. namespace MTWorkHR.Application.Services
  25. {
  26. public class OTPService : IOTPService
  27. {
  28. private readonly AppSettingsConfiguration appSettings;
  29. private readonly IMailSender mailSender;
  30. private readonly IUnitOfWork unitOfWork;
  31. private readonly IWebHostEnvironment env;
  32. public OTPService(AppSettingsConfiguration appSettings, IUnitOfWork unitOfWork, IMailSender mailSender,
  33. IWebHostEnvironment env)
  34. {
  35. this.appSettings = appSettings;
  36. this.mailSender = mailSender;
  37. this.unitOfWork = unitOfWork;
  38. this.env = env;
  39. }
  40. public async Task<string> RandomOneTimePassword(string userId)
  41. {
  42. string oneTimePassword = default;
  43. for (var index = 0; index < appSettings.OTPSettings.Length; index++)
  44. {
  45. oneTimePassword += $"{new Random().Next(0, 10)}";
  46. }
  47. var loginOTP = new LoginOTP()
  48. {
  49. OTP = oneTimePassword,
  50. UserId = userId,
  51. CreateDate = DateTime.Now,
  52. ExpireDate = DateTime.Now.AddMinutes(appSettings.OTPSettings.ExpirePeriodInMinutes)
  53. };
  54. await unitOfWork.LoginOTP.AddAsync(loginOTP);
  55. await unitOfWork.CompleteAsync();
  56. return oneTimePassword;
  57. }
  58. public async Task<bool> VerifyOTP(string userId, string oneTimePassword)
  59. {
  60. if (appSettings.OTPSettings.AllowZeros )
  61. {
  62. var dummyOTP = "";
  63. for (var index = 0; index < appSettings.OTPSettings.Length; index++)
  64. {
  65. dummyOTP += "1";
  66. }
  67. return oneTimePassword == "1111";
  68. }
  69. else
  70. return await unitOfWork.LoginOTP.VerifyOTP(userId, oneTimePassword);
  71. }
  72. //public async Task SentOTPBySMS(string phoneNumber, string oneTimePassword, SMSMethod method)
  73. //{
  74. // if (appSettings.OTPSettings.SendSMS)
  75. // {
  76. // string message = string.Format(appSettings.OTPSettings.MessageBody, oneTimePassword);
  77. // var smsModel = new SMSDTO
  78. // {
  79. // PhoneNumber = phoneNumber,
  80. // MessageBody = message,
  81. // Method = method.ToString(),
  82. // Module = "Auth",
  83. // };
  84. // var smsOutput = await smsProvider.SendSmsBySMSProviderSettings(smsModel);
  85. // }
  86. // }
  87. public async Task SentOTPByMail(string userId, string email, string oneTimePassword)
  88. {
  89. if (appSettings.OTPSettings.SendEmail)
  90. {
  91. string subject = appSettings.OTPSettings.MessageSubject;
  92. string message = string.Format(appSettings.OTPSettings.MessageBody , oneTimePassword);
  93. await mailSender.SendEmail(new EmailMessage
  94. {
  95. Subject = subject,
  96. To = email,
  97. Body = message,
  98. url = "",
  99. userId = userId
  100. });
  101. }
  102. }
  103. }
  104. }