From 687ba6df00b12b324a9a5eb0309c0d3b74078dcb Mon Sep 17 00:00:00 2001 From: Luiz Adolfo Date: Tue, 18 Mar 2025 09:34:28 -0300 Subject: [PATCH 1/5] preparing release 5.0.0 --- .github/workflows/main.yml | 36 +++--------------- .../AuthorizationHeadersJsonSerializer.cs | 2 +- .../HybridCacheAuthorizationInterceptor.cs | 2 +- .../Interceptors/MemoryCacheInterceptor.cs | 2 +- .../AuthorizationInterceptorLogDifinitions.cs | 16 -------- .../AuthorizationInterceptorOptions.cs | 6 +-- .../AuthorizationInterceptorStrategy.cs | 38 ++++++++----------- src/Directory.Build.props | 2 +- .../HttpClientBuilderExtensionsTests.cs | 2 +- 9 files changed, 29 insertions(+), 77 deletions(-) delete mode 100644 src/AuthorizationInterceptor/Log/AuthorizationInterceptorLogDifinitions.cs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ee4bb88..941d800 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,13 +2,13 @@ name: Main on: push: - branches: [ main, feature/*, hotfix/* ] + branches: [ main, feature/*, hotfix/*, release/* ] pull_request: - branches: [ main, feature/*, hotfix/* ] + branches: [ main, feature/*, hotfix/*, release/* ] jobs: - - build: + + code-validation: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -16,37 +16,11 @@ jobs: uses: actions/setup-dotnet@v1 with: dotnet-version: 9.0.x - - name: Restore dependencies + - name: Restore run: dotnet restore --property NuGetAudit=true --property NuGetAuditMode=All --property NuGetAuditLevel=Moderate - name: Build run: dotnet build --no-restore - - test: - needs: build - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Setup .NET - uses: actions/setup-dotnet@v1 - with: - dotnet-version: 9.0.x - - name: Restore dependencies - run: dotnet restore - - name: Build - run: dotnet build --no-restore - name: Test - run: dotnet test --no-build --verbosity normal - - analisys: - needs: test - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Setup .NET - uses: actions/setup-dotnet@v1 - with: - dotnet-version: 9.0.x - - name: Generate coverage report run: dotnet test /p:CollectCoverage=true /p:ExcludeByAttribute="GeneratedCodeAttribute" /p:ExcludeByAttribute="CompilerGeneratedAttribute" /p:CoverletOutputFormat=\"json,cobertura\" /p:CoverletOutput=../../coverage/ /p:MergeWith=../../coverage/coverage.json - name: Upload coverage to Codecov uses: codecov/codecov-action@v5 diff --git a/src/AuthorizationInterceptor.Extensions.Abstractions/Json/AuthorizationHeadersJsonSerializer.cs b/src/AuthorizationInterceptor.Extensions.Abstractions/Json/AuthorizationHeadersJsonSerializer.cs index 4a592a1..fd7b5d7 100644 --- a/src/AuthorizationInterceptor.Extensions.Abstractions/Json/AuthorizationHeadersJsonSerializer.cs +++ b/src/AuthorizationInterceptor.Extensions.Abstractions/Json/AuthorizationHeadersJsonSerializer.cs @@ -6,7 +6,7 @@ namespace AuthorizationInterceptor.Extensions.Abstractions.Json; /// /// A Json Serializer for the class. Used to print or save in a format where data loss does not occur. /// -public class AuthorizationHeadersJsonSerializer +public static class AuthorizationHeadersJsonSerializer { /// /// Custom converter used to print or save in a format where data loss does not occur. diff --git a/src/AuthorizationInterceptor.Extensions.HybridCache/Interceptors/HybridCacheAuthorizationInterceptor.cs b/src/AuthorizationInterceptor.Extensions.HybridCache/Interceptors/HybridCacheAuthorizationInterceptor.cs index 0764e56..4ea5009 100644 --- a/src/AuthorizationInterceptor.Extensions.HybridCache/Interceptors/HybridCacheAuthorizationInterceptor.cs +++ b/src/AuthorizationInterceptor.Extensions.HybridCache/Interceptors/HybridCacheAuthorizationInterceptor.cs @@ -12,7 +12,7 @@ internal sealed class HybridCacheAuthorizationInterceptor(Microsoft.Extensions.C public async ValueTask GetHeadersAsync(string name, CancellationToken cancellationToken) { var data = await hybridCache.GetOrCreateAsync(string.Format(CacheKey, name), - _ => ValueTask.FromResult((string?)null), + _ => ValueTask.FromResult(null), new HybridCacheEntryOptions { Flags = HybridCacheEntryFlags.DisableLocalCacheWrite | HybridCacheEntryFlags.DisableDistributedCacheWrite }, cancellationToken: cancellationToken); return string.IsNullOrEmpty(data) ? null : AuthorizationHeadersJsonSerializer.Deserialize(data); diff --git a/src/AuthorizationInterceptor.Extensions.MemoryCache/Interceptors/MemoryCacheInterceptor.cs b/src/AuthorizationInterceptor.Extensions.MemoryCache/Interceptors/MemoryCacheInterceptor.cs index 96e3aba..dbaffbe 100644 --- a/src/AuthorizationInterceptor.Extensions.MemoryCache/Interceptors/MemoryCacheInterceptor.cs +++ b/src/AuthorizationInterceptor.Extensions.MemoryCache/Interceptors/MemoryCacheInterceptor.cs @@ -13,7 +13,7 @@ internal class MemoryCacheInterceptor(IMemoryCache memoryCache) : IAuthorization cancellationToken.ThrowIfCancellationRequested(); var headers = memoryCache.Get(string.Format(CacheKey, name)); - return new(headers); + return new ValueTask(headers); } public ValueTask UpdateHeadersAsync(string name, AuthorizationHeaders? expiredHeaders, AuthorizationHeaders? newHeaders, CancellationToken cancellationToken) diff --git a/src/AuthorizationInterceptor/Log/AuthorizationInterceptorLogDifinitions.cs b/src/AuthorizationInterceptor/Log/AuthorizationInterceptorLogDifinitions.cs deleted file mode 100644 index 5480d57..0000000 --- a/src/AuthorizationInterceptor/Log/AuthorizationInterceptorLogDifinitions.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.Extensions.Logging; - -namespace AuthorizationInterceptor.Log; - -public static partial class AuthorizationInterceptorLogDifinitions -{ - [LoggerMessage(EventId = 1, Level = LogLevel.Warning, Message = "No interceptor was configured for HttpClient `{httpClientName}`. A Runtime interceptor was used instead. It is recommended to use at least the MemoryCache interceptor.")] - public static partial void LogNoInterceptorUsed(this ILogger logger, string httpClientName); - - [LoggerMessage(EventId = 2, Level = LogLevel.Warning, Message = "AuthorizationInterceptor is not available for synchronous requests. Consider using asynchronous requests!")] - public static partial void LogUnavailableForSyncRequests(this ILogger logger); - - [LoggerMessage(EventId = 3, Level = LogLevel.Warning, Message = "Operation canceled while getting headers from interceptor `{interceptor}` with integration `{httpClientName}`")] - public static partial void LogOperationCanceledInInterceptor(this ILogger logger, string interceptor, string httpClientName); -} - diff --git a/src/AuthorizationInterceptor/Options/AuthorizationInterceptorOptions.cs b/src/AuthorizationInterceptor/Options/AuthorizationInterceptorOptions.cs index ce75bbe..3f90ce0 100644 --- a/src/AuthorizationInterceptor/Options/AuthorizationInterceptorOptions.cs +++ b/src/AuthorizationInterceptor/Options/AuthorizationInterceptorOptions.cs @@ -21,8 +21,8 @@ public class AuthorizationInterceptorOptions : IAuthorizationInterceptorOptions /// Adds a custom interceptor to the interceptor sequence. Note that the interceptor addition sequence interferes with the headers query sequence. /// /// Implementation class of type - /// Access to if necessary inject some dependencies - public void UseCustomInterceptor(Func? func = null) where T : IAuthorizationInterceptor - => Interceptors.Add((typeof(T), func)); + /// Access to if necessary to inject some dependencies + public void UseCustomInterceptor(Func? services = null) where T : IAuthorizationInterceptor + => Interceptors.Add((typeof(T), services)); } diff --git a/src/AuthorizationInterceptor/Strategies/AuthorizationInterceptorStrategy.cs b/src/AuthorizationInterceptor/Strategies/AuthorizationInterceptorStrategy.cs index 7fb41e2..59c1b06 100644 --- a/src/AuthorizationInterceptor/Strategies/AuthorizationInterceptorStrategy.cs +++ b/src/AuthorizationInterceptor/Strategies/AuthorizationInterceptorStrategy.cs @@ -6,16 +6,10 @@ namespace AuthorizationInterceptor.Strategies; -internal class AuthorizationInterceptorStrategy : IAuthorizationInterceptorStrategy +internal class AuthorizationInterceptorStrategy(ILoggerFactory loggerFactory, IAuthorizationInterceptor[] interceptors) + : IAuthorizationInterceptorStrategy { - private readonly IAuthorizationInterceptor[] _interceptors; - private readonly ILogger _logger; - - public AuthorizationInterceptorStrategy(ILoggerFactory loggerFactory, IAuthorizationInterceptor[] interceptors) - { - _logger = loggerFactory.CreateLogger("AuthorizationInterceptorStrategy"); - _interceptors = interceptors; - } + private readonly ILogger _logger = loggerFactory.CreateLogger("AuthorizationInterceptorStrategy"); public async ValueTask GetHeadersAsync(string name, IAuthenticationHandler authenticationHandler, CancellationToken cancellationToken) { @@ -24,37 +18,37 @@ public AuthorizationInterceptorStrategy(ILoggerFactory loggerFactory, IAuthoriza AuthorizationHeaders? headers = null; int index; - for (index = 0; index < _interceptors.Length; index++) + for (index = 0; index < interceptors.Length; index++) { try { - LogDebug("Getting headers from interceptor '{interceptor}' with integration '{name}'", _interceptors[index].GetType().Name, name); + LogDebug("Getting headers from interceptor '{interceptor}' with integration '{name}'", interceptors[index].GetType().Name, name); cancellationToken.ThrowIfCancellationRequested(); - headers = await _interceptors[index].GetHeadersAsync(name, cancellationToken); + headers = await interceptors[index].GetHeadersAsync(name, cancellationToken); if (headers == null) continue; - LogDebug("Headers found in interceptor '{interceptor}' with integration '{name}'", _interceptors[index].GetType().Name, name); + LogDebug("Headers found in interceptor '{interceptor}' with integration '{name}'", interceptors[index].GetType().Name, name); if (headers.IsHeadersValid()) { - LogDebug("Headers still valid in interceptor '{interceptor}' with integration '{name}'", _interceptors[index].GetType().Name, name); + LogDebug("Headers still valid in interceptor '{interceptor}' with integration '{name}'", interceptors[index].GetType().Name, name); return await UpdateHeadersInInterceptorsAsync(name, index, headers, cancellationToken); } - LogDebug("Headers is expired in interceptor '{interceptor}' with integration '{name}'", _interceptors[index].GetType().Name, name); + LogDebug("Headers is expired in interceptor '{interceptor}' with integration '{name}'", interceptors[index].GetType().Name, name); break; } catch (OperationCanceledException) { - _logger.LogOperationCanceledInInterceptor(_interceptors[index].GetType().Name, name); + _logger.LogOperationCanceledInInterceptor(interceptors[index].GetType().Name, name); throw; } catch (Exception ex) { - _logger.LogError(ex, "Error getting headers from interceptor '{interceptor}' with integration '{name}'", _interceptors[index].GetType().Name, name); + _logger.LogError(ex, "Error getting headers from interceptor '{interceptor}' with integration '{name}'", interceptors[index].GetType().Name, name); } } @@ -74,7 +68,7 @@ public AuthorizationInterceptorStrategy(ILoggerFactory loggerFactory, IAuthoriza return null; } - return await UpdateHeadersInInterceptorsAsync(name, _interceptors.Length, newHeaders, cancellationToken); + return await UpdateHeadersInInterceptorsAsync(name, interceptors.Length, newHeaders, cancellationToken); } private async ValueTask UpdateHeadersInInterceptorsAsync(string name, int startIndex, AuthorizationHeaders? headers, CancellationToken cancellationToken) @@ -88,20 +82,20 @@ public AuthorizationInterceptorStrategy(ILoggerFactory loggerFactory, IAuthoriza { try { - LogDebug("Updating headers in interceptor '{interceptor}' with integration '{name}'", _interceptors[index].GetType().Name, name); + LogDebug("Updating headers in interceptor '{interceptor}' with integration '{name}'", interceptors[index].GetType().Name, name); cancellationToken.ThrowIfCancellationRequested(); - await _interceptors[index].UpdateHeadersAsync(name, null, headers, cancellationToken); + await interceptors[index].UpdateHeadersAsync(name, null, headers, cancellationToken); } catch (OperationCanceledException) { - _logger.LogOperationCanceledInInterceptor(_interceptors[index].GetType().Name, name); + _logger.LogOperationCanceledInInterceptor(interceptors[index].GetType().Name, name); throw; } catch (Exception ex) { - _logger.LogError(ex, "Error updating headers in interceptor '{interceptor}' with integration '{name}'", _interceptors[index].GetType().Name, name); + _logger.LogError(ex, "Error updating headers in interceptor '{interceptor}' with integration '{name}'", interceptors[index].GetType().Name, name); } } diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 4db61a9..5a36fce 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -3,7 +3,7 @@ 13 enable enable - 5.0.0-preview-1 + 5.0.0 net6.0;net7.0;net8.0;net9.0 Authorization Interceptor Adolfok3 diff --git a/tests/AuthorizationInterceptor.Tests/Extensions/HttpClientBuilderExtensionsTests.cs b/tests/AuthorizationInterceptor.Tests/Extensions/HttpClientBuilderExtensionsTests.cs index 9345c98..79b5deb 100644 --- a/tests/AuthorizationInterceptor.Tests/Extensions/HttpClientBuilderExtensionsTests.cs +++ b/tests/AuthorizationInterceptor.Tests/Extensions/HttpClientBuilderExtensionsTests.cs @@ -51,7 +51,7 @@ public void AddAuthorizationInterceptorHandler_WithOptions_ShouldBuildServicePro services.AddHttpClient("Test") .AddAuthorizationInterceptorHandler(options => { - options.UseCustomInterceptor(); + options.UseCustomInterceptor(func => func.AddSingleton()); }); // Act From a075843aaf58562d636148b12ac29c3216965294 Mon Sep 17 00:00:00 2001 From: Luiz Adolfo Date: Tue, 18 Mar 2025 14:08:54 -0300 Subject: [PATCH 2/5] Updated hybrid cache project .net target --- .../AuthorizationInterceptor.Extensions.HybridCache.csproj | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj b/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj index 58fe057..7204589 100644 --- a/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj +++ b/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj @@ -1,4 +1,7 @@  + + net8.0;net9.0 + From ca780688c6aaa5fba8b57df20e3c85bee42232fd Mon Sep 17 00:00:00 2001 From: Luiz Adolfo Date: Tue, 18 Mar 2025 14:13:50 -0300 Subject: [PATCH 3/5] Refactor logging and update CI configuration --- .github/workflows/main.yml | 4 ++-- ...onInterceptor.Extensions.HybridCache.csproj | 3 --- .../AuthorizationInterceptorHandler.cs | 2 +- .../AuthorizationInterceptorStrategy.cs | 2 +- .../AuthorizationInterceptorLogDefinitions.cs | 18 ++++++++++++++++++ .../AuthorizationInterceptorHandlerTests.cs | 2 +- 6 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 src/AuthorizationInterceptor/Utils/AuthorizationInterceptorLogDefinitions.cs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 941d800..74e79bb 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,9 +2,9 @@ name: Main on: push: - branches: [ main, feature/*, hotfix/*, release/* ] + branches: [ main ] pull_request: - branches: [ main, feature/*, hotfix/*, release/* ] + branches: [ feature/*, hotfix/*, release/* ] jobs: diff --git a/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj b/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj index 7204589..58fe057 100644 --- a/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj +++ b/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj @@ -1,7 +1,4 @@  - - net8.0;net9.0 - diff --git a/src/AuthorizationInterceptor/Handlers/AuthorizationInterceptorHandler.cs b/src/AuthorizationInterceptor/Handlers/AuthorizationInterceptorHandler.cs index 6c14319..2537785 100644 --- a/src/AuthorizationInterceptor/Handlers/AuthorizationInterceptorHandler.cs +++ b/src/AuthorizationInterceptor/Handlers/AuthorizationInterceptorHandler.cs @@ -1,7 +1,7 @@ using AuthorizationInterceptor.Extensions.Abstractions.Handlers; using AuthorizationInterceptor.Extensions.Abstractions.Headers; -using AuthorizationInterceptor.Log; using AuthorizationInterceptor.Strategies; +using AuthorizationInterceptor.Utils; using Microsoft.Extensions.Logging; namespace AuthorizationInterceptor.Handlers; diff --git a/src/AuthorizationInterceptor/Strategies/AuthorizationInterceptorStrategy.cs b/src/AuthorizationInterceptor/Strategies/AuthorizationInterceptorStrategy.cs index 59c1b06..b72cf1f 100644 --- a/src/AuthorizationInterceptor/Strategies/AuthorizationInterceptorStrategy.cs +++ b/src/AuthorizationInterceptor/Strategies/AuthorizationInterceptorStrategy.cs @@ -1,7 +1,7 @@ using AuthorizationInterceptor.Extensions.Abstractions.Handlers; using AuthorizationInterceptor.Extensions.Abstractions.Headers; using AuthorizationInterceptor.Extensions.Abstractions.Interceptors; -using AuthorizationInterceptor.Log; +using AuthorizationInterceptor.Utils; using Microsoft.Extensions.Logging; namespace AuthorizationInterceptor.Strategies; diff --git a/src/AuthorizationInterceptor/Utils/AuthorizationInterceptorLogDefinitions.cs b/src/AuthorizationInterceptor/Utils/AuthorizationInterceptorLogDefinitions.cs new file mode 100644 index 0000000..34c58b3 --- /dev/null +++ b/src/AuthorizationInterceptor/Utils/AuthorizationInterceptorLogDefinitions.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.Logging; +using System.Diagnostics.CodeAnalysis; + +namespace AuthorizationInterceptor.Utils; + +[ExcludeFromCodeCoverage] +public static partial class AuthorizationInterceptorLogDefinitions +{ + [LoggerMessage(EventId = 1, Level = LogLevel.Warning, Message = "No interceptor was configured for HttpClient `{httpClientName}`. A Runtime interceptor was used instead. It is recommended to use at least the MemoryCache interceptor.")] + public static partial void LogNoInterceptorUsed(this ILogger logger, string httpClientName); + + [LoggerMessage(EventId = 2, Level = LogLevel.Warning, Message = "AuthorizationInterceptor is not available for synchronous requests. Consider using asynchronous requests!")] + public static partial void LogUnavailableForSyncRequests(this ILogger logger); + + [LoggerMessage(EventId = 3, Level = LogLevel.Warning, Message = "Operation canceled while getting headers from interceptor `{interceptor}` with integration `{httpClientName}`")] + public static partial void LogOperationCanceledInInterceptor(this ILogger logger, string interceptor, string httpClientName); +} + diff --git a/tests/AuthorizationInterceptor.Tests/Handlers/AuthorizationInterceptorHandlerTests.cs b/tests/AuthorizationInterceptor.Tests/Handlers/AuthorizationInterceptorHandlerTests.cs index 60d9126..beb9f77 100644 --- a/tests/AuthorizationInterceptor.Tests/Handlers/AuthorizationInterceptorHandlerTests.cs +++ b/tests/AuthorizationInterceptor.Tests/Handlers/AuthorizationInterceptorHandlerTests.cs @@ -1,9 +1,9 @@ using AuthorizationInterceptor.Extensions.Abstractions.Handlers; using AuthorizationInterceptor.Extensions.Abstractions.Headers; using AuthorizationInterceptor.Handlers; -using AuthorizationInterceptor.Log; using AuthorizationInterceptor.Strategies; using AuthorizationInterceptor.Tests.Utils; +using AuthorizationInterceptor.Utils; using Microsoft.Extensions.Logging; namespace AuthorizationInterceptor.Tests.Handlers; From a77b680fb1a2c24637dc10d1bcd6b65988d71333 Mon Sep 17 00:00:00 2001 From: Luiz Adolfo Date: Tue, 18 Mar 2025 14:18:14 -0300 Subject: [PATCH 4/5] Update hybrid cache project to target .NET 8.0 and 9.0 Update github worflow --- .github/workflows/main.yml | 8 ++++++-- ...AuthorizationInterceptor.Extensions.HybridCache.csproj | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 74e79bb..46a3d27 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -2,9 +2,13 @@ name: Main on: push: - branches: [ main ] + branches: + - main pull_request: - branches: [ feature/*, hotfix/*, release/* ] + branches: + - feature/* + - hotfix/* + - release/* jobs: diff --git a/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj b/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj index 58fe057..7204589 100644 --- a/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj +++ b/src/AuthorizationInterceptor.Extensions.HybridCache/AuthorizationInterceptor.Extensions.HybridCache.csproj @@ -1,4 +1,7 @@  + + net8.0;net9.0 + From f9e09b493cb9324ec4f578d3fd3637ea85d645fe Mon Sep 17 00:00:00 2001 From: Luiz Adolfo Date: Tue, 18 Mar 2025 14:21:15 -0300 Subject: [PATCH 5/5] Update pull request branches in main.yml --- .github/workflows/main.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 46a3d27..5d5d857 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,9 +6,7 @@ on: - main pull_request: branches: - - feature/* - - hotfix/* - - release/* + - main jobs: