Skip to content

Commit 1641506

Browse files
committed
Now msal auth code flow explicitly accepts and validates nonce
1 parent 6b52b30 commit 1641506

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

msal/application.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ def get_authorization_request_url(
229229
redirect_uri=None,
230230
response_type="code", # Can be "token" if you use Implicit Grant
231231
prompt=None,
232+
nonce=None,
232233
**kwargs):
233234
"""Constructs a URL for you to start a Authorization Code Grant.
234235
@@ -247,6 +248,9 @@ def get_authorization_request_url(
247248
You will have to specify a value explicitly.
248249
Its valid values are defined in Open ID Connect specs
249250
https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest
251+
:param nonce:
252+
A cryptographically random value used to mitigate replay attacks. See also
253+
`OIDC specs <https://openid.net/specs/openid-connect-core-1_0.html#AuthRequest>`_.
250254
:return: The authorization url as a string.
251255
"""
252256
""" # TBD: this would only be meaningful in a new acquire_token_interactive()
@@ -276,6 +280,7 @@ def get_authorization_request_url(
276280
redirect_uri=redirect_uri, state=state, login_hint=login_hint,
277281
prompt=prompt,
278282
scope=decorate_scope(scopes, self.client_id),
283+
nonce=nonce,
279284
)
280285

281286
def acquire_token_by_authorization_code(
@@ -286,6 +291,7 @@ def acquire_token_by_authorization_code(
286291
# REQUIRED, if the "redirect_uri" parameter was included in the
287292
# authorization request as described in Section 4.1.1, and their
288293
# values MUST be identical.
294+
nonce=None,
289295
**kwargs):
290296
"""The second half of the Authorization Code Grant.
291297
@@ -306,6 +312,11 @@ def acquire_token_by_authorization_code(
306312
So the developer need to specify a scope so that we can restrict the
307313
token to be issued for the corresponding audience.
308314
315+
:param nonce:
316+
If you provided a nonce when calling :func:`get_authorization_request_url`,
317+
same nonce should also be provided here, so that we'll validate it.
318+
An exception will be raised if the nonce in id token mismatches.
319+
309320
:return: A dict representing the json response from AAD:
310321
311322
- A successful response would contain "access_token" key,
@@ -326,6 +337,7 @@ def acquire_token_by_authorization_code(
326337
CLIENT_CURRENT_TELEMETRY: _build_current_telemetry_request_header(
327338
self.ACQUIRE_TOKEN_BY_AUTHORIZATION_CODE_ID),
328339
},
340+
nonce=nonce,
329341
**kwargs)
330342

331343
def get_accounts(self, username=None):

0 commit comments

Comments
 (0)