mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
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:
@@ -1,24 +1,31 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.DTOs;
|
||||
using SharpbinV3.Server.Services;
|
||||
using SharpbinV3.Server.Services.Verification;
|
||||
using SharpbinV3.Server.Settings;
|
||||
using Bcrypt = BCrypt.Net.BCrypt;
|
||||
|
||||
namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AuthController(IUserService userService, IAuthService authService) : ControllerBase
|
||||
public class AuthController(UserService userService, AuthService authService, IOptions<AuthSettings> options, VerificationService verification) : ControllerBase
|
||||
{
|
||||
private readonly IAuthService _authService = authService;
|
||||
private readonly IUserService _userService = userService;
|
||||
private readonly AuthService _authService = authService;
|
||||
private readonly UserService _userService = userService;
|
||||
private readonly VerificationService _verification = verification;
|
||||
|
||||
[HttpPost]
|
||||
[EnableRateLimiting("Sliding")]
|
||||
[Route("register")]
|
||||
public async Task<IActionResult> Register([FromBody] RegisterRequest request)
|
||||
{
|
||||
if (!await _verification.VerifyAsync(request.Token, Utilities.GetRequestIP(HttpContext)))
|
||||
return BadRequest(new { message = "Verification failed." });
|
||||
|
||||
var existingUser = await _userService.GetByUsername(request.Username);
|
||||
if (existingUser != null)
|
||||
return Conflict(new { message = "Username already exists." });
|
||||
@@ -51,6 +58,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("login")]
|
||||
[EnableRateLimiting("Sliding")]
|
||||
@@ -59,6 +67,9 @@ namespace SharpbinV3.Server.Controllers
|
||||
if (string.IsNullOrEmpty(request.Username) && string.IsNullOrEmpty(request.Email))
|
||||
return BadRequest(new { message = "Username or email is required." });
|
||||
|
||||
if (!await _verification.VerifyAsync(request.Token, Utilities.GetRequestIP(HttpContext)))
|
||||
return BadRequest(new { message = "Verification failed." });
|
||||
|
||||
User? user = null;
|
||||
if (!string.IsNullOrEmpty(request.Username))
|
||||
user = await _userService.GetByUsername(request.Username);
|
||||
@@ -88,6 +99,17 @@ namespace SharpbinV3.Server.Controllers
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("info")]
|
||||
public async Task<IActionResult> GetSiteInfo()
|
||||
{
|
||||
var authSettings = options.Value;
|
||||
return Ok(new
|
||||
{
|
||||
cf_turnstile_site_key = string.IsNullOrEmpty(authSettings.CF_Turnstile_SiteKey) ? null : authSettings.CF_Turnstile_SiteKey
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private static bool IsValidPassword(string password)
|
||||
{
|
||||
|
||||
@@ -11,10 +11,10 @@ namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class PasteController(IPasteService pasteService, IUserService userService, IOptions<PasteSettings> options) : ControllerBase
|
||||
public class PasteController(PasteService pasteService, UserService userService, IOptions<PasteSettings> options) : ControllerBase
|
||||
{
|
||||
private readonly IPasteService _pasteService = pasteService;
|
||||
private readonly IUserService _userService = userService;
|
||||
private readonly PasteService _pasteService = pasteService;
|
||||
private readonly UserService _userService = userService;
|
||||
private readonly PasteSettings _pasteSettings = options.Value;
|
||||
|
||||
[HttpPost]
|
||||
|
||||
@@ -11,9 +11,9 @@ namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UserController(IUserService userService, AppDbContext db) : ControllerBase
|
||||
public class UserController(UserService userService, AppDbContext db) : ControllerBase
|
||||
{
|
||||
private readonly IUserService _userService = userService;
|
||||
private readonly UserService _userService = userService;
|
||||
private readonly AppDbContext _db = db;
|
||||
|
||||
[HttpGet("{username}")]
|
||||
|
||||
Reference in New Issue
Block a user