Skip to content
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
3 changes: 2 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ jobs:
- 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
# IdentityServer4 is EOL with known advisories and is accepted on purpose (docs/Decisions/ADR-002)
! grep -E '^\s+>' vulnerable.txt | grep -v ' IdentityServer4 '
91 changes: 47 additions & 44 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -30,59 +30,62 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.31" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection" Version="8.0.31" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="8.0.31" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.31" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.31" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.31" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.31" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.31" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.31" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.31" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.31" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.34" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection" Version="8.0.34" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="8.0.34" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.34" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.34" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.34" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.34" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.34" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.34" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.34" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.34" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageVersion Include="System.Security.Cryptography.Xml" Version="8.0.4" />
<PackageVersion Include="System.Security.Cryptography.Pkcs" Version="8.0.2" />
<PackageVersion Include="System.Security.Cryptography.Xml" Version="8.0.5" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.20" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection" Version="9.0.20" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="9.0.20" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.20" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.20" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.20" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="9.0.20" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.20" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.20" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.20" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.20" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="9.0.20" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="9.0.20" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.20" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="9.0.20" />
<PackageVersion Include="System.Security.Cryptography.Xml" Version="9.0.20" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.23" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection" Version="9.0.23" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="9.0.23" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.23" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.23" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.23" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="9.0.23" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="9.0.23" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.23" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.23" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.23" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="9.0.23" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="9.0.23" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.23" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="9.0.23" />
<PackageVersion Include="System.Security.Cryptography.Pkcs" Version="9.0.23" />
<PackageVersion Include="System.Security.Cryptography.Xml" Version="9.0.23" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.12" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection" Version="10.0.12" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="10.0.12" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.12" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="10.0.12" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.12" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.12" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="10.0.12" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.12" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.12" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.12" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="10.0.12" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="10.0.12" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="10.0.12" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="10.0.12" />
<PackageVersion Include="System.Security.Cryptography.Xml" Version="10.0.12" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="10.0.15" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection" Version="10.0.15" />
<PackageVersion Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="10.0.15" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="10.0.15" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="10.0.15" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.15" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="10.0.15" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="10.0.15" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.15" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.15" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.15" />
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="10.0.15" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="10.0.15" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="10.0.15" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="10.0.15" />
<PackageVersion Include="System.Security.Cryptography.Pkcs" Version="10.0.15" />
<PackageVersion Include="System.Security.Cryptography.Xml" Version="10.0.15" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions docs/Architecture/Stores.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
tags: [architecture, storage]
updated: 2026-10-01
updated: 2026-11-17
---
# Stores (`IJsonWebKeyStore`)

