diff --git a/csharp/App/Backend/Controller.cs b/csharp/App/Backend/Controller.cs index 7adf7d751..0ce0bee07 100644 --- a/csharp/App/Backend/Controller.cs +++ b/csharp/App/Backend/Controller.cs @@ -236,8 +236,6 @@ public class Controller : ControllerBase public ActionResult> GetAllFoldersAndInstallations(Token authToken) { var user = Db.GetSession(authToken)?.User; - - "GetAllFoldersAndInstallations".WriteLine(); if (user is null) return Unauthorized(); @@ -444,6 +442,35 @@ public class Controller : ControllerBase : Unauthorized(); } + [HttpGet(nameof(ResetPasswordRequest))] + public ActionResult> ResetPasswordRequest(String username) + { + var user = Db.GetUserByEmail(username); + + if (user is null) + return Unauthorized(); + + var session = new Session(user.HidePassword().HideParentIfUserHasNoAccessToParent(user)); + + return Db.Create(session) && Db.SendPasswordResetEmail(user, session.Token) + ? Ok() + : Unauthorized(); + } + + [HttpPost(nameof(ResetPassword))] + public ActionResult> ResetPassword(Token authToken) + { + var user = Db.GetSession(authToken)?.User; + + if (user is null) + return Unauthorized(); + + return Db.DeleteUserPassword(user) + ? RedirectToRoute("https://monitor.innov.energy") + : Unauthorized(); + } + + } diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index ed95f6340..7fdca6bf5 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -167,5 +167,16 @@ public static partial class Db await installation.RenewS3Credentials(); } } - + + public static Boolean SendPasswordResetEmail(User user, String sessionToken) + { + return Mailer.Mailer.SendPasswordResetMessage(user, sessionToken); + } + + public static Boolean DeleteUserPassword(User user) + { + user.Password = ""; + user.MustResetPassword = true; + return Update(user); + } } \ No newline at end of file diff --git a/csharp/App/Backend/Mailer/Mailer.cs b/csharp/App/Backend/Mailer/Mailer.cs index a04097e06..027c49707 100644 --- a/csharp/App/Backend/Mailer/Mailer.cs +++ b/csharp/App/Backend/Mailer/Mailer.cs @@ -40,4 +40,41 @@ public static class Mailer return true; } + public static Boolean SendPasswordResetMessage (User emailRecipientUser, String token) + { + var config = JsonSerializer.Deserialize(File.OpenRead("./Resources/smtpConfig.json"))!; + var email = new MimeMessage(); + + //todo build me dynamically + const String resetLink = "https://monitor.innov.energy/resetPassword"; + + try + { + + email.From.Add(new MailboxAddress("InnovEnergy", "noreply@innov.energy")); + email.To.Add(new MailboxAddress(emailRecipientUser.Name, emailRecipientUser.Email)); + + email.Subject = "Reset the password of your Innovenergy-Account"; + email.Body = new TextPart(MimeKit.Text.TextFormat.Plain) { + Text = "Dear " + emailRecipientUser.Name + + "\n To reset your password open this link:" + + resetLink + "/" + + token + }; + + using var smtp = new SmtpClient(); + smtp.Connect(config.Url, config.Port, false); + + smtp.Authenticate(config.Username, config.Password); + + smtp.Send(email); + smtp.Disconnect(true); + } + catch (Exception) + { + return false; + } + + return true; + } } diff --git a/csharp/App/S3Explorer/Program.cs b/csharp/App/S3Explorer/Program.cs index 69c9d8890..3fdc7baa5 100644 --- a/csharp/App/S3Explorer/Program.cs +++ b/csharp/App/S3Explorer/Program.cs @@ -1,4 +1,5 @@ -using InnovEnergy.App.Backend.S3; +using System.ComponentModel; +using InnovEnergy.App.Backend.S3; using InnovEnergy.Lib.Utils; namespace S3Explorer;