Skip to content

Commit

Permalink
Refactor RedirectUriError
Browse files Browse the repository at this point in the history
  • Loading branch information
rayluo committed Jan 25, 2022
1 parent 6a1ffa6 commit 66204a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
10 changes: 5 additions & 5 deletions msal/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ def _acquire_token_silent_from_cache_and_possibly_refresh_it(
))
return _clean_up(response)
except ImportError:
logger.exception("Mid-tier is not available in current version of Windows")
logger.warning("PyMsalRuntime is not available")
result = _clean_up(self._acquire_token_silent_by_finding_rt_belongs_to_me_or_my_family(
authority, self._decorate_scope(scopes), account,
refresh_reason=refresh_reason, claims_challenge=claims_challenge,
Expand Down Expand Up @@ -1584,9 +1584,9 @@ def acquire_token_interactive(
self._client_capabilities, claims_challenge)
if sys.platform == "win32":
try:
from .wam import _signin_interactively, NeedRedirectURI
from .wam import _signin_interactively, RedirectUriError
if extra_scopes_to_consent: # TODO: Not supported in WAM/Mid-tier
logger.warn(
logger.warning(
"Ignoring parameter extra_scopes_to_consent, "
"which is not supported on current platform")
response = _signin_interactively(
Expand All @@ -1609,8 +1609,8 @@ def acquire_token_interactive(
))
return _clean_up(response)
except ImportError:
logger.exception("Mid-tier is not available in current version of Windows")
except NeedRedirectURI as e: # Experimental: Catch, log, and fallback
logger.warning("PyMsalRuntime is not available")
except RedirectUriError as e: # Experimental: Catch, log, and fallback
logger.warning(e)

self._validate_ssh_cert_input_data(kwargs.get("data", {}))
Expand Down
2 changes: 1 addition & 1 deletion msal/wam.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
logger = logging.getLogger(__name__)


class NeedRedirectURI(ValueError):
class RedirectUriError(ValueError):
pass


Expand Down
23 changes: 17 additions & 6 deletions tests/test_wam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@
import logging
import sys

if not sys.platform.startswith("win"):
raise unittest.SkipTest("requires Windows")
from msal.wam import ( # Import them after the platform check
_signin_interactively, _acquire_token_silently, RedirectUriError)


logging.basicConfig(level=logging.DEBUG)

@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
class TestWam(unittest.TestCase):
_authority = "https://login.microsoftonline.com/common"
_scopes = ["https://graph.microsoft.com/.default"]

def test_interactive_then_silent(self):
from msal.wam import _signin_interactively, _acquire_token_silently # Lazy import
client_id = "26a7ee05-5602-4d76-a7ba-eae8b7b67941" # A pre-configured test app
authority = "https://login.microsoftonline.com/common"
scopes = ["https://graph.microsoft.com/.default"]

result = _signin_interactively(authority, client_id, scopes)
result = _signin_interactively(self._authority, client_id, self._scopes)
self.assertIsNotNone(result.get("access_token"), result)

account_id = result["_account_id"]
result = _acquire_token_silently(authority, client_id, account_id, scopes)
result = _acquire_token_silently(self._authority, client_id, account_id, self._scopes)
self.assertIsNotNone(result.get("access_token"), result)

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

0 comments on commit 66204a9

Please sign in to comment.