Skip to content

Commit 2a7e08d

Browse files
committed
add claims to validator, improve register methods, .net 9.0
1 parent f498e6f commit 2a7e08d

File tree

12 files changed

+86
-17
lines changed

12 files changed

+86
-17
lines changed

.github/workflows/dotnet.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ jobs:
3333
- name: Install .NET Core
3434
uses: actions/setup-dotnet@v4
3535
with:
36-
dotnet-version: |
37-
6.0.x
38-
7.0.x
39-
8.0.x
36+
dotnet-version: 9.x
4037

4138
- name: Restore Dependencies
4239
run: dotnet restore

samples/Sample.Controllers/Sample.Controllers.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>

samples/Sample.Middleware/Sample.Middleware.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.11" />
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
1111
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
1212
</ItemGroup>
1313

samples/Sample.MinimalApi/MinimalApi.http

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ X-API-KEY: 01HSGVBSF99SK6XMJQJYF0X3WQ
2323
GET {{HostAddress}}/config-debugger/
2424
Accept: application/json
2525
X-API-KEY: 01HSGVBSF99SK6XMJQJYF0X3WQ
26+
27+
###
28+
29+
GET {{HostAddress}}/current/
30+
Accept: application/json
31+
X-API-KEY: 01HSGVBSF99SK6XMJQJYF0X3WQ

samples/Sample.MinimalApi/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Security.Claims;
2+
13
using AspNetCore.SecurityKey;
24

35
using Sample.Shared;
@@ -45,6 +47,14 @@ public static void Main(string[] args)
4547
.WithOpenApi()
4648
.RequireAuthorization();
4749

50+
app.MapGet("/current", (ClaimsPrincipal? principal) =>
51+
{
52+
var p = principal;
53+
return WeatherFaker.Instance.Generate(5);
54+
})
55+
.WithName("GetCurrentUser")
56+
.WithOpenApi();
57+
4858
app.Run();
4959
}
5060
}

samples/Sample.MinimalApi/Sample.MinimalApi.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.11" />
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
1111
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
1212
</ItemGroup>
1313

samples/Sample.Shared/Sample.Shared.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>

src/AspNetCore.SecurityKey/AspNetCore.SecurityKey.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
4+
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0</TargetFrameworks>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>

src/AspNetCore.SecurityKey/DependencyInjectionExtensions.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,47 @@ public static class DependencyInjectionExtensions
1818
/// The <see cref="IServiceCollection"/> so that additional calls can be chained.
1919
/// </returns>
2020
public static IServiceCollection AddSecurityKey(this IServiceCollection services, Action<SecurityKeyOptions>? configure = null)
21+
{
22+
return AddSecurityKey<SecurityKeyValidator, SecurityKeyExtractor>(services, configure);
23+
}
24+
25+
/// <summary>
26+
/// Adds the security API key services to the specified <see cref="IServiceCollection" />.
27+
/// </summary>
28+
/// <typeparam name="TValidator">The type for validating the security API key.</typeparam>
29+
/// <param name="services">The <see cref="IServiceCollection" /> to add services.</param>
30+
/// <param name="configure">An action delegate to configure the provided <see cref="SecurityKeyOptions" />.</param>
31+
/// <returns>
32+
/// The <see cref="IServiceCollection" /> so that additional calls can be chained.
33+
/// </returns>
34+
public static IServiceCollection AddSecurityKey<TValidator>(this IServiceCollection services, Action<SecurityKeyOptions>? configure = null)
35+
where TValidator : class, ISecurityKeyValidator
36+
{
37+
return AddSecurityKey<TValidator, SecurityKeyExtractor>(services, configure);
38+
}
39+
40+
/// <summary>
41+
/// Adds the security API key services to the specified <see cref="IServiceCollection" />.
42+
/// </summary>
43+
/// <typeparam name="TValidator">The type for validating the security API key.</typeparam>
44+
/// <typeparam name="TExtractor">The type for extracting the security API key.</typeparam>
45+
/// <param name="services">The <see cref="IServiceCollection" /> to add services.</param>
46+
/// <param name="configure">An action delegate to configure the provided <see cref="SecurityKeyOptions" />.</param>
47+
/// <returns>
48+
/// The <see cref="IServiceCollection" /> so that additional calls can be chained.
49+
/// </returns>
50+
public static IServiceCollection AddSecurityKey<TValidator, TExtractor>(this IServiceCollection services, Action<SecurityKeyOptions>? configure = null)
51+
where TValidator : class, ISecurityKeyValidator
52+
where TExtractor : class, ISecurityKeyExtractor
2153
{
2254
services.AddHttpContextAccessor();
2355

2456
services.AddOptions<SecurityKeyOptions>();
2557
if (configure != null)
2658
services.Configure(configure);
2759

28-
services.TryAddSingleton<ISecurityKeyExtractor, SecurityKeyExtractor>();
29-
services.TryAddSingleton<ISecurityKeyValidator, SecurityKeyValidator>();
60+
services.TryAddSingleton<ISecurityKeyExtractor, TExtractor>();
61+
services.TryAddSingleton<ISecurityKeyValidator, TValidator>();
3062

3163
// used by SecurityKeyAttribute
3264
services.TryAddSingleton<SecurityKeyAuthorizationFilter>();

src/AspNetCore.SecurityKey/ISecurityKeyValidator.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Security.Claims;
2+
13
namespace AspNetCore.SecurityKey;
24

35
/// <summary>
@@ -11,4 +13,14 @@ public interface ISecurityKeyValidator
1113
/// <param name="value">The security API key to validate.</param>
1214
/// <returns>true if security API key is valid; otherwise false</returns>
1315
bool Validate(string? value);
16+
17+
/// <summary>
18+
/// Validates the specified security API key.
19+
/// </summary>
20+
/// <param name="value">The security API key to validate.</param>
21+
/// <param name="claims">The claims associated with the security API key.</param>
22+
/// <returns>
23+
/// true if security API key is valid; otherwise false
24+
/// </returns>
25+
bool Validate(string? value, out Claim[] claims);
1426
}

0 commit comments

Comments
 (0)