@@ -19,12 +19,12 @@ def _get_app_and_auth_code(
19
19
authority = "https://login.microsoftonline.com/common" ,
20
20
port = 44331 ,
21
21
scopes = ["https://graph.microsoft.com/.default" ], # Microsoft Graph
22
- ):
22
+ ** kwargs ):
23
23
from msal .oauth2cli .authcode import obtain_auth_code
24
24
app = msal .ClientApplication (client_id , client_secret , authority = authority )
25
25
redirect_uri = "http://localhost:%d" % port
26
26
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 ))
28
28
assert ac is not None
29
29
return (app , ac , redirect_uri )
30
30
@@ -124,20 +124,20 @@ def test_username_password(self):
124
124
self .skipUnlessWithConfig (["client_id" , "username" , "password" , "scope" ])
125
125
self ._test_username_password (** self .config )
126
126
127
- def _get_app_and_auth_code (self ):
127
+ def _get_app_and_auth_code (self , ** kwargs ):
128
128
return _get_app_and_auth_code (
129
129
self .config ["client_id" ],
130
130
client_secret = self .config .get ("client_secret" ),
131
131
authority = self .config .get ("authority" ),
132
132
port = self .config .get ("listen_port" , 44331 ),
133
133
scopes = self .config ["scope" ],
134
- )
134
+ ** kwargs )
135
135
136
- def test_auth_code (self ):
136
+ def _test_auth_code (self , auth_kwargs , token_kwargs ):
137
137
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 )
139
139
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 )
141
141
logger .debug ("%s.cache = %s" ,
142
142
self .id (), json .dumps (self .app .token_cache ._cache , indent = 4 ))
143
143
self .assertIn (
@@ -148,6 +148,18 @@ def test_auth_code(self):
148
148
error_description = result .get ("error_description" )))
149
149
self .assertCacheWorksForUser (result , self .config ["scope" ], username = None )
150
150
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" )
151
163
152
164
def test_ssh_cert (self ):
153
165
self .skipUnlessWithConfig (["client_id" , "scope" ])
@@ -412,22 +424,22 @@ def test_adfs2019_onprem_acquire_token_by_auth_code(self):
412
424
self .assertCacheWorksForUser (result , scopes , username = None )
413
425
414
426
@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 " )
417
429
def test_acquire_token_obo (self ):
418
430
# 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 " ]
421
433
config = self .get_lab_user (usertype = "cloud" )
422
434
423
435
# 1. An app obtains a token representing a user, for our mid-tier service
424
436
pca = msal .PublicClientApplication (
425
- "be9b0186-7dfd-448a-a944-f771029105bf " , authority = config .get ("authority" ))
437
+ "c0485386-1e9a-4663-bc96-7ab30656de7f " , authority = config .get ("authority" ))
426
438
pca_result = pca .acquire_token_by_username_password (
427
439
config ["username" ],
428
440
self .get_lab_user_secret (config ["lab_name" ]),
429
441
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 ],
431
443
)
432
444
self .assertIsNotNone (
433
445
pca_result .get ("access_token" ),
@@ -436,7 +448,7 @@ def test_acquire_token_obo(self):
436
448
# 2. Our mid-tier service uses OBO to obtain a token for downstream service
437
449
cca = msal .ConfidentialClientApplication (
438
450
obo_client_id ,
439
- client_credential = os .getenv ("OBO_CLIENT_SECRET " ),
451
+ client_credential = os .getenv ("LAB_OBO_CLIENT_SECRET " ),
440
452
authority = config .get ("authority" ),
441
453
# token_cache= ..., # Default token cache is all-tokens-store-in-memory.
442
454
# That's fine if OBO app uses short-lived msal instance per session.
0 commit comments