mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
Prepare for switch to controllers & remove hmac hashing, switch to bcrypt instead.
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
using Microsoft.Data.Sqlite;
|
using BCrypt.Net;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Bcrypt = BCrypt.Net.BCrypt;
|
||||||
|
|
||||||
namespace SharpbinV2.Server
|
namespace SharpbinV2.Server
|
||||||
{
|
{
|
||||||
@@ -191,7 +193,7 @@ namespace SharpbinV2.Server
|
|||||||
using (var command = connection.CreateCommand())
|
using (var command = connection.CreateCommand())
|
||||||
{
|
{
|
||||||
command.CommandText = "SELECT * FROM views WHERE Ip = @Ip AND UserAgent = @UserAgent;";
|
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);
|
command.Parameters.AddWithValue("@UserAgent", details.UserAgent);
|
||||||
using (var reader = await command.ExecuteReaderAsync())
|
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.CommandText = "INSERT INTO views (UserUUID, PasteUUID, Ip, UserAgent, Created) VALUES (@UserUUID, @PasteUUID, @Ip, @UserAgent, @Created);";
|
||||||
command.Parameters.AddWithValue("@UserUUID", _user.UUID);
|
command.Parameters.AddWithValue("@UserUUID", _user.UUID);
|
||||||
command.Parameters.AddWithValue("@PasteUUID", paste.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("@UserAgent", details.UserAgent);
|
||||||
command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,7 +39,6 @@ namespace SharpbinV2.Server
|
|||||||
"toml",
|
"toml",
|
||||||
"xml"
|
"xml"
|
||||||
};
|
};
|
||||||
public static string HMACSecret = "thisneedstobechanged";
|
|
||||||
public static JObject Configuration { get; set; }
|
public static JObject Configuration { get; set; }
|
||||||
|
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
@@ -57,16 +56,6 @@ namespace SharpbinV2.Server
|
|||||||
builder.WebHost.UseUrls($"http://*:{Environment.GetEnvironmentVariable("ASPNETCORE_HTTP_PORTS") ?? "5810"}");
|
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;
|
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 =>
|
builder.WebHost.ConfigureKestrel(options =>
|
||||||
{
|
{
|
||||||
@@ -392,7 +381,7 @@ namespace SharpbinV2.Server
|
|||||||
command.Parameters.AddWithValue("@Token", token);
|
command.Parameters.AddWithValue("@Token", token);
|
||||||
command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
||||||
command.Parameters.AddWithValue("@Expirary", DateTimeOffset.UtcNow.AddDays(7).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);
|
command.Parameters.AddWithValue("@UserAgent", requestdetails.UserAgent);
|
||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
}
|
}
|
||||||
@@ -483,7 +472,7 @@ namespace SharpbinV2.Server
|
|||||||
command.Parameters.AddWithValue("@Token", token);
|
command.Parameters.AddWithValue("@Token", token);
|
||||||
command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
command.Parameters.AddWithValue("@Created", DateTimeOffset.UtcNow.ToUnixTimeSeconds());
|
||||||
command.Parameters.AddWithValue("@Expirary", DateTimeOffset.UtcNow.AddDays(7).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);
|
command.Parameters.AddWithValue("@UserAgent", requestdetails.UserAgent);
|
||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
}
|
}
|
||||||
@@ -1437,14 +1426,6 @@ namespace SharpbinV2.Server
|
|||||||
}
|
}
|
||||||
return $"{len:0.##} {sizes[order]}";
|
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
|
#endregion
|
||||||
static async Task Initialize()
|
static async Task Initialize()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Folder Include="Controllers\" />
|
||||||
<Folder Include="wwwroot\" />
|
<Folder Include="wwwroot\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user