Skip to content

Commit 26f29ff

Browse files
committed
Refactor Logger handling
1 parent a60fe51 commit 26f29ff

36 files changed

Lines changed: 341 additions & 421 deletions

NpgsqlRest/Auth/BasicAuthHandler.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Text;
55
using Npgsql;
66
using NpgsqlTypes;
7-
using static NpgsqlRest.NpgsqlRestOptions;
87

98
namespace NpgsqlRest.Auth;
109

@@ -13,9 +12,7 @@ public static class BasicAuthHandler
1312
public static async Task HandleAsync(
1413
HttpContext context,
1514
RoutineEndpoint endpoint,
16-
17-
NpgsqlConnection connection,
18-
ILogger? logger)
15+
NpgsqlConnection connection)
1916
{
2017
var realm =
2118
string.IsNullOrEmpty(endpoint.BasicAuth?.Realm) ?
@@ -26,23 +23,23 @@ public static async Task HandleAsync(
2623
{
2724
if (Options.AuthenticationOptions.BasicAuth.SslRequirement == SslRequirement.Required)
2825
{
29-
logger?.LogError("Basic authentication with SslRequirement 'Required' cannot be used when SSL is disabled.");
26+
Logger?.LogError("Basic authentication with SslRequirement 'Required' cannot be used when SSL is disabled.");
3027
await Challenge(context, realm);
3128
return;
3229
}
3330
if (Options.AuthenticationOptions.BasicAuth.SslRequirement == SslRequirement.Warning)
3431
{
35-
logger?.LogWarning("Using Basic Authentication when SSL is disabled.");
32+
Logger?.LogWarning("Using Basic Authentication when SSL is disabled.");
3633
}
3734
else if (Options.AuthenticationOptions.BasicAuth.SslRequirement == SslRequirement.Ignore)
3835
{
39-
logger?.LogDebug("WARNING: Using Basic Authentication when SSL is disabled.");
36+
Logger?.LogDebug("WARNING: Using Basic Authentication when SSL is disabled.");
4037
}
4138
}
4239

4340
if (context.Request.Headers.TryGetValue("Authorization", out var authHeader) is false)
4441
{
45-
logger?.LogWarning("No Authorization header found in request with Basic Authentication Realm {realm}. Request: {Path}",
42+
Logger?.LogWarning("No Authorization header found in request with Basic Authentication Realm {realm}. Request: {Path}",
4643
realm,
4744
string.Concat(endpoint.Method.ToString(), endpoint.Path));
4845
await Challenge(context, realm);
@@ -52,7 +49,7 @@ public static async Task HandleAsync(
5249
var authValue = authHeader.FirstOrDefault();
5350
if (string.IsNullOrEmpty(authValue) || !authValue.StartsWith("Basic "))
5451
{
55-
logger?.LogWarning("Authorization header value missing or malformed found in request with Basic Authentication Realm {realm}. Request: {Path}",
52+
Logger?.LogWarning("Authorization header value missing or malformed found in request with Basic Authentication Realm {realm}. Request: {Path}",
5653
realm,
5754
string.Concat(endpoint.Method.ToString(), endpoint.Path));
5855
await Challenge(context, realm);
@@ -67,7 +64,7 @@ public static async Task HandleAsync(
6764
}
6865
catch (Exception ex)
6966
{
70-
logger?.LogError(ex, "Failed to decode Basic Authentication credentials in request with Basic Authentication Realm {realm}. Request: {Path}",
67+
Logger?.LogError(ex, "Failed to decode Basic Authentication credentials in request with Basic Authentication Realm {realm}. Request: {Path}",
7168
realm,
7269
string.Concat(endpoint.Method.ToString(), endpoint.Path));
7370
await Challenge(context, realm);
@@ -77,7 +74,7 @@ public static async Task HandleAsync(
7774
var colonIndex = decodedCredentials.IndexOf(':');
7875
if (colonIndex == -1)
7976
{
80-
logger?.LogWarning("Authorization header value malformed found in request with Basic Authentication Realm {realm}. Request: {Path}",
77+
Logger?.LogWarning("Authorization header value malformed found in request with Basic Authentication Realm {realm}. Request: {Path}",
8178
realm,
8279
string.Concat(endpoint.Method.ToString(), endpoint.Path));
8380
await Challenge(context, realm);
@@ -88,7 +85,7 @@ public static async Task HandleAsync(
8885

8986
if (string.IsNullOrEmpty(username) is true || string.IsNullOrEmpty(password) is true)
9087
{
91-
logger?.LogWarning("Username or password missing in request with Basic Authentication Realm {realm}. Request: {Path}",
88+
Logger?.LogWarning("Username or password missing in request with Basic Authentication Realm {realm}. Request: {Path}",
9289
realm,
9390
string.Concat(endpoint.Method.ToString(), endpoint.Path));
9491
await Challenge(context, realm);
@@ -114,7 +111,7 @@ public static async Task HandleAsync(
114111
{
115112
if (Options.AuthenticationOptions.PasswordHasher is null)
116113
{
117-
logger?.LogError("PasswordHasher not configured for Basic Authentication Realm {realm}. Request: {Path}",
114+
Logger?.LogError("PasswordHasher not configured for Basic Authentication Realm {realm}. Request: {Path}",
118115
realm,
119116
string.Concat(endpoint.Method.ToString(), endpoint.Path));
120117
await Challenge(context, realm);
@@ -133,7 +130,7 @@ public static async Task HandleAsync(
133130
if (string.IsNullOrEmpty(challengeCommand) is true)
134131
{
135132
// misconfigured: no user with password configured and no challenge command
136-
logger?.LogError("No Basic Authentication user configured for user {username} in request with Basic Authentication Realm {realm}. Request: {Path}",
133+
Logger?.LogError("No Basic Authentication user configured for user {username} in request with Basic Authentication Realm {realm}. Request: {Path}",
137134
username,
138135
realm,
139136
string.Concat(endpoint.Method.ToString(), endpoint.Path));
@@ -176,7 +173,6 @@ await LoginHandler.HandleAsync(
176173
command,
177174
context,
178175
endpoint.RetryStrategy,
179-
logger,
180176
tracePath: string.Concat(endpoint.Method.ToString(), " ", endpoint.Path),
181177
performHashVerification: false,
182178
assignUserPrincipalToContext: true);
@@ -186,7 +182,7 @@ await LoginHandler.HandleAsync(
186182
return;
187183
}
188184

189-
logger?.LogError("ChallengeCommand denied user {username} in request with Basic Authentication Realm {realm}. Request: {Path}",
185+
Logger?.LogError("ChallengeCommand denied user {username} in request with Basic Authentication Realm {realm}. Request: {Path}",
190186
username,
191187
realm,
192188
string.Concat(endpoint.Method.ToString(), endpoint.Path));
@@ -207,7 +203,7 @@ await LoginHandler.HandleAsync(
207203
}
208204
else
209205
{
210-
logger?.LogWarning("Invalid password for user {username} in request with Basic Authentication Realm {realm}. Request: {Path}",
206+
Logger?.LogWarning("Invalid password for user {username} in request with Basic Authentication Realm {realm}. Request: {Path}",
211207
username,
212208
realm,
213209
string.Concat(endpoint.Method.ToString(), endpoint.Path));

NpgsqlRest/Auth/LoginHandler.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.AspNetCore.Http.HttpResults;
55
using Npgsql;
66
using NpgsqlTypes;
7-
using static NpgsqlRest.NpgsqlRestOptions;
87

98
namespace NpgsqlRest.Auth;
109

@@ -13,9 +12,7 @@ public static class LoginHandler
1312
public static async Task HandleAsync(
1413
NpgsqlCommand command,
1514
HttpContext context,
16-
1715
RetryStrategy? retryStrategy,
18-
ILogger? logger,
1916
string tracePath = "HandleLoginAsync",
2017
bool performHashVerification = true,
2118
bool assignUserPrincipalToContext = false)
@@ -29,8 +26,8 @@ public static async Task HandleAsync(
2926
var verificationPerformed = false;
3027
var verificationFailed = false;
3128

32-
logger?.TraceCommand(command, tracePath);
33-
await using (NpgsqlDataReader reader = await command.ExecuteReaderWithRetryAsync(retryStrategy, logger))
29+
command.TraceCommand(tracePath);
30+
await using (NpgsqlDataReader reader = await command.ExecuteReaderWithRetryAsync(retryStrategy))
3431
{
3532
if (await reader.ReadAsync() is false)
3633
{
@@ -72,7 +69,7 @@ public static async Task HandleAsync(
7269
}
7370
else
7471
{
75-
logger?.WrongStatusType(command.CommandText);
72+
Logger?.WrongStatusType(command.CommandText);
7673
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
7774
await context.Response.CompleteAsync();
7875
return;
@@ -141,7 +138,7 @@ Options.AuthenticationOptions.PasswordHasher is not null &&
141138
verificationPerformed = true;
142139
if (Options.AuthenticationOptions.PasswordHasher?.VerifyHashedPassword(hash, pass) is false)
143140
{
144-
logger?.VerifyPasswordFailed(tracePath, userId, userName);
141+
Logger?.VerifyPasswordFailed(tracePath, userId, userName);
145142
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
146143
await context.Response.CompleteAsync();
147144
verificationFailed = true;
@@ -152,7 +149,7 @@ Options.AuthenticationOptions.PasswordHasher is not null &&
152149
}
153150
if (foundPasswordParameter is false)
154151
{
155-
logger?.CantFindPasswordParameter(tracePath,
152+
Logger?.CantFindPasswordParameter(tracePath,
156153
command.Parameters.Select(p => (p as NpgsqlRestParameter)?.ActualName)?.ToArray(),
157154
Options.AuthenticationOptions.PasswordParameterNameContains);
158155
}
@@ -187,8 +184,8 @@ Options.AuthenticationOptions.PasswordHasher is not null &&
187184
{
188185
failedCommand.Parameters.Add(NpgsqlRestParameter.CreateTextParam(userName));
189186
}
190-
logger?.TraceCommand(failedCommand, tracePath);
191-
await failedCommand.ExecuteNonQueryWithRetryAsync(retryStrategy, logger);
187+
failedCommand.TraceCommand(tracePath);
188+
await failedCommand.ExecuteNonQueryWithRetryAsync(retryStrategy);
192189
}
193190
}
194191
return;
@@ -215,8 +212,8 @@ Options.AuthenticationOptions.PasswordHasher is not null &&
215212
{
216213
succeededCommand.Parameters.Add(NpgsqlRestParameter.CreateTextParam(userName));
217214
}
218-
logger?.TraceCommand(succeededCommand, tracePath);
219-
await succeededCommand.ExecuteNonQueryWithRetryAsync(retryStrategy, logger);
215+
succeededCommand.TraceCommand(tracePath);
216+
await succeededCommand.ExecuteNonQueryWithRetryAsync(retryStrategy);
220217
}
221218
}
222219
}
@@ -234,7 +231,7 @@ Options.AuthenticationOptions.PasswordHasher is not null &&
234231
{
235232
if (Results.SignIn(principal: principal, authenticationScheme: scheme) is not SignInHttpResult result)
236233
{
237-
logger?.LogError("Failed in constructing user identity for authentication.");
234+
Logger?.LogError("Failed in constructing user identity for authentication.");
238235
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
239236
return;
240237
}

NpgsqlRest/Auth/LogoutHandler.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
using System;
2-
using System.Net;
3-
using System.Security.Claims;
4-
using System.Text;
5-
using Microsoft.AspNetCore.Http.HttpResults;
6-
using Microsoft.AspNetCore.DataProtection;
7-
using Npgsql;
8-
using NpgsqlTypes;
1+
using Npgsql;
92

103
namespace NpgsqlRest.Auth;
114

125
public static class LogoutHandler
136
{
14-
public static async Task HandleAsync(NpgsqlCommand command, RoutineEndpoint endpoint, HttpContext context, ILogger? logger)
7+
public static async Task HandleAsync(NpgsqlCommand command, RoutineEndpoint endpoint, HttpContext context)
158
{
169
var path = string.Concat(endpoint.Method.ToString(), " ", endpoint.Path);
17-
logger?.TraceCommand(command, path);
10+
command.TraceCommand(path);
1811

1912
if (endpoint.Routine.IsVoid)
2013
{

0 commit comments

Comments
 (0)