add option to disable mail

This commit is contained in:
Elijah R 2024-04-08 19:06:05 -04:00
parent 2763432672
commit 8a4a38a294
4 changed files with 39 additions and 14 deletions

View file

@ -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)

View file

@ -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

View file

@ -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);

View file

@ -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)
{