rtmpdash/Pages/Login.cshtml.cs

80 lines
2.4 KiB
C#
Raw Normal View History

2021-01-24 04:04:16 +01:00
using System;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.RazorPages;
2022-12-15 22:36:15 +01:00
using RTMPDash.Backend.Database;
2021-01-24 04:04:16 +01:00
2022-03-03 01:59:12 +01:00
namespace RTMPDash.Pages;
2021-01-24 04:04:16 +01:00
2022-02-09 22:32:17 +01:00
public class LoginModel : PageModel {
public void OnPost() {
if (!Request.HasFormContentType || string.IsNullOrWhiteSpace(Request.Form["user"]) || string.IsNullOrWhiteSpace(Request.Form["pass"]))
return;
2021-01-24 04:04:16 +01:00
2022-12-15 22:36:15 +01:00
using var db = new Database.DbConn();
2022-03-03 01:59:12 +01:00
var user = db.Users.FirstOrDefault(p => p.Username == Request.Form["user"].ToString());
2022-02-09 22:32:17 +01:00
if (user == null)
return;
2022-03-03 01:59:12 +01:00
if (user.Password != Request.Form["pass"].ToString().Sha256())
return;
2022-02-09 22:32:17 +01:00
HttpContext.Session.SetString("authenticatedUser", user.Username);
2021-01-24 04:04:16 +01:00
}
2022-02-09 22:32:17 +01:00
}
2021-01-24 04:04:16 +01:00
2022-02-09 22:32:17 +01:00
public static class StringExtensions {
public static string Sha256(this string rawData) {
2022-03-03 01:59:12 +01:00
// Create a SHA256
2022-02-09 22:32:17 +01:00
using var sha256Hash = SHA256.Create();
2021-01-24 04:04:16 +01:00
2022-03-03 01:59:12 +01:00
// ComputeHash - returns byte array
2022-02-09 22:32:17 +01:00
var bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
2021-01-24 04:04:16 +01:00
2022-03-03 01:59:12 +01:00
// Convert byte array to a string
2022-02-09 22:32:17 +01:00
var builder = new StringBuilder();
for (var i = 0; i < bytes.Length; i++)
builder.Append(bytes[i].ToString("x2"));
2021-01-24 04:04:16 +01:00
2022-02-09 22:32:17 +01:00
return builder.ToString();
}
2021-01-24 04:04:16 +01:00
2022-02-09 22:32:17 +01:00
public static string FirstCharToUpper(this string input) => input switch {
null => throw new ArgumentNullException(nameof(input)),
"" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
_ => input.First().ToString().ToUpper() + input.Substring(1)
};
2021-01-24 04:04:16 +01:00
2022-02-09 22:32:17 +01:00
public static string Base64Encode(this string plainText) {
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
return Convert.ToBase64String(plainTextBytes);
}
2021-01-24 04:04:16 +01:00
2022-02-09 22:32:17 +01:00
public static string UrlEncode(this string plainText) => HttpUtility.UrlEncode(plainText);
2021-01-24 04:04:16 +01:00
2022-02-09 22:32:17 +01:00
public static string Delimit(this string input, int max) => input.PadRight(max, ' ').Substring(0, max).TrimEnd();
2021-01-24 04:04:16 +01:00
2022-02-09 22:32:17 +01:00
public static string Bash(this string cmd) {
var escapedArgs = cmd.Replace("\"", "\\\"");
2021-01-24 04:04:16 +01:00
2022-02-09 22:32:17 +01:00
var process = new Process {
StartInfo = new ProcessStartInfo {
FileName = "/bin/bash",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
process.Start();
var result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
2021-01-24 04:04:16 +01:00
}
2022-03-03 01:59:12 +01:00
}