mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
style: format via csharpier
This commit is contained in:
@@ -19,8 +19,14 @@ namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AuthController(UserService userService, AuthService authService, IOptions<AuthSettings> options,
|
||||
VerificationService verification, TotpVerificationProvider totp, AppDbContext db) : ControllerBase
|
||||
public class AuthController(
|
||||
UserService userService,
|
||||
AuthService authService,
|
||||
IOptions<AuthSettings> options,
|
||||
VerificationService verification,
|
||||
TotpVerificationProvider totp,
|
||||
AppDbContext db
|
||||
) : ControllerBase
|
||||
{
|
||||
private readonly AuthService _authService = authService;
|
||||
private readonly UserService _userService = userService;
|
||||
@@ -37,11 +43,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
return BadRequest(new { success = false, message = "Registration is disabled." });
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
if (!await _verification.VerifyAsync(new VerificationContext
|
||||
{
|
||||
Token = request.Token,
|
||||
Ip = HttpContext.GetRequestIP()
|
||||
}))
|
||||
if (!await _verification.VerifyAsync(new VerificationContext { Token = request.Token, Ip = HttpContext.GetRequestIP() }))
|
||||
return BadRequest(new { success = false, message = "Verification failed." });
|
||||
|
||||
var existingUser = await _userService.GetByUsername(request.Username);
|
||||
@@ -57,21 +59,22 @@ namespace SharpbinV3.Server.Controllers
|
||||
return BadRequest(new { success = false, message = "Display name should not exceed 26 characters." });
|
||||
|
||||
var user = await _authService.CreateUser(request.Username, request.Password, request.Email, request.DisplayName);
|
||||
return Ok(new
|
||||
{
|
||||
message = "User registered successfully.",
|
||||
user = new UserResponseDto
|
||||
return Ok(
|
||||
new
|
||||
{
|
||||
UID = user.UID,
|
||||
UUID = user.UUID,
|
||||
Username = user.Username,
|
||||
DisplayName = user.DisplayName,
|
||||
Email = user.Email,
|
||||
Roles = user.Roles,
|
||||
Visibility = user.Visibility,
|
||||
LastLogin = user.LastLogin
|
||||
message = "User registered successfully.",
|
||||
user = new UserResponseDto
|
||||
{
|
||||
UID = user.UID,
|
||||
UUID = user.UUID,
|
||||
Username = user.Username,
|
||||
DisplayName = user.DisplayName,
|
||||
Email = user.Email,
|
||||
Roles = user.Roles,
|
||||
Visibility = user.Visibility,
|
||||
LastLogin = user.LastLogin,
|
||||
},
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -83,11 +86,7 @@ namespace SharpbinV3.Server.Controllers
|
||||
if (string.IsNullOrEmpty(request.Username) && string.IsNullOrEmpty(request.Email))
|
||||
return BadRequest(new { success = false, message = "Username or email is required." });
|
||||
|
||||
if (!await _verification.VerifyAsync(new VerificationContext
|
||||
{
|
||||
Token = request.Token,
|
||||
Ip = HttpContext.GetRequestIP()
|
||||
}))
|
||||
if (!await _verification.VerifyAsync(new VerificationContext { Token = request.Token, Ip = HttpContext.GetRequestIP() }))
|
||||
return BadRequest(new { success = false, message = "Verification failed." });
|
||||
|
||||
User? user = null;
|
||||
@@ -104,24 +103,25 @@ namespace SharpbinV3.Server.Controllers
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request.TotpCode))
|
||||
return BadRequest(new { success = false, message = "TOTP code is required." });
|
||||
var totpValid = await _totp.VerifyAsync(new VerificationContext
|
||||
{
|
||||
UserUUID = user.UUID,
|
||||
Code = request.TotpCode
|
||||
});
|
||||
|
||||
var totpValid = await _totp.VerifyAsync(new VerificationContext { UserUUID = user.UUID, Code = request.TotpCode });
|
||||
|
||||
if (!totpValid)
|
||||
return BadRequest(new { success = false, message = "Invalid TOTP code." });
|
||||
}
|
||||
var token = _authService.GenerateJWTToken(user);
|
||||
await _authService.UpdateLoginTime(user);
|
||||
var result = await token;
|
||||
return Ok(new LoginResponseDto
|
||||
{
|
||||
Success = true,
|
||||
Token = result.Token ?? string.Empty,
|
||||
RefreshToken = result.RefreshToken ?? string.Empty
|
||||
});
|
||||
return Ok(
|
||||
new LoginResponseDto
|
||||
{
|
||||
Success = true,
|
||||
Token = result.Token ?? string.Empty,
|
||||
RefreshToken = result.RefreshToken ?? string.Empty,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("refresh")]
|
||||
[EnableRateLimiting("Sliding")]
|
||||
@@ -130,16 +130,17 @@ namespace SharpbinV3.Server.Controllers
|
||||
try
|
||||
{
|
||||
var jwtResult = await _authService.RefreshJWTToken(request.Token, request.RefreshToken);
|
||||
return Ok(new TokenRefreshResponseDto
|
||||
{
|
||||
Success = jwtResult?.Success ?? false,
|
||||
Message = jwtResult?.Errors != null && jwtResult.Errors.Count > 0 ? string.Join("; ", jwtResult.Errors) : "Token refreshed successfully.",
|
||||
Token = jwtResult != null ? new TokenData
|
||||
return Ok(
|
||||
new TokenRefreshResponseDto
|
||||
{
|
||||
Token = jwtResult.Token,
|
||||
RefreshToken = jwtResult.RefreshToken
|
||||
} : null
|
||||
});
|
||||
Success = jwtResult?.Success ?? false,
|
||||
Message =
|
||||
jwtResult?.Errors != null && jwtResult.Errors.Count > 0
|
||||
? string.Join("; ", jwtResult.Errors)
|
||||
: "Token refreshed successfully.",
|
||||
Token = jwtResult != null ? new TokenData { Token = jwtResult.Token, RefreshToken = jwtResult.RefreshToken } : null,
|
||||
}
|
||||
);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -153,11 +154,13 @@ namespace SharpbinV3.Server.Controllers
|
||||
public IActionResult GetSiteInfo()
|
||||
{
|
||||
var authSettings = options.Value;
|
||||
return Ok(new
|
||||
{
|
||||
cf_turnstile_site_key = string.IsNullOrEmpty(authSettings.CF_Turnstile_SiteKey) ? null : authSettings.CF_Turnstile_SiteKey,
|
||||
registration_enabled = authSettings.Registration_Enabled
|
||||
});
|
||||
return Ok(
|
||||
new
|
||||
{
|
||||
cf_turnstile_site_key = string.IsNullOrEmpty(authSettings.CF_Turnstile_SiteKey) ? null : authSettings.CF_Turnstile_SiteKey,
|
||||
registration_enabled = authSettings.Registration_Enabled,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
@@ -166,13 +169,21 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> StartTotpEnrollment()
|
||||
{
|
||||
var user = await _authService.GetUserFromHttpContext(HttpContext);
|
||||
if (user is null) return BadRequest(new { success = false, message = "User not found." });
|
||||
if (user is null)
|
||||
return BadRequest(new { success = false, message = "User not found." });
|
||||
|
||||
var secret = _totp.GenerateSecret();
|
||||
var secretBase32 = _totp.ToBase32(secret);
|
||||
var account = string.IsNullOrWhiteSpace(user.Email) ? user.Username : user.Email!;
|
||||
var uri = _totp.BuildOtpAuthUri("SharpbinV3", account, secretBase32);
|
||||
return Ok(new { success = true, secret = secretBase32, otpauth = uri });
|
||||
return Ok(
|
||||
new
|
||||
{
|
||||
success = true,
|
||||
secret = secretBase32,
|
||||
otpauth = uri,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
@@ -215,17 +226,14 @@ namespace SharpbinV3.Server.Controllers
|
||||
public async Task<IActionResult> DisableTotp([FromBody] DisableTotpDto dto)
|
||||
{
|
||||
var user = await _authService.GetUserFromHttpContext(HttpContext);
|
||||
if (user == null) return BadRequest(new { success = false, message = "User not found." });
|
||||
if (user == null)
|
||||
return BadRequest(new { success = false, message = "User not found." });
|
||||
if (string.IsNullOrWhiteSpace(dto.Code))
|
||||
return BadRequest(new { success = false, message = "TOTP code is required." });
|
||||
var existing = await _db.UserTotps.FirstOrDefaultAsync(t => t.UserUUID == user.UUID);
|
||||
if (existing is null)
|
||||
return BadRequest(new { success = false, message = "TOTP is not enabled." });
|
||||
if (!_totp.VerifyAsync(new VerificationContext
|
||||
{
|
||||
UserUUID = user.UUID,
|
||||
Code = dto.Code
|
||||
}).Result)
|
||||
if (!_totp.VerifyAsync(new VerificationContext { UserUUID = user.UUID, Code = dto.Code }).Result)
|
||||
return BadRequest(new { success = false, message = "Invalid TOTP code." });
|
||||
|
||||
_db.UserTotps.Remove(existing);
|
||||
|
||||
Reference in New Issue
Block a user