From 1e204c948cd85f1f88e70d482412fb457d3106bc Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sat, 7 Sep 2024 04:19:51 +0300 Subject: [PATCH] refactor: set cookie name to attribute --- Endpoint/Common/Attributes/TokenAuthenticationAttribute.cs | 3 ++- Endpoint/Controllers/Configuration/SetupController.cs | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Endpoint/Common/Attributes/TokenAuthenticationAttribute.cs b/Endpoint/Common/Attributes/TokenAuthenticationAttribute.cs index 81812bf..88db4c1 100644 --- a/Endpoint/Common/Attributes/TokenAuthenticationAttribute.cs +++ b/Endpoint/Common/Attributes/TokenAuthenticationAttribute.cs @@ -9,10 +9,11 @@ namespace Mirea.Api.Endpoint.Common.Attributes; [AttributeUsage(AttributeTargets.Method)] public class TokenAuthenticationAttribute : Attribute, IActionFilter { + public const string AuthToken = "AuthToken"; public void OnActionExecuting(ActionExecutingContext context) { var setupToken = context.HttpContext.RequestServices.GetRequiredService(); - if (!context.HttpContext.Request.Cookies.TryGetValue("AuthToken", out string? tokenFromCookie)) + if (!context.HttpContext.Request.Cookies.TryGetValue(AuthToken, out string? tokenFromCookie)) { context.Result = new UnauthorizedResult(); return; diff --git a/Endpoint/Controllers/Configuration/SetupController.cs b/Endpoint/Controllers/Configuration/SetupController.cs index db69a18..dafa4a6 100644 --- a/Endpoint/Controllers/Configuration/SetupController.cs +++ b/Endpoint/Controllers/Configuration/SetupController.cs @@ -69,9 +69,10 @@ public class SetupController( [HttpGet("CheckToken")] public ActionResult CheckToken([FromQuery] string token) { - if (!setupToken.MatchToken(Convert.FromBase64String(token))) return Unauthorized("The token is not valid"); + if (!setupToken.MatchToken(Convert.FromBase64String(token))) + return Unauthorized("The token is not valid"); - Response.Cookies.Append("AuthToken", token, new CookieOptions + Response.Cookies.Append(TokenAuthenticationAttribute.AuthToken, token, new CookieOptions { Path = UrlHelper.GetSubPathWithoutFirstApiName + "api", Domain = HttpContext.GetCurrentDomain(),