feat: implement Turnstile verification for enhanced security during login and registration

refactor: remove unnecessary interfaces for services; change user key from UID to UUID
This commit is contained in:
2025-12-31 12:11:09 -05:00
parent 08c4be4f7e
commit 848b8598e5
27 changed files with 579 additions and 84 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ using Bcrypt = BCrypt.Net.BCrypt;
namespace SharpbinV3.Server.Services
{
public sealed class AuthService(AppDbContext db, IOptions<JWTSettings> options) : IAuthService
public sealed class AuthService(AppDbContext db, IOptions<JWTSettings> options)
{
private readonly AppDbContext _db = db;
private readonly JWTSettings _jwtSettings = options.Value;
@@ -1,14 +0,0 @@
using SharpbinV3.Server.Data.Entities;
using SharpbinV3.Server.DTOs;
namespace SharpbinV3.Server.Services
{
public interface IAuthService
{
Task<User> CreateUser(string username, string password, string? email, string? displayName);
Task<JWTResult> GenerateJWTToken(User user);
Task<JWTResult> RefreshJWTToken(string token, string refreshToken);
Task<User> UpdateUser(User user);
Task UpdateLoginTime(User user);
}
}
@@ -1,20 +0,0 @@
using SharpbinV3.Server.Data.Entities;
namespace SharpbinV3.Server.Services
{
public interface IPasteService
{
Task<Paste> Create(User? author, string content, string title, string syntax, int visibility, long expiresAt);
Task<bool> Delete(Paste paste);
Task<Paste> Edit(Paste paste);
Task<bool> Exists(string id);
Task<bool> Exists(Guid uuid);
Task<bool> EditText(Paste paste, string text);
Task<Paste?> Get(string id);
Task<List<Paste>> GetList(int offset, int count, bool publicOnly = false);
Task<bool> ValidateSyntax(string syntax);
Task<bool> ValidateTitle(string title);
Task<bool> ValidateVisibility(int visibility);
Task<bool> ValidateExpiresAt(long expiresAt);
}
}
@@ -1,13 +0,0 @@
using SharpbinV3.Server.Data.Entities;
namespace SharpbinV3.Server.Services
{
public interface IUserService
{
Task<User?> GetByUID(int uid, bool withPastes = false);
Task<User?> GetByUsername(string username, bool withPastes = false);
Task<User?> GetByUUID(Guid uuid, bool withPastes = false);
Task<User?> GetByEmail(string email, bool withPastes = false);
Task<User> Update(User user);
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ using System.Text;
namespace SharpbinV3.Server.Services
{
public sealed class PasteService(AppDbContext db, ICompressionService cs, IOptions<PasteSettings> options) : IPasteService
public sealed class PasteService(AppDbContext db, ICompressionService cs, IOptions<PasteSettings> options)
{
private readonly AppDbContext _db = db;
private readonly ICompressionService _cs = cs;
+1 -1
View File
@@ -5,7 +5,7 @@ using SharpbinV3.Server.Data.Entities;
namespace SharpbinV3.Server.Services
{
public sealed class UserService(AppDbContext db) : IUserService
public sealed class UserService(AppDbContext db)
{
private readonly AppDbContext _db = db;
public Task<User?> GetByUID(int uid, bool withPastes = false)
@@ -0,0 +1,9 @@
namespace SharpbinV3.Server.Services.Verification.Providers
{
public interface IVerificationProvider
{
int Priority { get; }
bool IsConfigured { get; }
Task<bool> VerifyAsync(string token, string? ip);
}
}
@@ -0,0 +1,39 @@
using Microsoft.Extensions.Options;
using SharpbinV3.Server.DTOs;
using SharpbinV3.Server.Settings;
using System.Text.Json;
namespace SharpbinV3.Server.Services.Verification.Providers
{
public sealed class TurnstileVerificationProvider(IOptions<AuthSettings> options, HttpClient httpClient) : IVerificationProvider
{
private readonly AuthSettings _authSettings = options.Value;
private const string SiteverifyUrl = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
public int Priority => 1;
public bool IsConfigured => !string.IsNullOrEmpty(_authSettings.CF_Turnstile_SecretKey) && !string.IsNullOrEmpty(_authSettings.CF_Turnstile_SiteKey);
public async Task<bool> VerifyAsync(string token, string? ip)
{
if (_authSettings.CF_Turnstile_SecretKey is null)
throw new InvalidOperationException("Turnstile secret key is not configured.");
var parameters = new Dictionary<string, string>
{
{ "secret", _authSettings.CF_Turnstile_SecretKey },
{ "response", token }
};
if (!string.IsNullOrEmpty(ip))
parameters.Add("remoteip", ip);
var postContent = new FormUrlEncodedContent(parameters);
var response = await httpClient.PostAsync(SiteverifyUrl, postContent);
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<TurnstileResponse>(json);
return result?.Success ?? false;
}
}
}
@@ -0,0 +1,31 @@
using SharpbinV3.Server.Services.Verification.Providers;
namespace SharpbinV3.Server.Services.Verification
{
public sealed class VerificationService(IEnumerable<IVerificationProvider> providers)
{
private readonly IReadOnlyList<IVerificationProvider> _providers = [.. providers];
private IVerificationProvider? GetActiveProvider()
{
return _providers
.Where(p => p.IsConfigured)
.OrderByDescending(p => p.Priority)
.FirstOrDefault();
}
public async Task<bool> VerifyAsync(string? token, string? ip)
{
var provider = GetActiveProvider();
if (provider is null)
return true;
if (string.IsNullOrWhiteSpace(token))
return false;
return await provider.VerifyAsync(token, ip);
}
}
}