Skip to content

Commit 57d1553

Browse files
authored
Merge pull request #16 from Black-Cockpit/hotfix/fixing_logging_dependency_injection
💯 Fixed logging dependency injection
2 parents 2eaffed + f0c7d18 commit 57d1553

File tree

15 files changed

+69
-35
lines changed

15 files changed

+69
-35
lines changed

.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.Tests/Modules/KcAuthorizationTests/KcAuthorizationExtensionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void ShouldRegisterAuthorizationHandler()
6868
});
6969

7070
// Create a mock for ILogger to simulate logging functionality.
71-
var mockLogger = new Mock<ILogger>();
71+
var mockLogger = new Mock<ILogger<IKcRealmAdminTokenHandler>>();
7272
_ = mockLogger.Setup(logger => logger.IsEnabled(It.IsAny<LogLevel>())).Returns(true);
7373

7474
// Add Keycloak services, the token handler, and the logger to the service collection.
@@ -112,7 +112,7 @@ public void ShouldRegisterKeycloakProtectedResourcesPoliciesServices()
112112
var mockAuthorizationOptions = new Mock<IOptions<AuthorizationOptions>>();
113113

114114
// Create a mock for ILogger to simulate logging functionality.
115-
var mockLogger = new Mock<ILogger>();
115+
var mockLogger = new Mock<ILogger<IKcRealmAdminTokenHandler>>();
116116
_ = mockLogger.Setup(logger => logger.IsEnabled(It.IsAny<LogLevel>())).Returns(true);
117117

118118
// Add mocked services to the service collection.

NETCore.Keycloak.Client.Tests/Modules/KcAuthorizationTests/KcBearerAuthorizationHandlerTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ public void SetUp()
139139
.Returns(mockScope.Object);
140140

141141
// Set up a mock logger to simulate logging functionality
142-
var mockLogger = new Mock<ILogger>();
142+
var mockLogger = new Mock<ILogger<IKcRealmAdminTokenHandler>>();
143143
_ = mockLogger.Setup(logger => logger.IsEnabled(It.IsAny<LogLevel>())).Returns(true);
144144

145145
// Configure the mock scope to resolve services
146146
_ = mockScope.Setup(x => x.ServiceProvider).Returns(_mockProvider.Object);
147147

148148
// Mock resolution of ILogger from the service provider
149-
_ = _mockProvider.Setup(x => x.GetService(typeof(ILogger)))
149+
_ = _mockProvider.Setup(x => x.GetService(typeof(ILogger<IKcRealmAdminTokenHandler>)))
150150
.Returns(mockLogger.Object);
151151

152152
// Mock resolution of IHttpContextAccessor from the service provider
@@ -772,14 +772,14 @@ private KcRealmAdminTokenHandler SetUpMockRealmAdminTokenHandler()
772772
.Returns(mockScope.Object);
773773

774774
// Set up a mock logger to simulate logging functionality
775-
var mockLogger = new Mock<ILogger>();
775+
var mockLogger = new Mock<ILogger<IKcRealmAdminTokenHandler>>();
776776
_ = mockLogger.Setup(logger => logger.IsEnabled(It.IsAny<LogLevel>())).Returns(true);
777777

778778
// Configure the mock scope to resolve services
779779
_ = mockScope.Setup(x => x.ServiceProvider).Returns(mockProvider.Object);
780780

781781
// Mock resolution of ILogger from the service provider
782-
_ = mockProvider.Setup(x => x.GetService(typeof(ILogger)))
782+
_ = mockProvider.Setup(x => x.GetService(typeof(ILogger<IKcRealmAdminTokenHandler>)))
783783
.Returns(mockLogger.Object);
784784

785785
// Define a test-specific configuration for the Keycloak realm

NETCore.Keycloak.Client.Tests/Modules/KcAuthorizationTests/KcRealmAdminTokenHandlerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ public void SetUp()
5858
.Returns(mockScope.Object);
5959

6060
// Mock the logger.
61-
var mockLogger = new Mock<ILogger>();
61+
var mockLogger = new Mock<ILogger<IKcRealmAdminTokenHandler>>();
6262
_ = mockLogger.Setup(logger => logger.IsEnabled(It.IsAny<LogLevel>())).Returns(true);
6363

6464
// Mock the service provider to return the logger when requested.
6565
_ = mockScope.Setup(x => x.ServiceProvider).Returns(_mockProvider.Object);
66-
_ = _mockProvider.Setup(x => x.GetService(typeof(ILogger)))
66+
_ = _mockProvider.Setup(x => x.GetService(typeof(ILogger<IKcRealmAdminTokenHandler>)))
6767
.Returns(mockLogger.Object);
6868

6969
// Define test configuration for the Keycloak realm.

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) )

0 commit comments

Comments
 (0)