Skip to content

Commit 7b0deab

Browse files
committed
💯 Fixed logging dependency injection
1 parent 2eaffed commit 7b0deab

12 files changed

Lines changed: 61 additions & 27 deletions

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ csharp_space_between_square_brackets = false
5454
csharp_preserve_single_line_statements = true
5555
csharp_preserve_single_line_blocks = true
5656

57+
# Ensure source code is documented
58+
dotnet_diagnostic.CS1591.severity = error
59+
5760
# Ensure names and members access are simplified
5861
dotnet_diagnostic.ide0001.severity = error
5962
dotnet_diagnostic.ide0002.severity = error

NETCore.Keycloak.Client/Authentication/Claims/KcRolesClaimsTransformer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,19 @@ namespace NETCore.Keycloak.Client.Authentication.Claims;
3131
/// </remarks>
3232
public class KcRolesClaimsTransformer : IClaimsTransformation
3333
{
34+
/// <summary>
35+
/// Represents the claim type used to identify user roles within the application.
36+
/// </summary>
3437
private readonly string _roleClaimType;
38+
39+
/// <summary>
40+
/// Defines the source of the role claims, used to identify how roles are sourced.
41+
/// </summary>
3542
private readonly KcRolesClaimSource _roleSource;
43+
44+
/// <summary>
45+
/// Represents the audience for the token, used to validate the intended recipient of the token.
46+
/// </summary>
3647
private readonly string _audience;
3748

3849
/// <summary>

NETCore.Keycloak.Client/Authentication/KcAuthenticationConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class KcAuthenticationConfiguration
8080
/// <summary>
8181
/// List of valid audiences
8282
/// </summary>
83-
public IEnumerable<string> ValidAudiences { get; set; } = new List<string>();
83+
public IEnumerable<string> ValidAudiences { get; set; } = [];
8484

8585
/// <summary>
8686
/// Get valid issuer

NETCore.Keycloak.Client/Authorization/Handlers/KcBearerAuthorizationHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public KcBearerAuthorizationHandler(IServiceProvider serviceProvider)
4242
using var scope = serviceProvider.CreateScope();
4343

4444
// Resolve the logger instance.
45-
_logger = scope.ServiceProvider.GetService<ILogger>();
45+
_logger = scope.ServiceProvider.GetService<ILogger<KcBearerAuthorizationHandler>>();
4646

4747
// Resolve the HTTP context accessor for accessing request data.
4848
_httpContextAccessor = scope.ServiceProvider.GetRequiredService<IHttpContextAccessor>();
@@ -61,10 +61,10 @@ protected override async Task HandleRequirementAsync(AuthorizationHandlerContext
6161
KcAuthorizationRequirement requirement)
6262
{
6363
// Ensure that authorization context is not null
64-
ArgumentNullException.ThrowIfNull(context, "Authorization context is required.");
64+
ArgumentNullException.ThrowIfNull(context);
6565

6666
// Ensure that authorization requirement is not null.
67-
ArgumentNullException.ThrowIfNull(requirement, "Authorization requirement is required.");
67+
ArgumentNullException.ThrowIfNull(requirement);
6868

6969
// Check if the user is authenticated
7070
if ( context.User.Identity?.IsAuthenticated ?? false )

NETCore.Keycloak.Client/Authorization/Handlers/KcRealmAdminTokenHandler.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public KcRealmAdminTokenHandler(KcRealmAdminConfigurationStore realmAdminConfigu
6262

6363
// Create a scoped service provider to resolve the logger service.
6464
using var scope = provider.CreateScope();
65-
_logger = scope.ServiceProvider.GetRequiredService<ILogger>(); // Get the logger from the scoped provider.
65+
_logger = scope.ServiceProvider
66+
.GetRequiredService<ILogger<IKcRealmAdminTokenHandler>>();
6667

6768
// Initialize the token cache to store access and refresh tokens for different realms.
6869
_tokensCache = new ConcurrentDictionary<string, KcCachedToken>();

NETCore.Keycloak.Client/Authorization/KcAuthorizationExtension.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public static class KcAuthorizationExtension
1818
/// <param name="services"></param>
1919
/// <returns></returns>
2020
public static IServiceCollection AddKeycloakAuthorization(this IServiceCollection services) =>
21-
services
22-
.AddSingleton<IAuthorizationHandler, KcBearerAuthorizationHandler>().AddHttpContextAccessor();
21+
services.AddSingleton<IAuthorizationHandler, KcBearerAuthorizationHandler>()
22+
.AddHttpContextAccessor()
23+
.AddLogging();
2324

2425
/// <summary>
2526
/// Add keycloak policy provider
@@ -35,9 +36,8 @@ public static IServiceCollection AddKeycloakProtectedResourcesPolicies<T, TV>(
3536
.AddSingleton<KcRealmAdminConfigurationStore, TV>()
3637
.AddSingleton<IKcRealmAdminTokenHandler, KcRealmAdminTokenHandler>()
3738
.AddSingleton<IAuthorizationPolicyProvider, KcProtectedResourcePolicyProvider>(
38-
provider =>
39-
new KcProtectedResourcePolicyProvider(provider,
40-
provider.GetRequiredService<IOptions<AuthorizationOptions>>()));
39+
provider => new KcProtectedResourcePolicyProvider(provider,
40+
provider.GetRequiredService<IOptions<AuthorizationOptions>>()));
4141
return services;
4242
}
4343
}

NETCore.Keycloak.Client/Authorization/Requirements/KcAuthorizationRequirement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class KcAuthorizationRequirement : IAuthorizationRequirement
5151
public KcAuthorizationRequirement(KcProtectedResourceStore protectedResourceStore, string resource, string scope)
5252
{
5353
// Ensure protected resources is not null
54-
ArgumentNullException.ThrowIfNull(protectedResourceStore, $"{nameof(protectedResourceStore)} cannot be null.");
54+
ArgumentNullException.ThrowIfNull(protectedResourceStore);
5555

5656
// Ensure resource is not null or empty
5757
if ( string.IsNullOrWhiteSpace(resource) )

NETCore.Keycloak.Client/Exceptions/KcException.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ namespace NETCore.Keycloak.Client.Exceptions;
33
/// <summary>
44
/// Represents an exception specific to Keycloak operations.
55
/// </summary>
6-
[Serializable]
76
public class KcException : Exception
87
{
98
/// <summary>

NETCore.Keycloak.Client/HttpClients/KcHttpClientBase.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,6 @@ private static StringContent GetBody(object o) =>
329329
{
330330
NullValueHandling = NullValueHandling.Ignore,
331331
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
332-
Converters = new List<JsonConverter>
333-
{
334-
new StringEnumConverter()
335-
}
332+
Converters = [new StringEnumConverter()]
336333
}));
337334
}

NETCore.Keycloak.Client/Models/KcOperationResponse.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ public class KcOperationResponse<T> : KcBaseResponse<T>
1919
/// or <c>null</c> if no metrics are available.
2020
/// </value>
2121
[JsonProperty("monitoringMetrics")]
22-
public ICollection<KcHttpApiMonitoringMetrics> MonitoringMetrics { get; } =
23-
new List<KcHttpApiMonitoringMetrics>();
22+
public ICollection<KcHttpApiMonitoringMetrics> MonitoringMetrics { get; } = [];
2423
}

0 commit comments

Comments
 (0)