Skip to content

Commit 9c2f05a

Browse files
CADBIMDeveloperabatishchev
authored andcommitted
add Json Web keys collection
with an ability to search keys by ids
1 parent e209850 commit 9c2f05a

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace JWT.Jwk
2+
{
3+
public interface IJwtWebKeysCollection
4+
{
5+
JwtWebKey Find(string keyId);
6+
}
7+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using JWT.Serializers;
4+
5+
namespace JWT.Jwk
6+
{
7+
public class JwtWebKeysCollection : IJwtWebKeysCollection
8+
{
9+
private readonly Dictionary<string, JwtWebKey> _keys;
10+
11+
public JwtWebKeysCollection(IEnumerable<JwtWebKey> keys)
12+
{
13+
_keys = keys.ToDictionary(x => x.KeyId);
14+
}
15+
16+
public JwtWebKeysCollection(JwtWebKeySet keySet) : this(keySet.Keys)
17+
{
18+
19+
}
20+
21+
public JwtWebKeysCollection(string keySet, IJsonSerializer serializer)
22+
: this(serializer.Deserialize<JwtWebKeySet>(keySet))
23+
{
24+
25+
}
26+
27+
public JwtWebKeysCollection(string keySet, IJsonSerializerFactory jsonSerializerFactory)
28+
: this(keySet, jsonSerializerFactory.Create())
29+
{
30+
31+
}
32+
33+
public JwtWebKey Find(string keyId)
34+
{
35+
return _keys.TryGetValue(keyId, out var key) ? key : null;
36+
}
37+
}
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using JWT.Jwk;
2+
using JWT.Serializers;
3+
using JWT.Tests.Models;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
6+
namespace JWT.Tests.Jwk;
7+
8+
[TestClass]
9+
public class JwtWebKeysCollectionTests
10+
{
11+
[TestMethod]
12+
public void Should_Find_Json_Web_Key_By_KeyId()
13+
{
14+
var serializerFactory = new DefaultJsonSerializerFactory();
15+
16+
var collection = new JwtWebKeysCollection(TestData.JsonWebKeySet, serializerFactory);
17+
18+
var jwk = collection.Find(TestData.ServerRsaPublicThumbprint1);
19+
20+
Assert.IsNotNull(jwk);
21+
}
22+
}

tests/JWT.Tests.Common/Models/TestData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public class TestDataSystemTextSerializerDecorated
6767

6868
public const string TokenByAsymmetricAlgorithm = "eyJraWQiOiJDRkFFQUUyRDY1MEE2Q0E5ODYyNTc1REU1NDM3MUVBOTgwNjQzODQ5IiwidHlwIjoiSldUIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJ0ZXN0IiwiZXhwIjoyMTQ3NDgzNjQ4LCJGaXJzdE5hbWUiOiJKZXN1cyIsIkFnZSI6MzN9.ZeGfWN3kBHZLiSh4jzzn6kx7F6lNu5OsowZW0Sv-_wpSgQO2_QXFUPLx23wm4J9rjMGQlSksEtCLd_X3iiBOBLbxAUWzdj59iJIAh485unZj12sBJ7KHDVsOMc6DcSJdwRo9S9yiJ_RJ57R-dn4uRdZTBXBZHrrmb35UjaAG6hFfu5d1Ap4ZjLxqDJGl0Wo4j5l6vR8HFpmiFHvqPQ4apjqkBGnitJ7oghbeRX0SIVNSkXbBDp3i9pC-hxzs2oHZC9ys0rJlfpxLls3MV4oQbQ7m6W9MrwwsdObJHI7PiTNfObLKdgySi6WkQS7rwXVz0DqRa8TXv8_USkvhsyGLMQ";
6969

70+
public const string JsonWebKeySet = "{\"keys\":[{\"kty\":\"RSA\",\"kid\":\"CFAEAE2D650A6CA9862575DE54371EA980643849\",\"use\":\"sig\",\"n\":\"uYTPtHCIztKC3MUDxnZ0ktGVSQ0jVbD5rYl4pki4RCD3M22d-TklmvTyPj0SM7a_8o7cI05QhEuBI8hKCfC2CEJhlS3WFeVC0vwsl1aYFqQ3Ykr-kDsAdqjL95ioj3JmiscvqKOM34oQahpAgukJ7Kcr1BT2Ylk8fOgKcN7t1qgURNx0Pj4zJ4w0p1nT2gLG--bYutUVPvamI9wcMQyUesZwGmM9UUpMRzsOPk8vv7TbTm62Zkx-5rFUaVe5DFNUIMg92NvyU0392FFNCwptSflidHDG1ayCwL1ZTkJ0Z9yJXCNSzi_3ulxMhE-bVcpr_EuRKCYxn9qPFZ07Bd77bQ\",\"e\":\"AQAB\"}]}";
71+
7072
public static readonly IDictionary<string, object> DictionaryPayload = new Dictionary<string, object>
7173
{
7274
{ nameof(Customer.FirstName), Customer.FirstName },

0 commit comments

Comments
 (0)