Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update key ring verification keys #3

Merged
merged 3 commits into from
Sep 23, 2022
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
4 changes: 3 additions & 1 deletion Rusty.Jwt.Abstractions/Keys/IKeyRing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public interface IKeyRing
ISigningKey GetSigningKey(string name);

/// <summary>
/// Used to get a key to verify data and a signature.
/// Used to get a key to verify data and a signature. Only fetches keys without
/// a name, this is so that explicitly defined keys are only used for their
/// intended purpose.
/// </summary>
/// <param name="algorithm">The signing algorithm of key to get.</param>
/// <param name="hashAlgorithm">The hashing algorithm of key to get.</param>
Expand Down
2 changes: 1 addition & 1 deletion Rusty.Jwt.Abstractions/Rusty.Jwt.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<RepositoryUrl>https://github.com/reecerussell/rusty-jwt.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>jwt</PackageTags>
<PackageVersion>1.0.1</PackageVersion>
<PackageVersion>1.0.2</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions Rusty.Jwt.Tests/Keys/KeyRingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void GetSigningKey_WhereKeyIsNotFound_ThrowsKeyNotFound()
[Fact]
public void GetVerificationKey_WhereKeyRingHasKeys_ReturnsAggregateKey()
{
// Matching HashAlgorithm, Matching SigningAlgorithm
var key1 = new Mock<ISigningKey>();
key1.Setup(x => x.Algorithm).Returns(SigningKeyAlgorithm.Hmac);
key1.Setup(x => x.HashAlgorithm).Returns(HashAlgorithm.SHA256);
Expand All @@ -91,6 +92,7 @@ public void GetVerificationKey_WhereKeyRingHasKeys_ReturnsAggregateKey()
keyDefinition1.SetupGet(x => x.Mode).Returns(SigningKeyMode.SignAndVerify);
keyDefinition1.SetupGet(x => x.Key).Returns(key1.Object);

// Matching HashAlgorithm, Mismatching SigningAlgorithm
var key2 = new Mock<ISigningKey>();
key2.Setup(x => x.Algorithm).Returns(SigningKeyAlgorithm.Rsa);
key2.Setup(x => x.HashAlgorithm).Returns(HashAlgorithm.SHA256);
Expand All @@ -99,17 +101,30 @@ public void GetVerificationKey_WhereKeyRingHasKeys_ReturnsAggregateKey()
keyDefinition2.SetupGet(x => x.Mode).Returns(SigningKeyMode.SignAndVerify);
keyDefinition2.SetupGet(x => x.Key).Returns(key2.Object);

// Mismatching HashAlgorithm, Mismatching SigningAlgorithm
var key3 = new Mock<ISigningKey>();
key3.Setup(x => x.Algorithm).Returns(SigningKeyAlgorithm.Rsa);
key3.Setup(x => x.HashAlgorithm).Returns(HashAlgorithm.SHA384);

var keyDefinition3 = new Mock<ISigningKeyDefinition>();
keyDefinition3.SetupGet(x => x.Mode).Returns(SigningKeyMode.SignAndVerify);
keyDefinition3.SetupGet(x => x.Key).Returns(key3.Object);

// Matching HashAlgorithm, Matching SigningAlgorithm, Has a name
var key4 = new Mock<ISigningKey>();
key4.Setup(x => x.Algorithm).Returns(SigningKeyAlgorithm.Hmac);
key4.Setup(x => x.HashAlgorithm).Returns(HashAlgorithm.SHA256);

var keyDefinition4 = new Mock<ISigningKeyDefinition>();
keyDefinition4.SetupGet(x => x.Mode).Returns(SigningKeyMode.SignAndVerify);
keyDefinition4.SetupGet(x => x.Key).Returns(key4.Object);
keyDefinition4.SetupGet(x => x.Name).Returns("test-key"); // cause to ignore

var services = new ServiceCollection();
services.AddTransient(_ => keyDefinition1.Object);
services.AddTransient(_ => keyDefinition2.Object);
services.AddTransient(_ => keyDefinition3.Object);
services.AddTransient(_ => keyDefinition4.Object);

var keyRing = new KeyRing(services.BuildServiceProvider());
var key = keyRing.GetVerificationKey(SigningKeyAlgorithm.Hmac, HashAlgorithm.SHA256);
Expand Down
3 changes: 2 additions & 1 deletion Rusty.Jwt/Keys/KeyRing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public ISigningKey GetSigningKey(string name)
public IVerificationKey GetVerificationKey(SigningKeyAlgorithm algorithm, HashAlgorithm hashAlgorithm)
{
var keys = _keys.Where(x => x.Key.Algorithm == algorithm &&
x.Key.HashAlgorithm == hashAlgorithm)
x.Key.HashAlgorithm == hashAlgorithm &&
x.Name == null)
.Select(x => x.Key);

return new AggregateVerificationKey(keys);
Expand Down
2 changes: 1 addition & 1 deletion Rusty.Jwt/Rusty.Jwt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageTags>jwt</PackageTags>
<PackageLicenseUrl>https://github.com/reecerussell/rusty-jwt/blob/master/LICENSE</PackageLicenseUrl>
<LangVersion>10</LangVersion>
<PackageVersion>1.0.2</PackageVersion>
<PackageVersion>1.0.3</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down