Skip to content

Commit 8dc8e12

Browse files
chore: merge
feat: Generate New Key
2 parents 0830430 + e809cd9 commit 8dc8e12

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

src/NetDevPack.Security.Jwt.Core/Interfaces/IJwtService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public interface IJwtService
1717
Task<EncryptingCredentials> GetCurrentEncryptingCredentials();
1818
Task<ReadOnlyCollection<KeyMaterial>> GetLastKeys(int? i = null);
1919
Task RevokeKey(string keyId, string reason = null);
20+
Task<SecurityKey> GenerateNewKey();
2021
}
2122
[Obsolete("Deprecate, use IJwtServiceInstead")]
2223
public interface IJsonWebKeySetService : IJwtService{}

src/NetDevPack.Security.Jwt.Core/Jwt/JwtService.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ public async Task RevokeKey(string keyId, string reason = null)
7979

8080
await _store.Revoke(key, reason);
8181
}
82+
83+
public async Task<SecurityKey> GenerateNewKey()
84+
{
85+
var oldCurrent = await _store.GetCurrent();
86+
await _store.Revoke(oldCurrent);
87+
return await GenerateKey();
88+
89+
}
90+
8291
private bool NeedsUpdate(KeyMaterial current)
8392
{
8493
return current == null || current.IsExpired(_options.Value.DaysUntilExpire) || current.IsRevoked;

tests/NetDevPack.Security.Jwt.Tests/JwtTests/JwtServiceTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ public JwtServiceTest(WarmupInMemoryStore warmup)
2323
}
2424

2525

26+
27+
[Fact]
28+
public async Task Should_Create_New_Key()
29+
{
30+
var currentKey = await _jwksService.GetCurrentSigningCredentials();
31+
32+
var newKey = await _jwksService.GenerateNewKey();
33+
34+
newKey.KeyId.Should().NotBe(currentKey.Kid);
35+
var newCurrentKey = await _jwksService.GetCurrentSigningCredentials();
36+
37+
newKey.KeyId.Should().Be(newCurrentKey.Kid);
38+
}
39+
2640
[Fact]
2741
public async Task ShouldGenerateDefaultSigning()
2842
{

0 commit comments

Comments
 (0)