Enhance user authentication and paste management

- Updated `AuthController.cs` to include token in `CreateSession` and improved validation for username and email.
- Refactored paste creation logic from `Program.cs` to a new `PasteController.cs`.
- Added email validation method in `HelperService.cs`.
- Introduced `CompressionService.cs` for handling string compression and decompression.
- Improved user experience with autocomplete and spellcheck in HTML forms.
- Enhanced date display in `archive.js` and refactored date formatting in `dash.js`.
- Updated API endpoints in `login.js` and `register.js` to reflect changes.
This commit is contained in:
whoswhip
2025-07-06 22:55:29 -04:00
parent cc0c6a8286
commit 3eb7374a07
16 changed files with 312 additions and 176 deletions
@@ -0,0 +1,74 @@
using System.IO.Compression;
using System.Text;
namespace SharpbinV2.Server.Services
{
public class CompressionService
{
public static async Task<byte[]> CompressString(string text)
{
byte[] compressedBytes;
using (var ms = new MemoryStream())
{
using (var cs = new GZipStream(ms, CompressionMode.Compress))
{
byte[] inputBytes = Encoding.UTF8.GetBytes(text);
await cs.WriteAsync(inputBytes, 0, inputBytes.Length);
}
compressedBytes = ms.ToArray();
}
return compressedBytes;
}
public static async Task<string> DecompressByteArrayToString(byte[] compressedBytes)
{
string decompressedText;
using (var ms = new MemoryStream(compressedBytes))
{
using (var cs = new GZipStream(ms, CompressionMode.Decompress))
{
using (var sr = new StreamReader(cs))
{
decompressedText = await sr.ReadToEndAsync();
}
}
}
return decompressedText;
}
public static async Task<byte[]> DecompressByteArray(byte[] compressedBytes)
{
byte[] decompressedBytes;
using (var ms = new MemoryStream(compressedBytes))
{
using (var cs = new GZipStream(ms, CompressionMode.Decompress))
{
using (var msDecompressed = new MemoryStream())
{
await cs.CopyToAsync(msDecompressed);
decompressedBytes = msDecompressed.ToArray();
}
}
}
return decompressedBytes;
}
public static async Task<bool> ShouldCompress(string text, int sizeThreshold = 512, double ratioThreshold = 0.8)
{
if (string.IsNullOrWhiteSpace(text))
return false;
byte[] inputBytes = Encoding.UTF8.GetBytes(text);
if (inputBytes.Length < sizeThreshold)
return false;
byte[] compressedBytes = await CompressString(text).ConfigureAwait(false);
double compressionRatio = (double)compressedBytes.Length / inputBytes.Length;
return compressionRatio < ratioThreshold;
}
public static bool IsCompressed(byte[] data)
{
return data.Length >= 2 && data[0] == 0x1F && data[1] == 0x8B;
}
}
}
+39 -5
View File
@@ -339,7 +339,7 @@ namespace SharpbinV2.Server.Services
}
}
}
public async Task<Session?> CreateSession(User user, RequestDetails details)
public async Task<Session?> CreateSession(User user, RequestDetails details, string token)
{
if (user == null || details == null)
return new Session { UUID = "0" };
@@ -347,7 +347,7 @@ namespace SharpbinV2.Server.Services
{
UUID = Guid.NewGuid().ToString(),
UserUUID = user.UUID,
Token = HelperService.GenerateToken(),
Token = token,
Created = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
Expirary = DateTimeOffset.UtcNow.AddDays(14).ToUnixTimeSeconds(),
Ip = Bcrypt.HashPassword(details.Ip, Bcrypt.GenerateSalt(8)),
@@ -447,14 +447,13 @@ namespace SharpbinV2.Server.Services
command.CommandText = "INSERT INTO users (UUID, Type, Email, Username, DisplayName, Password, Created, LastLogin) VALUES (@UUID, @Type, @Email, @Username, @DisplayName, @Password, @Created, @LastLogin);";
command.Parameters.AddWithValue("@UUID", user.UUID);
command.Parameters.AddWithValue("@Type", user.Type);
command.Parameters.AddWithValue("@Email", user.Email ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Email", string.IsNullOrWhiteSpace(user.Email) ? DBNull.Value : user.Email);
command.Parameters.AddWithValue("@Username", user.Username);
command.Parameters.AddWithValue("@DisplayName", user.DisplayName ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Password", user.Password);
command.Parameters.AddWithValue("@Created", user.Created);
command.Parameters.AddWithValue("@LastLogin", user.LastLogin);
var result = await command.ExecuteScalarAsync();
user.UID = result != null ? Convert.ToInt32(result) : 0;
await command.ExecuteNonQueryAsync();
}
}
return user;
@@ -495,5 +494,40 @@ namespace SharpbinV2.Server.Services
return true;
}
}
public async Task<bool> CreatePaste(Paste paste, ILogger logger)
{
try
{
if (paste == null || string.IsNullOrEmpty(paste.UUID) || string.IsNullOrEmpty(paste.ID) || paste.AuthorUUID == null)
return false;
using (var connection = new SqliteConnection(_connectionString))
{
await connection.OpenAsync();
using (var command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO pastes (UUID, ID, Visibility, Title, AuthorUUID, FilePath, Created, Edited, Size, TrueSize, Views, Syntax) VALUES (@UUID, @ID, @Visibility, @Title, @AuthorUUID, @FilePath, @Created, @Edited, @Size, @TrueSize, @Views, @Syntax);";
command.Parameters.AddWithValue("@UUID", paste.UUID);
command.Parameters.AddWithValue("@ID", paste.ID);
command.Parameters.AddWithValue("@Visibility", paste.Visibility ?? 0);
command.Parameters.AddWithValue("@Title", paste.Title ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@AuthorUUID", paste.AuthorUUID);
command.Parameters.AddWithValue("@FilePath", paste.FilePath ?? (object)DBNull.Value);
command.Parameters.AddWithValue("@Created", paste.Created ?? DateTimeOffset.UtcNow.ToUnixTimeSeconds());
command.Parameters.AddWithValue("@Edited", paste.Edited ?? DateTimeOffset.UtcNow.ToUnixTimeSeconds());
command.Parameters.AddWithValue("@Size", paste.Size ?? 0);
command.Parameters.AddWithValue("@TrueSize", paste.TrueSize ?? 0);
command.Parameters.AddWithValue("@Views", paste.Views ?? 0);
command.Parameters.AddWithValue("@Syntax", paste.Syntax ?? (object)DBNull.Value);
await command.ExecuteNonQueryAsync();
return true;
}
}
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to create paste in database.");
return false;
}
}
}
}
@@ -1,6 +1,7 @@
using Newtonsoft.Json.Linq;
using SharpbinV2.Server.Models;
using System.Text;
using System.Text.RegularExpressions;
using Bcrypt = BCrypt.Net.BCrypt;
namespace SharpbinV2.Server.Services
@@ -81,5 +82,10 @@ namespace SharpbinV2.Server.Services
}
return $"{len:0.##} {sizes[order]}";
}
public static bool IsValidEmail(string email)
{
return Regex.IsMatch(email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$");
}
}
}