From a33a678ee001a87340c37afc2462fb86992a5fb8 Mon Sep 17 00:00:00 2001 From: mo-esmp Date: Wed, 8 Nov 2023 15:52:41 +0100 Subject: [PATCH] Change user and pass properties name in BasicAuthenticationFilter. --- .../Authorization/BasicAuthenticationFilter.cs | 17 +++++++++-------- src/Serilog.Ui.Web/Serilog.Ui.Web.csproj | 2 +- .../BasicAuthenticationFilterTests.cs | 12 ++++++------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Serilog.Ui.Web/Authorization/BasicAuthenticationFilter.cs b/src/Serilog.Ui.Web/Authorization/BasicAuthenticationFilter.cs index 1f4606ba..875ecdce 100644 --- a/src/Serilog.Ui.Web/Authorization/BasicAuthenticationFilter.cs +++ b/src/Serilog.Ui.Web/Authorization/BasicAuthenticationFilter.cs @@ -1,19 +1,20 @@ -using System; +using Microsoft.AspNetCore.Http; +using System; using System.Net.Http.Headers; using System.Security.Cryptography; using System.Text; -using Microsoft.AspNetCore.Http; namespace Serilog.Ui.Web.Authorization; public class BasicAuthenticationFilter : IUiAuthorizationFilter { - public string User { get; set; } - public string Pass { get; set; } - private const string AuthenticationScheme = "Basic"; internal const string AuthenticationCookieName = "SerilogAuth"; + public string UserName { get; set; } + + public string Password { get; set; } + public bool Authorize(HttpContext httpContext) { var header = httpContext.Request.Headers["Authorization"]; @@ -24,7 +25,7 @@ public bool Authorize(HttpContext httpContext) var authCookie = httpContext.Request.Cookies[AuthenticationCookieName]; if (!string.IsNullOrWhiteSpace(authCookie)) { - var hashedCredentials = EncryptCredentials(User, Pass); + var hashedCredentials = EncryptCredentials(UserName, Password); isAuthenticated = authCookie.Equals(hashedCredentials, StringComparison.OrdinalIgnoreCase); } } @@ -39,7 +40,7 @@ public bool Authorize(HttpContext httpContext) if (CredentialsMatch(tokens)) { isAuthenticated = true; - var hashedCredentials = EncryptCredentials(User, Pass); + var hashedCredentials = EncryptCredentials(UserName, Password); httpContext.Response.Cookies.Append(AuthenticationCookieName, hashedCredentials); } } @@ -75,7 +76,7 @@ private static (string, string) ExtractAuthenticationTokens(AuthenticationHeader private bool CredentialsMatch((string Username, string Password) tokens) { - return tokens.Username == User && tokens.Password == Pass; + return tokens.Username == UserName && tokens.Password == Password; } private void SetChallengeResponse(HttpContext httpContext) diff --git a/src/Serilog.Ui.Web/Serilog.Ui.Web.csproj b/src/Serilog.Ui.Web/Serilog.Ui.Web.csproj index c702a1fd..99f3db45 100644 --- a/src/Serilog.Ui.Web/Serilog.Ui.Web.csproj +++ b/src/Serilog.Ui.Web/Serilog.Ui.Web.csproj @@ -3,7 +3,7 @@ netcoreapp3.1;net5.0;net6.0;net7.0 latest - 2.5.0 + 2.5.1 diff --git a/tests/Serilog.Ui.Web.Tests/Authorization/BasicAuthenticationFilterTests.cs b/tests/Serilog.Ui.Web.Tests/Authorization/BasicAuthenticationFilterTests.cs index 7ba6f6c3..e6ec73f0 100644 --- a/tests/Serilog.Ui.Web.Tests/Authorization/BasicAuthenticationFilterTests.cs +++ b/tests/Serilog.Ui.Web.Tests/Authorization/BasicAuthenticationFilterTests.cs @@ -15,8 +15,8 @@ public async Task Authorize_WithValidCredentials_ShouldReturnTrue() // Arrange var filter = new BasicAuthenticationFilter { - User = "User", - Pass = "P@ss" + UserName = "User", + Password = "P@ss" }; var httpContext = new DefaultHttpContext(); @@ -37,8 +37,8 @@ public async Task Authorize_WithInvalidCredentials_ShouldReturnFalse() // Arrange var filter = new BasicAuthenticationFilter { - User = "User", - Pass = "P@ss" + UserName = "User", + Password = "P@ss" }; var httpContext = new DefaultHttpContext(); @@ -57,8 +57,8 @@ public async Task Authorize_WithMissingAuthorizationHeader_ShouldSetChallengeRes // Arrange var filter = new BasicAuthenticationFilter { - User = "User", - Pass = "P@ss" + UserName = "User", + Password = "P@ss" }; var httpContext = new DefaultHttpContext();