Skip to content

Commit cef706c

Browse files
authored
Merge pull request #45 from WeihanLi/dev
2.1.0
2 parents cb0be50 + 13b4268 commit cef706c

27 files changed

+203
-71
lines changed

Directory.Build.props

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<Project>
2-
<PropertyGroup>
3-
<VersionMajor>2</VersionMajor>
4-
<VersionMinor>0</VersionMinor>
5-
<VersionPatch>0</VersionPatch>
6-
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
7-
<VersionSuffix Condition="'$(Configuration)'=='DEBUG'">develop</VersionSuffix>
8-
<InformationalVersion>$(PackageVersion)</InformationalVersion>
9-
</PropertyGroup>
2+
<Import Project="./build/version.props"/>
103
<PropertyGroup>
114
<LangVersion>latest</LangVersion>
125
<ImplicitUsings>enable</ImplicitUsings>

Directory.Packages.props

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22
<PropertyGroup>
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
<CentralPackageFloatingVersionsEnabled>true</CentralPackageFloatingVersionsEnabled>
5+
<!-- https://learn.microsoft.com/en-us/nuget/concepts/auditing-packages -->
6+
<NuGetAudit>true</NuGetAudit>
7+
<NuGetAuditMode>all</NuGetAuditMode>
8+
<!-- https://learn.microsoft.com/en-us/nuget/reference/errors-and-warnings/nu1901-nu1904 -->
9+
<WarningsAsErrors>NU1901;NU1902;NU1903;NU1904</WarningsAsErrors>
510
</PropertyGroup>
611
<ItemGroup>
712
<PackageVersion Condition="'$(TargetFramework)'=='net8.0'" Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.11" />
813
<PackageVersion Condition="'$(TargetFramework)'=='net9.0'" Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.0" />
914
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
10-
<PackageVersion Include="Scalar.AspNetCore" Version="1.2.36" />
11-
<PackageVersion Include="WeihanLi.Common" Version="1.0.72" />
15+
<PackageVersion Include="Scalar.AspNetCore" Version="1.2.72" />
16+
<PackageVersion Include="WeihanLi.Common" Version="1.0.74" />
1217
</ItemGroup>
1318
<ItemGroup>
14-
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
19+
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
1520
<PackageVersion Include="xunit" Version="2.9.2" />
1621
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
1722
</ItemGroup>
18-
</Project>
23+
</Project>

build/sign.props

Lines changed: 0 additions & 7 deletions
This file was deleted.

build/version.props

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project>
2+
<PropertyGroup>
3+
<VersionMajor>2</VersionMajor>
4+
<VersionMinor>1</VersionMinor>
5+
<VersionPatch>0</VersionPatch>
6+
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
7+
<VersionSuffix Condition="'$(Configuration)'=='DEBUG'">develop</VersionSuffix>
8+
<InformationalVersion>$(PackageVersion)</InformationalVersion>
9+
</PropertyGroup>
10+
</Project>

docs/ReleaseNotes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Release Notes
22

3+
see pull requests: https://github.com/WeihanLi/WeihanLi.Web.Extensions/pulls?q=is%3Apr+is%3Aclosed+is%3Amerged
4+
35
## 1.4.0
46

57
- Add Basic auth support

samples/WeihanLi.Web.Extensions.Samples/Program.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.IdentityModel.Tokens;
55
using Scalar.AspNetCore;
6+
using System.Security.Claims;
67
using System.Text.Json.Serialization;
78
using WeihanLi.Common.Aspect;
89
using WeihanLi.Common.Models;
@@ -18,7 +19,7 @@
1819

1920
var builder = WebApplication.CreateBuilder(args);
2021

