refactor(HomeController): Statische Eigenschaften SmsTotpStep und SmsFormat hinzugefügt.

This commit is contained in:
Developer 02 2025-01-27 15:01:34 +01:00
parent 80f9107e4e
commit be44f9f436

View File

@ -24,9 +24,9 @@ using EnvelopeGenerator.Application.Extensions;
namespace EnvelopeGenerator.Web.Controllers namespace EnvelopeGenerator.Web.Controllers
{ {
public class HomeController : Controller public class HomeController : Controller
{ {
private readonly ILogger<HomeController> _logger; private readonly ILogger<HomeController> _logger;
private readonly EnvelopeOldService envelopeOldService; private readonly EnvelopeOldService envelopeOldService;
private readonly IEnvelopeReceiverService _envRcvService; private readonly IEnvelopeReceiverService _envRcvService;
private readonly IEnvelopeHistoryService _historyService; private readonly IEnvelopeHistoryService _historyService;
private readonly IStringLocalizer<Resource> _localizer; private readonly IStringLocalizer<Resource> _localizer;
@ -38,6 +38,8 @@ namespace EnvelopeGenerator.Web.Controllers
private readonly IMessagingService _msgService; private readonly IMessagingService _msgService;
private readonly ICodeGenerator _codeGenerator; private readonly ICodeGenerator _codeGenerator;
private readonly IReceiverService _rcvService; private readonly IReceiverService _rcvService;
private static readonly int SmsTotpStep = 60 * 3;
private static readonly string SmsFormat = "{0}";
public HomeController(EnvelopeOldService envelopeOldService, ILogger<HomeController> logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeHistoryService historyService, IStringLocalizer<Resource> localizer, IConfiguration configuration, HtmlSanitizer sanitizer, Cultures cultures, IEnvelopeMailService envelopeMailService, IEnvelopeReceiverReadOnlyService readOnlyService, IMessagingService messagingService, ICodeGenerator codeGenerator, IReceiverService receiverService) public HomeController(EnvelopeOldService envelopeOldService, ILogger<HomeController> logger, IEnvelopeReceiverService envelopeReceiverService, IEnvelopeHistoryService historyService, IStringLocalizer<Resource> localizer, IConfiguration configuration, HtmlSanitizer sanitizer, Cultures cultures, IEnvelopeMailService envelopeMailService, IEnvelopeReceiverReadOnlyService readOnlyService, IMessagingService messagingService, ICodeGenerator codeGenerator, IReceiverService receiverService)
{ {
@ -195,9 +197,9 @@ namespace EnvelopeGenerator.Web.Controllers
if (viaSms) if (viaSms)
{ {
//add date time cache //add date time cache
var res = await _msgService.SendSmsCodeAsync(er_secret.PhoneNumber!, er_secret.Receiver.TotpSecretkey); var res = await _msgService.SendSmsCodeAsync(er_secret.PhoneNumber!, er_secret.Receiver!.TotpSecretkey!, SmsFormat);
if (res.Ok) if (res.Ok)
return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", res.Expiration); return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", _codeGenerator.GetTotpExpirationTime(SmsTotpStep));
else if (!res.Allowed) else if (!res.Allowed)
return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", res.AllowedAt); return View("EnvelopeLocked").WithData("CodeType", "smsCode").WithData("SmsExpiration", res.AllowedAt);
else else
@ -253,7 +255,7 @@ namespace EnvelopeGenerator.Web.Controllers
if (er_secret.Receiver!.TotpSecretkey is null) if (er_secret.Receiver!.TotpSecretkey is null)
throw new InvalidOperationException($"TotpSecretkey of DTO cannot validate without TotpSecretkey. Dto: {JsonConvert.SerializeObject(er_secret)}"); throw new InvalidOperationException($"TotpSecretkey of DTO cannot validate without TotpSecretkey. Dto: {JsonConvert.SerializeObject(er_secret)}");
if (_codeGenerator.VerifyTotp(auth.SmsCode!, er_secret.Receiver.TotpSecretkey, step: 60 * 5)) if (_codeGenerator.VerifyTotp(auth.SmsCode!, er_secret.Receiver.TotpSecretkey, step: SmsTotpStep))
{ {
Response.StatusCode = StatusCodes.Status401Unauthorized; Response.StatusCode = StatusCodes.Status401Unauthorized;
ViewData["ErrorMessage"] = _localizer[WebKey.WrongAccessCode].Value; ViewData["ErrorMessage"] = _localizer[WebKey.WrongAccessCode].Value;
@ -279,14 +281,14 @@ namespace EnvelopeGenerator.Web.Controllers
//continue the process without important data to minimize security errors. //continue the process without important data to minimize security errors.
EnvelopeReceiverDto er = er_secret; EnvelopeReceiverDto er = er_secret;
ViewData["EnvelopeKey"] = envelopeReceiverId; ViewData["EnvelopeKey"] = envelopeReceiverId;
//check rejection //check rejection
var rejRcvrs = await _historyService.ReadRejectingReceivers(er.Envelope!.Id); var rejRcvrs = await _historyService.ReadRejectingReceivers(er.Envelope!.Id);
if(rejRcvrs.Any()) if(rejRcvrs.Any())
{ {
ViewBag.IsExt = !rejRcvrs.Contains(er.Receiver); //external if the current user is not rejected ViewBag.IsExt = !rejRcvrs.Contains(er.Receiver); //external if the current user is not rejected
return View("EnvelopeRejected", er); return View("EnvelopeRejected", er);
} }
//check if it has already signed //check if it has already signed
if (await _historyService.IsSigned(envelopeId: er.Envelope!.Id, userReference: er.Receiver!.EmailAddress)) if (await _historyService.IsSigned(envelopeId: er.Envelope!.Id, userReference: er.Receiver!.EmailAddress))
@ -299,8 +301,8 @@ namespace EnvelopeGenerator.Web.Controllers
else else
{ {
_logger.LogEnvelopeError(envelopeReceiverId: envelopeReceiverId, message: "No document byte-data was found in ENVELOPE_DOCUMENT table."); _logger.LogEnvelopeError(envelopeReceiverId: envelopeReceiverId, message: "No document byte-data was found in ENVELOPE_DOCUMENT table.");
return this.ViewDocumentNotFound(); return this.ViewDocumentNotFound();
} }
var claims = new List<Claim> { var claims = new List<Claim> {
new(ClaimTypes.NameIdentifier, uuid), new(ClaimTypes.NameIdentifier, uuid),
@ -381,7 +383,8 @@ namespace EnvelopeGenerator.Web.Controllers
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return await _envRcvService.ReadByEnvelopeReceiverIdAsync(envelopeReceiverId).ThenAsync( return await _envRcvService.ReadByEnvelopeReceiverIdAsync(envelopeReceiverId).ThenAsync(
SuccessAsync: async (er) => SuccessAsync: async (er) =>
{ViewData["UserCulture"] = _cultures[UserLanguage]; {
ViewData["UserCulture"] = _cultures[UserLanguage];
ViewData["UserCulture"] = _cultures[UserLanguage]; ViewData["UserCulture"] = _cultures[UserLanguage];
return await _historyService.IsRejected(envelopeId: er.EnvelopeId) return await _historyService.IsRejected(envelopeId: er.EnvelopeId)
? View(er) ? View(er)