Skip to content

Commit 36326ca

Browse files
committed
Use unsigned redirect uri for mac broker flows
1 parent faf5dce commit 36326ca

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

msal/broker.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ class TokenTypeError(ValueError):
4545
pass
4646

4747

48-
_redirect_uri_on_mac = "msauth.com.msauth.unsignedapp://auth" # Note:
48+
_default_redirect_uri_on_mac = "msauth.com.msauth.unsignedapp://auth" # Note:
4949
# On Mac, the native Python has a team_id which links to bundle id
5050
# com.apple.python3 however it won't give Python scripts better security.
5151
# Besides, the homebrew-installed Pythons have no team_id
5252
# so they have to use a generic placeholder anyway.
5353
# The v-team chose to combine two situations into using same placeholder.
5454

55+
_default_redirect_uri = "https://login.microsoftonline.com/common/oauth2/nativeclient"
56+
# Linux Java Broker requires a non-empty valid redirect_uri.
57+
# On Windows, WAM does not currently use this default redirect_uri,
58+
# but MSAL.cpp still requires it to be non-empty and valid.
5559

5660
def _convert_error(error, client_id):
5761
context = error.get_context() # Available since pymsalruntime 0.0.4
@@ -63,8 +67,7 @@ def _convert_error(error, client_id):
6367
"""MsalRuntime needs the current app to register these redirect_uri
6468
(1) ms-appx-web://Microsoft.AAD.BrokerPlugin/{}
6569
(2) {}
66-
(3) https://login.microsoftonline.com/common/oauth2/nativeclient""".format(
67-
client_id, _redirect_uri_on_mac))
70+
(3) {}""".format(client_id, _default_redirect_uri_on_mac, _default_redirect_uri))
6871
# OTOH, AAD would emit other errors when other error handling branch was hit first,
6972
# so, the AADSTS50011/RedirectUriError is not guaranteed to happen.
7073
return {
@@ -145,20 +148,19 @@ def _build_msal_runtime_auth_params(client_id, authority):
145148
params.set_additional_parameter("msal_client_ver", __version__)
146149
return params
147150

148-
def _set_redirect_uri_for_linux(params):
149-
if sys.platform == "linux":
150-
# This is required by Linux Java Broker to set a non-empty valid redirect_uri
151-
params.set_redirect_uri(
152-
"https://login.microsoftonline.com/common/oauth2/nativeclient"
153-
)
151+
def _set_redirect_uri(params):
152+
if sys.platform == "darwin":
153+
params.set_redirect_uri(_default_redirect_uri_on_mac)
154+
else:
155+
params.set_redirect_uri(_default_redirect_uri)
154156

155157
def _signin_silently(
156158
authority, client_id, scopes, correlation_id=None, claims=None,
157159
enable_msa_pt=False,
158160
auth_scheme=None,
159161
**kwargs):
160162
params = _build_msal_runtime_auth_params(client_id, authority)
161-
_set_redirect_uri_for_linux(params)
163+
_set_redirect_uri(params)
162164
params.set_requested_scopes(scopes)
163165
if claims:
164166
params.set_decoded_claims(claims)
@@ -193,12 +195,7 @@ def _signin_interactively(
193195
**kwargs):
194196
params = _build_msal_runtime_auth_params(client_id, authority)
195197
params.set_requested_scopes(scopes)
196-
params.set_redirect_uri(
197-
_redirect_uri_on_mac if sys.platform == "darwin" else
198-
"https://login.microsoftonline.com/common/oauth2/nativeclient"
199-
# This default redirect_uri value is not currently used by WAM
200-
# but it is required by the MSAL.cpp to be set to a non-empty valid URI.
201-
)
198+
_set_redirect_uri(params)
202199
if prompt:
203200
if prompt == "select_account":
204201
if login_hint:
@@ -248,7 +245,7 @@ def _acquire_token_silently(
248245
if account is None:
249246
return
250247
params = _build_msal_runtime_auth_params(client_id, authority)
251-
_set_redirect_uri_for_linux(params)
248+
_set_redirect_uri(params)
252249
params.set_requested_scopes(scopes)
253250
if claims:
254251
params.set_decoded_claims(claims)

0 commit comments

Comments
 (0)