Skip to content

[release/9.0] Fix loading dotnet user-jwts config #59473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void Configure(string? name, JwtBearerOptions options)
ValidAudiences = audiences,
ValidAudience = audience,
ValidateIssuerSigningKey = true,
IssuerSigningKeys = GetIssuerSigningKeys(configSection, issuers),
IssuerSigningKeys = GetIssuerSigningKeys(configSection, [issuer, ..issuers]),
};
}

Expand Down
8 changes: 8 additions & 0 deletions src/Security/Authentication/test/JwtBearerTests_Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ public async Task ExpirationAndIssuedWhenMinOrMaxValue()
public void CanReadJwtBearerOptionsFromConfig()
{
var services = new ServiceCollection();
var key = "qPG6tDtfxFYZifHW3sEueQ==";
var config = new ConfigurationBuilder().AddInMemoryCollection([
new("Authentication:Schemes:Bearer:ValidIssuer", "dotnet-user-jwts"),
new("Authentication:Schemes:Bearer:ValidIssuers:0", "dotnet-user-jwts-2"),
Expand All @@ -965,6 +966,9 @@ public void CanReadJwtBearerOptionsFromConfig()
new("Authentication:Schemes:Bearer:BackchannelTimeout", "00:01:00"),
new("Authentication:Schemes:Bearer:RequireHttpsMetadata", "false"),
new("Authentication:Schemes:Bearer:SaveToken", "True"),
new("Authentication:Schemes:Bearer:SigningKeys:0:Issuer", "dotnet-user-jwts"),
new("Authentication:Schemes:Bearer:SigningKeys:0:Value", key),
new("Authentication:Schemes:Bearer:SigningKeys:0:Length", "32"),
]).Build();
services.AddSingleton<IConfiguration>(config);

Expand All @@ -987,6 +991,10 @@ public void CanReadJwtBearerOptionsFromConfig()
Assert.True(jwtBearerOptions.MapInboundClaims);
Assert.True(jwtBearerOptions.TokenValidationParameters.ValidateIssuer);
Assert.True(jwtBearerOptions.TokenValidationParameters.ValidateAudience);

var securityKey = Assert.Single(jwtBearerOptions.TokenValidationParameters.IssuerSigningKeys);
var symmetricKey = Assert.IsType<SymmetricSecurityKey>(securityKey);
Assert.Equal(key, Convert.ToBase64String(symmetricKey.Key));
}

[Fact]
Expand Down
5 changes: 3 additions & 2 deletions src/Tools/Tools.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"src\\Hosting\\Abstractions\\src\\Microsoft.AspNetCore.Hosting.Abstractions.csproj",
"src\\Hosting\\Hosting\\src\\Microsoft.AspNetCore.Hosting.csproj",
"src\\Hosting\\Server.Abstractions\\src\\Microsoft.AspNetCore.Hosting.Server.Abstractions.csproj",
"src\\Hosting\\TestHost\\src\\Microsoft.AspNetCore.TestHost.csproj",
"src\\Html.Abstractions\\src\\Microsoft.AspNetCore.Html.Abstractions.csproj",
"src\\Http\\Authentication.Abstractions\\src\\Microsoft.AspNetCore.Authentication.Abstractions.csproj",
"src\\Http\\Authentication.Core\\src\\Microsoft.AspNetCore.Authentication.Core.csproj",
Expand Down Expand Up @@ -109,9 +110,9 @@
"src\\Tools\\Extensions.ApiDescription.Server\\src\\Microsoft.Extensions.ApiDescription.Server.csproj",
"src\\Tools\\FirstRunCertGenerator\\src\\Microsoft.AspNetCore.DeveloperCertificates.XPlat.csproj",
"src\\Tools\\FirstRunCertGenerator\\test\\Microsoft.AspNetCore.DeveloperCertificates.XPlat.Tests.csproj",
"src\\Tools\\GetDocumentInsider\\sample\\GetDocumentSample.csproj",
"src\\Tools\\GetDocumentInsider\\src\\GetDocument.Insider.csproj",
"src\\Tools\\GetDocumentInsider\\tests\\GetDocumentInsider.Tests.csproj",
"src\\Tools\\GetDocumentInsider\\sample\\GetDocumentSample.csproj",
"src\\Tools\\LinkabilityChecker\\LinkabilityChecker.csproj",
"src\\Tools\\Microsoft.dotnet-openapi\\src\\Microsoft.dotnet-openapi.csproj",
"src\\Tools\\Microsoft.dotnet-openapi\\test\\dotnet-microsoft.openapi.Tests.csproj",
Expand All @@ -125,4 +126,4 @@
"src\\WebEncoders\\src\\Microsoft.Extensions.WebEncoders.csproj"
]
}
}
}
45 changes: 41 additions & 4 deletions src/Tools/dotnet-user-jwts/test/UserJwtsTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.UserSecrets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Tools.Internal;
using Xunit.Abstractions;
using System.Text.RegularExpressions;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.IdentityModel.Tokens.Jwt;

namespace Microsoft.AspNetCore.Authentication.JwtBearer.Tools.Tests;

Expand Down Expand Up @@ -62,6 +67,38 @@ public void Create_WritesGeneratedTokenToDisk()
Assert.Contains("dotnet-user-jwts", File.ReadAllText(appsettings));
}

[Fact]
public async Task Create_TokenAcceptedByJwtBearerHandler()
{
var project = Path.Combine(fixture.CreateProject(), "TestProject.csproj");
var appsettings = Path.Combine(Path.GetDirectoryName(project), "appsettings.Development.json");
var secrets = PathHelper.GetSecretsPathFromSecretsId(fixture.TestSecretsId);
var app = new Program(_console);

app.Run(["create", "--project", project, "-o", "token"]);
var token = _console.GetOutput().Trim();

var builder = WebApplication.CreateEmptyBuilder(new());
builder.WebHost.UseTestServer();

builder.Configuration.AddJsonFile(appsettings);
builder.Configuration.AddJsonFile(secrets);

builder.Services.AddRouting();
builder.Services.AddAuthentication().AddJwtBearer();
builder.Services.AddAuthorization();

using var webApp = builder.Build();
webApp.MapGet("/secret", (ClaimsPrincipal user) => $"Hello {user.Identity?.Name}!")
.RequireAuthorization();

await webApp.StartAsync();

var client = webApp.GetTestClient();
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
Assert.Equal($"Hello {Environment.UserName}!", await client.GetStringAsync("/secret"));
}

[Fact]
public void Create_CanModifyExistingScheme()
{
Expand Down
6 changes: 6 additions & 0 deletions src/Tools/dotnet-user-jwts/test/dotnet-user-jwts.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@
<ProjectReference Include="..\src\dotnet-user-jwts.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="Microsoft.AspNetCore" />
<Reference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<Reference Include="Microsoft.AspNetCore.TestHost" />
</ItemGroup>

</Project>
Loading