Skip to content

Commit

Permalink
moved towards GeneratedRegex, applied FeatureFlags within BooksModule
Browse files Browse the repository at this point in the history
  • Loading branch information
lkurzyniec committed Jan 6, 2025
1 parent a564610 commit 4986322
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 49 deletions.
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageVersion Include="AspNetCore.HealthChecks.SqlServer" Version="8.0.2" />
<PackageVersion Include="AspNetCore.HealthChecks.UI.Client" Version="8.0.1" />
<PackageVersion Include="Microsoft.FeatureManagement.AspNetCore" Version="4.0.0" />
<PackageVersion Include="Microsoft.FeatureManagement" Version="4.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.3" />
<PackageVersion Include="Serilog.Enrichers.Environment" Version="3.0.1" />
Expand Down Expand Up @@ -72,4 +73,4 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</GlobalPackageReference>
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

namespace HappyCode.NetCoreBoilerplate.Api.Infrastructure.Filters
{
public class ApiKeyAuthorizationFilter : IAsyncAuthorizationFilter
public partial class ApiKeyAuthorizationFilter : IAsyncAuthorizationFilter
{
private static readonly Regex _apiKeyRegex = new Regex(@"^[Aa][Pp][Ii][Kk][Ee][Yy]\s+(?<ApiKey>.+)$", RegexOptions.Compiled);
[GeneratedRegex(@"^[Aa][Pp][Ii][Kk][Ee][Yy]\s+(?<ApiKey>.+)$")]
private static partial Regex ApiKeyRegex();

private readonly IOptions<ApiKeySettings> _options;
private readonly IFeatureManager _featureManager;
Expand All @@ -25,10 +26,11 @@ public ApiKeyAuthorizationFilter(IOptions<ApiKeySettings> options, IFeatureManag

public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
{
if (!(await _featureManager.IsEnabledAsync(FeatureFlags.ApiKey.ToString())))
if (!await _featureManager.IsEnabledAsync(FeatureFlags.ApiKey.ToString()))
{
return;
}

bool hasAllowAnonymous = context.ActionDescriptor.EndpointMetadata.OfType<IAllowAnonymous>().Any();
if (hasAllowAnonymous)
{
Expand All @@ -48,7 +50,7 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext context)
return;
}

var match = _apiKeyRegex.Match(authorization);
var match = ApiKeyRegex().Match(authorization);
if (!match.Success)
{
context.Result = new UnauthorizedObjectResult("ApiKey Authorization header value not match `ApiKey xxx-xxx`");
Expand Down
2 changes: 1 addition & 1 deletion src/HappyCode.NetCoreBoilerplate.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public virtual void ConfigureServices(IServiceCollection services)

var healthChecksBuilder = services.AddHealthChecks()
.AddBooksModule(_configuration);
if (_configuration.GetValue<bool>("FeatureManagement:DockerCompose"))
if (_configuration.GetValue<bool>($"FeatureManagement:{FeatureFlags.DockerCompose}"))
{
healthChecksBuilder
.AddMySql(_configuration.GetConnectionString("MySqlDb"), tags: ["ready"])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using System.Diagnostics.CodeAnalysis;
using HappyCode.NetCoreBoilerplate.BooksModule.Features.DeleteBook;
using HappyCode.NetCoreBoilerplate.BooksModule.Features.GetBook;
using HappyCode.NetCoreBoilerplate.BooksModule.Features.GetBooks;
Expand All @@ -13,6 +14,7 @@

namespace HappyCode.NetCoreBoilerplate.BooksModule;

[ExcludeFromCodeCoverage]
public static class BooksModuleConfigurations
{
public static IServiceCollection AddBooksModule(this IServiceCollection services, IConfiguration configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
<PackageReference Include="AspNetCore.HealthChecks.Sqlite" />
<PackageReference Include="Dapper" />
<PackageReference Include="Microsoft.Data.Sqlite" />
</ItemGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage" />
<PackageReference Include="Microsoft.FeatureManagement" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.FeatureManagement;

namespace HappyCode.NetCoreBoilerplate.BooksModule.Infrastructure
{
internal class AuthFilter : IEndpointFilter
internal partial class AuthFilter : IEndpointFilter
{
[GeneratedRegex(@"APIKEY\s+", RegexOptions.IgnoreCase)]
private static partial Regex ApiKeyRegex();

private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _env;
private readonly IFeatureManager _featureManager;

public AuthFilter(IConfiguration configuration, IWebHostEnvironment env)
public AuthFilter(IConfiguration configuration, IFeatureManager featureManager)
{
_configuration = configuration;
_env = env;
_featureManager = featureManager;
}

public async ValueTask<object> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next)
{
if (!_env.IsProduction())
if (!await _featureManager.IsEnabledAsync("ApiKey"))
{
return await next(context);
}
Expand All @@ -32,7 +34,7 @@ public async ValueTask<object> InvokeAsync(EndpointFilterInvocationContext conte
return Results.Unauthorized();
}

var key = Regex.Replace(authorization, @"APIKEY\s+", string.Empty, RegexOptions.IgnoreCase);
var key = ApiKeyRegex().Replace(authorization, string.Empty);

var secretKey = _configuration.GetValue<string>("ApiKey:SecretKey");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Data;
using System.Diagnostics.CodeAnalysis;
using Dapper;
using HappyCode.NetCoreBoilerplate.BooksModule.Dtos;

namespace HappyCode.NetCoreBoilerplate.BooksModule.Infrastructure;

[ExcludeFromCodeCoverage]
internal class DbInitializer
{
private static readonly string _createBooks = @$"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Data;
using HappyCode.NetCoreBoilerplate.BooksModule.IntegrationTests.Infrastructure.DataFeeders;
using Microsoft.AspNetCore.TestHost;
using Microsoft.FeatureManagement;

namespace HappyCode.NetCoreBoilerplate.BooksModule.IntegrationTests.Infrastructure
{
Expand All @@ -21,6 +22,7 @@ public TestServerClientFixture()
.AddInMemoryCollection(new Dictionary<string, string>(1) { { "ConnectionStrings:SqliteDb", $"Data Source=tests_tempdb_{DateTimeOffset.Now.ToUnixTimeSeconds()}.db" } })
.Build();
services.AddRouting();
services.AddFeatureManagement();
services.AddBooksModule(config);
})
.Configure(app =>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"FeatureManagement": {
"ApiKey": false
}
}

0 comments on commit 4986322

Please sign in to comment.