diff --git a/csharp/App/Backend/Backend.csproj b/csharp/App/Backend/Backend.csproj index 4b498515e..71cefbddc 100644 --- a/csharp/App/Backend/Backend.csproj +++ b/csharp/App/Backend/Backend.csproj @@ -9,7 +9,6 @@ - @@ -34,6 +33,7 @@ + diff --git a/csharp/App/Backend/Controller.cs b/csharp/App/Backend/Controller.cs index 0ce0bee07..80f6d32a1 100644 --- a/csharp/App/Backend/Controller.cs +++ b/csharp/App/Backend/Controller.cs @@ -442,7 +442,7 @@ public class Controller : ControllerBase : Unauthorized(); } - [HttpGet(nameof(ResetPasswordRequest))] + [HttpPost(nameof(ResetPasswordRequest))] public ActionResult> ResetPasswordRequest(String username) { var user = Db.GetUserByEmail(username); @@ -457,7 +457,7 @@ public class Controller : ControllerBase : Unauthorized(); } - [HttpPost(nameof(ResetPassword))] + [HttpGet(nameof(ResetPassword))] public ActionResult> ResetPassword(Token authToken) { var user = Db.GetSession(authToken)?.User; diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index 7fdca6bf5..09adf1776 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -1,14 +1,10 @@ -using System.Data.SQLite; using System.Reactive.Concurrency; using System.Reactive.Linq; -using System.Runtime.InteropServices; using CliWrap; using CliWrap.Buffered; using InnovEnergy.App.Backend.DataTypes; using InnovEnergy.App.Backend.DataTypes.Methods; using InnovEnergy.App.Backend.Relations; -using InnovEnergy.Lib.Utils; -using Microsoft.Identity.Client; using SQLite; using SQLiteConnection = SQLite.SQLiteConnection; @@ -170,7 +166,7 @@ public static partial class Db public static Boolean SendPasswordResetEmail(User user, String sessionToken) { - return Mailer.Mailer.SendPasswordResetMessage(user, sessionToken); + return Email.Email.SendPasswordResetMessage(user, sessionToken); } public static Boolean DeleteUserPassword(User user) diff --git a/csharp/App/Backend/Email/Email.cs b/csharp/App/Backend/Email/Email.cs new file mode 100644 index 000000000..9e5262e36 --- /dev/null +++ b/csharp/App/Backend/Email/Email.cs @@ -0,0 +1,54 @@ +using System.Diagnostics.CodeAnalysis; +using InnovEnergy.App.Backend.DataTypes; +using InnovEnergy.Lib.Mailer; +using MimeKit; +using JsonSerializer = System.Text.Json.JsonSerializer; + +namespace InnovEnergy.App.Backend.Email; +public static class Email + { + [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "")] + public static Boolean SendVerificationMessage(User emailRecipientUser) + { + var config = JsonSerializer.Deserialize(File.OpenRead("./Resources/smtpConfig.json"))!; + var mailer = new Mailer(); + + Mailer.From("InnovEnergy", "noreply@innov.energy"); + Mailer.To(emailRecipientUser.Name, emailRecipientUser.Email); + + Mailer.Subject("Create a new password for your Innovenergy-Account"); + Mailer.Body("Dear " + emailRecipientUser.Name + + "\n Please create a new password for your Innovenergy-account." + + "\n To do this just login at https://HEEEEELP"); + + return Mailer.SendEmailUsingSmtpConfig(config); + } + + [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "")] + public static Boolean SendPasswordResetMessage (User emailRecipientUser, String token) + { + var config = JsonSerializer.Deserialize(File.OpenRead("./Resources/smtpConfig.json"))!; + + //todo am I right? + const String resetLink = "https://monitor.innov.energy/api/resetPassword"; + + try + { + + Mailer.From("InnovEnergy", "noreply@innov.energy"); + Mailer.To(emailRecipientUser.Name, emailRecipientUser.Email); + + Mailer.Subject("Reset the password of your Innovenergy-Account"); + Mailer.Body("Dear " + emailRecipientUser.Name + + "\n To reset your password open this link:" + + resetLink + "/" + + token); + + return Mailer.SendEmailUsingSmtpConfig(config); + } + catch (Exception) + { + return false; + } + } + } diff --git a/csharp/App/Backend/Mailer/Mailer.cs b/csharp/App/Backend/Mailer/Mailer.cs deleted file mode 100644 index 0c64955d4..000000000 --- a/csharp/App/Backend/Mailer/Mailer.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using InnovEnergy.App.Backend.DataTypes; -using MailKit.Net.Smtp; -using MimeKit; -using JsonSerializer = System.Text.Json.JsonSerializer; - -namespace InnovEnergy.App.Backend.Mailer; -public static class Mailer - { - [UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "")] - public static Boolean SendVerificationMessage (User emailRecipientUser) - { - var config = JsonSerializer.Deserialize(File.OpenRead("./Resources/smtpConfig.json"))!; - var email = new MimeMessage(); - - try - { - - email.From.Add(new MailboxAddress("InnovEnergy", "noreply@innov.energy")); - email.To.Add(new MailboxAddress(emailRecipientUser.Name, emailRecipientUser.Email)); - - email.Subject = "Create a new password for your Innovenergy-Account"; - email.Body = new TextPart(MimeKit.Text.TextFormat.Plain) { - Text = "Dear " + emailRecipientUser.Name + "\n Please create a new password for your Innovenergy-account." + - "\n To do this just login at https://HEEEEELP" - }; - - 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; - } - public static Boolean SendPasswordResetMessage (User emailRecipientUser, String token) - { - var config = JsonSerializer.Deserialize(File.OpenRead("./Resources/smtpConfig.json"))!; - var email = new MimeMessage(); - - //todo am I right? - const String resetLink = "https://monitor.innov.energy/api/sresetPassword"; - - 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/VrmGrabber/Controller.cs b/csharp/App/VrmGrabber/Controller.cs index dcc6bfb66..cf174de10 100644 --- a/csharp/App/VrmGrabber/Controller.cs +++ b/csharp/App/VrmGrabber/Controller.cs @@ -188,7 +188,7 @@ th { /* header cell */ await SendNewBatteryFirmware(installationIp); var batteryTtyName = split[1].Split(".").Last(); - var localCommand = $"/opt/innovenergy/scripts/upload-bms-firmware {batteryTtyName} 2 /opt/innovenergy/{FirmwareVersion}.bin"; + var localCommand = $"/opt/innovenergy/scripts/upload-bms-firmware {batteryTtyName} 2 /opt/innovenergy/bms-firmware/{FirmwareVersion}.bin"; var installation = Db.Installations.First(installation => installation.Ip == installationIp); installation.BatteryUpdateStatus = "Running"; Db.Update(installation: installation); diff --git a/csharp/App/VrmGrabber/db.sqlite b/csharp/App/VrmGrabber/db.sqlite index 382d49a9e..81843140c 100644 Binary files a/csharp/App/VrmGrabber/db.sqlite and b/csharp/App/VrmGrabber/db.sqlite differ diff --git a/csharp/InnovEnergy.sln b/csharp/InnovEnergy.sln index 311dd0f76..1a364ffe3 100644 --- a/csharp/InnovEnergy.sln +++ b/csharp/InnovEnergy.sln @@ -81,6 +81,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "S3Explorer", "App\S3Explore EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VrmGrabber", "App\VrmGrabber\VrmGrabber.csproj", "{88633C71-D701-49B3-A6DE-9D7CED9046E3}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mailer", "Lib\Mailer\Mailer.csproj", "{73B97F6E-2BDC-40DA-84A7-7FB0264387D6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -208,6 +210,10 @@ Global {88633C71-D701-49B3-A6DE-9D7CED9046E3}.Debug|Any CPU.Build.0 = Debug|Any CPU {88633C71-D701-49B3-A6DE-9D7CED9046E3}.Release|Any CPU.ActiveCfg = Release|Any CPU {88633C71-D701-49B3-A6DE-9D7CED9046E3}.Release|Any CPU.Build.0 = Release|Any CPU + {73B97F6E-2BDC-40DA-84A7-7FB0264387D6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {73B97F6E-2BDC-40DA-84A7-7FB0264387D6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {73B97F6E-2BDC-40DA-84A7-7FB0264387D6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {73B97F6E-2BDC-40DA-84A7-7FB0264387D6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {CF4834CB-91B7-4172-AC13-ECDA8613CD17} = {145597B4-3E30-45E6-9F72-4DD43194539A} @@ -244,5 +250,6 @@ Global {1391165D-51F1-45B4-8B7F-042A20AA0277} = {4931A385-24DC-4E78-BFF4-356F8D6D5183} {EB56EF94-D8A7-4111-A8E7-A87EF13596DA} = {145597B4-3E30-45E6-9F72-4DD43194539A} {88633C71-D701-49B3-A6DE-9D7CED9046E3} = {145597B4-3E30-45E6-9F72-4DD43194539A} + {73B97F6E-2BDC-40DA-84A7-7FB0264387D6} = {AD5B98A8-AB7F-4DA2-B66D-5B4E63E7D854} EndGlobalSection EndGlobal diff --git a/csharp/Lib/Mailer/Mailer.cs b/csharp/Lib/Mailer/Mailer.cs new file mode 100644 index 000000000..cade38440 --- /dev/null +++ b/csharp/Lib/Mailer/Mailer.cs @@ -0,0 +1,55 @@ +using MailKit.Net.Smtp; +using MimeKit; + +namespace InnovEnergy.Lib.Mailer; + + +public class Mailer +{ + private static MimeMessage Email = new(); + + public static MimeMessage To(String name, String emailAddress) + { + Email.To.Add(new MailboxAddress(name, emailAddress)); + return Email; + } + + public static MimeMessage From(String name, String emailAddress) + { + Email.From.Add(new MailboxAddress(name, emailAddress)); + return Email; + } + + public static MimeMessage Subject(String subjectText) + { + Email.Subject = subjectText; + return Email; + } + + public static MimeMessage Body(String bodyText) + { + Email.Body = new TextPart(MimeKit.Text.TextFormat.Plain) + { + Text = bodyText + }; + return Email; + } + + public static Boolean SendEmailUsingSmtpConfig(SmtpConfig config) + { + try{ + 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; + } +} \ No newline at end of file diff --git a/csharp/Lib/Mailer/Mailer.csproj b/csharp/Lib/Mailer/Mailer.csproj new file mode 100644 index 000000000..bebf4a19d --- /dev/null +++ b/csharp/Lib/Mailer/Mailer.csproj @@ -0,0 +1,10 @@ + + + + InnovEnergy.Lib.Mailer + + + + + + diff --git a/csharp/App/Backend/Mailer/SmptConfig.cs b/csharp/Lib/Mailer/SmtpConfig.cs similarity index 81% rename from csharp/App/Backend/Mailer/SmptConfig.cs rename to csharp/Lib/Mailer/SmtpConfig.cs index 02b63653a..22fdb84bd 100644 --- a/csharp/App/Backend/Mailer/SmptConfig.cs +++ b/csharp/Lib/Mailer/SmtpConfig.cs @@ -1,13 +1,12 @@ using System.Diagnostics.CodeAnalysis; +namespace InnovEnergy.Lib.Mailer; -namespace InnovEnergy.App.Backend.Mailer; [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] -public class SmptConfig +public class SmtpConfig { public String Url { get; init; } = null!; public String Username { get; init; } = null!; public String Password { get; init; } = null!; public Int32 Port { get; init; } = 587; - } \ No newline at end of file