Started to try and organize all the authentication code.

This commit is contained in:
2021-01-03 22:09:36 -06:00
parent ddb42da7bb
commit e03610e390
16 changed files with 174 additions and 91 deletions
+26 -4
View File
@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SecureCore.Services;
using SecureCore.Authentication;
namespace SecureCore.Controllers
{
@@ -17,9 +18,12 @@ namespace SecureCore.Controllers
[AcceptVerbs("POST")]
public IActionResult Login(LoginInfo info)
{
//Very the user has login data.
if (!UserDataService.UserHasLoginData(info.UserName)) return Unauthorized("User doesn't have login creds");
var (password, saltHash) = UserDataService.GetUserPasswordHash(info.UserName);
if (Authentication.PasswordIsValid(info.Password, saltHash, password))
if (PasswordManager.PasswordIsValid(info.Password, saltHash, password))
{
if (HttpContext.Request.Cookies.ContainsKey("Session"))
{
@@ -27,7 +31,7 @@ namespace SecureCore.Controllers
return Ok($"Session is live{Environment.NewLine}");
}
var sessionToken = Authentication.CreateSessionToken();
var sessionToken = SessionManager.CreateSessionToken();
UserDataService.SetUserSessionToken(UserDataService.GetUserId(info.UserName), sessionToken, DateTime.Now.AddDays(7));
@@ -45,8 +49,8 @@ namespace SecureCore.Controllers
[AcceptVerbs("POST")]
public IActionResult Register(LoginInfo info)
{
var (hash, salt) = Authentication.HashPassword(info.Password);
var sessionToken = Authentication.CreateSessionToken();
var (hash, salt) = PasswordManager.HashPassword(info.Password);
var sessionToken = SessionManager.CreateSessionToken();
try
{
@@ -62,6 +66,17 @@ namespace SecureCore.Controllers
}
}
[HttpPost("Logout")]
[AcceptVerbs("POST")]
public IActionResult Logout(LoginInfo info)
{
UserDataService.DestorySession(HttpContext.Request.Cookies["Session"]);
HttpContext.Response.Cookies.Delete("Session");
return Ok();
}
private CookieOptions GetCookieOptions()
{
return new CookieOptions
@@ -76,6 +91,13 @@ namespace SecureCore.Controllers
}
}
public class LoginInfo
{
public string UserName { get; set; }
public string Password { get; set; }
public string Email { get; set; }
}
//TODO: Read more https://www.valentinog.com/blog/cookies/
// And this https://blog.webf.zone/ultimate-guide-to-http-cookies-2aa3e083dbae
}