Skip to content

Commit cf41a79

Browse files
committed
Refactor RedirectUriError
1 parent 6a1ffa6 commit cf41a79

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

msal/application.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ def _acquire_token_silent_from_cache_and_possibly_refresh_it(
12391239
))
12401240
return _clean_up(response)
12411241
except ImportError:
1242-
logger.exception("Mid-tier is not available in current version of Windows")
1242+
logger.warning("PyMsalRuntime is not available")
12431243
result = _clean_up(self._acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family(
12441244
authority, self._decorate_scope(scopes), account,
12451245
refresh_reason=refresh_reason, claims_challenge=claims_challenge,
@@ -1584,9 +1584,9 @@ def acquire_token_interactive(
15841584
self._client_capabilities, claims_challenge)
15851585
if sys.platform == "win32":
15861586
try:
1587-
from .wam import _signin_interactively, NeedRedirectURI
1587+
from .wam import _signin_interactively, RedirectUriError
15881588
if extra_scopes_to_consent: # TODO: Not supported in WAM/Mid-tier
1589-
logger.warn(
1589+
logger.warning(
15901590
"Ignoring parameter extra_scopes_to_consent, "
15911591
"which is not supported on current platform")
15921592
response = _signin_interactively(
@@ -1609,9 +1609,9 @@ def acquire_token_interactive(
16091609
))
16101610
return _clean_up(response)
16111611
except ImportError:
1612-
logger.exception("Mid-tier is not available in current version of Windows")
1613-
except NeedRedirectURI as e: # Experimental: Catch, log, and fallback
1614-
logger.warning(e)
1612+
logger.warning("PyMsalRuntime is not available")
1613+
except RedirectUriError as e: # Experimental: Catch, log, and fallback
1614+
logger.warning(str(e) + " Now we fallback to use browser.")
16151615

16161616
self._validate_ssh_cert_input_data(kwargs.get("data", {}))
16171617
telemetry_context = self._build_telemetry_context(

msal/wam.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
logger = logging.getLogger(__name__)
1414

1515

16-
class NeedRedirectURI(ValueError):
16+
class RedirectUriError(ValueError):
1717
pass
1818

1919

tests/test_wam.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,31 @@
22
import logging
33
import sys
44

5+
if not sys.platform.startswith("win"):
6+
raise unittest.SkipTest("requires Windows")
7+
from msal.wam import ( # Import them after the platform check
8+
_signin_interactively, _acquire_token_silently, RedirectUriError)
9+
10+
511
logging.basicConfig(level=logging.DEBUG)
612

7-
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
813
class TestWam(unittest.TestCase):
14+
_authority = "https://login.microsoftonline.com/common"
15+
_scopes = ["https://graph.microsoft.com/.default"]
16+
917
def test_interactive_then_silent(self):
10-
from msal.wam import _signin_interactively, _acquire_token_silently # Lazy import
1118
client_id = "26a7ee05-5602-4d76-a7ba-eae8b7b67941" # A pre-configured test app
12-
authority = "https://login.microsoftonline.com/common"
13-
scopes = ["https://graph.microsoft.com/.default"]
1419

15-
result = _signin_interactively(authority, client_id, scopes)
20+
result = _signin_interactively(self._authority, client_id, self._scopes)
1621
self.assertIsNotNone(result.get("access_token"), result)
1722

1823
account_id = result["_account_id"]
19-
result = _acquire_token_silently(authority, client_id, account_id, scopes)
24+
result = _acquire_token_silently(self._authority, client_id, account_id, self._scopes)
2025
self.assertIsNotNone(result.get("access_token"), result)
2126

27+
def test_unconfigured_app_should_raise_exception(self):
28+
client_id = "289a413d-284b-4303-9c79-94380abe5d22" # A test app without proper redirect_uri
29+
with self.assertRaises(RedirectUriError):
30+
_signin_interactively(self._authority, client_id, self._scopes)
31+
# Note: _acquire_token_silently() would raise same exception,
32+
# we skip its test here due to the lack of a valid account_id

0 commit comments

Comments
 (0)