|
"""If the following ENV VAR were available, many end-to-end test cases would run. |
|
LAB_OBO_CLIENT_SECRET=... |
|
LAB_APP_CLIENT_ID=... |
|
LAB_APP_CLIENT_CERT_PFX_PATH=... |
|
LAB_OBO_PUBLIC_CLIENT_ID=... |
|
LAB_OBO_CONFIDENTIAL_CLIENT_ID=... |
|
""" |
|
try: |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
except: |
|
pass |
|
import base64 |
|
import logging |
|
import os |
|
import json |
|
import time |
|
import unittest |
|
import sys |
|
try: |
|
from unittest.mock import patch, ANY |
|
except: |
|
from mock import patch, ANY |
|
|
|
import requests |
|
|
|
import msal |
|
from tests.http_client import MinimalHttpClient, MinimalResponse |
|
from msal.oauth2cli import AuthCodeReceiver |
|
from msal.oauth2cli.oidc import decode_part |
|
|
|
try: |
|
import pymsalruntime |
|
broker_available = True |
|
except ImportError: |
|
broker_available = False |
|
logger = logging.getLogger(__name__) |
|
logging.basicConfig(level=logging.DEBUG if "-v" in sys.argv else logging.INFO) |
|
|
|
try: |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
except ImportError: |
|
logger.warn("Run pip install -r requirements.txt for optional dependency") |
|
|
|
_AZURE_CLI = "04b07795-8ddb-461a-bbee-02f9e1bf7b46" |
|
|
|
def _get_app_and_auth_code( |
|
client_id, |
|
client_secret=None, |
|
authority="https://login.microsoftonline.com/common", |
|
port=44331, |
|
scopes=["https://graph.microsoft.com/.default"], |
|
**kwargs): |
|
from msal.oauth2cli.authcode import obtain_auth_code |
|
if client_secret: |
|
app = msal.ConfidentialClientApplication( |
|
client_id, |
|
client_credential=client_secret, |
|
authority=authority, http_client=MinimalHttpClient()) |
|
else: |
|
app = msal.PublicClientApplication( |
|
client_id, authority=authority, http_client=MinimalHttpClient()) |
|
redirect_uri = "http://localhost:%d" % port |
|
ac = obtain_auth_code(port, auth_uri=app.get_authorization_request_url( |
|
scopes, redirect_uri=redirect_uri, **kwargs)) |
|
assert ac is not None |
|
return (app, ac, redirect_uri) |
|
|
|
def _render(url, description=None): |
|
|
|
return "<a href='{url}' target=_blank>{description}</a>".format( |
|
url=url, description=description) if description else url |
|
|
|
|
|
def _get_hint(html_mode=None, username=None, lab_name=None, username_uri=None): |
|
return "Sign in with {user} whose password is available from {lab}".format( |
|
user=("<b>{}</b>".format(username) if html_mode else username) |
|
if username |
|
else "the upn from {}".format(_render( |
|
username_uri, description="here" if html_mode else None)), |
|
lab=_render( |
|
"https://aka.ms/GetLabSecret?Secret=" + (lab_name or "msidlabXYZ"), |
|
description="this password api" if html_mode else None, |
|
), |
|
) |
|
|
|
|
|
@unittest.skipIf(os.getenv("TRAVIS_TAG"), "Skip e2e tests during tagged release") |
|
class E2eTestCase(unittest.TestCase): |
|
|
|
def assertLoosely(self, response, assertion=None, |
|
skippable_errors=("invalid_grant", "interaction_required")): |
|
if response.get("error") in skippable_errors: |
|
logger.debug("Response = %s", response) |
|
|
|
raise unittest.SkipTest(response.get("error_description")) |
|
else: |
|
if assertion is None: |
|
assertion = lambda: self.assertIn( |
|
"access_token", response, |
|
"{error}: {error_description}".format( |
|
|
|
error=response.get("error"), |
|
error_description=response.get("error_description"))) |
|
assertion() |
|
|
|
def assertCacheWorksForUser( |
|
self, result_from_wire, scope, username=None, data=None, auth_scheme=None): |
|
logger.debug( |
|
"%s: cache = %s, id_token_claims = %s", |
|
self.id(), |
|
json.dumps(self.app.token_cache._cache, indent=4), |
|
json.dumps(result_from_wire.get("id_token_claims"), indent=4), |
|
) |
|
|
|
accounts = self.app.get_accounts(username=username) |
|
self.assertNotEqual(0, len(accounts)) |
|
account = accounts[0] |
|
if ("scope" not in result_from_wire |
|
or |
|
set(scope) <= set(result_from_wire["scope"].split(" ")) |
|
): |
|
|
|
silent_result = self.app.acquire_token_silent( |
|
scope, account=account, data=data or {}, auth_scheme=auth_scheme) |
|
self.assertIsNotNone(silent_result) |
|
self.assertIsNone( |
|
silent_result.get("refresh_token"), "acquire_token_silent() should return no RT") |
|
if auth_scheme: |
|
self.assertNotEqual( |
|
self.app._TOKEN_SOURCE_CACHE, silent_result[self.app._TOKEN_SOURCE]) |
|
else: |
|
self.assertEqual( |
|
self.app._TOKEN_SOURCE_CACHE, silent_result[self.app._TOKEN_SOURCE]) |
|
|
|
if "refresh_token" in result_from_wire: |
|
assert auth_scheme is None |
|
|
|
self.app.token_cache._cache["AccessToken"] = {} |
|
silent_result = self.app.acquire_token_silent( |
|
scope, account=account, data=data or {}) |
|
self.assertIsNotNone(silent_result, |
|
"We should get a result from acquire_token_silent(...) call") |
|
self.assertEqual( |
|
|
|
|
|
|
|
|
|
|
|
|
|
self.app._TOKEN_SOURCE_IDP, silent_result[self.app._TOKEN_SOURCE]) |
|
|
|
def assertCacheWorksForApp(self, result_from_wire, scope): |
|
logger.debug( |
|
"%s: cache = %s, id_token_claims = %s", |
|
self.id(), |
|
json.dumps(self.app.token_cache._cache, indent=4), |
|
json.dumps(result_from_wire.get("id_token_claims"), indent=4), |
|
) |
|
self.assertIsNone( |
|
self.app.acquire_token_silent(scope, account=None), |
|
"acquire_token_silent(..., account=None) shall always return None") |
|
|
|
silent_result = self.app.acquire_token_for_client(scope) |
|
self.assertIsNotNone(silent_result) |
|
self.assertEqual(self.app._TOKEN_SOURCE_CACHE, silent_result[self.app._TOKEN_SOURCE]) |
|
|
|
@classmethod |
|
def _build_app(cls, |
|
client_id, |
|
client_credential=None, |
|
authority="https://login.microsoftonline.com/common", |
|
scopes=["https://graph.microsoft.com/.default"], |
|
http_client=None, |
|
azure_region=None, |
|
**kwargs): |
|
if client_credential: |
|
return msal.ConfidentialClientApplication( |
|
client_id, |
|
client_credential=client_credential, |
|
authority=authority, |
|
azure_region=azure_region, |
|
http_client=http_client or MinimalHttpClient(), |
|
) |
|
else: |
|
|
|
try: |
|
import pymsalruntime |
|
broker_available = True |
|
except ImportError: |
|
broker_available = False |
|
return msal.PublicClientApplication( |
|
client_id, |
|
authority=authority, |
|
http_client=http_client or MinimalHttpClient(), |
|
enable_broker_on_windows=broker_available, |
|
) |
|
|
|
def _test_username_password(self, |
|
authority=None, client_id=None, username=None, password=None, scope=None, |
|
client_secret=None, |
|
azure_region=None, |
|
http_client=None, |
|
auth_scheme=None, |
|
**ignored): |
|
assert authority and client_id and username and password and scope |
|
self.app = self._build_app( |
|
client_id, authority=authority, |
|
http_client=http_client, |
|
azure_region=azure_region, |
|
|
|
client_credential=client_secret) |
|
self.assertEqual( |
|
self.app.get_accounts(username=username), [], "Cache starts empty") |
|
result = self.app.acquire_token_by_username_password( |
|
username, password, scopes=scope, auth_scheme=auth_scheme) |
|
self.assertLoosely(result) |
|
self.assertCacheWorksForUser( |
|
result, scope, |
|
username=username, |
|
auth_scheme=auth_scheme, |
|
) |
|
return result |
|
|
|
@unittest.skipIf( |
|
os.getenv("TRAVIS"), |
|
"Although it is doable, we still choose to skip device flow to save time") |
|
def _test_device_flow( |
|
self, client_id=None, authority=None, scope=None, **ignored): |
|
assert client_id and authority and scope |
|
self.app = self._build_app(client_id, authority=authority) |
|
flow = self.app.initiate_device_flow(scopes=scope) |
|
assert "user_code" in flow, "DF does not seem to be provisioned: %s".format( |
|
json.dumps(flow, indent=4)) |
|
logger.info(flow["message"]) |
|
|
|
duration = 60 |
|
logger.info("We will wait up to %d seconds for you to sign in" % duration) |
|
flow["expires_at"] = min( |
|
flow["expires_at"], time.time() + duration) |
|
result = self.app.acquire_token_by_device_flow(flow) |
|
self.assertLoosely( |
|
result, |
|
assertion=lambda: self.assertIn('access_token', result), |
|
skippable_errors=self.app.client.DEVICE_FLOW_RETRIABLE_ERRORS) |
|
if "access_token" not in result: |
|
self.skipTest("End user did not complete Device Flow in time") |
|
self.assertCacheWorksForUser(result, scope, username=None) |
|
result["access_token"] = result["refresh_token"] = "************" |
|
logger.info( |
|
"%s obtained tokens: %s", self.id(), json.dumps(result, indent=4)) |
|
|
|
@unittest.skipIf(os.getenv("TRAVIS"), "Browser automation is not yet implemented") |
|
def _test_acquire_token_interactive( |
|
self, client_id=None, authority=None, scope=None, port=None, |
|
username=None, lab_name=None, |
|
username_uri="", |
|
data=None, |
|
prompt=None, |
|
enable_msa_passthrough=None, |
|
auth_scheme=None, |
|
**ignored): |
|
assert client_id and authority and scope |
|
self.app = self._build_app(client_id, authority=authority) |
|
logger.info(_get_hint( |
|
username=username, lab_name=lab_name, username_uri=username_uri)) |
|
result = self.app.acquire_token_interactive( |
|
scope, |
|
login_hint=username, |
|
prompt=prompt, |
|
timeout=120, |
|
port=port, |
|
parent_window_handle=self.app.CONSOLE_WINDOW_HANDLE, |
|
enable_msa_passthrough=enable_msa_passthrough, |
|
welcome_template= |
|
"""<html><body><h1>{id}</h1><ol> |
|
<li>{hint}</li> |
|
<li><a href="$auth_uri">Sign In</a> or <a href="$abort_uri">Abort</a></li> |
|
</ol></body></html>""".format(id=self.id(), hint=_get_hint( |
|
html_mode=True, |
|
username=username, lab_name=lab_name, username_uri=username_uri)), |
|
auth_scheme=auth_scheme, |
|
data=data or {}, |
|
) |
|
self.assertIn( |
|
"access_token", result, |
|
"{error}: {error_description}".format( |
|
|
|
error=result.get("error"), |
|
error_description=result.get("error_description"))) |
|
if username and result.get("id_token_claims", {}).get("preferred_username"): |
|
self.assertEqual( |
|
username, result["id_token_claims"]["preferred_username"], |
|
"You are expected to sign in as account {}, but tokens returned is for {}".format( |
|
username, result["id_token_claims"]["preferred_username"])) |
|
self.assertCacheWorksForUser( |
|
result, scope, username=None, data=data or {}, auth_scheme=auth_scheme) |
|
return result |
|
|
|
|
|
@unittest.skipUnless( |
|
msal.application._is_running_in_cloud_shell(), |
|
"Manually run this test case from inside Cloud Shell") |
|
class CloudShellTestCase(E2eTestCase): |
|
app = msal.PublicClientApplication("client_id") |
|
scope_that_requires_no_managed_device = "https://management.core.windows.net/" |
|
def test_access_token_should_be_obtained_for_a_supported_scope(self): |
|
result = self.app.acquire_token_interactive( |
|
[self.scope_that_requires_no_managed_device], prompt="none") |
|
self.assertEqual( |
|
"Bearer", result.get("token_type"), "Unexpected result: %s" % result) |
|
self.assertIsNotNone(result.get("access_token")) |
|
|
|
|
|
THIS_FOLDER = os.path.dirname(__file__) |
|
CONFIG = os.path.join(THIS_FOLDER, "config.json") |
|
@unittest.skipUnless(os.path.exists(CONFIG), "Optional %s not found" % CONFIG) |
|
class FileBasedTestCase(E2eTestCase): |
|
|
|
|
|
|
|
@classmethod |
|
def setUpClass(cls): |
|
with open(CONFIG) as f: |
|
cls.config = json.load(f) |
|
|
|
def skipUnlessWithConfig(self, fields): |
|
for field in fields: |
|
if field not in self.config: |
|
self.skipTest('Skipping due to lack of configuration "%s"' % field) |
|
|
|
def test_username_password(self): |
|
self.skipUnlessWithConfig(["client_id", "username", "password", "scope"]) |
|
self._test_username_password(**self.config) |
|
|
|
def _get_app_and_auth_code(self, scopes=None, **kwargs): |
|
return _get_app_and_auth_code( |
|
self.config["client_id"], |
|
client_secret=self.config.get("client_secret"), |
|
authority=self.config.get("authority"), |
|
port=self.config.get("listen_port", 44331), |
|
scopes=scopes or self.config["scope"], |
|
**kwargs) |
|
|
|
def _test_auth_code(self, auth_kwargs, token_kwargs): |
|
self.skipUnlessWithConfig(["client_id", "scope"]) |
|
(self.app, ac, redirect_uri) = self._get_app_and_auth_code(**auth_kwargs) |
|
result = self.app.acquire_token_by_authorization_code( |
|
ac, self.config["scope"], redirect_uri=redirect_uri, **token_kwargs) |
|
logger.debug("%s.cache = %s", |
|
self.id(), json.dumps(self.app.token_cache._cache, indent=4)) |
|
self.assertIn( |
|
"access_token", result, |
|
"{error}: {error_description}".format( |
|
|
|
error=result.get("error"), |
|
error_description=result.get("error_description"))) |
|
self.assertCacheWorksForUser(result, self.config["scope"], username=None) |
|
|
|
def test_auth_code(self): |
|
self._test_auth_code({}, {}) |
|
|
|
def test_auth_code_with_matching_nonce(self): |
|
self._test_auth_code({"nonce": "foo"}, {"nonce": "foo"}) |
|
|
|
def test_auth_code_with_mismatching_nonce(self): |
|
self.skipUnlessWithConfig(["client_id", "scope"]) |
|
(self.app, ac, redirect_uri) = self._get_app_and_auth_code(nonce="foo") |
|
with self.assertRaises(ValueError): |
|
self.app.acquire_token_by_authorization_code( |
|
ac, self.config["scope"], redirect_uri=redirect_uri, nonce="bar") |
|
|
|
def test_client_secret(self): |
|
self.skipUnlessWithConfig(["client_id", "client_secret"]) |
|
self.app = msal.ConfidentialClientApplication( |
|
self.config["client_id"], |
|
client_credential=self.config.get("client_secret"), |
|
authority=self.config.get("authority"), |
|
http_client=MinimalHttpClient()) |
|
scope = self.config.get("scope", []) |
|
result = self.app.acquire_token_for_client(scope) |
|
self.assertIn('access_token', result) |
|
self.assertCacheWorksForApp(result, scope) |
|
|
|
def test_client_certificate(self): |
|
self.skipUnlessWithConfig(["client_id", "client_certificate"]) |
|
client_cert = self.config["client_certificate"] |
|
assert "private_key_path" in client_cert and "thumbprint" in client_cert |
|
with open(os.path.join(THIS_FOLDER, client_cert['private_key_path'])) as f: |
|
private_key = f.read() |
|
self.app = msal.ConfidentialClientApplication( |
|
self.config['client_id'], |
|
{"private_key": private_key, "thumbprint": client_cert["thumbprint"]}, |
|
http_client=MinimalHttpClient()) |
|
scope = self.config.get("scope", []) |
|
result = self.app.acquire_token_for_client(scope) |
|
self.assertIn('access_token', result) |
|
self.assertCacheWorksForApp(result, scope) |
|
|
|
def test_subject_name_issuer_authentication(self): |
|
self.skipUnlessWithConfig(["client_id", "client_certificate"]) |
|
client_cert = self.config["client_certificate"] |
|
assert "private_key_path" in client_cert and "thumbprint" in client_cert |
|
if not "public_certificate" in client_cert: |
|
self.skipTest("Skipping SNI test due to lack of public_certificate") |
|
with open(os.path.join(THIS_FOLDER, client_cert['private_key_path'])) as f: |
|
private_key = f.read() |
|
with open(os.path.join(THIS_FOLDER, client_cert['public_certificate'])) as f: |
|
public_certificate = f.read() |
|
self.app = msal.ConfidentialClientApplication( |
|
self.config['client_id'], authority=self.config["authority"], |
|
client_credential={ |
|
"private_key": private_key, |
|
"thumbprint": self.config["thumbprint"], |
|
"public_certificate": public_certificate, |
|
}, |
|
http_client=MinimalHttpClient()) |
|
scope = self.config.get("scope", []) |
|
result = self.app.acquire_token_for_client(scope) |
|
self.assertIn('access_token', result) |
|
self.assertCacheWorksForApp(result, scope) |
|
|
|
def test_client_assertion(self): |
|
self.skipUnlessWithConfig(["client_id", "client_assertion"]) |
|
self.app = msal.ConfidentialClientApplication( |
|
self.config['client_id'], authority=self.config["authority"], |
|
client_credential={"client_assertion": self.config["client_assertion"]}, |
|
http_client=MinimalHttpClient()) |
|
scope = self.config.get("scope", []) |
|
result = self.app.acquire_token_for_client(scope) |
|
self.assertIn('access_token', result) |
|
self.assertCacheWorksForApp(result, scope) |
|
|
|
@unittest.skipUnless(os.path.exists(CONFIG), "Optional %s not found" % CONFIG) |
|
class DeviceFlowTestCase(E2eTestCase): |
|
@classmethod |
|
def setUpClass(cls): |
|
with open(CONFIG) as f: |
|
cls.config = json.load(f) |
|
|
|
def test_device_flow(self): |
|
self._test_device_flow(**self.config) |
|
|
|
|
|
def get_lab_app( |
|
env_client_id="LAB_APP_CLIENT_ID", |
|
env_name2="LAB_APP_CLIENT_SECRET", |
|
env_client_cert_path="LAB_APP_CLIENT_CERT_PFX_PATH", |
|
authority="https://login.microsoftonline.com/" |
|
"72f988bf-86f1-41af-91ab-2d7cd011db47", |
|
timeout=None, |
|
**kwargs): |
|
"""Returns the lab app as an MSAL confidential client. |
|
|
|
Get it from environment variables if defined, otherwise fall back to use MSI. |
|
""" |
|
logger.info( |
|
"Reading ENV variables %s and %s for lab app defined at " |
|
"https://docs.msidlab.com/accounts/confidentialclient.html", |
|
env_client_id, env_name2) |
|
if os.getenv(env_client_id) and os.getenv(env_client_cert_path): |
|
|
|
client_id = os.getenv(env_client_id) |
|
|
|
client_credential = { |
|
"private_key_pfx_path": os.getenv(env_client_cert_path), |
|
"public_certificate": True, |
|
} |
|
elif os.getenv(env_client_id) and os.getenv(env_name2): |
|
|
|
|
|
client_id = os.getenv(env_client_id) |
|
client_credential = os.getenv(env_name2) |
|
else: |
|
logger.info("ENV variables are not defined. Fall back to MSI.") |
|
|
|
raise unittest.SkipTest("MSI-based mechanism has not been implemented yet") |
|
return msal.ConfidentialClientApplication( |
|
client_id, |
|
client_credential=client_credential, |
|
authority=authority, |
|
http_client=MinimalHttpClient(timeout=timeout), |
|
**kwargs) |
|
|
|
class LabTokenError(RuntimeError): |
|
pass |
|
|
|
def get_session(lab_app, scopes): |
|
logger.info("Creating session") |
|
result = lab_app.acquire_token_for_client(scopes) |
|
if not result.get("access_token"): |
|
raise LabTokenError( |
|
"Unable to obtain token for lab. Encountered {}: {}".format( |
|
result.get("error"), result.get("error_description") |
|
)) |
|
session = requests.Session() |
|
session.headers.update({"Authorization": "Bearer %s" % result["access_token"]}) |
|
session.hooks["response"].append(lambda r, *args, **kwargs: r.raise_for_status()) |
|
return session |
|
|
|
|
|
class LabBasedTestCase(E2eTestCase): |
|
_secrets = {} |
|
adfs2019_scopes = ["placeholder"] |
|
|
|
|
|
|
|
|
|
@classmethod |
|
def setUpClass(cls): |
|
|
|
cls.session = get_session(get_lab_app(), [ |
|
"https://request.msidlab.com/.default", |
|
]) |
|
|
|
@classmethod |
|
def tearDownClass(cls): |
|
cls.session.close() |
|
|
|
@classmethod |
|
def get_lab_app_object(cls, client_id=None, **query): |
|
url = "https://msidlab.com/api/app/{}".format(client_id or "") |
|
resp = cls.session.get(url, params=query) |
|
result = resp.json()[0] |
|
result["scopes"] = [ |
|
s.strip() for s in result["defaultScopes"].split(',')] |
|
return result |
|
|
|
@classmethod |
|
def get_lab_user_secret(cls, lab_name="msidlab4"): |
|
lab_name = lab_name.lower() |
|
if lab_name not in cls._secrets: |
|
logger.info("Querying lab user password for %s", lab_name) |
|
url = "https://msidlab.com/api/LabSecret?secret=%s" % lab_name |
|
resp = cls.session.get(url) |
|
cls._secrets[lab_name] = resp.json()["value"] |
|
return cls._secrets[lab_name] |
|
|
|
@classmethod |
|
def get_lab_user(cls, **query): |
|
resp = cls.session.get("https://msidlab.com/api/user", params=query) |
|
result = resp.json()[0] |
|
assert result.get("upn"), "Found no test user but {}".format( |
|
json.dumps(result, indent=2)) |
|
_env = query.get("azureenvironment", "").lower() |
|
authority_base = { |
|
"azureusgovernment": "https://login.microsoftonline.us/" |
|
}.get(_env, "https://login.microsoftonline.com/") |
|
scope = { |
|
"azureusgovernment": ["https://graph.microsoft.us/.default"], |
|
}.get(_env, ["https://graph.microsoft.com/.default"]) |
|
return { |
|
"authority": authority_base + result["tenantID"], |
|
"client_id": result["appId"], |
|
"username": result["upn"], |
|
"lab_name": result["labName"], |
|
"scope": scope, |
|
} |
|
|
|
@unittest.skipIf(os.getenv("TRAVIS"), "Browser automation is not yet implemented") |
|
def _test_acquire_token_by_auth_code( |
|
self, client_id=None, authority=None, port=None, scope=None, |
|
**ignored): |
|
assert client_id and authority and port and scope |
|
(self.app, ac, redirect_uri) = _get_app_and_auth_code( |
|
client_id, authority=authority, port=port, scopes=scope) |
|
result = self.app.acquire_token_by_authorization_code( |
|
ac, scope, redirect_uri=redirect_uri) |
|
logger.debug( |
|
"%s: cache = %s, id_token_claims = %s", |
|
self.id(), |
|
json.dumps(self.app.token_cache._cache, indent=4), |
|
json.dumps(result.get("id_token_claims"), indent=4), |
|
) |
|
self.assertIn( |
|
"access_token", result, |
|
"{error}: {error_description}".format( |
|
|
|
error=result.get("error"), |
|
error_description=result.get("error_description"))) |
|
self.assertCacheWorksForUser(result, scope, username=None) |
|
|
|
@unittest.skipIf(os.getenv("TRAVIS"), "Browser automation is not yet implemented") |
|
def _test_acquire_token_by_auth_code_flow( |
|
self, client_id=None, authority=None, port=None, scope=None, |
|
username=None, lab_name=None, |
|
username_uri="", |
|
**ignored): |
|
assert client_id and authority and scope |
|
self.app = msal.ClientApplication( |
|
client_id, authority=authority, http_client=MinimalHttpClient()) |
|
with AuthCodeReceiver(port=port) as receiver: |
|
flow = self.app.initiate_auth_code_flow( |
|
scope, |
|
redirect_uri="http://localhost:%d" % receiver.get_port(), |
|
) |
|
auth_response = receiver.get_auth_response( |
|
auth_uri=flow["auth_uri"], state=flow["state"], timeout=60, |
|
welcome_template="""<html><body><h1>{id}</h1><ol> |
|
<li>{hint}</li> |
|
<li><a href="$auth_uri">Sign In</a> or <a href="$abort_uri">Abort</a></li> |
|
</ol></body></html>""".format(id=self.id(), hint=_get_hint( |
|
html_mode=True, |
|
username=username, lab_name=lab_name, username_uri=username_uri)), |
|
) |
|
if auth_response is None: |
|
self.skipTest("Timed out. Did not have test settings in hand? Prepare and retry.") |
|
self.assertIsNotNone( |
|
auth_response.get("code"), "Error: {}, Detail: {}".format( |
|
auth_response.get("error"), auth_response)) |
|
result = self.app.acquire_token_by_auth_code_flow(flow, auth_response) |
|
logger.debug( |
|
"%s: cache = %s, id_token_claims = %s", |
|
self.id(), |
|
json.dumps(self.app.token_cache._cache, indent=4), |
|
json.dumps(result.get("id_token_claims"), indent=4), |
|
) |
|
self.assertIn( |
|
"access_token", result, |
|
"{error}: {error_description}".format( |
|
|
|
error=result.get("error"), |
|
error_description=result.get("error_description"))) |
|
if username and result.get("id_token_claims", {}).get("preferred_username"): |
|
self.assertEqual( |
|
username, result["id_token_claims"]["preferred_username"], |
|
"You are expected to sign in as account {}, but tokens returned is for {}".format( |
|
username, result["id_token_claims"]["preferred_username"])) |
|
self.assertCacheWorksForUser(result, scope, username=None) |
|
|
|
def _test_acquire_token_obo(self, config_pca, config_cca, |
|
azure_region=None, |
|
|
|
http_client=None, |
|
): |
|
|
|
pca = msal.PublicClientApplication( |
|
config_pca["client_id"], authority=config_pca["authority"], |
|
azure_region=azure_region, |
|
http_client=http_client or MinimalHttpClient()) |
|
pca_result = pca.acquire_token_by_username_password( |
|
config_pca["username"], |
|
config_pca["password"], |
|
scopes=config_pca["scope"], |
|
) |
|
self.assertIsNotNone( |
|
pca_result.get("access_token"), |
|
"PCA failed to get AT because %s" % json.dumps(pca_result, indent=2)) |
|
|
|
|
|
cca = msal.ConfidentialClientApplication( |
|
config_cca["client_id"], |
|
client_credential=config_cca["client_secret"], |
|
authority=config_cca["authority"], |
|
azure_region=azure_region, |
|
http_client=http_client or MinimalHttpClient(), |
|
|
|
|
|
|
|
) |
|
cca_result = cca.acquire_token_on_behalf_of( |
|
pca_result['access_token'], config_cca["scope"]) |
|
self.assertNotEqual(None, cca_result.get("access_token"), str(cca_result)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
username = cca_result.get("id_token_claims", {}).get("preferred_username") |
|
if username: |
|
self.assertEqual(config_cca["username"], username) |
|
accounts = cca.get_accounts(username=username) |
|
assert len(accounts) == 1, "App is expected to partition token cache per user" |
|
account = accounts[0] |
|
result = cca.acquire_token_silent(config_cca["scope"], account) |
|
self.assertEqual(cca_result["access_token"], result["access_token"]) |
|
|
|
def _test_acquire_token_by_client_secret( |
|
self, client_id=None, client_secret=None, authority=None, scope=None, |
|
**ignored): |
|
assert client_id and client_secret and authority and scope |
|
self.app = msal.ConfidentialClientApplication( |
|
client_id, client_credential=client_secret, authority=authority, |
|
http_client=MinimalHttpClient()) |
|
result = self.app.acquire_token_for_client(scope) |
|
self.assertIsNotNone(result.get("access_token"), "Got %s instead" % result) |
|
self.assertCacheWorksForApp(result, scope) |
|
|
|
|
|
class PopWithExternalKeyTestCase(LabBasedTestCase): |
|
def _test_service_principal(self): |
|
app = get_lab_app() |
|
result = app.acquire_token_for_client(self.SCOPE, data=self.DATA1) |
|
self.assertIsNotNone(result.get("access_token"), "Encountered {}: {}".format( |
|
result.get("error"), result.get("error_description"))) |
|
self.assertEqual(self.EXPECTED_TOKEN_TYPE, result["token_type"]) |
|
self.assertEqual(result["token_source"], "identity_provider") |
|
|
|
|
|
cached_result = app.acquire_token_for_client(self.SCOPE, data=self.DATA1) |
|
self.assertIsNotNone( |
|
cached_result.get("access_token"), "Encountered {}: {}".format( |
|
cached_result.get("error"), cached_result.get("error_description"))) |
|
self.assertEqual(self.EXPECTED_TOKEN_TYPE, cached_result["token_type"]) |
|
self.assertEqual(cached_result["token_source"], "cache") |
|
|
|
|
|
refreshed_result = app.acquire_token_for_client(self.SCOPE, data=self.DATA2) |
|
self.assertIsNotNone( |
|
refreshed_result.get("access_token"), "Encountered {}: {}".format( |
|
refreshed_result.get("error"), refreshed_result.get("error_description"))) |
|
self.assertEqual(self.EXPECTED_TOKEN_TYPE, refreshed_result["token_type"]) |
|
self.assertEqual(refreshed_result["token_source"], "identity_provider") |
|
|
|
def _test_user_account(self): |
|
lab_user = self.get_lab_user(usertype="cloud") |
|
result = self._test_acquire_token_interactive( |
|
client_id="04b07795-8ddb-461a-bbee-02f9e1bf7b46", |
|
|
|
authority="https://login.microsoftonline.com/common", |
|
scope=self.SCOPE, |
|
data=self.DATA1, |
|
username=lab_user["username"], |
|
lab_name=lab_user["lab_name"], |
|
prompt="none" if msal.application._is_running_in_cloud_shell() else None, |
|
) |
|
|
|
self.assertIsNotNone(result.get("access_token"), "Encountered {}: {}".format( |
|
result.get("error"), result.get("error_description"))) |
|
self.assertEqual(self.EXPECTED_TOKEN_TYPE, result["token_type"]) |
|
self.assertEqual(result["token_source"], "identity_provider") |
|
logger.debug("%s.cache = %s", |
|
self.id(), json.dumps(self.app.token_cache._cache, indent=4)) |
|
|
|
|
|
account = self.app.get_accounts()[0] |
|
cached_result = self.app.acquire_token_silent( |
|
self.SCOPE, account=account, data=self.DATA1) |
|
self.assertIsNotNone(cached_result) |
|
self.assertEqual(self.EXPECTED_TOKEN_TYPE, cached_result["token_type"]) |
|
|
|
|
|
|
|
|
|
self.assertEqual(cached_result["token_source"], "cache") |
|
|
|
|
|
account = self.app.get_accounts()[0] |
|
refreshed_result = self.app.acquire_token_silent( |
|
self.SCOPE, account=account, data=self.DATA2) |
|
self.assertIsNotNone(refreshed_result) |
|
self.assertEqual(self.EXPECTED_TOKEN_TYPE, refreshed_result["token_type"]) |
|
self.assertNotEqual(result["access_token"], refreshed_result['access_token']) |
|
self.assertEqual(refreshed_result["token_source"], "identity_provider") |
|
|
|
|
|
class SshCertTestCase(PopWithExternalKeyTestCase): |
|
EXPECTED_TOKEN_TYPE = "ssh-cert" |
|
_JWK1 = """{"kty":"RSA", "n":"2tNr73xwcj6lH7bqRZrFzgSLj7OeLfbn8216uOMDHuaZ6TEUBDN8Uz0ve8jAlKsP9CQFCSVoSNovdE-fs7c15MxEGHjDcNKLWonznximj8pDGZQjVdfK-7mG6P6z-lgVcLuYu5JcWU_PeEqIKg5llOaz-qeQ4LEDS4T1D2qWRGpAra4rJX1-kmrWmX_XIamq30C9EIO0gGuT4rc2hJBWQ-4-FnE1NXmy125wfT3NdotAJGq5lMIfhjfglDbJCwhc8Oe17ORjO3FsB5CLuBRpYmP7Nzn66lRY3Fe11Xz8AEBl3anKFSJcTvlMnFtu3EpD-eiaHfTgRBU7CztGQqVbiQ", "e":"AQAB"}""" |
|
_JWK2 = """{"kty":"RSA", "n":"72u07mew8rw-ssw3tUs9clKstGO2lvD7ZNxJU7OPNKz5PGYx3gjkhUmtNah4I4FP0DuF1ogb_qSS5eD86w10Wb1ftjWcoY8zjNO9V3ph-Q2tMQWdDW5kLdeU3-EDzc0HQeou9E0udqmfQoPbuXFQcOkdcbh3eeYejs8sWn3TQprXRwGh_TRYi-CAurXXLxQ8rp-pltUVRIr1B63fXmXhMeCAGwCPEFX9FRRs-YHUszUJl9F9-E0nmdOitiAkKfCC9LhwB9_xKtjmHUM9VaEC9jWOcdvXZutwEoW2XPMOg0Ky-s197F9rfpgHle2gBrXsbvVMvS0D-wXg6vsq6BAHzQ", "e":"AQAB"}""" |
|
DATA1 = {"token_type": "ssh-cert", "key_id": "key1", "req_cnf": _JWK1} |
|
DATA2 = {"token_type": "ssh-cert", "key_id": "key2", "req_cnf": _JWK2} |
|
_SCOPE_USER = ["https://pas.windows.net/CheckMyAccess/Linux/user_impersonation"] |
|
_SCOPE_SP = ["https://pas.windows.net/CheckMyAccess/Linux/.default"] |
|
SCOPE = _SCOPE_SP |
|
|
|
def test_service_principal(self): |
|
self._test_service_principal() |
|
|
|
def test_user_account(self): |
|
self._test_user_account() |
|
|
|
|
|
def _data_for_pop(key): |
|
raw_req_cnf = json.dumps({"kid": key, "xms_ksl": "sw"}) |
|
return { |
|
'token_type': 'pop', |
|
'key_id': key, |
|
"req_cnf": base64.urlsafe_b64encode(raw_req_cnf.encode('utf-8')).decode('utf-8').rstrip('='), |
|
|
|
} |
|
|
|
|
|
class AtPopWithExternalKeyTestCase(PopWithExternalKeyTestCase): |
|
EXPECTED_TOKEN_TYPE = "pop" |
|
DATA1 = _data_for_pop('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA-AAAAAAAA') |
|
DATA2 = _data_for_pop('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-BBBBBBBB') |
|
SCOPE = [ |
|
'6256c85f-0aad-4d50-b960-e6e9b21efe35/.default', |
|
|
|
] |
|
|
|
def test_service_principal(self): |
|
self._test_service_principal() |
|
|
|
def test_user_account(self): |
|
self._test_user_account() |
|
|
|
|
|
class WorldWideTestCase(LabBasedTestCase): |
|
|
|
def test_aad_managed_user(self): |
|
config = self.get_lab_user(usertype="cloud") |
|
config["password"] = self.get_lab_user_secret(config["lab_name"]) |
|
self._test_username_password(**config) |
|
|
|
def test_adfs4_fed_user(self): |
|
config = self.get_lab_user(usertype="federated", federationProvider="ADFSv4") |
|
config["password"] = self.get_lab_user_secret(config["lab_name"]) |
|
self._test_username_password(**config) |
|
|
|
def test_adfs3_fed_user(self): |
|
config = self.get_lab_user(usertype="federated", federationProvider="ADFSv3") |
|
config["password"] = self.get_lab_user_secret(config["lab_name"]) |
|
self._test_username_password(**config) |
|
|
|
def test_adfs2_fed_user(self): |
|
config = self.get_lab_user(usertype="federated", federationProvider="ADFSv2") |
|
config["password"] = self.get_lab_user_secret(config["lab_name"]) |
|
self._test_username_password(**config) |
|
|
|
def test_adfs2019_fed_user(self): |
|
try: |
|
config = self.get_lab_user(usertype="federated", federationProvider="ADFSv2019") |
|
config["password"] = self.get_lab_user_secret(config["lab_name"]) |
|
self._test_username_password(**config) |
|
except requests.exceptions.HTTPError: |
|
if os.getenv("TRAVIS"): |
|
self.skipTest("MEX endpoint in our test environment tends to fail") |
|
raise |
|
|
|
def test_cloud_acquire_token_interactive(self): |
|
self._test_acquire_token_interactive(**self.get_lab_user(usertype="cloud")) |
|
|
|
def test_msa_pt_app_signin_via_organizations_authority_without_login_hint(self): |
|
"""There is/was an upstream bug. See test case full docstring for the details. |
|
|
|
When a MSAL-PT flow that account control is launched, user has 2+ AAD accounts in WAM, |
|
selects an AAD account that is NOT the default AAD account from the OS, |
|
it will incorrectly get tokens for default AAD account. |
|
""" |
|
self._test_acquire_token_interactive(**dict( |
|
self.get_lab_user(usertype="cloud"), |
|
authority="https://login.microsoftonline.com/organizations", |
|
client_id="04b07795-8ddb-461a-bbee-02f9e1bf7b46", |
|
enable_msa_passthrough=True, |
|
prompt="select_account", |
|
)) |
|
|
|
def test_ropc_adfs2019_onprem(self): |
|
|
|
config = self.get_lab_user(usertype="onprem", federationProvider="ADFSv2019") |
|
config["authority"] = "https://fs.%s.com/adfs" % config["lab_name"] |
|
config["scope"] = self.adfs2019_scopes |
|
config["password"] = self.get_lab_user_secret(config["lab_name"]) |
|
self._test_username_password(**config) |
|
|
|
def test_adfs2019_onprem_acquire_token_by_auth_code(self): |
|
"""When prompted, you can manually login using this account: |
|
|
|
# https://msidlab.com/api/user?usertype=onprem&federationprovider=ADFSv2019 |
|
username = "..." # The upn from the link above |
|
password="***" # From https://aka.ms/GetLabSecret?Secret=msidlabXYZ |
|
""" |
|
config = self.get_lab_user(usertype="onprem", federationProvider="ADFSv2019") |
|
config["authority"] = "https://fs.%s.com/adfs" % config["lab_name"] |
|
config["scope"] = self.adfs2019_scopes |
|
config["port"] = 8080 |
|
self._test_acquire_token_by_auth_code(**config) |
|
|
|
def test_adfs2019_onprem_acquire_token_by_auth_code_flow(self): |
|
config = self.get_lab_user(usertype="onprem", federationProvider="ADFSv2019") |
|
self._test_acquire_token_by_auth_code_flow(**dict( |
|
config, |
|
authority="https://fs.%s.com/adfs" % config["lab_name"], |
|
scope=self.adfs2019_scopes, |
|
port=8080, |
|
)) |
|
|
|
def test_adfs2019_onprem_acquire_token_interactive(self): |
|
config = self.get_lab_user(usertype="onprem", federationProvider="ADFSv2019") |
|
self._test_acquire_token_interactive(**dict( |
|
config, |
|
authority="https://fs.%s.com/adfs" % config["lab_name"], |
|
scope=self.adfs2019_scopes, |
|
port=8080, |
|
)) |
|
|
|
@unittest.skipUnless( |
|
os.getenv("LAB_OBO_CLIENT_SECRET"), |
|
"Need LAB_OBO_CLIENT_SECRET from https://aka.ms/GetLabSecret?Secret=TodoListServiceV2-OBO") |
|
@unittest.skipUnless( |
|
os.getenv("LAB_OBO_CONFIDENTIAL_CLIENT_ID"), |
|
"Need LAB_OBO_CONFIDENTIAL_CLIENT_ID from https://docs.msidlab.com/flows/onbehalfofflow.html") |
|
@unittest.skipUnless( |
|
os.getenv("LAB_OBO_PUBLIC_CLIENT_ID"), |
|
"Need LAB_OBO_PUBLIC_CLIENT_ID from https://docs.msidlab.com/flows/onbehalfofflow.html") |
|
def test_acquire_token_obo(self): |
|
config = self.get_lab_user(usertype="cloud") |
|
|
|
config_cca = {} |
|
config_cca.update(config) |
|
config_cca["client_id"] = os.getenv("LAB_OBO_CONFIDENTIAL_CLIENT_ID") |
|
config_cca["scope"] = ["https://graph.microsoft.com/.default"] |
|
config_cca["client_secret"] = os.getenv("LAB_OBO_CLIENT_SECRET") |
|
|
|
config_pca = {} |
|
config_pca.update(config) |
|
config_pca["client_id"] = os.getenv("LAB_OBO_PUBLIC_CLIENT_ID") |
|
config_pca["password"] = self.get_lab_user_secret(config_pca["lab_name"]) |
|
config_pca["scope"] = ["api://%s/read" % config_cca["client_id"]] |
|
|
|
self._test_acquire_token_obo(config_pca, config_cca) |
|
|
|
def test_acquire_token_by_client_secret(self): |
|
|
|
_app = self.get_lab_app_object( |
|
publicClient="no", signinAudience="AzureAdMyOrg") |
|
self._test_acquire_token_by_client_secret( |
|
client_id=_app["appId"], |
|
client_secret=self.get_lab_user_secret( |
|
_app["clientSecret"].split("/")[-1]), |
|
authority="{}{}.onmicrosoft.com".format( |
|
_app["authority"], _app["labName"].lower().rstrip(".com")), |
|
scope=["https://graph.microsoft.com/.default"], |
|
) |
|
|
|
@unittest.skipUnless( |
|
os.getenv("LAB_OBO_CLIENT_SECRET"), |
|
"Need LAB_OBO_CLIENT_SECRET from https://aka.ms/GetLabSecret?Secret=TodoListServiceV2-OBO") |
|
@unittest.skipUnless( |
|
os.getenv("LAB_OBO_CONFIDENTIAL_CLIENT_ID"), |
|
"Need LAB_OBO_CONFIDENTIAL_CLIENT_ID from https://docs.msidlab.com/flows/onbehalfofflow.html") |
|
def test_confidential_client_acquire_token_by_username_password(self): |
|
|
|
|
|
|
|
config = self.get_lab_user(usertype="cloud") |
|
config["password"] = self.get_lab_user_secret(config["lab_name"]) |
|
|
|
config["client_id"] = os.getenv("LAB_OBO_CONFIDENTIAL_CLIENT_ID") |
|
config["scope"] = ["https://graph.microsoft.com/.default"] |
|
config["client_secret"] = os.getenv("LAB_OBO_CLIENT_SECRET") |
|
self._test_username_password(**config) |
|
|
|
def _build_b2c_authority(self, policy): |
|
base = "https://msidlabb2c.b2clogin.com/msidlabb2c.onmicrosoft.com" |
|
return base + "/" + policy |
|
|
|
def test_b2c_acquire_token_by_auth_code(self): |
|
""" |
|
When prompted, you can manually login using this account: |
|
|
|
username="b2clocal@msidlabb2c.onmicrosoft.com" |
|
# This won't work https://msidlab.com/api/user?usertype=b2c |
|
password="***" # From https://aka.ms/GetLabSecret?Secret=msidlabb2c |
|
""" |
|
config = self.get_lab_app_object(azureenvironment="azureb2ccloud") |
|
self._test_acquire_token_by_auth_code( |
|
authority=self._build_b2c_authority("B2C_1_SignInPolicy"), |
|
client_id=config["appId"], |
|
port=3843, |
|
scope=config["scopes"], |
|
) |
|
|
|
def test_b2c_acquire_token_by_auth_code_flow(self): |
|
self._test_acquire_token_by_auth_code_flow(**dict( |
|
self.get_lab_user(usertype="b2c", b2cprovider="local"), |
|
authority=self._build_b2c_authority("B2C_1_SignInPolicy"), |
|
port=3843, |
|
scope=self.get_lab_app_object(azureenvironment="azureb2ccloud")["scopes"], |
|
)) |
|
|
|
def test_b2c_acquire_token_by_ropc(self): |
|
config = self.get_lab_app_object(azureenvironment="azureb2ccloud") |
|
self._test_username_password( |
|
authority=self._build_b2c_authority("B2C_1_ROPC_Auth"), |
|
client_id=config["appId"], |
|
username="b2clocal@msidlabb2c.onmicrosoft.com", |
|
password=self.get_lab_user_secret("msidlabb2c"), |
|
scope=config["scopes"], |
|
) |
|
|
|
def test_b2c_allows_using_client_id_as_scope(self): |
|
|
|
config = self.get_lab_app_object(azureenvironment="azureb2ccloud") |
|
config["scopes"] = [config["appId"]] |
|
self._test_username_password( |
|
authority=self._build_b2c_authority("B2C_1_ROPC_Auth"), |
|
client_id=config["appId"], |
|
username="b2clocal@msidlabb2c.onmicrosoft.com", |
|
password=self.get_lab_user_secret("msidlabb2c"), |
|
scope=config["scopes"], |
|
) |
|
|
|
|
|
class CiamTestCase(LabBasedTestCase): |
|
|
|
|
|
|
|
@classmethod |
|
def setUpClass(cls): |
|
super(CiamTestCase, cls).setUpClass() |
|
cls.user = cls.get_lab_user(federationProvider="ciam") |
|
|
|
|
|
cls.app_config = cls.get_lab_app_object(cls.user["client_id"]) |
|
|
|
def test_ciam_acquire_token_interactive(self): |
|
self._test_acquire_token_interactive( |
|
authority=self.app_config["authority"], |
|
client_id=self.app_config["appId"], |
|
scope=self.app_config["scopes"], |
|
username=self.user["username"], |
|
lab_name=self.user["lab_name"], |
|
) |
|
|
|
def test_ciam_acquire_token_for_client(self): |
|
self._test_acquire_token_by_client_secret( |
|
client_id=self.app_config["appId"], |
|
client_secret=self.get_lab_user_secret( |
|
self.app_config["clientSecret"].split("=")[-1]), |
|
authority=self.app_config["authority"], |
|
|
|
|
|
scope=self.app_config["scopes"], |
|
) |
|
|
|
def test_ciam_acquire_token_by_ropc(self): |
|
"""CIAM does not officially support ROPC, especially not for external emails. |
|
|
|
We keep this test case for now, because the test data will use a local email. |
|
""" |
|
|
|
|
|
|
|
self._test_username_password( |
|
authority=self.app_config["authority"], |
|
client_id=self.app_config["appId"], |
|
username=self.user["username"], |
|
password=self.get_lab_user_secret(self.user["lab_name"]), |
|
scope=self.app_config["scopes"], |
|
) |
|
|
|
def test_ciam_device_flow(self): |
|
self._test_device_flow( |
|
authority=self.app_config["authority"], |
|
client_id=self.app_config["appId"], |
|
scope=self.app_config["scopes"], |
|
) |
|
|
|
|
|
class WorldWideRegionalEndpointTestCase(LabBasedTestCase): |
|
region = "westus" |
|
timeout = 2 |
|
|
|
def _test_acquire_token_for_client(self, configured_region, expected_region): |
|
"""This is the only grant supported by regional endpoint, for now""" |
|
self.app = get_lab_app( |
|
|
|
|
|
|
|
|
|
authority="https://login.microsoftonline.com/microsoft.onmicrosoft.com", |
|
azure_region=configured_region, |
|
timeout=2, |
|
) |
|
scopes = ["https://graph.microsoft.com/.default"] |
|
|
|
with patch.object( |
|
self.app.http_client, "post", return_value=MinimalResponse( |
|
status_code=400, text='{"error": "mock"}')) as mocked_method: |
|
self.app.acquire_token_for_client(scopes) |
|
expected_host = '{}.login.microsoft.com'.format( |
|
expected_region) if expected_region else 'login.microsoftonline.com' |
|
mocked_method.assert_called_with( |
|
'https://{}/{}/oauth2/v2.0/token'.format( |
|
expected_host, self.app.authority.tenant), |
|
params=ANY, data=ANY, headers=ANY) |
|
result = self.app.acquire_token_for_client( |
|
scopes, |
|
params={"AllowEstsRNonMsi": "true"}, |
|
) |
|
self.assertIn('access_token', result) |
|
self.assertCacheWorksForApp(result, scopes) |
|
|
|
def test_acquire_token_for_client_should_hit_global_endpoint_by_default(self): |
|
self._test_acquire_token_for_client(None, None) |
|
|
|
def test_acquire_token_for_client_should_ignore_env_var_by_default(self): |
|
os.environ["REGION_NAME"] = "eastus" |
|
self._test_acquire_token_for_client(None, None) |
|
del os.environ["REGION_NAME"] |
|
|
|
def test_acquire_token_for_client_should_use_a_specified_region(self): |
|
self._test_acquire_token_for_client("westus", "westus") |
|
|
|
def test_acquire_token_for_client_should_use_an_env_var_with_short_region_name(self): |
|
os.environ["REGION_NAME"] = "eastus" |
|
self._test_acquire_token_for_client( |
|
msal.ConfidentialClientApplication.ATTEMPT_REGION_DISCOVERY, "eastus") |
|
del os.environ["REGION_NAME"] |
|
|
|
def test_acquire_token_for_client_should_use_an_env_var_with_long_region_name(self): |
|
os.environ["REGION_NAME"] = "East Us 2" |
|
self._test_acquire_token_for_client( |
|
msal.ConfidentialClientApplication.ATTEMPT_REGION_DISCOVERY, "eastus2") |
|
del os.environ["REGION_NAME"] |
|
|
|
@unittest.skipUnless( |
|
os.getenv("LAB_OBO_CLIENT_SECRET"), |
|
"Need LAB_OBO_CLIENT_SECRET from https://aka.ms/GetLabSecret?Secret=TodoListServiceV2-OBO") |
|
@unittest.skipUnless( |
|
os.getenv("LAB_OBO_CONFIDENTIAL_CLIENT_ID"), |
|
"Need LAB_OBO_CONFIDENTIAL_CLIENT_ID from https://docs.msidlab.com/flows/onbehalfofflow.html") |
|
@unittest.skipUnless( |
|
os.getenv("LAB_OBO_PUBLIC_CLIENT_ID"), |
|
"Need LAB_OBO_PUBLIC_CLIENT_ID from https://docs.msidlab.com/flows/onbehalfofflow.html") |
|
def test_cca_obo_should_bypass_regional_endpoint_therefore_still_work(self): |
|
"""We test OBO because it is implemented in sub class ConfidentialClientApplication""" |
|
config = self.get_lab_user(usertype="cloud") |
|
|
|
config_cca = {} |
|
config_cca.update(config) |
|
config_cca["client_id"] = os.getenv("LAB_OBO_CONFIDENTIAL_CLIENT_ID") |
|
config_cca["scope"] = ["https://graph.microsoft.com/.default"] |
|
config_cca["client_secret"] = os.getenv("LAB_OBO_CLIENT_SECRET") |
|
|
|
config_pca = {} |
|
config_pca.update(config) |
|
config_pca["client_id"] = os.getenv("LAB_OBO_PUBLIC_CLIENT_ID") |
|
config_pca["password"] = self.get_lab_user_secret(config_pca["lab_name"]) |
|
config_pca["scope"] = ["api://%s/read" % config_cca["client_id"]] |
|
|
|
self._test_acquire_token_obo( |
|
config_pca, config_cca, |
|
azure_region=self.region, |
|
http_client=MinimalHttpClient(timeout=self.timeout), |
|
) |
|
|
|
@unittest.skipUnless( |
|
os.getenv("LAB_OBO_CLIENT_SECRET"), |
|
"Need LAB_OBO_CLIENT_SECRET from https://aka.ms/GetLabSecret?Secret=TodoListServiceV2-OBO") |
|
@unittest.skipUnless( |
|
os.getenv("LAB_OBO_CONFIDENTIAL_CLIENT_ID"), |
|
"Need LAB_OBO_CONFIDENTIAL_CLIENT_ID from https://docs.msidlab.com/flows/onbehalfofflow.html") |
|
def test_cca_ropc_should_bypass_regional_endpoint_therefore_still_work(self): |
|
"""We test ROPC because it is implemented in base class ClientApplication""" |
|
config = self.get_lab_user(usertype="cloud") |
|
config["password"] = self.get_lab_user_secret(config["lab_name"]) |
|
|
|
|
|
config["client_id"] = os.getenv("LAB_OBO_CONFIDENTIAL_CLIENT_ID") |
|
config["scope"] = ["https://graph.microsoft.com/.default"] |
|
config["client_secret"] = os.getenv("LAB_OBO_CLIENT_SECRET") |
|
self._test_username_password( |
|
azure_region=self.region, |
|
http_client=MinimalHttpClient(timeout=self.timeout), |
|
**config) |
|
|
|
|
|
class ArlingtonCloudTestCase(LabBasedTestCase): |
|
environment = "azureusgovernment" |
|
|
|
def test_acquire_token_by_ropc(self): |
|
config = self.get_lab_user(azureenvironment=self.environment) |
|
config["password"] = self.get_lab_user_secret(config["lab_name"]) |
|
self._test_username_password(**config) |
|
|
|
def test_acquire_token_by_client_secret(self): |
|
config = self.get_lab_user(usertype="cloud", azureenvironment=self.environment, publicClient="no") |
|
config["client_secret"] = self.get_lab_user_secret("ARLMSIDLAB1-IDLASBS-App-CC-Secret") |
|
self._test_acquire_token_by_client_secret(**config) |
|
|
|
def test_acquire_token_obo(self): |
|
config_cca = self.get_lab_user( |
|
usertype="cloud", azureenvironment=self.environment, publicClient="no") |
|
config_cca["scope"] = ["https://graph.microsoft.us/.default"] |
|
config_cca["client_secret"] = self.get_lab_user_secret("ARLMSIDLAB1-IDLASBS-App-CC-Secret") |
|
|
|
config_pca = self.get_lab_user(usertype="cloud", azureenvironment=self.environment, publicClient="yes") |
|
obo_app_object = self.get_lab_app_object( |
|
usertype="cloud", azureenvironment=self.environment, publicClient="no") |
|
config_pca["password"] = self.get_lab_user_secret(config_pca["lab_name"]) |
|
config_pca["scope"] = ["{app_uri}/files.read".format(app_uri=obo_app_object.get("identifierUris"))] |
|
|
|
self._test_acquire_token_obo(config_pca, config_cca) |
|
|
|
def test_acquire_token_device_flow(self): |
|
config = self.get_lab_user(usertype="cloud", azureenvironment=self.environment, publicClient="yes") |
|
config["scope"] = ["user.read"] |
|
self._test_device_flow(**config) |
|
|
|
def test_acquire_token_silent_with_an_empty_cache_should_return_none(self): |
|
config = self.get_lab_user( |
|
usertype="cloud", azureenvironment=self.environment, publicClient="no") |
|
app = msal.ConfidentialClientApplication( |
|
config['client_id'], authority=config['authority'], |
|
http_client=MinimalHttpClient()) |
|
result = app.acquire_token_silent(scopes=config['scope'], account=None) |
|
self.assertEqual(result, None) |
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(broker_available, "AT POP feature is only supported by using broker") |
|
class PopTestCase(LabBasedTestCase): |
|
def test_at_pop_should_contain_pop_scheme_content(self): |
|
auth_scheme = msal.PopAuthScheme( |
|
http_method=msal.PopAuthScheme.HTTP_GET, |
|
url="https://www.Contoso.com/Path1/Path2?queryParam1=a&queryParam2=b", |
|
nonce="placeholder", |
|
) |
|
result = self._test_acquire_token_interactive( |
|
|
|
client_id=_AZURE_CLI, |
|
authority="https://login.microsoftonline.com/organizations", |
|
scope=["https://management.azure.com/.default"], |
|
auth_scheme=auth_scheme, |
|
) |
|
self.assertEqual(result["token_source"], "broker", "POP is only supported by broker") |
|
self.assertEqual(result["token_type"], "pop") |
|
payload = json.loads(decode_part(result["access_token"].split(".")[1])) |
|
logger.debug("AT POP payload = %s", json.dumps(payload, indent=2)) |
|
self.assertEqual(payload["m"], auth_scheme._http_method) |
|
self.assertEqual(payload["u"], auth_scheme._url.netloc) |
|
self.assertEqual(payload["p"], auth_scheme._url.path) |
|
self.assertEqual(payload["nonce"], auth_scheme._nonce) |
|
|
|
|
|
def test_at_pop_via_testingsts_service(self): |
|
"""Based on https://testingsts.azurewebsites.net/ServerNonce""" |
|
self.skipTest("ROPC support is removed by Broker-on-Win") |
|
auth_scheme = msal.PopAuthScheme( |
|
http_method="POST", |
|
url="https://www.Contoso.com/Path1/Path2?queryParam1=a&queryParam2=b", |
|
nonce=requests.get( |
|
|
|
"https://testingsts.azurewebsites.net/servernonce/get").text, |
|
) |
|
config = self.get_lab_user(usertype="cloud") |
|
config["password"] = self.get_lab_user_secret(config["lab_name"]) |
|
result = self._test_username_password(auth_scheme=auth_scheme, **config) |
|
self.assertEqual(result["token_type"], "pop") |
|
shr = result["access_token"] |
|
payload = json.loads(decode_part(result["access_token"].split(".")[1])) |
|
logger.debug("AT POP payload = %s", json.dumps(payload, indent=2)) |
|
self.assertEqual(payload["m"], auth_scheme._http_method) |
|
self.assertEqual(payload["u"], auth_scheme._url.netloc) |
|
self.assertEqual(payload["p"], auth_scheme._url.path) |
|
self.assertEqual(payload["nonce"], auth_scheme._nonce) |
|
|
|
validation = requests.post( |
|
|
|
"https://testingsts.azurewebsites.net/servernonce/validateshr", |
|
data={"SHR": shr}, |
|
) |
|
self.assertEqual(validation.status_code, 200) |
|
|
|
def test_at_pop_calling_pattern(self): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api_endpoint = "https://20.190.132.47/beta/me" |
|
resp = requests.get(api_endpoint, verify=False) |
|
self.assertEqual(resp.status_code, 401, "Initial call should end with an http 401 error") |
|
result = self._get_shr_pop(**dict( |
|
self.get_lab_user(usertype="cloud"), |
|
scope=["https://graph.microsoft.com/.default"], |
|
auth_scheme=msal.PopAuthScheme( |
|
http_method=msal.PopAuthScheme.HTTP_GET, |
|
url=api_endpoint, |
|
nonce=self._extract_pop_nonce(resp.headers.get("WWW-Authenticate")), |
|
), |
|
)) |
|
resp = requests.get(api_endpoint, verify=False, headers={ |
|
"Authorization": "pop {}".format(result["access_token"]), |
|
}) |
|
self.assertEqual(resp.status_code, 200, "POP resource should be accessible") |
|
|
|
def _extract_pop_nonce(self, www_authenticate): |
|
|
|
|
|
import re |
|
found = re.search(r'nonce="(.+?)"', www_authenticate) |
|
if found: |
|
return found.group(1) |
|
|
|
def _get_shr_pop( |
|
self, client_id=None, authority=None, scope=None, auth_scheme=None, |
|
**kwargs): |
|
result = self._test_acquire_token_interactive( |
|
|
|
client_id=client_id, |
|
authority=authority, |
|
scope=scope, |
|
auth_scheme=auth_scheme, |
|
**kwargs) |
|
self.assertEqual(result["token_source"], "broker", "POP is only supported by broker") |
|
self.assertEqual(result["token_type"], "pop") |
|
payload = json.loads(decode_part(result["access_token"].split(".")[1])) |
|
logger.debug("AT POP payload = %s", json.dumps(payload, indent=2)) |
|
self.assertEqual(payload["m"], auth_scheme._http_method) |
|
self.assertEqual(payload["u"], auth_scheme._url.netloc) |
|
self.assertEqual(payload["p"], auth_scheme._url.path) |
|
self.assertEqual(payload["nonce"], auth_scheme._nonce) |
|
return result |
|
|
|
|
|
if __name__ == "__main__": |
|
unittest.main() |
|
|