@@ -123,7 +123,11 @@ class OpenIdConnectConfiguration(BaseAuthenticatorConfiguration):
123
123
)
124
124
125
125
JWT_ALGORITHMS = ListField (
126
- help_text = _ ("The algorithm(s) for decoding JWT responses from the IDP." ),
126
+ help_text = _ (
127
+ "The algorithm(s) for decoding JWT responses from the IDP. "
128
+ "Leave blank to extract from the .well-known configuration (if that fails we will attempt the default algorithms). "
129
+ "Set to ['none'] to not use encrypted tokens (the provider must send unencrypted tokens for this to work)"
130
+ ),
127
131
default = None ,
128
132
allow_null = True ,
129
133
validators = [JWTAlgorithmListFieldValidator ()],
@@ -300,3 +304,56 @@ def get_alternative_uid(self, **kwargs):
300
304
return preferred_username
301
305
302
306
return None
307
+
308
+ def _discover_algorithms_from_config (self , config ):
309
+ """
310
+ Discover JWT algorithms from OIDC configuration
311
+ """
312
+ # Try signing algorithms first (most common)
313
+ algorithms = config .get ("id_token_signing_alg_values_supported" )
314
+ if algorithms :
315
+ logger .debug (f"JWT signing algorithms supported by the IDP: { algorithms } " )
316
+ return algorithms
317
+
318
+ # Fallback to encryption algorithms
319
+ algorithms = config .get ("id_token_encryption_alg_values_supported" )
320
+ if algorithms :
321
+ logger .debug (f"JWT encryption algorithms supported by the IDP: { algorithms } " )
322
+ return algorithms
323
+
324
+ # Try userinfo signing algorithms as final fallback
325
+ algorithms = config .get ("userinfo_signing_alg_values_supported" )
326
+ if algorithms :
327
+ logger .debug (f"JWT userinfo signing algorithms supported by the IDP: { algorithms } " )
328
+ return algorithms
329
+
330
+ return None
331
+
332
+ def _get_jwt_algorithms (self , existing_setting = None ) -> list [str ]:
333
+ """
334
+ Get the JWT algorithms to pass to the decode
335
+ """
336
+ if existing_setting :
337
+ algorithms = existing_setting
338
+ else :
339
+ try :
340
+ logger .debug ("Attempting to get the JWT algorithms from the .well-known/openid-configuration" )
341
+ config = self .oidc_config ()
342
+ algorithms = self ._discover_algorithms_from_config (config )
343
+
344
+ if not algorithms :
345
+ raise Exception ("No algorithms found in OIDC config" )
346
+
347
+ except Exception as e :
348
+ # Fallback to default algorithms if discovery fails
349
+ lib_defaults = OpenIdConnectAuth .JWT_ALGORITHMS
350
+ logger .error (f"Unable to get JWT algorithms from the .well-known/openid-configuration, defaulting to { lib_defaults } : { e } " )
351
+ algorithms = lib_defaults
352
+
353
+ # Ensure we always return a list
354
+ if not isinstance (algorithms , list ):
355
+ algorithms = [algorithms ] if algorithms else []
356
+
357
+ # Set the property for use elsewhere
358
+ self .JWT_ALGORITHMS = algorithms
359
+ return algorithms
0 commit comments