me traigo todos los cambios del recuperar cuenta y set email respaldo
This commit is contained in:
@@ -2,6 +2,7 @@ using AlquilaFacil.Builder;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Modelo;
|
||||
using Entidades;
|
||||
using AlquilaFacil.Emailer.Sender;
|
||||
|
||||
namespace AlquilaFacil.Controllers;
|
||||
|
||||
@@ -51,7 +52,15 @@ public class UsuarioController : ControllerBase
|
||||
if (!emailrecuperacion.Contains("@")) return BadRequest(new { message = "Tiene que ser un email" });
|
||||
|
||||
bool ret = RepositorioUsuarios.Singleton.SetEmailRecuperacion(emailrecuperacion, cli);
|
||||
return ret ? Ok(new { message = "Email de recuperación actualizado con éxito" }) : BadRequest(new { message = "No se pudo actualizar el email de recuperación" });
|
||||
if (ret == false) return BadRequest(new { message = "No se pudo actualizar el email de recuperación" });
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
AvisoEmailSender s = new();
|
||||
s.Send(cli.Email, setemail.EmailRecuperacion);
|
||||
});
|
||||
|
||||
return Ok(new { message = "Email de recuperación actualizado con éxito" });
|
||||
}
|
||||
|
||||
public record recuperarusuario(string Email, string EmailRecuperacion);
|
||||
@@ -61,7 +70,34 @@ public class UsuarioController : ControllerBase
|
||||
bool check = RepositorioUsuarios.Singleton.CheckEmailRecuperacion(mails.Email, mails.EmailRecuperacion);
|
||||
if (check == false) return BadRequest(new { message = "El email no corresponde al email de recuperacion" });
|
||||
|
||||
//WIP hacer emailer
|
||||
string pin = "";
|
||||
var ran = new Random();
|
||||
for (int i = 0; i < 6; i++) pin += ran.Next(0, 10);
|
||||
|
||||
bool ret = RepositorioUsuarios.Singleton.SetF2aPin(pin, mails.Email);
|
||||
if (ret == false) return BadRequest(new { message = "no se pudo generar/guardar el codigo 2fa" });
|
||||
|
||||
OtpEmailSender s = new();
|
||||
s.Send(mails.EmailRecuperacion, mails.Email, pin);
|
||||
|
||||
return Ok(new { message = $"Se envio un email de recuperacion a {mails.EmailRecuperacion}" });
|
||||
}
|
||||
|
||||
public record ingreso2fa(string Pin, string Email);
|
||||
[HttpPost("/api/ingresar2fa")]
|
||||
public IActionResult IngresarUsuario([FromBody] ingreso2fa data)
|
||||
{
|
||||
if (!data.Email.Contains("@")) return BadRequest(new { message = "Tiene que ser un email" });
|
||||
if (data.Pin.Length != 6) return BadRequest(new { message = "el pin tiene que tener 6 digitos" });
|
||||
|
||||
(bool check, long Dni) = RepositorioUsuarios.Singleton.Check2fa(data.Email, data.Pin);
|
||||
if (check == false) return BadRequest(new { message = "El pin es incorrecto" });
|
||||
|
||||
var cli = RepositorioUsuarios.Singleton.ObtenerClientePorDni(Dni);
|
||||
|
||||
//esto literalmente no se puede triggerear pero lo pongo para evitar una warning
|
||||
if (cli == null) return BadRequest(new { message = "El usuario no existe" });
|
||||
|
||||
return Ok(new { token = cli.Token });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,20 @@ public class EmailBuilder
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmailBuilder Body(string email, string pin)
|
||||
public EmailBuilder Body(string email, string pin, string modo = "2fa")
|
||||
{
|
||||
_message.IsBodyHtml = true;
|
||||
_message.Body = new HtmlGenerator().GenerarMail2fa(email, pin);
|
||||
switch (modo)
|
||||
{
|
||||
case "2fa":
|
||||
_message.Body = new HtmlGenerator().GenerarMail2fa(email, pin);
|
||||
break;
|
||||
case "aviso":
|
||||
_message.Body = new HtmlGenerator().AvisoSetEmail(email, pin);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,4 +55,58 @@ public class HtmlGenerator
|
||||
|
||||
return msg;
|
||||
}
|
||||
public string AvisoSetEmail(string emailUsuario, string emailreq)
|
||||
{
|
||||
var msg = $"""
|
||||
<!doctype html>
|
||||
<html>
|
||||
<body>
|
||||
<div
|
||||
style='background-color:#000000;color:#FFFFFF;font-family:"Iowan Old Style", "Palatino Linotype", "URW Palladio L", P052, serif;font-size:16px;font-weight:400;letter-spacing:0.15008px;line-height:1.5;margin:0;padding:32px 0;min-height:100%;width:100%'
|
||||
>
|
||||
<table
|
||||
align="center"
|
||||
width="100%"
|
||||
style="margin:0 auto;max-width:600px;background-color:#000000"
|
||||
role="presentation"
|
||||
cellspacing="0"
|
||||
cellpadding="0"
|
||||
border="0"
|
||||
>
|
||||
<tbody>
|
||||
<tr style="width:100%">
|
||||
<td>
|
||||
<div
|
||||
style="color:#ffffff;font-size:16px;font-weight:normal;text-align:center;padding:16px 24px 16px 24px"
|
||||
>
|
||||
Aviso:
|
||||
</div>
|
||||
<h1
|
||||
style='font-weight:bold;text-align:center;margin:0;font-family:"Nimbus Mono PS", "Courier New", "Cutive Mono", monospace;font-size:32px;padding:16px 24px 16px 24px'
|
||||
>
|
||||
Se seteo este email : {emailreq}, como email de respaldo
|
||||
</h1>
|
||||
<div
|
||||
style="color:#868686;font-size:16px;font-weight:normal;text-align:center;padding:16px 24px 16px 24px"
|
||||
>
|
||||
|
||||
</div>
|
||||
<div
|
||||
style="color:#868686;font-size:14px;font-weight:normal;text-align:center;padding:16px 24px 16px 24px"
|
||||
>
|
||||
Si no sabes para que es el email, ignoralo.
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
""";
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
11
Aspnet/Emailer/Decorator/AvisoEmailSender.cs
Normal file
11
Aspnet/Emailer/Decorator/AvisoEmailSender.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace AlquilaFacil.Emailer.Sender;
|
||||
using AlquilaFacil.Emailer.Builder;
|
||||
|
||||
public class AvisoEmailSender : EmailSender
|
||||
{
|
||||
public void Send(string emailusu, string emailreq)
|
||||
{
|
||||
var mail = new EmailBuilder().Body(emailusu, emailreq, "aviso").To(emailreq).Subject("AvisoEmail").Build();
|
||||
base.Send(mail);
|
||||
}
|
||||
}
|
||||
50
Aspnet/Emailer/Decorator/EmailerSender.cs
Normal file
50
Aspnet/Emailer/Decorator/EmailerSender.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Net.Mail;
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace AlquilaFacil.Emailer.Sender;
|
||||
|
||||
public class EmailSender
|
||||
{
|
||||
protected static SmtpClient? smtp = null;
|
||||
protected void configSmtp(MailMessage mail)
|
||||
{
|
||||
var jsonContent = File.ReadAllText("settings.json");
|
||||
var options = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonContent);
|
||||
if (options == null) return;
|
||||
|
||||
|
||||
bool check = options.ContainsKey("smtpHost");
|
||||
check = options.ContainsKey("smtpPort");
|
||||
check = options.ContainsKey("emailAddr");
|
||||
check = options.ContainsKey("emailPass");
|
||||
|
||||
if (check == false) return;
|
||||
mail.Sender = new MailAddress(options["emailAddr"]);
|
||||
mail.From = new MailAddress(options["emailAddr"]);
|
||||
|
||||
if (null != smtp) return;
|
||||
smtp = new();
|
||||
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
|
||||
smtp.EnableSsl = true;
|
||||
smtp.Host = options["smtpHost"];
|
||||
smtp.Port = int.Parse(options["smtpPort"].ToString());
|
||||
smtp.Credentials = new NetworkCredential(options["emailAddr"], options["emailPass"]);
|
||||
}
|
||||
|
||||
public virtual void Send(MailMessage message)
|
||||
{
|
||||
configSmtp(message);
|
||||
if (smtp == null) return;
|
||||
try
|
||||
{
|
||||
smtp.Send(message);
|
||||
message.Dispose();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
using System.Net.Mail;
|
||||
|
||||
namespace AlquilaFacil.Emailer.Sender;
|
||||
|
||||
public interface IEmailSender
|
||||
{
|
||||
public void Send(MailMessage message, SmtpClient smtp);
|
||||
|
||||
}
|
||||
13
Aspnet/Emailer/Decorator/OtpEmailSender.cs
Normal file
13
Aspnet/Emailer/Decorator/OtpEmailSender.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace AlquilaFacil.Emailer.Sender;
|
||||
using AlquilaFacil.Emailer.Builder;
|
||||
public class OtpEmailSender : EmailSender
|
||||
{
|
||||
|
||||
public void Send(string To, string email, string pin)
|
||||
{
|
||||
var mail = new EmailBuilder().To(To).Body(email, pin).Subject("Mail de Recuperacion").Build();
|
||||
|
||||
base.Send(mail);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
namespace AlquilaFacil.Emailer.Sender;
|
||||
using System.Net.Mail;
|
||||
public class OtpEmailSender : IEmailSender
|
||||
{
|
||||
private readonly int _codigoLength;
|
||||
|
||||
public OtpEmailSenderDecorator(int codigoLength = 6)
|
||||
{
|
||||
_codigoLength = codigoLength;
|
||||
}
|
||||
|
||||
public void Send(MailMessage message, SmtpClient? smtp = null)
|
||||
{
|
||||
if (smtp == null)
|
||||
{
|
||||
smtp = new();
|
||||
//WIP
|
||||
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 4.2 Construir HTML de verificación
|
||||
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
{
|
||||
"usr": "nwFNMLJcn5m0owbzeXMs",
|
||||
"scrt": "Mf9HxTir5mIGwWSBtQXd6DRK2k00V0EyXk7QTu70",
|
||||
"connectiondb": "Server=127.0.0.1;Port=3306;Database=AlquilaFacil;Uid=AlquilaFacil;Pwd=.n@9c2ve*0,b1ETv].Kipa/~pR~V;Connection Timeout=5;SslMode=none"
|
||||
"connectiondb": "Server=127.0.0.1;Port=3306;Database=AlquilaFacil;Uid=AlquilaFacil;Pwd=.n@9c2ve*0,b1ETv].Kipa/~pR~V;Connection Timeout=5;SslMode=none",
|
||||
"smtpHost": "smtp.gmail.com",
|
||||
"smtpPort": "587",
|
||||
"emailAddr": "emailerpasillo@gmail.com",
|
||||
"emailPass": "hgwa mznx xuff exws"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user