diff --git a/SharpbinV2.Server/Database.cs b/SharpbinV2.Server/Database.cs index 8ec1c4d..36532fe 100644 --- a/SharpbinV2.Server/Database.cs +++ b/SharpbinV2.Server/Database.cs @@ -1,4 +1,6 @@ -using Microsoft.Data.Sqlite; +using BCrypt.Net; +using Microsoft.Data.Sqlite; +using Bcrypt = BCrypt.Net.BCrypt; namespace SharpbinV2.Server { @@ -191,7 +193,7 @@ namespace SharpbinV2.Server using (var command = connection.CreateCommand()) { command.CommandText = "SELECT * FROM views WHERE Ip = @Ip AND UserAgent = @UserAgent;"; - command.Parameters.AddWithValue("@Ip", Program.HMAC256HASH(details.Ip)); + command.Parameters.AddWithValue("@Ip", Bcrypt.HashPassword(details.Ip, Bcrypt.GenerateSalt(8))); command.Parameters.AddWithValue("@UserAgent", details.UserAgent); using (var reader = await command.ExecuteReaderAsync()) { @@ -229,7 +231,7 @@ namespace SharpbinV2.Server command.CommandText = "INSERT INTO views (UserUUID, PasteUUID, Ip, UserAgent, Created) VALUES (@UserUUID, @PasteUUID, @Ip, @UserAgent, @Created);"; command.Parameters.AddWithValue("@UserUUID", _user.UUID); command.Parameters.AddWithValue("@PasteUUID", paste.UUID); - command.Parameters.AddWithValue("@Ip", Program.HMAC256HASH(details.Ip)); + command.Parameters.AddWithValue("@Ip", Bcrypt.HashPassword(details.Ip, Bcrypt.GenerateSalt(8))); command.Parameters.AddWithValue("@UserAgent", details.UserAgent); command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds()); await command.ExecuteNonQueryAsync(); diff --git a/SharpbinV2.Server/Models/PasswordReset.cs b/SharpbinV2.Server/Models/PasswordReset.cs new file mode 100644 index 0000000..3995572 --- /dev/null +++ b/SharpbinV2.Server/Models/PasswordReset.cs @@ -0,0 +1,13 @@ +namespace SharpbinV2.Server.Models +{ + public class PasswordReset + { + public string? UID { get; set; } + public string? UUID { get; set; } + public string? USERUUID { get; set; } + public string? URL { get; set; } + public string? Token { get; set; } + public long? Created { get; set; } + public long? Expirary { get; set; } + } +} diff --git a/SharpbinV2.Server/Models/Paste.cs b/SharpbinV2.Server/Models/Paste.cs new file mode 100644 index 0000000..c7c3d35 --- /dev/null +++ b/SharpbinV2.Server/Models/Paste.cs @@ -0,0 +1,18 @@ +namespace SharpbinV2.Server.Models +{ + public class Paste + { + public string? UUID { get; set; } + public string? ID { get; set; } + public int? Visibility { get; set; } + public string? Title { get; set; } + public string? AuthorUUID { get; set; } + public string? FilePath { get; set; } + public long? Created { get; set; } + public long? Edited { get; set; } + public int? Size { get; set; } + public int? TrueSize { get; set; } + public int? Views { get; set; } + public string? Syntax { get; set; } + } +} diff --git a/SharpbinV2.Server/Models/RequestDetails.cs b/SharpbinV2.Server/Models/RequestDetails.cs new file mode 100644 index 0000000..61df494 --- /dev/null +++ b/SharpbinV2.Server/Models/RequestDetails.cs @@ -0,0 +1,9 @@ +namespace SharpbinV2.Server.Models +{ + public class RequestDetails + { + public string? Ip { get; set; } + public string? UserAgent { get; set; } + public string? Token { get; set; } + } +} diff --git a/SharpbinV2.Server/Models/Session.cs b/SharpbinV2.Server/Models/Session.cs new file mode 100644 index 0000000..94b3107 --- /dev/null +++ b/SharpbinV2.Server/Models/Session.cs @@ -0,0 +1,13 @@ +namespace SharpbinV2.Server.Models +{ + public class Session + { + public string? UUID { get; set; } + public string? UserUUID { get; set; } + public string? Token { get; set; } + public long? Created { get; set; } + public long? Expirary { get; set; } + public string? Ip { get; set; } + public string? UserAgent { get; set; } + } +} diff --git a/SharpbinV2.Server/Models/User.cs b/SharpbinV2.Server/Models/User.cs new file mode 100644 index 0000000..8adf746 --- /dev/null +++ b/SharpbinV2.Server/Models/User.cs @@ -0,0 +1,15 @@ +namespace SharpbinV2.Server.Models +{ + public class User + { + public int? UID { get; set; } + public string? UUID { get; set; } + public int? Type { get; set; } + public string? Email { get; set; } + public string? Username { get; set; } + public string? DisplayName { get; set; } + public string? Password { get; set; } + public long? Created { get; set; } + public long? LastLogin { get; set; } + } +} diff --git a/SharpbinV2.Server/Models/View.cs b/SharpbinV2.Server/Models/View.cs new file mode 100644 index 0000000..fbb41bd --- /dev/null +++ b/SharpbinV2.Server/Models/View.cs @@ -0,0 +1,12 @@ +namespace SharpbinV2.Server.Models +{ + public class View + { + public string? UUID { get; set; } + public string? PasteUUID { get; set; } + public string? UserUUID { get; set; } + public string? IP { get; set; } + public string? UserAgent { get; set; } + public long? Created { get; set; } + } +} diff --git a/SharpbinV2.Server/Program.cs b/SharpbinV2.Server/Program.cs index 4295810..874a00e 100644 --- a/SharpbinV2.Server/Program.cs +++ b/SharpbinV2.Server/Program.cs @@ -39,7 +39,6 @@ namespace SharpbinV2.Server "toml", "xml" }; - public static string HMACSecret = "thisneedstobechanged"; public static JObject Configuration { get; set; } static async Task Main(string[] args) @@ -57,16 +56,6 @@ namespace SharpbinV2.Server builder.WebHost.UseUrls($"http://*:{Environment.GetEnvironmentVariable("ASPNETCORE_HTTP_PORTS") ?? "5810"}"); } MaxFileSize = Environment.GetEnvironmentVariable("MAX_FILE_SIZE") != null ? Convert.ToInt64(Environment.GetEnvironmentVariable("MAX_FILE_SIZE")) : MaxFileSize; - HMACSecret = Environment.GetEnvironmentVariable("HMAC_SECRET") ?? HMACSecret; - if (HMACSecret == "thisneedstobechanged") - logger.LogWarning("HMAC_SECRET is still set to default. Please change this in your .env file."); - if (HMACSecret.Length < 32 && HMACSecret != "thisneedstobechanged") - logger.LogWarning("HMAC_SECRET is less than 32 characters. Please change this in your .env file."); - if (HMACSecret.Length < 6 || HMACSecret.Length <= 0) - { - logger.LogError("HMAC_SECRET is less than 6 characters. Please change this in your .env file."); - Environment.Exit(1); - } builder.WebHost.ConfigureKestrel(options => { @@ -392,7 +381,7 @@ namespace SharpbinV2.Server command.Parameters.AddWithValue("@Token", token); command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds()); command.Parameters.AddWithValue("@Expirary", DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()); - command.Parameters.AddWithValue("@Ip", HMAC256HASH(requestdetails.Ip)); + command.Parameters.AddWithValue("@Ip", Bcrypt.HashPassword(requestdetails.Ip, Bcrypt.GenerateSalt(8))); command.Parameters.AddWithValue("@UserAgent", requestdetails.UserAgent); await command.ExecuteNonQueryAsync(); } @@ -483,7 +472,7 @@ namespace SharpbinV2.Server command.Parameters.AddWithValue("@Token", token); command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds()); command.Parameters.AddWithValue("@Expirary", DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()); - command.Parameters.AddWithValue("@Ip", HMAC256HASH(requestdetails.Ip)); + command.Parameters.AddWithValue("@Ip", Bcrypt.HashPassword(requestdetails.Ip, Bcrypt.GenerateSalt(8))); command.Parameters.AddWithValue("@UserAgent", requestdetails.UserAgent); await command.ExecuteNonQueryAsync(); } @@ -1437,14 +1426,6 @@ namespace SharpbinV2.Server } return $"{len:0.##} {sizes[order]}"; } - public static string HMAC256HASH(string data) - { - using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(HMACSecret))) - { - byte[] hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(data)); - return Convert.ToBase64String(hashBytes); - } - } #endregion static async Task Initialize() { diff --git a/SharpbinV2.Server/SharpbinV2.Server.csproj b/SharpbinV2.Server/SharpbinV2.Server.csproj index 805b94f..60d335a 100644 --- a/SharpbinV2.Server/SharpbinV2.Server.csproj +++ b/SharpbinV2.Server/SharpbinV2.Server.csproj @@ -19,6 +19,7 @@ +