File tree Expand file tree Collapse file tree 3 files changed +31
-0
lines changed Expand file tree Collapse file tree 3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change 16
16
Added
17
17
~~~~~
18
18
19
+ - Add ``PyJWKSet.__getitem__ `` for indexing keysets by key ID `#725 <https://github.com/jpadilla/pyjwt/pull/725 >`__
20
+
19
21
`v2.3.0 <https://github.com/jpadilla/pyjwt/compare/2.2.0...2.3.0 >`__
20
22
-----------------------------------------------------------------------
21
23
Original file line number Diff line number Diff line change @@ -95,3 +95,9 @@ def from_dict(obj):
95
95
def from_json (data ):
96
96
obj = json .loads (data )
97
97
return PyJWKSet .from_dict (obj )
98
+
99
+ def __getitem__ (self , kid ):
100
+ for key in self .keys :
101
+ if key .key_id == kid :
102
+ return key
103
+ raise KeyError ("keyset has no key for kid: %s" % kid )
Original file line number Diff line number Diff line change @@ -252,3 +252,26 @@ def test_should_load_keys_from_jwk_data_json_string(self):
252
252
assert jwk .key_type == "RSA"
253
253
assert jwk .key_id == "keyid-abc123"
254
254
assert jwk .public_key_use == "sig"
255
+
256
+ @crypto_required
257
+ def test_keyset_should_index_by_kid (self ):
258
+ algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
259
+
260
+ with open (key_path ("jwk_rsa_pub.json" )) as keyfile :
261
+ pub_key = algo .from_jwk (keyfile .read ())
262
+
263
+ key_data_str = algo .to_jwk (pub_key )
264
+ key_data = json .loads (key_data_str )
265
+
266
+ # TODO Should `to_jwk` set these?
267
+ key_data ["alg" ] = "RS256"
268
+ key_data ["use" ] = "sig"
269
+ key_data ["kid" ] = "keyid-abc123"
270
+
271
+ jwk_set = PyJWKSet .from_dict ({"keys" : [key_data ]})
272
+
273
+ jwk = jwk_set .keys [0 ]
274
+ assert jwk == jwk_set ["keyid-abc123" ]
275
+
276
+ with pytest .raises (KeyError ):
277
+ jwk_set ["this-kid-does-not-exist" ]
You can’t perform that action at this time.
0 commit comments