@@ -262,14 +262,15 @@ def _verify(
262262 request : Optional [Union [dict , Message ]] = None ,
263263 authorization_token : Optional [str ] = None ,
264264 endpoint = None , # Optional[Endpoint]
265+ get_client_id_from_token = None ,
265266 ** kwargs ,
266267 ):
267268 _token = request .get ("access_token" )
268269 if _token is None :
269270 raise ClientAuthenticationError ("No access token" )
270271
271272 res = {"token" : _token }
272- _client_id = request . get ( "client_id" )
273+ _client_id = get_client_id_from_token ( endpoint_context , _token , request )
273274 if _client_id :
274275 res ["client_id" ] = _client_id
275276 return res
@@ -483,6 +484,7 @@ def verify_client(
483484
484485 auth_info = {}
485486 methods = endpoint_context .client_authn_method
487+ client_id = None
486488 allowed_methods = getattr (endpoint , "client_authn_method" )
487489 if not allowed_methods :
488490 allowed_methods = list (methods .keys ())
@@ -499,48 +501,47 @@ def verify_client(
499501 endpoint = endpoint ,
500502 get_client_id_from_token = get_client_id_from_token ,
501503 )
502- break
503504 except (BearerTokenAuthenticationError , ClientAuthenticationError ):
504505 raise
505506 except Exception as err :
506507 logger .info ("Verifying auth using {} failed: {}" .format (_method .tag , err ))
508+ continue
507509
508- if auth_info .get ("method" ) == "none" :
509- return auth_info
510+ if auth_info .get ("method" ) == "none" and auth_info . get ( "client_id" ) is None :
511+ break
510512
511- client_id = auth_info .get ("client_id" )
512- if client_id is None :
513- raise ClientAuthenticationError ("Failed to verify client" )
513+ client_id = auth_info .get ("client_id" )
514+ if client_id is None :
515+ raise ClientAuthenticationError ("Failed to verify client" )
514516
515- if also_known_as :
516- client_id = also_known_as [client_id ]
517- auth_info ["client_id" ] = client_id
517+ if also_known_as :
518+ client_id = also_known_as [client_id ]
519+ auth_info ["client_id" ] = client_id
518520
519- if client_id not in endpoint_context .cdb :
520- raise UnknownClient ("Unknown Client ID" )
521+ if client_id not in endpoint_context .cdb :
522+ raise UnknownClient ("Unknown Client ID" )
521523
522- _cinfo = endpoint_context .cdb [client_id ]
524+ _cinfo = endpoint_context .cdb [client_id ]
523525
524- if not valid_client_info (_cinfo ):
525- logger .warning ("Client registration has timed out or " "client secret is expired." )
526- raise InvalidClient ("Not valid client" )
526+ if not valid_client_info (_cinfo ):
527+ logger .warning ("Client registration has timed out or " "client secret is expired." )
528+ raise InvalidClient ("Not valid client" )
527529
528- # Validate that the used method is allowed for this client/endpoint
529- client_allowed_methods = _cinfo .get (
530- f"{ endpoint .endpoint_name } _client_authn_method" , _cinfo .get ("client_authn_method" )
531- )
532- if client_allowed_methods is not None and _method and _method .tag not in client_allowed_methods :
533- logger .info (
534- f"Allowed methods for client: { client_id } at endpoint: { endpoint .name } are: "
535- f"`{ ', ' .join (client_allowed_methods )} `"
536- )
537- raise UnAuthorizedClient (
538- f"Authentication method: { _method .tag } not allowed for client: { client_id } in "
539- f"endpoint: { endpoint .name } "
530+ # Validate that the used method is allowed for this client/endpoint
531+ client_allowed_methods = _cinfo .get (
532+ f"{ endpoint .endpoint_name } _client_authn_method" , _cinfo .get ("client_authn_method" )
540533 )
534+ if client_allowed_methods is not None and auth_info ["method" ] not in client_allowed_methods :
535+ logger .info (
536+ f"Allowed methods for client: { client_id } at endpoint: { endpoint .name } are: "
537+ f"`{ ', ' .join (client_allowed_methods )} `"
538+ )
539+ auth_info = {}
540+ continue
541+ break
541542
542543 # store what authn method was used
543- if auth_info . get ( "method" ) :
544+ if "method" in auth_info and client_id :
544545 _request_type = request .__class__ .__name__
545546 _used_authn_method = _cinfo .get ("auth_method" )
546547 if _used_authn_method :
0 commit comments