21-
builder.Services.AddAuthentication(HeaderAuthenticationDefaults.AuthenticationSchema)
22+
builder.Services.AddAuthentication(HeaderAuthenticationDefaults.AuthenticationScheme)
2223
.AddJwtBearer()
2324
.AddBasic(options =>
2425
{
@@ -39,6 +40,19 @@
3940
options.ApiKeyName = "X-ApiKey";
4041
options.KeyLocation = KeyLocation.HeaderOrQuery;
4142
})
43+
.AddDelegate(options =>
44+
{
45+
options.Validator = c => (c.Request.Headers.TryGetValue("x-delegate-key", out var values) && values.ToString().Equals("test"))
46+
.WrapTask();
47+
options.ClaimsGenerator = c =>
48+
{
49+
Claim[] claims =
50+
[
51+
new (ClaimTypes.Name, "test")
52+
];
53+
return Task.FromResult<IReadOnlyCollection<Claim>>(claims);
54+
};
55+
})
4256
;
4357
builder.Services.AddJwtServiceWithJwtBearerAuth(options =>
4458
{

samples/WeihanLi.Web.Extensions.Samples/ValuesController.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using WeihanLi.Common.Models;
1010
using WeihanLi.Common.Services;
1111
using WeihanLi.Web.Authentication.ApiKeyAuthentication;
12+
using WeihanLi.Web.Authentication.DelegateAuthentication;
1213
using WeihanLi.Web.Authentication.HeaderAuthentication;
1314
using WeihanLi.Web.Authentication.QueryAuthentication;
1415
using WeihanLi.Web.Authorization.Token;
@@ -40,9 +41,10 @@ public IActionResult ServiceScopeTest()
4041
[HttpGet]
4142
public async Task<IActionResult> Get([FromServices] IUserIdProvider userIdProvider)
4243
{
43-
var headerAuthResult = await HttpContext.AuthenticateAsync(HeaderAuthenticationDefaults.AuthenticationSchema);
44-
var queryAuthResult = await HttpContext.AuthenticateAsync(QueryAuthenticationDefaults.AuthenticationSchema);
45-
var apiKeyAuthResult = await HttpContext.AuthenticateAsync(ApiKeyAuthenticationDefaults.AuthenticationSchema);
44+
var headerAuthResult = await HttpContext.AuthenticateAsync(HeaderAuthenticationDefaults.AuthenticationScheme);
45+
var queryAuthResult = await HttpContext.AuthenticateAsync(QueryAuthenticationDefaults.AuthenticationScheme);
46+
var apiKeyAuthResult = await HttpContext.AuthenticateAsync(ApiKeyAuthenticationDefaults.AuthenticationScheme);
47+
var delegateAuthResult = await HttpContext.AuthenticateAsync(DelegateAuthenticationDefaults.AuthenticationScheme);
4648
var bearerAuthResult = await HttpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);
4749

4850
return Ok(new
@@ -52,12 +54,13 @@ public async Task<IActionResult> Get([FromServices] IUserIdProvider userIdProvid
5254
headerAuthResult = headerAuthResult.Principal?.Identity,
5355
queryAuthResult = queryAuthResult.Principal?.Identity,
5456
apiKeyAuthResult = apiKeyAuthResult.Principal?.Identity,
55-
bearerAuthResult = bearerAuthResult.Principal?.Identity
57+
bearerAuthResult = bearerAuthResult.Principal?.Identity,
58+
delegateAuthResult = delegateAuthResult.Principal?.Identity,
5659
});
5760
}
5861

5962
[HttpGet("apiKeyTest")]
60-
[Authorize(AuthenticationSchemes = ApiKeyAuthenticationDefaults.AuthenticationSchema)]
63+
[Authorize(AuthenticationSchemes = ApiKeyAuthenticationDefaults.AuthenticationScheme)]
6164
public IActionResult ApiKeyAuthTest()
6265
{
6366
return Ok(User.Identity);

src/Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
3-
<Import Project="../build/sign.props" />
4-
<PropertyGroup>
3+
<PropertyGroup>
4+
<NuGetAuditMode>direct</NuGetAuditMode>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
<NoWarn>$(NoWarn);1591</NoWarn>
77
<PackageReadmeFile>README.md</PackageReadmeFile>

src/WeihanLi.Web.Extensions/Authentication/ApiKeyAuthentication/ApiKeyAuthenticationDefaults.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ namespace WeihanLi.Web.Authentication.ApiKeyAuthentication;
55

66
public static class ApiKeyAuthenticationDefaults
77
{
8-
public const string AuthenticationSchema = "ApiKey";
8+
public const string AuthenticationScheme = "ApiKey";
99
}

src/WeihanLi.Web.Extensions/Authentication/ApiKeyAuthentication/ApiKeyAuthenticationHandler.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
3434

3535
var claims = new[]
3636
{
37-
new Claim("issuer", ClaimsIssuer),
38-
}.Union(Options.ClaimsGenerator?.Invoke(Context, Options) ?? []);
37+
new Claim("issuer", ClaimsIssuer)
38+
};
39+
40+
if (Options.ClaimsGenerator != null)
41+
{
42+
var generatedClaims = await Options.ClaimsGenerator.Invoke(Context, Options);
43+
if (generatedClaims is { Count: > 0 })
44+
{
45+
claims = claims.Union(generatedClaims).ToArray();
46+
}
47+
}
48+
3949
return AuthenticateResult.Success(
4050
new AuthenticationTicket(
4151
new ClaimsPrincipal([

0 commit comments

Comments
 (0)