1010 ImmatureSignatureError ,
1111 InvalidAlgorithmError ,
1212 InvalidSignatureError ,
13- PyJWKClientError ,
1413 encode ,
1514)
1615from pydantic import ValidationError
@@ -129,8 +128,8 @@ def _provide_jwts(self):
129128 False ,
130129 """eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6
131130 IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.dyt0CoTl4WoVjAHI9Q_CwSKhl6d_9rhM3NrXuJttkao""" ,
132- PyJWKClientError ,
133- 'Unable to find a signing key that matches: "None"' ,
131+ InvalidAlgorithmError ,
132+ "Algorithm not allowed" ,
134133 ),
135134 # Not before (nfb) in future
136135 (
@@ -180,6 +179,13 @@ def _provide_jwts(self):
180179 None ,
181180 None ,
182181 ),
182+ # Disallowed algorithm "none"
183+ (
184+ False ,
185+ self ._generate_jwt (iss = "https://auth.acme.com" , exp = int (time ()) + 100 , nbf = int (time ()) - 100 , algorithm = "none" ),
186+ InvalidAlgorithmError ,
187+ "Algorithm not allowed" ,
188+ ),
183189 # Success with old Frontend API URL in config (2)
184190 (
185191 True ,
@@ -194,20 +200,17 @@ def _provide_jwts(self):
194200 None ,
195201 None ,
196202 ),
197- # Disallowed algorithm "none"
198- (
199- False ,
200- "eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0."
201- "eyJpc3MiOiJodHRwczovL2F1dGguYWNtZS5jb20iLCJzdWIiOiIxMjM0NSIsImlhdCI6"
202- + str (int (time ()))
203- + "f." ,
204- InvalidAlgorithmError ,
205- "Algorithm not allowed" ,
206- ),
207203 ]
208204
209205 @classmethod
210- def _generate_jwt (cls , iss : str , exp : int , nbf : int , valid_key : bool = True ) -> str :
206+ def _generate_jwt (
207+ cls ,
208+ iss : str ,
209+ exp : int ,
210+ nbf : int ,
211+ valid_key : bool = True ,
212+ algorithm : str = "RS256" ,
213+ ) -> str :
211214 payload = {
212215 "iss" : iss ,
213216 "iat" : int (time ()),
@@ -217,9 +220,24 @@ def _generate_jwt(cls, iss: str, exp: int, nbf: int, valid_key: bool = True) ->
217220 "name" : TEST_NAME ,
218221 }
219222
220- if valid_key :
221- return encode (payload , key = cls .private_key , algorithm = "RS256" , headers = {"kid" : "kid123" })
222- return encode (payload , key = cls .invalid_private_key , algorithm = "RS256" , headers = {"kid" : "kid123" })
223+ key_to_use = cls .private_key if valid_key else cls .invalid_private_key
224+
225+ # unsecured JWT (“none”)
226+ if algorithm .lower () == "none" :
227+ # key must be None for alg=none
228+ return encode (
229+ payload ,
230+ key = None ,
231+ headers = {"alg" : "none" , "typ" : "JWT" },
232+ )
233+
234+ # signed JWT (RS256 by default)
235+ return encode (
236+ payload ,
237+ key = key_to_use ,
238+ algorithm = algorithm ,
239+ headers = {"kid" : "kid123" },
240+ )
223241
224242
225243class TestSessionService (TestBase ):
0 commit comments