Skip to content

Commit dff27f9

Browse files
committed
Support path in acquire_token_interactive
1 parent b07337f commit dff27f9

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

msal/__main__.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,26 @@
1212
"""
1313
import base64, getpass, json, logging, sys, msal
1414

15+
# This tester can test scenarios of these apps
1516
_AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46"
1617
_VISUAL_STUDIO = "04f0c124-f2bc-4f59-8241-bf6df9866bbd"
18+
_WHITE_BOARD = "95de633a-083e-42f5-b444-a4295d8e9314"
19+
_KNOWN_APPS = {
20+
_AZURE_CLI: {
21+
"client_id": _AZURE_CLI,
22+
"name": "Azure CLI (Correctly configured for MSA-PT)",
23+
"path_in_redirect_uri": None,
24+
},
25+
_VISUAL_STUDIO: {
26+
"client_id": _VISUAL_STUDIO,
27+
"name": "Visual Studio (Correctly configured for MSA-PT)",
28+
"path_in_redirect_uri": None,
29+
},
30+
_WHITE_BOARD: {
31+
"client_id": _WHITE_BOARD,
32+
"name": "Whiteboard Services (Non MSA-PT app. Accepts AAD & MSA accounts.)",
33+
},
34+
}
1735

1836
def print_json(blob):
1937
print(json.dumps(blob, indent=2, sort_keys=True))
@@ -82,6 +100,13 @@ def _acquire_token_silent(app):
82100
force_refresh=_input_boolean("Bypass MSAL Python's token cache?"),
83101
))
84102

103+
def _get_redirect_uri_path(app):
104+
if app._enable_broker:
105+
return None
106+
if "path_in_redirect_uri" in _KNOWN_APPS.get(app.client_id, {}):
107+
return _KNOWN_APPS[app.client_id]["path_in_redirect_uri"]
108+
return input("What is the path in this app's redirect_uri?")
109+
85110
def _acquire_token_interactive(app, scopes=None, data=None):
86111
"""acquire_token_interactive() - User will be prompted if app opts to do select_account."""
87112
scopes = scopes or _input_scopes() # Let user input scope param before less important prompt and login_hint
@@ -108,6 +133,7 @@ def _acquire_token_interactive(app, scopes=None, data=None):
108133
_AZURE_CLI, _VISUAL_STUDIO,
109134
], # Here this test app mimics the setting for some known MSA-PT apps
110135
prompt=prompt, login_hint=login_hint, data=data or {},
136+
path=_get_redirect_uri_path(app),
111137
)
112138
if login_hint and "id_token_claims" in result:
113139
signed_in_user = result.get("id_token_claims", {}).get("preferred_username")
@@ -181,11 +207,8 @@ def _exit(app):
181207

182208
def _main():
183209
print("Welcome to the Msal Python {} Tester (Experimental)\n".format(msal.__version__))
184-
chosen_app = _select_options([
185-
{"client_id": _AZURE_CLI, "name": "Azure CLI (Correctly configured for MSA-PT)"},
186-
{"client_id": _VISUAL_STUDIO, "name": "Visual Studio (Correctly configured for MSA-PT)"},
187-
{"client_id": "95de633a-083e-42f5-b444-a4295d8e9314", "name": "Whiteboard Services (Non MSA-PT app. Accepts AAD & MSA accounts.)"},
188-
],
210+
chosen_app = _select_options(
211+
list(_KNOWN_APPS.values()),
189212
option_renderer=lambda a: a["name"],
190213
header="Impersonate this app (or you can type in the client_id of your own app)",
191214
accept_nonempty_string=True)

msal/application.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,7 @@ def acquire_token_interactive(
17471747
max_age=None,
17481748
parent_window_handle=None,
17491749
on_before_launching_ui=None,
1750+
path=None,
17501751
**kwargs):
17511752
"""Acquire token interactively i.e. via a local browser.
17521753
@@ -1786,8 +1787,21 @@ def acquire_token_interactive(
17861787
17871788
:param int port:
17881789
The port to be used to listen to an incoming auth response.
1789-
By default we will use a system-allocated port.
1790-
(The rest of the redirect_uri is hard coded as ``http://localhost``.)
1790+
By default, a system-allocated port will be used.
1791+
The unspecified parts of the ``redirect_uri`` are hard coded as
1792+
``http://localhost``.
1793+
Only useful when using system browser authentication
1794+
(i.e., not an authentication brokerlike WAM).
1795+
1796+
:param str path:
1797+
The path to be used in the redirect URI.
1798+
By default, no path is used.
1799+
The unspecified parts of the ``redirect_uri`` are hard coded as
1800+
``http://localhost``.
1801+
Only useful when using system browser authentication
1802+
(i.e., not an authentication brokerlike WAM).
1803+
1804+
New in version 1.25.0.
17911805
17921806
:param list extra_scopes_to_consent:
17931807
"Extra scopes to consent" is a concept only available in AAD.
@@ -1877,9 +1891,12 @@ def acquire_token_interactive(
18771891
response = _clean_up(self.client.obtain_token_by_browser(
18781892
scope=self._decorate_scope(scopes) if scopes else None,
18791893
extra_scope_to_consent=extra_scopes_to_consent,
1880-
redirect_uri="http://localhost:{port}".format(
1894+
redirect_uri="http://localhost:{port}/{path}".format(
18811895
# Hardcode the host, for now. AAD portal rejects 127.0.0.1 anyway
1882-
port=port or 0),
1896+
port=port or 0,
1897+
path=path or "", # There could be multiple localhost uri only differ by path
1898+
# https://learn.microsoft.com/en-us/azure/active-directory/develop/reply-url#localhost-exceptions
1899+
),
18831900
prompt=prompt,
18841901
login_hint=login_hint,
18851902
max_age=max_age,

0 commit comments

Comments
 (0)