-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace JWT.Jwk | ||
{ | ||
public interface IJwtWebKeysCollection | ||
{ | ||
JwtWebKey Find(string keyId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using JWT.Serializers; | ||
|
||
namespace JWT.Jwk | ||
{ | ||
public class JwtWebKeysCollection : IJwtWebKeysCollection | ||
{ | ||
private readonly Dictionary<string, JwtWebKey> _keys; | ||
|
||
public JwtWebKeysCollection(IEnumerable<JwtWebKey> keys) | ||
{ | ||
_keys = keys.ToDictionary(x => x.KeyId); | ||
} | ||
|
||
public JwtWebKeysCollection(JwtWebKeySet keySet) : this(keySet.Keys) | ||
{ | ||
|
||
} | ||
|
||
public JwtWebKeysCollection(string keySet, IJsonSerializer serializer) | ||
: this(serializer.Deserialize<JwtWebKeySet>(keySet)) | ||
{ | ||
|
||
} | ||
|
||
public JwtWebKeysCollection(string keySet, IJsonSerializerFactory jsonSerializerFactory) | ||
: this(keySet, jsonSerializerFactory.Create()) | ||
{ | ||
|
||
} | ||
|
||
public JwtWebKey Find(string keyId) | ||
{ | ||
return _keys.TryGetValue(keyId, out var key) ? key : null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using JWT.Jwk; | ||
using JWT.Serializers; | ||
using JWT.Tests.Models; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace JWT.Tests.Jwk; | ||
|
||
[TestClass] | ||
public class JwtWebKeysCollectionTests | ||
{ | ||
[TestMethod] | ||
public void Should_Find_Json_Web_Key_By_KeyId() | ||
{ | ||
var serializerFactory = new DefaultJsonSerializerFactory(); | ||
|
||
var collection = new JwtWebKeysCollection(TestData.JsonWebKeySet, serializerFactory); | ||
|
||
var jwk = collection.Find(TestData.ServerRsaPublicThumbprint1); | ||
|
||
Assert.IsNotNull(jwk); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters