@@ -19,12 +19,12 @@ def _get_app_and_auth_code(
1919 authority = "https://login.microsoftonline.com/common" ,
2020 port = 44331 ,
2121 scopes = ["https://graph.microsoft.com/.default" ], # Microsoft Graph
22- ):
22+ ** kwargs ):
2323 from msal .oauth2cli .authcode import obtain_auth_code
2424 app = msal .ClientApplication (client_id , client_secret , authority = authority )
2525 redirect_uri = "http://localhost:%d" % port
2626 ac = obtain_auth_code (port , auth_uri = app .get_authorization_request_url (
27- scopes , redirect_uri = redirect_uri ))
27+ scopes , redirect_uri = redirect_uri , ** kwargs ))
2828 assert ac is not None
2929 return (app , ac , redirect_uri )
3030
@@ -124,20 +124,20 @@ def test_username_password(self):
124124 self .skipUnlessWithConfig (["client_id" , "username" , "password" , "scope" ])
125125 self ._test_username_password (** self .config )
126126
127- def _get_app_and_auth_code (self ):
127+ def _get_app_and_auth_code (self , ** kwargs ):
128128 return _get_app_and_auth_code (
129129 self .config ["client_id" ],
130130 client_secret = self .config .get ("client_secret" ),
131131 authority = self .config .get ("authority" ),
132132 port = self .config .get ("listen_port" , 44331 ),
133133 scopes = self .config ["scope" ],
134- )
134+ ** kwargs )
135135
136- def test_auth_code (self ):
136+ def _test_auth_code (self , auth_kwargs , token_kwargs ):
137137 self .skipUnlessWithConfig (["client_id" , "scope" ])
138- (self .app , ac , redirect_uri ) = self ._get_app_and_auth_code ()
138+ (self .app , ac , redirect_uri ) = self ._get_app_and_auth_code (** auth_kwargs )
139139 result = self .app .acquire_token_by_authorization_code (
140- ac , self .config ["scope" ], redirect_uri = redirect_uri )
140+ ac , self .config ["scope" ], redirect_uri = redirect_uri , ** token_kwargs )
141141 logger .debug ("%s.cache = %s" ,
142142 self .id (), json .dumps (self .app .token_cache ._cache , indent = 4 ))
143143 self .assertIn (
@@ -148,6 +148,18 @@ def test_auth_code(self):
148148 error_description = result .get ("error_description" )))
149149 self .assertCacheWorksForUser (result , self .config ["scope" ], username = None )
150150
151+ def test_auth_code (self ):
152+ self ._test_auth_code ({}, {})
153+
154+ def test_auth_code_with_matching_nonce (self ):
155+ self ._test_auth_code ({"nonce" : "foo" }, {"nonce" : "foo" })
156+
157+ def test_auth_code_with_mismatching_nonce (self ):
158+ self .skipUnlessWithConfig (["client_id" , "scope" ])
159+ (self .app , ac , redirect_uri ) = self ._get_app_and_auth_code (nonce = "foo" )
160+ with self .assertRaises (ValueError ):
161+ self .app .acquire_token_by_authorization_code (
162+ ac , self .config ["scope" ], redirect_uri = redirect_uri , nonce = "bar" )
151163
152164 def test_ssh_cert (self ):
153165 self .skipUnlessWithConfig (["client_id" , "scope" ])
@@ -412,22 +424,22 @@ def test_adfs2019_onprem_acquire_token_by_auth_code(self):
412424 self .assertCacheWorksForUser (result , scopes , username = None )
413425
414426 @unittest .skipUnless (
415- os .getenv ("OBO_CLIENT_SECRET " ),
416- "Need OBO_CLIENT_SECRET from https://buildautomation .vault.azure.net/secrets/IdentityDivisionDotNetOBOServiceSecret " )
427+ os .getenv ("LAB_OBO_CLIENT_SECRET " ),
428+ "Need LAB_OBO_CLIENT SECRET from https://msidlabs .vault.azure.net/secrets/TodoListServiceV2-OBO/c58ba97c34ca4464886943a847d1db56 " )
417429 def test_acquire_token_obo (self ):
418430 # Some hardcoded, pre-defined settings
419- obo_client_id = "23c64cd8-21e4-41dd-9756-ab9e2c23f58c "
420- downstream_scopes = ["https://graph.microsoft.com/User.Read " ]
431+ obo_client_id = "f4aa5217-e87c-42b2-82af-5624dd14ee72 "
432+ downstream_scopes = ["https://graph.microsoft.com/.default " ]
421433 config = self .get_lab_user (usertype = "cloud" )
422434
423435 # 1. An app obtains a token representing a user, for our mid-tier service
424436 pca = msal .PublicClientApplication (
425- "be9b0186-7dfd-448a-a944-f771029105bf " , authority = config .get ("authority" ))
437+ "c0485386-1e9a-4663-bc96-7ab30656de7f " , authority = config .get ("authority" ))
426438 pca_result = pca .acquire_token_by_username_password (
427439 config ["username" ],
428440 self .get_lab_user_secret (config ["lab_name" ]),
429441 scopes = [ # The OBO app's scope. Yours might be different.
430- "%s/access_as_user " % obo_client_id ],
442+ "api:// %s/read " % obo_client_id ],
431443 )
432444 self .assertIsNotNone (
433445 pca_result .get ("access_token" ),
@@ -436,7 +448,7 @@ def test_acquire_token_obo(self):
436448 # 2. Our mid-tier service uses OBO to obtain a token for downstream service
437449 cca = msal .ConfidentialClientApplication (
438450 obo_client_id ,
439- client_credential = os .getenv ("OBO_CLIENT_SECRET " ),
451+ client_credential = os .getenv ("LAB_OBO_CLIENT_SECRET " ),
440452 authority = config .get ("authority" ),
441453 # token_cache= ..., # Default token cache is all-tokens-store-in-memory.
442454 # That's fine if OBO app uses short-lived msal instance per session.
0 commit comments