From 79276573bce2df742d393e16f0b6ffe6530dd09d Mon Sep 17 00:00:00 2001 From: whoswhip Date: Fri, 23 Aug 2024 12:26:41 -0400 Subject: [PATCH] Signup api, signup and login frontend --- PasteBin/Program.cs | 170 +++++++++++++++++++++- PasteBin/assets/error.html | 1 + PasteBin/assets/index.html | 1 + PasteBin/assets/login.html | 195 +++++++++++++++++++++++++ PasteBin/assets/paste.html | 1 + PasteBin/assets/recent_pastes.html | 1 + PasteBin/assets/signup.html | 221 +++++++++++++++++++++++++++++ 7 files changed, 588 insertions(+), 2 deletions(-) create mode 100644 PasteBin/assets/login.html create mode 100644 PasteBin/assets/signup.html diff --git a/PasteBin/Program.cs b/PasteBin/Program.cs index 70739d4..0399ee7 100644 --- a/PasteBin/Program.cs +++ b/PasteBin/Program.cs @@ -65,6 +65,41 @@ class Program app.MapGet("/", async (HttpContext context) => { var html = File.ReadAllText("assets\\index.html"); + bool loggedIn = await IsLoggedInAsync(context.Request.Cookies["token"]); + if (loggedIn) + { + html = html.Replace("{html}", "Dashboard"); + } + else + { + html = html.Replace("{html}", "Login Sign up "); + } + context.Response.ContentType = "text/html"; + await context.Response.WriteAsync(html); + return; + }); + app.MapGet("/login", async (HttpContext context) => + { + if (await IsLoggedInAsync(context.Request.Cookies["token"])) + { + context.Response.Redirect("/"); + return; + } + var html = File.ReadAllText("assets\\login.html"); + html = html.Replace("{html}", "Sign up"); + context.Response.ContentType = "text/html"; + await context.Response.WriteAsync(html); + return; + }); + app.MapGet("/sign-up", async (HttpContext context) => + { + if (await IsLoggedInAsync(context.Request.Cookies["token"])) + { + context.Response.Redirect("/"); + return; + } + var html = File.ReadAllText("assets\\signup.html"); + html = html.Replace("{html}", "Login"); context.Response.ContentType = "text/html"; await context.Response.WriteAsync(html); return; @@ -75,6 +110,15 @@ class Program var error_html = File.ReadAllText("assets\\error.html"); error_html = error_html.Replace("{code}", "404"); error_html = error_html.Replace("{message}", "Paste not found"); + bool loggedIn = await IsLoggedInAsync(context.Request.Cookies["token"]); + if (loggedIn) + { + error_html = error_html.Replace("{html}", "Dashboard"); + } + else + { + error_html = error_html.Replace("{html}", "Login Sign up"); + } context.Response.ContentType = "text/html"; await context.Response.WriteAsync(error_html); return; @@ -129,6 +173,15 @@ class Program html = html.Replace("{pastetitle}", paste.Title); html = html.Replace("{content}", content); html = html.Replace("{pasteid}", paste.Id); + bool loggedIn = await IsLoggedInAsync(context.Request.Cookies["token"]); + if (loggedIn) + { + html = html.Replace("{html}", "Dashboard"); + } + else + { + html = html.Replace("{html}", "Login Sign up"); + } context.Response.ContentType = "text/html"; await context.Response.WriteAsync(html); return; @@ -150,6 +203,15 @@ class Program app.MapGet("/pastes", async (HttpContext context) => { var html = File.ReadAllText("assets\\recent_pastes.html"); + bool loggedIn = await IsLoggedInAsync(context.Request.Cookies["token"]); + if (loggedIn) + { + html = html.Replace("{html}", "Dashboard"); + } + else + { + html = html.Replace("{html}", "Login Sign up"); + } using (var connection = new SqliteConnection("Data Source=pastes.sqlite")) { await connection.OpenAsync(); @@ -200,6 +262,16 @@ class Program var html = File.ReadAllText("assets\\archives.html"); var page = context.Request?.Query["page"].ToString(); + bool loggedIn = await IsLoggedInAsync(context.Request.Cookies["token"]); + if (loggedIn) + { + html = html.Replace("{html}", "Dashboard"); + } + else + { + html = html.Replace("{html}", "Login Sign up"); + } + int pageNumber; if (!int.TryParse(page, out pageNumber) || pageNumber <= 0) { @@ -346,7 +418,6 @@ class Program app.MapPost("/api/accounts/create", async (HttpContext context) => { string requestBody = await new StreamReader(context.Request.Body).ReadToEndAsync(); - string ip = context.Request.Headers["X-Forwarded-For"].ToString() ?? context.Connection.RemoteIpAddress.ToString(); Log log = await Logging.LogRequestAsync(context); char[] disallowed = { ' ', '\'', '\"', '\\', '/', '(', ')', '[', ']', '{', '}', '<', '>', ';', ':', ',', '.', '!', '?', '@', '#', '$', '%', '^', '&', '*', '-', '+', '=', '~', '`' }; @@ -428,7 +499,7 @@ class Program loginCommand.CommandText = "INSERT INTO logins (UUID,UserAgent, IP, LoginTime,ExpireTime, Token) VALUES (@UUID,@UserAgent, @IP, @LoginTime,@ExpireTime, @Token)"; loginCommand.Parameters.AddWithValue("@UserAgent", context.Request.Headers["User-Agent"].ToString()); loginCommand.Parameters.AddWithValue("@UUID", insertCommand.Parameters["@UUID"].Value); - loginCommand.Parameters.AddWithValue("@IP", ip); + loginCommand.Parameters.AddWithValue("@IP", log.IP); loginCommand.Parameters.AddWithValue("@LoginTime", DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss")); loginCommand.Parameters.AddWithValue("@ExpireTime", expirationDate.ToString("dd/MM/yyyy-HH:mm:ss")); loginCommand.Parameters.AddWithValue("@Token", token); @@ -451,6 +522,101 @@ class Program }).RequireRateLimiting("fixed"); + app.MapPost("/api/accounts/login", async (HttpContext Context) => + { + string requestBody = await new StreamReader(Context.Request.Body).ReadToEndAsync(); + Log log = await Logging.LogRequestAsync(Context); + + if (IsLoggedInAsync(Context.Request.Cookies["token"]).Result) + { + Context.Response.StatusCode = 400; + Context.Response.ContentType = "application/json"; + await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Already logged in" }.ToString()); + return; + } + if (string.IsNullOrEmpty(requestBody)) + { + Context.Response.StatusCode = 400; + Context.Response.ContentType = "application/json"; + await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "No data uploaded" }.ToString()); + return; + } + if (!requestBody.Contains("username") || !requestBody.Contains("password") || Context.Request.ContentType != "application/json") + { + Context.Response.StatusCode = 400; + Context.Response.ContentType = "application/json"; + await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid request body" }.ToString()); + return; + } + + var data = JObject.Parse(requestBody); + var username = SanitizeInput(data["username"]?.ToString() ?? ""); + var password = SanitizeInput(data["password"]?.ToString() ?? ""); + + if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) + { + Context.Response.StatusCode = 400; + Context.Response.ContentType = "application/json"; + await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid username or password" }.ToString()); + return; + } + + string passwordHash = BCrypt.Net.BCrypt.HashPassword(password, salt); + + using (var connection = new SqliteConnection("Data Source=pastes.sqlite")) + { + await connection.OpenAsync(); + var command = connection.CreateCommand(); + command.CommandText = "SELECT * FROM users WHERE Username = @Username"; + command.Parameters.AddWithValue("@Username", username); + var reader = await command.ExecuteReaderAsync(); + await reader.ReadAsync(); + if (!reader.HasRows) + { + Context.Response.StatusCode = 400; + Context.Response.ContentType = "application/json"; + await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid username or password" }.ToString()); + return; + } + if (BCrypt.Net.BCrypt.Verify(password, reader.GetString(3))) + { + string token = Convert.ToBase64String(Encoding.UTF8.GetBytes(BCrypt.Net.BCrypt.HashPassword(Guid.NewGuid().ToString()))); + DateTime expirationDate = DateTime.Now.AddYears(1); + + var loginCommand = connection.CreateCommand(); + loginCommand.CommandText = "INSERT INTO logins (UUID,UserAgent, IP, LoginTime,ExpireTime, Token) VALUES (@UUID,@UserAgent, @IP, @LoginTime,@ExpireTime, @Token)"; + loginCommand.Parameters.AddWithValue("@UserAgent", log.UserAgent); + loginCommand.Parameters.AddWithValue("@UUID", reader.GetString(1)); + loginCommand.Parameters.AddWithValue("@IP", log.IP); + loginCommand.Parameters.AddWithValue("@LoginTime", DateTime.Now.ToString("dd/MM/yyyy-HH:mm:ss")); + loginCommand.Parameters.AddWithValue("@ExpireTime", expirationDate.ToString("dd/MM/yyyy-HH:mm:ss")); + loginCommand.Parameters.AddWithValue("@Token", token); + await loginCommand.ExecuteNonQueryAsync(); + + var cookie = new CookieOptions + { + Expires = expirationDate, + Secure = true, + HttpOnly = true, + SameSite = SameSiteMode.Strict + }; + + Context.Response.Cookies.Append("token", token, cookie); + Context.Response.StatusCode = 200; + Context.Response.ContentType = "application/json"; + Console.WriteLine($"[{DateTime.Now}] Account {username} has logged in"); + File.AppendAllText("logs.log", $"[{DateTime.Now}] Account {username} has logged in from {log.IP}\n"); + await Context.Response.WriteAsJsonAsync(new JObject { ["message"] = "Logged in" }.ToString()); + return; + } + else + { + Context.Response.StatusCode = 400; + Context.Response.ContentType = "application/json"; + await Context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Invalid username or password" }.ToString()); + } + } + }); #endregion diff --git a/PasteBin/assets/error.html b/PasteBin/assets/error.html index b97a7db..a0026c5 100644 --- a/PasteBin/assets/error.html +++ b/PasteBin/assets/error.html @@ -11,6 +11,7 @@ home recent pastes archive + {html}
diff --git a/PasteBin/assets/index.html b/PasteBin/assets/index.html index e2c622c..fc7a9ec 100644 --- a/PasteBin/assets/index.html +++ b/PasteBin/assets/index.html @@ -11,6 +11,7 @@ home recent pastes archive + {html}
diff --git a/PasteBin/assets/login.html b/PasteBin/assets/login.html new file mode 100644 index 0000000..47af732 --- /dev/null +++ b/PasteBin/assets/login.html @@ -0,0 +1,195 @@ + + + + + + + create paste + + + +
+
+

login

+
+ + + + + +
+
+
+ + + + \ No newline at end of file diff --git a/PasteBin/assets/paste.html b/PasteBin/assets/paste.html index d001283..30f9d22 100644 --- a/PasteBin/assets/paste.html +++ b/PasteBin/assets/paste.html @@ -11,6 +11,7 @@ home recent pastes archive + {html}
diff --git a/PasteBin/assets/recent_pastes.html b/PasteBin/assets/recent_pastes.html index 1e16ff6..169535a 100644 --- a/PasteBin/assets/recent_pastes.html +++ b/PasteBin/assets/recent_pastes.html @@ -11,6 +11,7 @@ home recent pastes archive + {html}
diff --git a/PasteBin/assets/signup.html b/PasteBin/assets/signup.html new file mode 100644 index 0000000..ef2b3ba --- /dev/null +++ b/PasteBin/assets/signup.html @@ -0,0 +1,221 @@ + + + + + + + create paste + + + +
+
+

sign-up

+
+ + + + + + + +
+
+
+ + + + \ No newline at end of file