Skip to content

Commit 6b5508b

Browse files
committed
code improvments
1 parent 4931f27 commit 6b5508b

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/Serilog.Ui.Web/Authorization/BasicAuthenticationFilter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class BasicAuthenticationFilter : IUiAuthorizationFilter
1212
public string Pass { get; set; }
1313

1414
private const string AuthenticationScheme = "Basic";
15-
private const string AuthenticationCookieName = "SerilogAuth";
15+
internal const string AuthenticationCookieName = "SerilogAuth";
1616

1717
public bool Authorize(HttpContext httpContext)
1818
{
@@ -25,7 +25,7 @@ public bool Authorize(HttpContext httpContext)
2525
if (!string.IsNullOrWhiteSpace(authCookie))
2626
{
2727
var hashedCredentials = EncryptCredentials(User, Pass);
28-
isAuthenticated = string.Equals(authCookie, hashedCredentials, StringComparison.OrdinalIgnoreCase);
28+
isAuthenticated = authCookie.Equals(hashedCredentials, StringComparison.OrdinalIgnoreCase);
2929
}
3030
}
3131
else
@@ -53,9 +53,9 @@ public bool Authorize(HttpContext httpContext)
5353
return isAuthenticated;
5454
}
5555

56-
public string EncryptCredentials(string user, string pass)
56+
private string EncryptCredentials(string user, string pass)
5757
{
58-
var sha256 = SHA256.Create();
58+
using var sha256 = SHA256.Create();
5959
var hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes($"{user}:{pass}"));
6060
var hashedCredentials = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
6161
return hashedCredentials;
@@ -81,7 +81,7 @@ private bool CredentialsMatch((string Username, string Password) tokens)
8181
private void SetChallengeResponse(HttpContext httpContext)
8282
{
8383
httpContext.Response.StatusCode = 401;
84-
httpContext.Response.Headers.Append("WWW-Authenticate", "Basic realm=\"SeriLog Ui\"");
84+
httpContext.Response.Headers.Append("WWW-Authenticate", "Basic realm=\"Serilog UI\"");
8585
httpContext.Response.WriteAsync("Authentication is required.");
8686
}
8787
}

tests/Serilog.Ui.Web.Tests/Authorization/BasicAuthenticationFilterTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using FluentAssertions;
23
using Microsoft.AspNetCore.Http;
34
using Microsoft.Net.Http.Headers;
@@ -23,9 +24,11 @@ public async Task Authorize_WithValidCredentials_ShouldReturnTrue()
2324

2425
// Act
2526
var result = filter.Authorize(httpContext);
27+
var authCookie = httpContext.Response.GetTypedHeaders().SetCookie.FirstOrDefault(sc => sc.Name == BasicAuthenticationFilter.AuthenticationCookieName);
2628

2729
// Assert
2830
result.Should().BeTrue();
31+
authCookie.Should().NotBeNull();
2932
}
3033

3134
[Fact]
@@ -66,6 +69,6 @@ public async Task Authorize_WithMissingAuthorizationHeader_ShouldSetChallengeRes
6669
// Assert
6770
result.Should().BeFalse();
6871
httpContext.Response.StatusCode.Should().Be(401);
69-
httpContext.Response.Headers[HeaderNames.WWWAuthenticate].Should().Contain("Basic realm=\"Hangfire Dashboard\"");
72+
httpContext.Response.Headers[HeaderNames.WWWAuthenticate].Should().Contain("Basic realm=\"Serilog UI\"");
7073
}
7174
}

0 commit comments

Comments
 (0)