Skip to content

Commit

Permalink
Change user and pass properties name in BasicAuthenticationFilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
mo-esmp committed Nov 8, 2023
1 parent f6c2e28 commit a33a678
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
17 changes: 9 additions & 8 deletions src/Serilog.Ui.Web/Authorization/BasicAuthenticationFilter.cs
Original file line number Diff line number Diff line change
@@ -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"];
Expand All @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Ui.Web/Serilog.Ui.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<Version>2.5.0</Version>
<Version>2.5.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit a33a678

Please sign in to comment.