diff --git a/.vs/SecureCore/v16/.suo b/.vs/SecureCore/v16/.suo index 4b7545f..cd91533 100644 Binary files a/.vs/SecureCore/v16/.suo and b/.vs/SecureCore/v16/.suo differ diff --git a/SecureCore/Authentication/SessionManager.cs b/SecureCore/Authentication/SessionManager.cs index 658b4d7..5495804 100644 --- a/SecureCore/Authentication/SessionManager.cs +++ b/SecureCore/Authentication/SessionManager.cs @@ -1,12 +1,14 @@ using System; using System.Data; using System.Data.SqlClient; +using Microsoft.AspNetCore.Http; namespace SecureCore.Authentication { public static class SessionManager { private static int SessionKeySize { get; } = 64; //n bytes + public static string SessionCookieName { get; } = "Session"; public static string CreateSessionToken() { @@ -32,6 +34,15 @@ namespace SecureCore.Authentication return Convert.ToBoolean(result); } + public static bool IsSessionTokenValid(HttpContext context) + { + AppSettingsManager.TryGetConnectionStringByName("MainDataConnectionString", out string connectionString); + + if (!context.Request.Cookies.ContainsKey(SessionCookieName)) return false; + + return IsSessionTokenValid(context.Request.Cookies[SessionCookieName], connectionString); + } + public static void Logout(string sessionToken, string connectionString) { using var connection = new SqlConnection(connectionString); diff --git a/SecureCore/Controllers/AuthController.cs b/SecureCore/Controllers/AuthController.cs index 13b4dcc..3b65183 100644 --- a/SecureCore/Controllers/AuthController.cs +++ b/SecureCore/Controllers/AuthController.cs @@ -20,9 +20,8 @@ namespace SecureCore.Controllers try { - if (HttpContext.Request.Cookies.ContainsKey("Session")) - if (SessionManager.IsSessionTokenValid(HttpContext.Request.Cookies["Session"], connectionString)) - return Ok("Logged in\n"); + if (SessionManager.IsSessionTokenValid(HttpContext)) + return Ok("Logged in\n"); //Verify that the username provided is valid, i.e. no whitespace, special characters, etc. var result = UserDataService.IsUsernameValid(info.Username); @@ -47,7 +46,7 @@ namespace SecureCore.Controllers SessionManager.Login(info.Username, sessionToken, cookieOptions.Expires.Value.UtcDateTime, agent, ip, connectionString); - HttpContext.Response.Cookies.Append("Session", sessionToken, cookieOptions); + HttpContext.Response.Cookies.Append(SessionManager.SessionCookieName, sessionToken, cookieOptions); return Ok($"Logged in success{Environment.NewLine}"); } @@ -89,7 +88,7 @@ namespace SecureCore.Controllers var i = UserDataService.RegisterNewUser(info.Username, info.Email, hash, salt, sessionToken, cookieOptions.Expires.Value.UtcDateTime, agent, ip, connectionString); - HttpContext.Response.Cookies.Append("Session", sessionToken, cookieOptions); + HttpContext.Response.Cookies.Append(SessionManager.SessionCookieName, sessionToken, cookieOptions); return Ok($"New User ID: {i}"); } @@ -109,14 +108,14 @@ namespace SecureCore.Controllers try { - SessionManager.Logout(HttpContext.Request.Cookies["Session"], connectionString); + SessionManager.Logout(HttpContext.Request.Cookies[SessionManager.SessionCookieName], connectionString); } catch(Exception e) { //TODO: decide what to do here. If the SQL fails to clear the token from the database, do we want to clear the token cookie? } - HttpContext.Response.Cookies.Delete("Session"); + HttpContext.Response.Cookies.Delete(SessionManager.SessionCookieName); return Ok(); } diff --git a/SecureCore/Controllers/EmployeeController.cs b/SecureCore/Controllers/EmployeeController.cs index 8759034..8ecec09 100644 --- a/SecureCore/Controllers/EmployeeController.cs +++ b/SecureCore/Controllers/EmployeeController.cs @@ -24,19 +24,19 @@ namespace SecureCore.Controllers [HttpGet] public IActionResult Get() { - //if (Authentication.IsAllowed(HttpContext)) - return Ok(DataService.Get()); - //else - // return Unauthorized(); + if(SessionManager.IsSessionTokenValid(HttpContext)) + return Ok(DataService.Get()); + else + return Unauthorized(); } [HttpGet("{id}", Name = "Get")] public IActionResult Get(int id) { - //if (Authentication.IsAllowed(HttpContext)) - return Ok(DataService.GetById(id)); - //else - // return Unauthorized(); + if (SessionManager.IsSessionTokenValid(HttpContext)) + return Ok(DataService.GetById(id)); + else + return Unauthorized(); } }