Skip to content

Commit

Permalink
chore: in mem store implements revoke reason too
Browse files Browse the repository at this point in the history
  • Loading branch information
50c committed Apr 3, 2023
1 parent 350fcaa commit 8a2bf80
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace NetDevPack.Security.Jwt.Core.DefaultStore;

internal class InMemoryStore : IJsonWebKeyStore
{

internal const string DefaultRevocationReason = "Revoked";
private static readonly List<KeyMaterial> _store = new();
private readonly SemaphoreSlim _slim = new(1);
public Task Store(KeyMaterial keyMaterial)
Expand All @@ -27,8 +27,8 @@ public async Task Revoke(KeyMaterial keyMaterial, string reason = null)
{
if(keyMaterial == null)
return;

keyMaterial.Revoke();
var revokeReason = reason ?? DefaultRevocationReason;
keyMaterial.Revoke(revokeReason);
var oldOne = _store.Find(f => f.Id == keyMaterial.Id);
if (oldOne != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,5 @@ public DataProtectionStoreTest(WarmupDataProtectionStore unifiedContext) : base(
{
}

[Fact]
public async Task Should_Read_Default_Revocation_Reason()
{
var keyMaterial = await StoreRandomKey();
/*Revoke*/
await _store.Revoke(keyMaterial);
await CheckRevocationReasonIsStored(keyMaterial.KeyId, DataProtectionStore.DefaultRevocationReason);
}

[Theory]
[InlineData("ManualRevocation")]
[InlineData("StolenKey")]
public async Task Should_Read_NonDefault_Revocation_Reason(string reason)
{
var keyMaterial = await StoreRandomKey();
/*Revoke with reason*/
await _store.Revoke(keyMaterial, reason);
await CheckRevocationReasonIsStored(keyMaterial.KeyId, reason);
}

private async Task CheckRevocationReasonIsStored(string keyId, string revocationReason)
{
var dbKey = (await _store.GetLastKeys(5)).First(w => w.KeyId == keyId);
dbKey.Type.Should().NotBeNullOrEmpty();
dbKey.RevokedReason.Should().BeEquivalentTo(revocationReason);
}

private async Task<KeyMaterial> StoreRandomKey()
{
var alg = Algorithm.Create(DigitalSignaturesAlgorithm.RsaSha512);
var key = new CryptographicKey(alg);
var keyMaterial = new KeyMaterial(key);
await _store.Store(keyMaterial);
return keyMaterial;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;
using NetDevPack.Security.Jwt.Core;
using NetDevPack.Security.Jwt.Core.DefaultStore;
using NetDevPack.Security.Jwt.Core.Interfaces;
using NetDevPack.Security.Jwt.Core.Jwa;
using NetDevPack.Security.Jwt.Core.Model;
Expand Down Expand Up @@ -498,6 +499,42 @@ public async Task ShouldGenerateAndValidateJweAndJws()

}

[Fact]
public async Task Should_Read_Default_Revocation_Reason()
{
var keyMaterial = await StoreRandomKey();
/*Revoke*/
await _store.Revoke(keyMaterial);
await CheckRevocationReasonIsStored(keyMaterial.KeyId, DataProtectionStore.DefaultRevocationReason);
}

[Theory]
[InlineData("ManualRevocation")]
[InlineData("StolenKey")]
public async Task Should_Read_NonDefault_Revocation_Reason(string reason)
{
var keyMaterial = await StoreRandomKey();
/*Revoke with reason*/
await _store.Revoke(keyMaterial, reason);
await CheckRevocationReasonIsStored(keyMaterial.KeyId, reason);
}

private async Task CheckRevocationReasonIsStored(string keyId, string revocationReason)
{
var dbKey = (await _store.GetLastKeys(5)).First(w => w.KeyId == keyId);
dbKey.Type.Should().NotBeNullOrEmpty();
dbKey.RevokedReason.Should().BeEquivalentTo(revocationReason);
}

private async Task<KeyMaterial> StoreRandomKey()
{
var alg = Algorithm.Create(DigitalSignaturesAlgorithm.RsaSha512);
var key = new CryptographicKey(alg);
var keyMaterial = new KeyMaterial(key);
await _store.Store(keyMaterial);
return keyMaterial;
}



private Task GenerateKey()
Expand Down

0 comments on commit 8a2bf80

Please sign in to comment.