From e27fbe5245636100830eba403d86620e831e6880 Mon Sep 17 00:00:00 2001 From: Bruno Brito Date: Tue, 17 Nov 2026 13:14:33 -0300 Subject: [PATCH 1/4] feat!: target .NET 10 and update dependencies - Target net10.0 only; drop netstandard2.1 (pulled vulnerable ASP.NET Core 2.1 packages: Microsoft.AspNetCore.Http 2.1.1, Newtonsoft.Json 10.0.1, System.Security.Cryptography.Xml 4.5.0), net8.0 and net9.0; tests drop net6/net7 - Central Package Management, NuGet Audit as errors, global.json, .editorconfig, dependabot - JwtBearer 10.0.15, System.IdentityModel.Tokens.Jwt 8.23.0 - Samples/tests: stale NetDevPack.Security.Jwt.Core 8.2.2 replaced by NetDevPack.Security.Jwt 10.0.0; pin patched System.Security.Cryptography.Xml/Pkcs - Tests: xunit.v3 on Microsoft.Testing.Platform, AwesomeAssertions, fix xUnit1051 - CI: actions/checkout v7, setup-dotnet v6, publish now runs the tests, vulnerable package check, semantic-release 24, pack/push only on new release - Rename README.MD to README.md (the package references README.md; case matters on Linux) - Fix RepositoryBranch (master) BREAKING CHANGE: the package now targets .NET 10 only. Stay on 9.x for .NET Standard 2.1, .NET 8 or .NET 9. The public API did not change. --- .editorconfig | 18 ++++ .github/dependabot.yml | 23 +++++ .github/workflows/publish-package.yml | 46 +++++----- .github/workflows/pull-request.yml | 32 +++---- Directory.Build.props | 11 +++ Directory.Packages.props | 33 +++++++ README.MD => README.md | 9 ++ global.json | 9 ++ ...rity.JwtExtensions.ApiAuthenticator.csproj | 23 ++--- ...ck.Security.JwtExtensions.ApiClient.csproj | 6 +- .../NetDevPack.Security.JwtExtensions.csproj | 86 ++++++++----------- ...ack.Security.JwtExtensions.ApiTests.csproj | 22 ++--- .../JwksTests.cs | 15 ++-- ...evPack.Security.JwtExtensions.Tests.csproj | 34 +++----- 14 files changed, 210 insertions(+), 157 deletions(-) create mode 100644 .editorconfig create mode 100644 .github/dependabot.yml create mode 100644 Directory.Build.props create mode 100644 Directory.Packages.props rename README.MD => README.md (86%) create mode 100644 global.json 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..86a8550 100644 --- a/.github/workflows/publish-package.yml +++ b/.github/workflows/publish-package.yml @@ -7,8 +7,13 @@ on: env: CURRENT_REPO_URL: https://github.com/${{ github.repository }} +permissions: + contents: write + issues: write + pull-requests: write + jobs: - build: + publish: runs-on: ubuntu-latest defaults: run: @@ -16,42 +21,39 @@ 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 + 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 + 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 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - 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.semantic.outputs.new_release_published == 'true' + run: dotnet pack -c Release -o out -p:Version=${{ steps.semantic.outputs.new_release_version }} -p:RepositoryUrl=${{ env.CURRENT_REPO_URL }} - 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.semantic.outputs.new_release_published == 'true' + run: dotnet nuget push ./out/*.nupkg --skip-duplicate -k ${{ secrets.NUGET_AUTH_TOKEN }} -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/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/JwksTests.cs b/tests/NetDevPack.Security.JwtExtensions.Tests/JwksTests.cs index 38670b1..2f90954 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,7 +10,6 @@ using System.Threading.Tasks; using NetDevPack.Security.JwtExtensions.ApiTests; using Xunit; -using Xunit.Abstractions; namespace NetDevPack.Security.JwtExtensions.Tests { @@ -43,7 +42,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 +52,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 +62,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,12 +71,12 @@ 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; }; } 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 + + + + + - - - - - - - - - - From dd62013329f88dea0fa1561535850c249ead5c71 Mon Sep 17 00:00:00 2001 From: Bruno Brito Date: Tue, 17 Nov 2026 13:25:10 -0300 Subject: [PATCH 2/4] ci: use conventionalcommits release preset --- .github/workflows/publish-package.yml | 1 + .releaserc | 31 ++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml index 86a8550..f48fa83 100644 --- a/.github/workflows/publish-package.yml +++ b/.github/workflows/publish-package.yml @@ -47,6 +47,7 @@ jobs: extra_plugins: | @semantic-release/changelog@6 @semantic-release/git@10 + conventional-changelog-conventionalcommits@8 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 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" + ] +} From a943690a56808b6d22eef1a95448c577d51155ca Mon Sep 17 00:00:00 2001 From: Bruno Brito Date: Tue, 17 Nov 2026 13:26:49 -0300 Subject: [PATCH 3/4] test: share the JWKS server across the test class Each test created its own JWKS server (new keys), while the API under test is a class fixture that reads JWKS_URL once and AuthMiddleware cached the first token in a static field. Results depended on test order, which changed with xunit.v3 and fails on Linux CI. --- .../Infra/AuthMiddleware.cs | 2 +- .../JwksTests.cs | 15 +++++---------- 2 files changed, 6 insertions(+), 11 deletions(-) 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 2f90954..a59b23a 100644 --- a/tests/NetDevPack.Security.JwtExtensions.Tests/JwksTests.cs +++ b/tests/NetDevPack.Security.JwtExtensions.Tests/JwksTests.cs @@ -13,7 +13,9 @@ 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; @@ -22,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 { @@ -81,12 +83,5 @@ public async Task Should_Validate_Bearer_Token() try { response.EnsureSuccessStatusCode(); } catch { _output.WriteLine(string.Join(" ", response.Headers.GetValues("WWW-Authenticate"))); throw; }; } - - - - public void Dispose() - { - _server.Dispose(); - } } } From f84f81d376b90af294725cbe99abdec96ddf53ae Mon Sep 17 00:00:00 2001 From: Bruno Brito Date: Tue, 17 Nov 2026 21:42:00 -0300 Subject: [PATCH 4/4] ci: publish to NuGet with trusted publishing nuget.org API keys expire and the org key already did, which made the last releases fail with 403. Publish with NuGet/login (OIDC) instead: the job gets a short-lived key from the trusted publishing policy for this repository and workflow file. Also adds a workflow_dispatch input to publish an existing release tag again, since re-running the job skips the push once the release exists. --- .github/workflows/publish-package.yml | 32 +++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/.github/workflows/publish-package.yml b/.github/workflows/publish-package.yml index f48fa83..2480efe 100644 --- a/.github/workflows/publish-package.yml +++ b/.github/workflows/publish-package.yml @@ -3,6 +3,11 @@ 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 }} @@ -11,6 +16,7 @@ permissions: contents: write issues: write pull-requests: write + id-token: write # NuGet trusted publishing (OIDC) jobs: publish: @@ -24,6 +30,7 @@ jobs: uses: actions/checkout@v7 with: fetch-depth: 0 + ref: ${{ inputs.version && format('v{0}', inputs.version) || github.sha }} - name: Setup .NET uses: actions/setup-dotnet@v6 @@ -41,6 +48,7 @@ jobs: - name: Semantic Release id: semantic + if: github.event_name == 'push' uses: cycjimmy/semantic-release-action@v4 with: semantic_version: 24 @@ -51,10 +59,26 @@ jobs: 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 - if: steps.semantic.outputs.new_release_published == 'true' - run: dotnet pack -c Release -o out -p:Version=${{ 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 - if: steps.semantic.outputs.new_release_published == 'true' - run: dotnet nuget push ./out/*.nupkg --skip-duplicate -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