Expand All @@ -9,7 +9,7 @@ Contract (`Interfaces/IJsonWebKeyStore.cs`): `Store`, `GetCurrent(type)`, `Revok
| Store | Registration | Persistence | Notes |
| --- | --- | --- | --- |
| `DataProtectionStore` (default, internal) | `AddJwksManager()` | ASP.NET DataProtection `IXmlRepository` (same place as cookie keys) | Payload protected with `IDataProtector`. Revocation is a separate XML element because `IXmlRepository` can't update. If no repository can be found, it falls back to Azure WebSites → default folder → Windows registry, then throws |
| `InMemoryStore` | `.PersistKeysInMemory()` | Process memory | Tests/dev only |
| `InMemoryStore` | `.PersistKeysInMemory()` | Memory of the service provider (singleton) | Tests/dev only. Each service provider has its own keys: hosts in the same process (e.g. parallel tests) don't share them. Lost on restart |
| `FileSystemStore` | `.PersistKeysToFileSystem(dir)` | JSON files in a folder | Needs `IMemoryCache` |
| `DatabaseJsonWebKeyStore<TContext>` | `.PersistKeysToDatabaseStore<TContext>()` | EF Core, `TContext : DbContext, ISecurityKeyContext` (`DbSet<KeyMaterial> SecurityKeys`) | Mapping in `KeyMaterialMap.cs`; uses `AsNoTrackingWithIdentityResolution` |

Expand Down
54 changes: 31 additions & 23 deletions src/NetDevPack.Security.Jwt.Core/DefaultStore/InMemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,55 @@

namespace NetDevPack.Security.Jwt.Core.DefaultStore;

/// <summary>
/// Keeps the keys in memory. Registered as a singleton, so each service provider (application)
/// has its own keys instead of sharing a static list with every other host in the process.
/// </summary>
internal class InMemoryStore : IJsonWebKeyStore
{
private static readonly List<KeyMaterial> _store = new();
private readonly SemaphoreSlim _slim = new(1);
private readonly List<KeyMaterial> _store = new();
private readonly object _lock = new();
internal const string DefaultRevocationReason = "Revoked";

public Task Store(KeyMaterial keyMaterial)
{
if (keyMaterial is null) throw new InvalidOperationException("Can't store empty value.");

_slim.Wait();
_store.Add(keyMaterial);
_slim.Release();
lock (_lock)
_store.Add(keyMaterial);

return Task.CompletedTask;
}

public Task<KeyMaterial> GetCurrent(JwtKeyType jwtKeyType = JwtKeyType.Jws)
{
return Task.FromResult(_store.Where(s => s.Use == (jwtKeyType == JwtKeyType.Jws ? "sig" : "enc")).OrderByDescending(s => s.CreationDate).FirstOrDefault());
var use = jwtKeyType == JwtKeyType.Jws ? "sig" : "enc";
lock (_lock)
return Task.FromResult(_store.Where(s => s.Use == use).OrderByDescending(s => s.CreationDate).FirstOrDefault());
}

public async Task Revoke(KeyMaterial keyMaterial, string reason = null)
public Task Revoke(KeyMaterial keyMaterial, string reason = null)

Check warning on line 35 in src/NetDevPack.Security.Jwt.Core/DefaultStore/InMemoryStore.cs

View workflow job for this annotation

GitHub Actions / pull-request

Cannot convert null literal to non-nullable reference type.

Check warning on line 35 in src/NetDevPack.Security.Jwt.Core/DefaultStore/InMemoryStore.cs

View workflow job for this annotation

GitHub Actions / pull-request

Cannot convert null literal to non-nullable reference type.
{
if(keyMaterial == null)
return;
var revokeReason = reason ?? DefaultRevocationReason;
keyMaterial.Revoke(revokeReason);
var oldOne = _store.Find(f => f.Id == keyMaterial.Id);
if (oldOne != null)
if (keyMaterial == null)
return Task.CompletedTask;

keyMaterial.Revoke(reason ?? DefaultRevocationReason);
lock (_lock)
{
var index = _store.FindIndex(f => f.Id == keyMaterial.Id);
await _slim.WaitAsync();
_store.RemoveAt(index);
_store.Insert(index, keyMaterial);
_slim.Release();
if (index >= 0)
_store[index] = keyMaterial;
}

return Task.CompletedTask;
}

public Task<ReadOnlyCollection<KeyMaterial>> GetLastKeys(int quantity, JwtKeyType? jwtKeyType = null)
{
return Task.FromResult(
_store
.Where(s => jwtKeyType == null || s.Use == (jwtKeyType == JwtKeyType.Jws ? "sig" : "enc"))
lock (_lock)
return Task.FromResult(
_store
.Where(s => jwtKeyType == null || s.Use == (jwtKeyType == JwtKeyType.Jws ? "sig" : "enc"))
.OrderByDescending(s => s.CreationDate)
.GroupBy(s => s.Use)
.SelectMany(g => g.Take(quantity))
Expand All @@ -57,12 +62,15 @@

public Task<KeyMaterial> Get(string keyId)
{
return Task.FromResult(_store.FirstOrDefault(w => w.KeyId == keyId));
lock (_lock)
return Task.FromResult(_store.FirstOrDefault(w => w.KeyId == keyId));
}

public Task Clear()
{
_store.Clear();
lock (_lock)
_store.Clear();

return Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/// Sets the signing credential.
/// </summary>
/// <returns></returns>
public static IJwksBuilder AddJwksManager(this IServiceCollection services, Action<JwtOptions> action = null)

Check warning on line 14 in src/NetDevPack.Security.Jwt.Core/JsonWebKeySetManagerDependencyInjection.cs

View workflow job for this annotation

GitHub Actions / pull-request

Cannot convert null literal to non-nullable reference type.

Check warning on line 14 in src/NetDevPack.Security.Jwt.Core/JsonWebKeySetManagerDependencyInjection.cs

View workflow job for this annotation

GitHub Actions / pull-request

Cannot convert null literal to non-nullable reference type.
{
if (action != null)
services.Configure(action);
Expand All @@ -24,12 +24,13 @@
}

/// <summary>
/// Sets the signing credential.
/// Keeps the keys in memory, for the lifetime of the service provider. Keys are lost on restart
/// and are not shared between instances, so use it only for development and tests.
/// </summary>
/// <returns></returns>
public static IJwksBuilder PersistKeysInMemory(this IJwksBuilder builder)
{
builder.Services.AddScoped<IJsonWebKeyStore, InMemoryStore>();
builder.Services.AddSingleton<IJsonWebKeyStore, InMemoryStore>();

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Threading.Tasks;
using AwesomeAssertions;
using Microsoft.Extensions.DependencyInjection;
using NetDevPack.Security.Jwt.Core.Interfaces;
using Xunit;

namespace NetDevPack.Security.Jwt.Tests.StoreTests;

[Trait("Category", "InMemory Tests")]
public class InMemoryStoreIsolationTests
{
private static ServiceProvider CreateProvider()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddMemoryCache();
services.AddJwksManager().PersistKeysInMemory();
return services.BuildServiceProvider();
}

[Fact]
public async Task ServiceProviders_ShouldNotShareKeys()
{
await using var first = CreateProvider();
await using var second = CreateProvider();

using (var scope = first.CreateScope())
await scope.ServiceProvider.GetRequiredService<IJwtService>().GenerateKey();

using (var scope = second.CreateScope())
(await scope.ServiceProvider.GetRequiredService<IJsonWebKeyStore>().GetLastKeys(5)).Should().BeEmpty();
}

[Fact]
public async Task Scopes_OfTheSameServiceProvider_ShouldShareKeys()
{
await using var provider = CreateProvider();

string keyId;
using (var scope = provider.CreateScope())
keyId = (await scope.ServiceProvider.GetRequiredService<IJwtService>().GenerateKey()).KeyId;

using (var scope = provider.CreateScope())
(await scope.ServiceProvider.GetRequiredService<IJsonWebKeyStore>().Get(keyId)).Should().NotBeNull();
}
}
Loading