mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
feat: auth, compression, jwt, paste wip, roles, ratelimiting
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SharpbinV3.Services;
|
||||
using SharpbinV3.DTOs;
|
||||
using Bcrypt = BCrypt.Net.BCrypt;
|
||||
using SharpbinV3.Data.Entities;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
|
||||
namespace SharpbinV3.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AuthController(IUserService userService, IAuthService authService) : ControllerBase
|
||||
{
|
||||
private readonly IAuthService _authService = authService;
|
||||
private readonly IUserService _userService = userService;
|
||||
|
||||
[HttpPost]
|
||||
[EnableRateLimiting("Sliding")]
|
||||
[Route("register")]
|
||||
public async Task<IActionResult> Register([FromBody] RegisterRequest request)
|
||||
{
|
||||
var existingUser = await _userService.GetByUsername(request.Username);
|
||||
if (existingUser != null)
|
||||
return Conflict(new { message = "Username already exists." });
|
||||
|
||||
var user = await _authService.CreateUser(request.Username, request.Password, request.Email, request.DisplayName);
|
||||
return Ok(new
|
||||
{
|
||||
message = "User registered successfully.",
|
||||
user = new
|
||||
{
|
||||
user.UID,
|
||||
user.UUID,
|
||||
user.Username,
|
||||
user.Email,
|
||||
user.DisplayName,
|
||||
user.Roles
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
[HttpPost]
|
||||
[Route("login")]
|
||||
[EnableRateLimiting("Sliding")]
|
||||
public async Task<IActionResult> Login([FromBody] LoginRequest request)
|
||||
{
|
||||
if (string.IsNullOrEmpty(request.Username) && string.IsNullOrEmpty(request.Email))
|
||||
return BadRequest(new { message = "Username or email is required." });
|
||||
|
||||
User? user = null;
|
||||
if (!string.IsNullOrEmpty(request.Username))
|
||||
user = await _userService.GetByUsername(request.Username);
|
||||
if (user == null && !string.IsNullOrEmpty(request.Email))
|
||||
user = await _userService.GetByEmail(request.Email);
|
||||
|
||||
if (user == null) return BadRequest(new { message = "Invalid username/email or password." });
|
||||
if (!Bcrypt.Verify(request.Password, user.PasswordHash))
|
||||
return BadRequest(new { message = "Invalid username/email or password." });
|
||||
var token = _authService.GenerateJWTToken(user);
|
||||
return Ok(new { token });
|
||||
}
|
||||
[HttpPost]
|
||||
[Route("refresh")]
|
||||
[EnableRateLimiting("Sliding")]
|
||||
public async Task<IActionResult> RefreshToken([FromBody] RefreshTokenRequest request)
|
||||
{
|
||||
try
|
||||
{
|
||||
var jwtResult = await _authService.RefreshJWTToken(request.Token, request.RefreshToken);
|
||||
return Ok(new { token = jwtResult });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(new { message = ex.Message });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SharpbinV3.DTOs;
|
||||
using SharpbinV3.Services;
|
||||
|
||||
namespace SharpbinV3.Controllers
|
||||
@@ -10,21 +10,7 @@ namespace SharpbinV3.Controllers
|
||||
{
|
||||
private readonly IUserService _userService = userService;
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Create([FromBody] CreateUserDto dto)
|
||||
{
|
||||
if (await _userService.GetByUsername(dto.Username) != null)
|
||||
return Conflict("Username already exists.");
|
||||
|
||||
if (!string.IsNullOrEmpty(dto.Email) && await _userService.GetByEmail(dto.Email) != null)
|
||||
return Conflict("Email already exists.");
|
||||
|
||||
var user = await _userService.CreateUser(dto.Username, dto.Password, dto.Email, dto.DisplayName);
|
||||
|
||||
return CreatedAtAction(nameof(GetByUid), new { uid = user.UID }, new { user.UID, user.Username, user.UUID });
|
||||
}
|
||||
|
||||
[HttpGet("{uid:int}")]
|
||||
[HttpGet("{uid}")]
|
||||
public async Task<IActionResult> GetByUid(int uid)
|
||||
{
|
||||
var user = await _userService.GetByUID(uid);
|
||||
@@ -32,15 +18,16 @@ namespace SharpbinV3.Controllers
|
||||
|
||||
return Ok(new { user.UID, user.Username, user.UUID, user.DisplayName });
|
||||
}
|
||||
[HttpGet("username/{username:string}")]
|
||||
[HttpGet("username/{username}")]
|
||||
public async Task<IActionResult> GetByUsername(string username)
|
||||
{
|
||||
var user = await _userService.GetByUsername(username);
|
||||
if (user == null) return NotFound();
|
||||
return Ok(new { user.UID, user.Username, user.UUID, user.DisplayName });
|
||||
}
|
||||
[HttpGet("uuid/{uuid:guid}")]
|
||||
public async Task<IActionResult> GetByUuid(Guid uuid)
|
||||
[HttpGet("uuid/{uuid}")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetByUUID(Guid uuid)
|
||||
{
|
||||
var user = await _userService.GetByUUID(uuid);
|
||||
if (user == null) return NotFound();
|
||||
|
||||
Reference in New Issue
Block a user