diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..808f9eb --- /dev/null +++ b/.editorconfig @@ -0,0 +1,18 @@ +root = true + +[*] +charset = utf-8 +end_of_line = crlf +insert_final_newline = true +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true + +[*.{cs,csproj,props,targets,sln}] +charset = utf-8-bom + +[*.{json,yml,yaml}] +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..aac1598 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,23 @@ +version: 2 +updates: + - package-ecosystem: nuget + directory: / + schedule: + interval: weekly + open-pull-requests-limit: 10 + commit-message: + prefix: chore + include: scope + groups: + tests: + patterns: ["xunit*", "AwesomeAssertions"] + ignore: + # Never reintroduce FluentAssertions (commercial license since v8) + - dependency-name: "FluentAssertions" + + - package-ecosystem: github-actions + directory: / + schedule: + interval: weekly + commit-message: + prefix: ci diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml index fa48498..2480efe 100644 --- a/.github/workflows/publish-package.yml +++ b/.github/workflows/publish-package.yml @@ -3,12 +3,23 @@ name: Master - Publish packages on: push: branches: [ master ] + workflow_dispatch: + inputs: + version: + description: Existing release to publish again (e.g. 10.0.0). Builds tag v and pushes it to nuget.org. + required: true env: CURRENT_REPO_URL: https://github.com/${{ github.repository }} +permissions: + contents: write + issues: write + pull-requests: write + id-token: write # NuGet trusted publishing (OIDC) + jobs: - build: + publish: runs-on: ubuntu-latest defaults: run: @@ -16,42 +27,58 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 - - - name: Setup .NET 8 - uses: actions/setup-dotnet@v4 + uses: actions/checkout@v7 with: - dotnet-version: 8.0.x + fetch-depth: 0 + ref: ${{ inputs.version && format('v{0}', inputs.version) || github.sha }} - - name: Setup .NET 9 - uses: actions/setup-dotnet@v4 + - name: Setup .NET + uses: actions/setup-dotnet@v6 with: - dotnet-version: 9.0.x + global-json-file: global.json - - name: Setup .NET 10 - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 10.0.x - - name: Restore dependencies - run: dotnet restore + run: dotnet restore - name: Build - run: dotnet build --no-restore + run: dotnet build --no-restore -c Release + + - name: Test + run: dotnet test --no-build -c Release - name: Semantic Release id: semantic - uses: cycjimmy/semantic-release-action@v3 + if: github.event_name == 'push' + uses: cycjimmy/semantic-release-action@v4 with: - semantic_version: 19.0.5 + semantic_version: 24 extra_plugins: | - @semantic-release/changelog - @semantic-release/git + @semantic-release/changelog@6 + @semantic-release/git@10 + conventional-changelog-conventionalcommits@8 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Resolve version + id: version + if: github.event_name == 'workflow_dispatch' || steps.semantic.outputs.new_release_published == 'true' + env: + VERSION: ${{ inputs.version || steps.semantic.outputs.new_release_version }} + run: | + [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] || { echo "::error::Invalid version '$VERSION'"; exit 1; } + echo "value=$VERSION" >> "$GITHUB_OUTPUT" + - name: Generate Package - run: dotnet pack -c Release -o out -p:PackageVersion=${{ steps.semantic.outputs.new_release_version }} -p:RepositoryUrl=${{env.CURRENT_REPO_URL}} + if: steps.version.outputs.value != '' + run: dotnet pack -c Release -o out -p:Version=${{ steps.version.outputs.value }} -p:RepositoryUrl=${{ env.CURRENT_REPO_URL }} + + - name: NuGet login (trusted publishing) + id: nuget + if: steps.version.outputs.value != '' + uses: NuGet/login@v1 + with: + user: ${{ vars.NUGET_USER }} - name: Publish the package to nuget.org - run: dotnet nuget push ./out/*.nupkg -n -d -k ${{ secrets.NUGET_AUTH_TOKEN}} -s https://api.nuget.org/v3/index.json + if: steps.version.outputs.value != '' + run: dotnet nuget push ./out/*.nupkg --skip-duplicate -k ${{ steps.nuget.outputs.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 4ef1a6e..062eca6 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -1,9 +1,12 @@ -name: Pull Request Analisys +name: Pull Request Analysis on: pull_request: branches: [ master ] +permissions: + contents: read + jobs: build: runs-on: ubuntu-latest @@ -13,28 +16,25 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 - - - name: Setup .NET 8 - uses: actions/setup-dotnet@v4 + uses: actions/checkout@v7 with: - dotnet-version: 8.0.x + fetch-depth: 0 - - name: Setup .NET 9 - uses: actions/setup-dotnet@v4 + - name: Setup .NET + uses: actions/setup-dotnet@v6 with: - dotnet-version: 9.0.x + global-json-file: global.json - - name: Setup .NET 10 - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 10.0.x - - name: Restore dependencies run: dotnet restore - name: Build - run: dotnet build --no-restore + run: dotnet build --no-restore -c Release - name: Test - run: dotnet test --no-build + run: dotnet test --no-build -c Release + + - name: Check vulnerable packages + run: | + dotnet list package --vulnerable --include-transitive 2>&1 | tee vulnerable.txt + ! grep -q "has the following vulnerable packages" vulnerable.txt diff --git a/.releaserc b/.releaserc index ce9cb40..2ffccd3 100644 --- a/.releaserc +++ b/.releaserc @@ -1,11 +1,22 @@ { - "branches": ["master"], - - "plugins": [ - "@semantic-release/commit-analyzer", - "@semantic-release/release-notes-generator", - "@semantic-release/changelog", - "@semantic-release/github", - "@semantic-release/git" - ] -} \ No newline at end of file + "branches": [ + "master" + ], + "plugins": [ + [ + "@semantic-release/commit-analyzer", + { + "preset": "conventionalcommits" + } + ], + [ + "@semantic-release/release-notes-generator", + { + "preset": "conventionalcommits" + } + ], + "@semantic-release/changelog", + "@semantic-release/github", + "@semantic-release/git" + ] +} diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..a5c8809 --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,11 @@ + + + + latest + + true + all + $(WarningsAsErrors);NU1901;NU1902;NU1903;NU1904 + true + + diff --git a/Directory.Packages.props b/Directory.Packages.props new file mode 100644 index 0000000..00cea4a --- /dev/null +++ b/Directory.Packages.props @@ -0,0 +1,33 @@ + + + + true + true + + + + + + + + + + + + + + + + + + + + + + diff --git a/README.MD b/README.md similarity index 86% rename from README.MD rename to README.md index 3853dc4..6bacbdc 100644 --- a/README.MD +++ b/README.md @@ -24,6 +24,15 @@ If you liked the project or if NetDevPack helped you, please give a star ;) dotnet add package NetDevPack.Security.JwtExtensions ``` +### Compatibility + +| Version | Target frameworks | +| ------- | ----------------- | +| 10.x | .NET 10 (LTS) | +| 9.x | .NET Standard 2.1, .NET 8, .NET 9, .NET 10 | + +> **Breaking change in v10:** .NET Standard 2.1, .NET 8 and .NET 9 are no longer targeted. .NET 8 and .NET 9 are out of support since 2026-11-10, and the `netstandard2.1` target pulled ASP.NET Core 2.1 packages with known vulnerabilities. If you need them, stay on 9.x. The public API did not change. + Then use Extension at configuration in `ConfigureServices` method of your `Startup.cs`: ```csharp diff --git a/global.json b/global.json new file mode 100644 index 0000000..1d364c6 --- /dev/null +++ b/global.json @@ -0,0 +1,9 @@ +{ + "sdk": { + "version": "10.0.100", + "rollForward": "latestFeature" + }, + "test": { + "runner": "Microsoft.Testing.Platform" + } +} diff --git a/samples/NetDevPack.Security.JwtExtensions.ApiAuthenticator/NetDevPack.Security.JwtExtensions.ApiAuthenticator.csproj b/samples/NetDevPack.Security.JwtExtensions.ApiAuthenticator/NetDevPack.Security.JwtExtensions.ApiAuthenticator.csproj index a286aef..5b2eb32 100644 --- a/samples/NetDevPack.Security.JwtExtensions.ApiAuthenticator/NetDevPack.Security.JwtExtensions.ApiAuthenticator.csproj +++ b/samples/NetDevPack.Security.JwtExtensions.ApiAuthenticator/NetDevPack.Security.JwtExtensions.ApiAuthenticator.csproj @@ -1,25 +1,16 @@  - net8.0;net9.0;net10.0 + net10.0 + false - - - - - - - - - - - - - - - + + + + + diff --git a/samples/NetDevPack.Security.JwtExtensions.ApiClient/NetDevPack.Security.JwtExtensions.ApiClient.csproj b/samples/NetDevPack.Security.JwtExtensions.ApiClient/NetDevPack.Security.JwtExtensions.ApiClient.csproj index f738103..9255998 100644 --- a/samples/NetDevPack.Security.JwtExtensions.ApiClient/NetDevPack.Security.JwtExtensions.ApiClient.csproj +++ b/samples/NetDevPack.Security.JwtExtensions.ApiClient/NetDevPack.Security.JwtExtensions.ApiClient.csproj @@ -1,10 +1,10 @@ - + - net8.0;net9.0;net10.0 + net10.0 + false - diff --git a/src/NetDevPack.Security.JwtExtensions/NetDevPack.Security.JwtExtensions.csproj b/src/NetDevPack.Security.JwtExtensions/NetDevPack.Security.JwtExtensions.csproj index a0a7aa1..6b1c287 100644 --- a/src/NetDevPack.Security.JwtExtensions/NetDevPack.Security.JwtExtensions.csproj +++ b/src/NetDevPack.Security.JwtExtensions/NetDevPack.Security.JwtExtensions.csproj @@ -1,41 +1,37 @@ - + - - netstandard2.1;net8.0;net9.0;net10.0 - enable - enable - latest - NetDevPack.Security.JwtExtensions - 3.1.0 - brunobritodev - NetDevPack - JWKS URI Loader for JWT Validation - - A .NET library that simplifies consuming JSON Web Key Sets (JWKS) from external URIs - to validate JWT tokens using asymmetric keys. Reduces the complexity of handling remote key rotation. - - jwt;jwks;jwk;jwks-uri;token-validation;security;cryptography;key-rotation;dotnet - en - MIT - https://github.com/NetDevPack/Security.JwtExtensions - git - main - https://github.com/NetDevPack/Security.JwtExtensions - icon.png - README.md - true - true - snupkg - true + + net10.0 + enable + enable + NetDevPack.Security.JwtExtensions + 10.0.0 + brunobritodev + NetDevPack + JWKS URI Loader for JWT Validation + + A .NET library that simplifies consuming JSON Web Key Sets (JWKS) from external URIs + to validate JWT tokens using asymmetric keys. Reduces the complexity of handling remote key rotation. + + jwt;jwks;jwk;jwks-uri;token-validation;security;cryptography;key-rotation;dotnet + en + MIT + https://github.com/NetDevPack/Security.JwtExtensions + git + master + https://github.com/NetDevPack/Security.JwtExtensions + icon.png + README.md + true + true + snupkg - - - v10.0.0: - - Simplifies loading JWKS from remote URIs - - Supports automatic key refresh scenarios - - Designed for JWT validation with asymmetric keys - - + + - Targets net10.0 only (netstandard2.1, net8.0 and net9.0 were removed) + - Loads JWKS from remote URIs for JWT validation with asymmetric keys + - Supports automatic key refresh scenarios + + @@ -43,21 +39,7 @@ - - - - - - - - - - - - - - - - + + diff --git a/tests/NetDevPack.Security.JwtExtensions.ApiTests/NetDevPack.Security.JwtExtensions.ApiTests.csproj b/tests/NetDevPack.Security.JwtExtensions.ApiTests/NetDevPack.Security.JwtExtensions.ApiTests.csproj index 37243a0..fef7afb 100644 --- a/tests/NetDevPack.Security.JwtExtensions.ApiTests/NetDevPack.Security.JwtExtensions.ApiTests.csproj +++ b/tests/NetDevPack.Security.JwtExtensions.ApiTests/NetDevPack.Security.JwtExtensions.ApiTests.csproj @@ -1,27 +1,15 @@ - + - net6.0;net7.0;net8.0;net9.0;net10.0 + net10.0 + false - - - - - - - - - - - - - - - + + diff --git a/tests/NetDevPack.Security.JwtExtensions.Tests/Infra/AuthMiddleware.cs b/tests/NetDevPack.Security.JwtExtensions.Tests/Infra/AuthMiddleware.cs index d1ef233..84d9851 100644 --- a/tests/NetDevPack.Security.JwtExtensions.Tests/Infra/AuthMiddleware.cs +++ b/tests/NetDevPack.Security.JwtExtensions.Tests/Infra/AuthMiddleware.cs @@ -12,7 +12,7 @@ namespace NetDevPack.Security.JwtExtensions.Tests.Infra public class AuthMiddleware { private readonly RequestDelegate _next; - private static string _jws; + private string _jws; public AuthMiddleware(RequestDelegate next) { _next = next; diff --git a/tests/NetDevPack.Security.JwtExtensions.Tests/JwksTests.cs b/tests/NetDevPack.Security.JwtExtensions.Tests/JwksTests.cs index 38670b1..a59b23a 100644 --- a/tests/NetDevPack.Security.JwtExtensions.Tests/JwksTests.cs +++ b/tests/NetDevPack.Security.JwtExtensions.Tests/JwksTests.cs @@ -1,4 +1,4 @@ -using FluentAssertions; +using AwesomeAssertions; using Microsoft.AspNetCore.Mvc.Testing; using NetDevPack.Security.JwtExtensions.Tests.Infra; using System; @@ -10,11 +10,12 @@ using System.Threading.Tasks; using NetDevPack.Security.JwtExtensions.ApiTests; using Xunit; -using Xunit.Abstractions; namespace NetDevPack.Security.JwtExtensions.Tests { - public class JwksTests : IClassFixture>, IDisposable + // One JWKS server per test class: the API under test is a class fixture and reads JWKS_URL once, + // so every test must use tokens signed by the same server. + public class JwksTests : IClassFixture>, IClassFixture { private readonly WebApplicationFactory _webApplicationFactory; private readonly ITestOutputHelper _output; @@ -23,11 +24,11 @@ public class JwksTests : IClassFixture>, IDisposa private const string InvalidSignature = "eyJhbGciOiJFUzI1NiIsImtpZCI6Im1qeEQyQUphcXE4cW8zMWRiMjFyTGciLCJ0eXAiOiJhdCtqd3QifQ.eyJuYmYiOjE1OTU3MzQ0MzksImV4cCI6MTU5NTczODAzOSwiaXNzIjoiaHR0cHM6Ly9zc28uanBwcm9qZWN0Lm5ldCIsImF1ZCI6ImpwX2FwaSIsImNsaWVudF9pZCI6IklTNC1BZG1pbiIsInN1YiI6Ijc0MkYyMzk2LTg1N0MtNEI0Qy01NUExLTA4RDYzMTczMUIzRCIsImF1dGhfdGltZSI6MTU5NTczNDQzMiwiaWRwIjoiR29vZ2xlIiwiaXM0LXJpZ2h0cyI6Im1hbmFnZXIiLCJyb2xlIjoiQWRtaW5pc3RyYXRvciIsImVtYWlsIjoiYmhkZWJyaXRvQGdtYWlsLmNvbSIsInVzZXJuYW1lIjoiYnJ1bm8iLCJzY29wZSI6WyJvcGVuaWQiLCJwcm9maWxlIiwiZW1haWwiLCJyb2xlIiwianBfYXBpLmlzNCJdLCJhbXIiOlsiZXh0ZXJuYWwiXX0.i4_sfpzcS5oVVdgQqyH2qJfp02Jl-dAm3KuDr8G1BEz2flw6d3yPB8C82qhuM9PDNO7UBcXm8LJdMfI--SUK0A"; - public JwksTests(WebApplicationFactory webApplicationFactory, ITestOutputHelper output) + public JwksTests(WebApplicationFactory webApplicationFactory, Server server, ITestOutputHelper output) { _webApplicationFactory = webApplicationFactory; _output = output; - _server = new Server(); + _server = server; Environment.SetEnvironmentVariable("JWKS_URL", $"{_server.ServerUrl}/jwks"); _client = webApplicationFactory.CreateClient(new WebApplicationFactoryClientOptions { @@ -43,7 +44,7 @@ public async Task Should_200() { var request = new HttpRequestMessage(HttpMethod.Get, "/WeatherForecast"); - var response = await _client.SendAsync(request); + var response = await _client.SendAsync(request, TestContext.Current.CancellationToken); response.StatusCode.Should().Be(HttpStatusCode.OK); } @@ -53,7 +54,7 @@ public async Task Should_401() { var request = new HttpRequestMessage(HttpMethod.Get, "/ProtectedWeatherForecast"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "generic token"); - var response = await _client.SendAsync(request); + var response = await _client.SendAsync(request, TestContext.Current.CancellationToken); response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); } @@ -63,7 +64,7 @@ public async Task Should_Not_Validate_Bearer_Token_With_Invalid_Signature() { var request = new HttpRequestMessage(HttpMethod.Get, "/ProtectedWeatherForecast"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", InvalidSignature); - var response = await _client.SendAsync(request); + var response = await _client.SendAsync(request, TestContext.Current.CancellationToken); response.StatusCode.Should().Be(HttpStatusCode.Unauthorized); } @@ -72,22 +73,15 @@ public async Task Should_Not_Validate_Bearer_Token_With_Invalid_Signature() public async Task Should_Validate_Bearer_Token() { var tokenRequest = new HttpRequestMessage(HttpMethod.Get, new Uri("/auth", UriKind.Relative)); - var tokenResponse = await _server.HttpClient.SendAsync(tokenRequest); - var token = await tokenResponse.Content.ReadAsStringAsync(); + var tokenResponse = await _server.HttpClient.SendAsync(tokenRequest, TestContext.Current.CancellationToken); + var token = await tokenResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); var request = new HttpRequestMessage(HttpMethod.Get, "/ProtectedWeatherForecast"); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token); - var response = await _client.SendAsync(request); + var response = await _client.SendAsync(request, TestContext.Current.CancellationToken); try { response.EnsureSuccessStatusCode(); } catch { _output.WriteLine(string.Join(" ", response.Headers.GetValues("WWW-Authenticate"))); throw; }; } - - - - public void Dispose() - { - _server.Dispose(); - } } } diff --git a/tests/NetDevPack.Security.JwtExtensions.Tests/NetDevPack.Security.JwtExtensions.Tests.csproj b/tests/NetDevPack.Security.JwtExtensions.Tests/NetDevPack.Security.JwtExtensions.Tests.csproj index ffeeef7..946d2c7 100644 --- a/tests/NetDevPack.Security.JwtExtensions.Tests/NetDevPack.Security.JwtExtensions.Tests.csproj +++ b/tests/NetDevPack.Security.JwtExtensions.Tests/NetDevPack.Security.JwtExtensions.Tests.csproj @@ -1,39 +1,27 @@ - + - net8.0;net9.0;net10.0 - 10.0 + net10.0 + Exe false + true - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + - - - - - - - - - -