@@ -378,4 +378,104 @@ def test_get_authentication_retries_5xx_responses(self, mock_request):
378378 self .assertEqual (mock_request .call_count , 4 ) # 3 retries, 1 success
379379 self .assertEqual (auth_header , {"Authorization" : "Bearer AABBCCDD" })
380380
381+ @patch .object (rest .RESTClientObject , "request" )
382+ def test_get_authentication_without_audience (self , mock_request ):
383+ """
384+ Test that audience is omitted from the token request when not provided
385+ (standard OAuth2 flow without Auth0 audience extension)
386+ """
387+ response_body = """
388+ {
389+ "expires_in": 120,
390+ "access_token": "AABBCCDD"
391+ }
392+ """
393+ mock_request .return_value = mock_response (response_body , 200 )
394+
395+ credentials = Credentials (
396+ method = "client_credentials" ,
397+ configuration = CredentialConfiguration (
398+ client_id = "myclientid" ,
399+ client_secret = "mysecret" ,
400+ api_issuer = "issuer.fga.example" ,
401+ ),
402+ )
403+ rest_client = rest .RESTClientObject (Configuration ())
404+ client = OAuth2Client (credentials )
405+ auth_header = client .get_authentication_header (rest_client )
406+ self .assertEqual (auth_header , {"Authorization" : "Bearer AABBCCDD" })
407+ expected_header = urllib3 .response .HTTPHeaderDict (
408+ {
409+ "Accept" : "application/json" ,
410+ "Content-Type" : "application/x-www-form-urlencoded" ,
411+ "User-Agent" : USER_AGENT ,
412+ }
413+ )
414+ mock_request .assert_called_once_with (
415+ method = "POST" ,
416+ url = "https://issuer.fga.example/oauth/token" ,
417+ headers = expected_header ,
418+ query_params = None ,
419+ body = None ,
420+ _preload_content = True ,
421+ _request_timeout = None ,
422+ post_params = {
423+ "client_id" : "myclientid" ,
424+ "client_secret" : "mysecret" ,
425+ "grant_type" : "client_credentials" ,
426+ },
427+ )
428+ rest_client .close ()
429+
430+ @patch .object (rest .RESTClientObject , "request" )
431+ def test_get_authentication_with_scopes_no_audience (self , mock_request ):
432+ """
433+ Test that scope is sent and audience is omitted when only scopes are provided
434+ (standard OAuth2 flow)
435+ """
436+ response_body = """
437+ {
438+ "expires_in": 120,
439+ "access_token": "AABBCCDD"
440+ }
441+ """
442+ mock_request .return_value = mock_response (response_body , 200 )
443+
444+ credentials = Credentials (
445+ method = "client_credentials" ,
446+ configuration = CredentialConfiguration (
447+ client_id = "myclientid" ,
448+ client_secret = "mysecret" ,
449+ api_issuer = "issuer.fga.example" ,
450+ scopes = "read write" ,
451+ ),
452+ )
453+ rest_client = rest .RESTClientObject (Configuration ())
454+ client = OAuth2Client (credentials )
455+ auth_header = client .get_authentication_header (rest_client )
456+ self .assertEqual (auth_header , {"Authorization" : "Bearer AABBCCDD" })
457+ expected_header = urllib3 .response .HTTPHeaderDict (
458+ {
459+ "Accept" : "application/json" ,
460+ "Content-Type" : "application/x-www-form-urlencoded" ,
461+ "User-Agent" : USER_AGENT ,
462+ }
463+ )
464+ mock_request .assert_called_once_with (
465+ method = "POST" ,
466+ url = "https://issuer.fga.example/oauth/token" ,
467+ headers = expected_header ,
468+ query_params = None ,
469+ body = None ,
470+ _preload_content = True ,
471+ _request_timeout = None ,
472+ post_params = {
473+ "client_id" : "myclientid" ,
474+ "client_secret" : "mysecret" ,
475+ "grant_type" : "client_credentials" ,
476+ "scope" : "read write" ,
477+ },
478+ )
479+ rest_client .close ()
480+
381481 rest_client .close ()
0 commit comments