mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
fix(email): validate settings before sending; use hmac for tokens
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using System.Data;
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
using System.IdentityModel.Tokens.Jwt;
|
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
@@ -30,6 +29,7 @@ namespace SharpbinV3.Server.Services
|
|||||||
IOptions<JWTSettings> jwtOptions,
|
IOptions<JWTSettings> jwtOptions,
|
||||||
IOptions<AuthSettings> authOptions,
|
IOptions<AuthSettings> authOptions,
|
||||||
IOptions<AppSettings> appSettings,
|
IOptions<AppSettings> appSettings,
|
||||||
|
IOptions<EmailSettings> emailSettings,
|
||||||
ILogger<AuthService> logger
|
ILogger<AuthService> logger
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -38,6 +38,7 @@ namespace SharpbinV3.Server.Services
|
|||||||
private readonly JWTSettings _jwtSettings = jwtOptions.Value;
|
private readonly JWTSettings _jwtSettings = jwtOptions.Value;
|
||||||
private readonly AuthSettings _authSettings = authOptions.Value;
|
private readonly AuthSettings _authSettings = authOptions.Value;
|
||||||
private readonly AppSettings _appSettings = appSettings.Value;
|
private readonly AppSettings _appSettings = appSettings.Value;
|
||||||
|
private readonly EmailSettings _emailSettings = emailSettings.Value;
|
||||||
private readonly ILogger _logger = logger;
|
private readonly ILogger _logger = logger;
|
||||||
private const int EmailVerificationTokenLength = 64;
|
private const int EmailVerificationTokenLength = 64;
|
||||||
|
|
||||||
@@ -225,9 +226,14 @@ namespace SharpbinV3.Server.Services
|
|||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(user.Email))
|
if (string.IsNullOrWhiteSpace(user.Email))
|
||||||
return;
|
return;
|
||||||
|
if (string.IsNullOrWhiteSpace(_emailSettings.Verification_HMAC_Secret))
|
||||||
|
{
|
||||||
|
_logger.LogWarning($"Email verification is not configured. Cannot send verification email to {user.Email}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
string token = Utilities.GenerateSecureRandomString(EmailVerificationTokenLength);
|
string token = Utilities.GenerateSecureRandomString(EmailVerificationTokenLength);
|
||||||
string tokenHash = Utilities.ComputeSha256(token);
|
string tokenHash = Utilities.ComputeHmacSha256(token, _emailSettings.Verification_HMAC_Secret);
|
||||||
|
|
||||||
await _email.SendAsync(
|
await _email.SendAsync(
|
||||||
user.Email,
|
user.Email,
|
||||||
@@ -250,8 +256,10 @@ namespace SharpbinV3.Server.Services
|
|||||||
{
|
{
|
||||||
if (token.Length != EmailVerificationTokenLength)
|
if (token.Length != EmailVerificationTokenLength)
|
||||||
return (false, "Invalid token.");
|
return (false, "Invalid token.");
|
||||||
|
if (string.IsNullOrWhiteSpace(_emailSettings.Verification_HMAC_Secret))
|
||||||
|
return (false, "Email verification is not configured.");
|
||||||
|
|
||||||
string tokenHash = Utilities.ComputeSha256(token);
|
string tokenHash = Utilities.ComputeHmacSha256(token, _emailSettings.Verification_HMAC_Secret);
|
||||||
var verificationToken = await _db.EmailVerificationTokens.Include(t => t.User).FirstOrDefaultAsync(t => t.TokenHash == tokenHash);
|
var verificationToken = await _db.EmailVerificationTokens.Include(t => t.User).FirstOrDefaultAsync(t => t.TokenHash == tokenHash);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ namespace SharpbinV3.Server.Services
|
|||||||
|
|
||||||
public async Task SendAsync(string to, string subject, string body)
|
public async Task SendAsync(string to, string subject, string body)
|
||||||
{
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(settings.Host) || string.IsNullOrWhiteSpace(settings.User) || string.IsNullOrWhiteSpace(settings.Password))
|
||||||
|
{
|
||||||
|
logger.LogWarning($"Email settings are not fully configured. Skipping sending email to {to}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
var email = new QueuedEmail(to, subject, body);
|
var email = new QueuedEmail(to, subject, body);
|
||||||
|
|
||||||
if (queue.Writer.TryWrite(email))
|
if (queue.Writer.TryWrite(email))
|
||||||
|
|||||||
Reference in New Issue
Block a user