From 8a4a38a294a629a4678de8b575d770e86b284d53 Mon Sep 17 00:00:00 2001 From: Elijah R Date: Mon, 8 Apr 2024 19:06:05 -0400 Subject: [PATCH] add option to disable mail --- CollabVMAuthServer/HTTP/Routes.cs | 10 ++++++++++ CollabVMAuthServer/IConfig.cs | 21 +++++++++++---------- CollabVMAuthServer/Mailer.cs | 12 ++++++++++-- CollabVMAuthServer/Program.cs | 10 ++++++++-- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/CollabVMAuthServer/HTTP/Routes.cs b/CollabVMAuthServer/HTTP/Routes.cs index 98ad355..7eb5a96 100644 --- a/CollabVMAuthServer/HTTP/Routes.cs +++ b/CollabVMAuthServer/HTTP/Routes.cs @@ -149,6 +149,16 @@ public static class Routes error = "Invalid request body" }, Utilities.JsonSerializerOptions); } + // Is mailer enabled? + if (Program.Mailer == null) + { + context.Response.StatusCode = 400; + return Results.Json(new ResetPasswordResponse + { + success = false, + error = "Password reset is disabled" + }, Utilities.JsonSerializerOptions); + } // Check username and E-Mail var user = await Program.Database.GetUser(payload.username); if (user == null || user.Email != payload.email) diff --git a/CollabVMAuthServer/IConfig.cs b/CollabVMAuthServer/IConfig.cs index 931339c..feb8f8c 100644 --- a/CollabVMAuthServer/IConfig.cs +++ b/CollabVMAuthServer/IConfig.cs @@ -47,16 +47,17 @@ public class MySQLConfig public class SMTPConfig { - public string Host { get; set; } - public int Port { get; set; } - public string Username { get; set; } - public string Password { get; set; } - public string FromName { get; set; } - public string FromEmail { get; set; } - public string VerificationCodeSubject { get; set; } - public string VerificationCodeBody { get; set; } - public string ResetPasswordSubject { get; set; } - public string ResetPasswordBody { get; set; } + public bool Enabled { get; set; } + public string? Host { get; set; } + public int? Port { get; set; } + public string? Username { get; set; } + public string? Password { get; set; } + public string? FromName { get; set; } + public string? FromEmail { get; set; } + public string? VerificationCodeSubject { get; set; } + public string? VerificationCodeBody { get; set; } + public string? ResetPasswordSubject { get; set; } + public string? ResetPasswordBody { get; set; } } public class hCaptchaConfig diff --git a/CollabVMAuthServer/Mailer.cs b/CollabVMAuthServer/Mailer.cs index 5754b8e..74e46fd 100644 --- a/CollabVMAuthServer/Mailer.cs +++ b/CollabVMAuthServer/Mailer.cs @@ -9,6 +9,14 @@ public class Mailer private SMTPConfig Config; public Mailer(SMTPConfig config) { + if (config.Host == null || config.Port == null || config.Username == null || config.Password == null || + config.FromName == null || config.FromEmail == null || config.VerificationCodeSubject == null || + config.VerificationCodeBody == null || config.ResetPasswordSubject == null || + config.ResetPasswordBody == null) + { + Utilities.Log(LogLevel.FATAL,"SMTPConfig is missing required fields"); + Environment.Exit(1); + } Config = config; } @@ -29,7 +37,7 @@ public class Mailer .Replace("$CODE", code) }; using var client = new SmtpClient(); - await client.ConnectAsync(Config.Host, Config.Port, SecureSocketOptions.StartTlsWhenAvailable); + await client.ConnectAsync(Config.Host, (int)Config.Port, SecureSocketOptions.StartTlsWhenAvailable); await client.AuthenticateAsync(Config.Username, Config.Password); await client.SendAsync(message); await client.DisconnectAsync(true); @@ -53,7 +61,7 @@ public class Mailer .Replace("$CODE", code) }; using var client = new SmtpClient(); - await client.ConnectAsync(Config.Host, Config.Port, SecureSocketOptions.StartTlsWhenAvailable); + await client.ConnectAsync(Config.Host, (int)Config.Port, SecureSocketOptions.StartTlsWhenAvailable); await client.AuthenticateAsync(Config.Username, Config.Password); await client.SendAsync(message); await client.DisconnectAsync(true); diff --git a/CollabVMAuthServer/Program.cs b/CollabVMAuthServer/Program.cs index 40600bf..95c00f7 100644 --- a/CollabVMAuthServer/Program.cs +++ b/CollabVMAuthServer/Program.cs @@ -9,7 +9,7 @@ public class Program public static IConfig Config { get; private set; } public static Database Database { get; private set; } public static hCaptchaClient? hCaptcha { get; private set; } - public static Mailer Mailer { get; private set; } + public static Mailer? Mailer { get; private set; } public static string[] BannedPasswords { get; set; } public static readonly Random Random = new Random(); public static async Task Main(string[] args) @@ -42,7 +42,13 @@ public class Program await Database.Init(); Utilities.Log(LogLevel.INFO, "Connected to database"); // Create mailer - Mailer = new Mailer(Config.SMTP); + if (!Config.SMTP.Enabled && Config.Registration.EmailVerificationRequired) + { + Utilities.Log(LogLevel.FATAL, "Email verification is required but SMTP is disabled"); + Environment.Exit(1); + return; + } + Mailer = Config.SMTP.Enabled ? new Mailer(Config.SMTP) : null; // Create hCaptcha client if (Config.hCaptcha.Enabled) {