Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
867 commits
Select commit Hold shift + click to select a range
d885927
Fix doc typo
rayluo Aug 26, 2022
980e898
Merge branch 'doc-param-links' into dev
rayluo Aug 26, 2022
95e523d
Broker (WAM) integration
rayluo Aug 29, 2022
bcefb10
Refine document
rayluo Aug 29, 2022
3773e00
Gracefully handle RuntimeError upfront
rayluo Sep 12, 2022
5ca97ee
Implement instance_discovery only
rayluo Sep 14, 2022
24d243f
Merge pull request #496 from AzureAD/instance-discovery-endpoint
rayluo Sep 19, 2022
a0a1286
MSAL Python 1.19.0
rayluo Sep 20, 2022
81b48ce
Bump up cryptography upperbound
rayluo Sep 20, 2022
45d0dc5
Merge pull request #501 from AzureAD/release-1.19.0
rayluo Sep 21, 2022
639f1b2
Test acquire_token_silent() for confidential client
rayluo Sep 21, 2022
f365f4e
Merge branch 'dev' into wam
rayluo Sep 28, 2022
11675e0
Adopt pymsalruntime 0.13
rayluo Sep 28, 2022
962fb0e
Remove automatic msa-pt for Azure CLI and Visual Studio
rayluo Sep 28, 2022
a08d6cd
Refactor test infrastructure to expose a known bug
rayluo Sep 30, 2022
3696acb
Add more docs
rayluo Oct 7, 2022
ac71fe8
Merge pull request #415 from AzureAD/wam
rayluo Oct 7, 2022
f9ff1a6
MSAL Python 1.20.0
rayluo Oct 7, 2022
0c73724
Update installation instruction
rayluo Oct 7, 2022
273868b
Merge pull request #504 from AzureAD/release-1.20.0
rayluo Oct 7, 2022
8026c02
Test matrix covers Python 3.11
rayluo Nov 2, 2022
efcb399
Merge pull request #512 from AzureAD/test-for-py3.11
rayluo Nov 3, 2022
c79d32e
Switch to new region endpoints
rayluo Nov 14, 2022
61e3ec8
Merge pull request #513 from AzureAD/new-region-endpoint
rayluo Nov 15, 2022
383fcfc
Test only Python versions available on github's ubuntu 22.04
rayluo Dec 8, 2022
4a93a6c
Merge branch 'update-ci' into dev
rayluo Dec 8, 2022
d0e4bb9
Cleaner skip declaration
rayluo Dec 9, 2022
fcb21b9
Merge branch 'refactor-e2e' into dev
rayluo Dec 9, 2022
14a4b79
Fallback to expires_on when expires_in is absent
rayluo Dec 8, 2022
eb8b71f
Merge pull request #519 from AzureAD/support_expires_on
rayluo Dec 9, 2022
6dc0fdd
Show lab api error, useful when trying api params
rayluo Jan 21, 2023
19d6aea
Merge branch 'lab-api-refactor' into dev
rayluo Jan 21, 2023
5dfe85e
Clarify when (not) to read API section
rayluo Oct 21, 2022
21bd422
Use broker for SSH Cert feature
rayluo Nov 10, 2022
918b93f
Merge pull request #515 from AzureAD/enable-msalruntime-for-ssh-cert
rayluo Jan 26, 2023
e8b52b7
Allow using client_id as scope. Needed by B2C.
rayluo Jan 27, 2023
8880918
Merge pull request #530 from AzureAD/allow-client_id-as-scope
rayluo Jan 30, 2023
206cf3e
Bump PyMsalRuntime to 0.13.2+
rayluo Jan 30, 2023
df5fefb
Merge branch 'use-pymsalruntime-0.13.2' into dev
rayluo Jan 31, 2023
8216e2c
MSAL Python 1.21.0
rayluo Jan 31, 2023
7593d08
Merge pull request #532 from AzureAD/release-1.21.0
rayluo Jan 31, 2023
e61e8a0
Remind user to use proper helper for ssh cert test
rayluo Jan 29, 2023
b277002
Merge branch 'refine-ssh-cert-test' into dev
rayluo Jan 30, 2023
7ecf651
point to correct quickstart
Dickson-Mwendia Feb 23, 2023
5cc9cd9
Merge pull request #537 from Dickson-Mwendia/update-doc-references
rayluo Feb 23, 2023
b9ac33f
Fix Markdown link
pamelafox Mar 2, 2023
a5fed26
Merge pull request #540 from pamelafox/patch-2
rayluo Mar 2, 2023
2fb3056
Remove effectiveless in-place clean
rayluo Feb 13, 2023
4673f4d
Merge branch 'cache-without-side-effect' into dev
rayluo Feb 15, 2023
0781ba1
Simplify and easier debugging
rayluo Feb 16, 2023
d4717ed
Merge branch 'refactor-oidc-discovery' into dev
rayluo Feb 17, 2023
8a429bd
Update setup.cfg with documentation URL (#539)
pamelafox Apr 6, 2023
ec4748d
Fix type introduced in #537
rayluo Feb 21, 2023
4b29298
Merge branch 'fix-docs-typo' into dev
rayluo Feb 23, 2023
078a573
Remove a deprecated attribute, scheduled for 1.21+
rayluo Feb 20, 2023
7e6a478
Merge pull request #547 from AzureAD/remove-deprecated-authority-http…
rayluo Apr 14, 2023
89ef015
CIAM end-to-end test cases based on new lab API
rayluo Dec 8, 2022
57c417b
Support https://contoso.ciamlogin.com as authority
rayluo Mar 1, 2023
ae7f40f
Bumping version numbers
rayluo Apr 4, 2023
74752cb
Merge pull request #520 from AzureAD/ciam
rayluo Apr 17, 2023
a0d0f0e
Merge branch 'release-1.22.0' into dev
rayluo Apr 7, 2023
a6023ea
Turns out they changed to a new tag for MSAL. Fix #539
rayluo Mar 30, 2023
d72409c
Merge branch 'fix-stackoverflow-link' into dev
rayluo Mar 31, 2023
a48e22b
Clarify that allow_broker is not applicable to ConfidentialClientAppl…
rayluo Mar 18, 2023
7d83e07
Merge pull request #559 from AzureAD/docs-staging
rayluo May 7, 2023
e1d2318
Merge remote-tracking branch 'oauth2cli/dev' into http-interface-polish
rayluo Mar 14, 2023
3517aed
No need for DummyHttpResponse
rayluo Mar 16, 2023
4a0eb39
Merge branch 'http-interface-polish' into dev
rayluo Mar 21, 2023
bc699f8
Adjustment for new CIAM partition
rayluo Apr 29, 2023
518d04e
Merge pull request #564 from AzureAD/ciam-in-new-partition
rayluo May 24, 2023
d4ca347
Improve logs
rayluo May 26, 2023
2638917
Merge branch 'improve-logs' into dev
rayluo May 30, 2023
634d328
Add more sections into TOC for the now long doc
rayluo May 31, 2023
6fe180f
Remove many Sphinx warnings
rayluo Jun 2, 2023
0d9050c
Merge branch 'docs-staging' into dev
rayluo Jun 6, 2023
97ec665
Github removes Python 2.7 support on 2023-6-19
rayluo Jun 8, 2023
d515d18
Merge branch 'fix-build-error' into dev
rayluo Jun 10, 2023
2184e50
Remove acquire_token_silent(..., account=None) usage in a backward-co…
rayluo Jun 29, 2023
0f635cd
Merge pull request #577 from AzureAD/silent-adjustment
rayluo Jul 22, 2023
0e5c685
Bumping up version numbers
rayluo Jul 12, 2023
7491207
Merge branch 'release-1.23.0' into dev
rayluo Jul 17, 2023
44df9c7
msaltest.py switches from confusing pprint to json
rayluo Jun 14, 2023
dfe4efa
Merge branch 'msaltest-json' into dev
rayluo Jun 15, 2023
76259f9
Switch from setup.py to setup.cfg
rayluo Jun 20, 2023
8d6d6c5
Ship release- branch of a non-draft PR to TestPyPI
rayluo Jun 21, 2023
54b65a2
Surface msal telemetry as a long opaque string
rayluo Jun 23, 2023
d98bb8f
Merge branch 'pipeline-adjustment' into dev
rayluo Jun 22, 2023
450ca64
Use a neutral name to hopefully avoid false alarm
rayluo Jul 24, 2023
4ae433d
Merge pull request #586 from AzureAD/avoid-false-alarm
rayluo Jul 27, 2023
4ca460f
Merge pull request #575 from AzureAD/msalruntime-telemetry
rayluo Aug 9, 2023
c943dc8
Fix typo in test names (warnning → warning)
musicinmybrain Aug 9, 2023
f5d2067
Merge pull request #588 from musicinmybrain/warnning
rayluo Aug 10, 2023
23e3ce2
Explicitly pip cache seems unnecessary
rayluo Jun 27, 2023
e90e36a
Merge branch 'simplify-workflow' into dev
rayluo Jun 28, 2023
546760d
Add enable_pii_log and wire it up with MsalRuntime
rayluo Aug 11, 2023
ca713b4
Merge pull request #590 from AzureAD/enable-pii
rayluo Aug 23, 2023
45d7682
Guarding against perf regression for acquire_token_for_client()
rayluo Jul 3, 2023
a3e6017
Add benchmark action and publish it to gh-pages
rayluo Jul 4, 2023
8d122f1
Merge pull request #580 from AzureAD/benchmark
rayluo Sep 6, 2023
dd9e090
Automatically check cryptography version
rayluo Aug 1, 2023
ae7cf14
Merge branch 'cryptography-ceiling' into dev
rayluo Aug 2, 2023
42516f5
Placeholders in some error will use curly brackets
rayluo Aug 22, 2023
e41b8f1
Merge branch 'improve-error-message' into dev
rayluo Aug 23, 2023
4850c81
Provide guidance on how to DIY the pkcs12-to-pem
rayluo Aug 24, 2023
8624982
Merge branch 'docs-staging' into dev
rayluo Aug 25, 2023
d788a88
Experimental: More precise regression detection
rayluo Aug 17, 2023
de3b757
Merge branch 'perf-baseline' into dev
rayluo Aug 28, 2023
0dd52f0
Refactor SshCert e2e test to use lab user
rayluo Aug 29, 2023
97152d8
E2E test for Azure CLI's connectedk8s AT POP
rayluo Aug 30, 2023
91f0b14
Add POP test function
rayluo Aug 31, 2023
dbd1508
Merge branch 'at-pop-with-external-key' into dev
rayluo Sep 1, 2023
4f146f7
Calls out that each commit triggers a TestPyPI release
rayluo Sep 4, 2023
62050de
Bumping version number
rayluo Sep 7, 2023
9a618ab
Merge branch 'release-1.24.0' into dev
rayluo Sep 11, 2023
841bb0b
CLI tester will be shipped with msal library
rayluo Sep 9, 2023
934414b
Merge branch 'tester' into dev
rayluo Sep 14, 2023
a4b76ab
Fix regression on input order for interactive test
rayluo Sep 10, 2023
1382469
Merge branch 'tester' into dev
rayluo Sep 16, 2023
affe78b
Merge remote-tracking branch 'oauth2cli/dev' into wip
rayluo Sep 3, 2023
0d9e07f
Bumping version number
rayluo Sep 26, 2023
1434c03
Merge branch 'release-1.24.1' into dev
rayluo Sep 29, 2023
82319cf
Mark package as supporting Python 3.12
tonybaloney Oct 6, 2023
a8e3811
Merge pull request #604 from tonybaloney/flag_312
rayluo Oct 6, 2023
e65781a
Remove x-client-cpu
rayluo Oct 6, 2023
593bcad
Merge pull request #605 from AzureAD/remove-x-client-cpu
rayluo Oct 11, 2023
a19f3e4
Resolve warnings node12 deprecation warnings
rayluo Sep 14, 2023
432ccc1
Merge branch 'upgrade-github-actions' into dev
rayluo Sep 21, 2023
395742f
Switch to ReadTheDocs configuration file v2
rayluo Sep 27, 2023
f4dc877
Merge branch 'docs-staging' into dev
rayluo Sep 28, 2023
aac3e26
Merge remote-tracking branch 'oauth2cli/dev' into docker-support
rayluo Oct 10, 2023
90d61ab
Hard code port for testing purpose
rayluo Oct 12, 2023
103a096
Merge branch 'docker-support' into dev
rayluo Oct 24, 2023
c113866
Fix a typo in api reference doc
rayluo Oct 2, 2023
7e2381d
Explain how to use global token cache and app
rayluo Oct 5, 2023
a482d93
Merge branch 'demo-global-token-cache' into dev
rayluo Oct 18, 2023
cf4b0da
Expose token_source for observability
rayluo Oct 19, 2023
e21a83a
Merge branch 'token-source' into dev
rayluo Oct 19, 2023
0b1a2fa
Deprecate allow_broker, use enable_broker_on_windows
rayluo Oct 25, 2023
9be25c1
add triage labels to bug report (#612)
jennyf19 Oct 30, 2023
a11469f
Merge branch 'broker-new-param' into dev
rayluo Oct 30, 2023
29abc4e
Only invoke broker for selected flows (grants)
rayluo Nov 2, 2023
5a6edde
Merge pull request #569 from AzureAD/device-flow-and-msal-runtime
rayluo Nov 3, 2023
943c75e
MSAL Python 1.25
rayluo Nov 6, 2023
1d9a426
Add more docs
rayluo Nov 9, 2023
87417a5
Merge pull request #621 from AzureAD/release-1.25.0
rayluo Nov 10, 2023
631472b
Remove newlines from description.
micwoj92 Nov 18, 2023
e50d86f
Merge pull request #626 from micwoj92/patch-1
rayluo Nov 23, 2023
63911a7
#629 - skip region discory when region=None (#630)
bgavrilMS Dec 1, 2023
fb182b7
AT POP for Public Client based on broker (#511)
rayluo Dec 5, 2023
b546cfe
Prepare 1.26 release
rayluo Nov 29, 2023
8e81f76
Merge branch 'release-1.26.0' into dev
rayluo Nov 29, 2023
afc6b1a
Integrate with PyMsalRuntime on mac
May 15, 2023
b26858b
How to smoke test MSAL Python
rayluo Aug 9, 2023
5a16d55
Preparing MSAL Python 1.27.0 beta release(s)
rayluo Dec 9, 2023
6788901
Update issue templates (#642)
bgavrilMS Dec 20, 2023
ae5b467
Merge branch 'oauth2cli/dev' to close #546
rayluo Dec 25, 2023
827e934
AT POP with SHR is tested with Graph end-to-end
rayluo Dec 28, 2023
50aad3c
Sort scopes before writing to token cache
rayluo Jan 2, 2024
a044379
O(1) happy path for access token hits
rayluo Jan 5, 2024
5688654
Might as well refactor a _get_app_metadata()
rayluo Jan 6, 2024
d1bf3fe
Merge pull request #644 from AzureAD/order-scopes
rayluo Jan 9, 2024
817fe8f
Prevent crash on token_cache.find(..., query=None)
rayluo Jan 9, 2024
7415c4d
Merge branch 'order-scopes' into dev
rayluo Jan 10, 2024
b8f9fe3
Attempts account removal from broker first
rayluo Jan 18, 2024
f9616b1
Adding docs for PopAuthScheme
rayluo Jan 19, 2024
0017b0f
Tested with latest cryptography 42.x
rayluo Jan 22, 2024
d3dd97e
Mention instance_discovery instead of validate_authority in an error …
rayluo Feb 22, 2023
3848c58
Merge remote-tracking branch 'oauth2cli/dev' into oauth2
rayluo Jan 29, 2024
6571e62
Tolerate ID token time errors
rayluo Jan 26, 2024
d5d88ce
Merge pull request #657 from AzureAD/id-token-adjustment
rayluo Jan 29, 2024
90a3e07
Provide examples for B2C and CIAM
rayluo May 15, 2023
0072f9d
Give a hint on where the client_id came from
rayluo Feb 3, 2024
bf2f1fe
Merge pull request #661 from AzureAD/document-client-id
rayluo Feb 6, 2024
1b77385
Allow github action to write perf result into repo
rayluo Feb 1, 2024
b718929
Adding attributes that were not auto documented
rayluo Feb 27, 2023
67baae5
Implement remove_tokens_for_client()
rayluo Feb 7, 2024
4f99373
Remove premature int(...)
rayluo Mar 10, 2023
656cc95
MSAL's fallback-from-broker behavior remains a FAQ
rayluo Jun 5, 2023
63f85e0
Change back to use print(result) in error path
rayluo Oct 31, 2023
d07e5d2
CCA can be tested by: python -m msal
rayluo Feb 8, 2024
6aa91cf
Pick up latest PyMsalRuntime 0.14.x
rayluo Feb 9, 2024
835ec07
Don't use bare except when importing (#667)
Singletoned Feb 22, 2024
1f064a9
Releasing 1.27
rayluo Dec 21, 2023
18db82d
update the default broker redirect uri
iulico-1 Mar 1, 2024
757b2ce
Merge pull request #673 from AzureAD/iulico/update-broker-default-red…
rayluo Mar 6, 2024
af5cdd5
Rebrand from AAD to Microsoft Entra (#655)
SHERMANOUKO Mar 11, 2024
341ca90
Implements a new optional oidc_authority parameter
rayluo Feb 27, 2024
cb47ad6
A semi-auto script to test Azure CLI with broker
rayluo Mar 5, 2024
b6ea305
MSAL Python 1.28.0
rayluo Mar 13, 2024
1bc2a47
Convert most built-in samples from json to dotenv
rayluo Jul 15, 2023
caf8480
client_credential has its link in RTD now. Finally.
rayluo Jul 25, 2023
cde68a4
Add CODEOWNERS file (#689)
pmaytak Apr 15, 2024
bef12b7
Should have used the constant instead of a raw str
rayluo Dec 8, 2023
0cec70f
Merges remote-tracking branch and fixes #516
rayluo Apr 21, 2023
79f2381
Upgrade action's versions and also enable cache
rayluo Nov 14, 2023
c5cc6e0
Suggests to use XDG_RUNTIME_DIR for token cache
rayluo Apr 18, 2024
3c6628f
Support reading CCA cert from a pfx file. Tested.
rayluo May 3, 2024
00107fb
Lab API changed since May 14, 2024
rayluo May 24, 2024
c0c385c
No longer need to fake device code flow endpoint
rayluo Aug 3, 2023
cf68d13
Promote TokenCache._find() to TokenCache.search()
rayluo Apr 12, 2024
d2d2373
Update the ENV VAR hints in the top of test_e2e.py
rayluo May 28, 2024
6dea928
Pick up PyMsalRuntime 0.16.x
rayluo Jun 7, 2024
deb9fe1
Adapting to a lab change introduced today
rayluo Jun 11, 2024
255bf80
Merge branch 'release-1.28.1' into dev
rayluo Jun 12, 2024
b31360d
Read credential from pfx
rayluo Jun 5, 2024
6ebaa80
Enable public client ROPC via broker. On Windows, ROPC will call WAM; on
fengga Jun 17, 2024
c7ec8a8
Remove a fallback which ends up breaking tests now
rayluo Jun 19, 2024
27e1bf7
Improve error message to fix 710
rayluo Jun 14, 2024
964972e
Merge branch 'dev' into enable-ROPC-via-broker
fengga Jun 19, 2024
a2c189e
Merge pull request #712 from AzureAD/enable-ROPC-via-broker
fengga Jun 20, 2024
a0bf53e
Managed Identity implementation
rayluo Feb 22, 2023
750d8ff
Refactor throttling and add it to Managed Identity
rayluo Apr 20, 2024
ba00804
Use a short throttling threshold for MI (and CCA)
rayluo Apr 22, 2024
c7c9dc4
Managed Identity for Machine Learning
rayluo Apr 23, 2024
44434c8
Implementation based on feature requirement
rayluo May 24, 2024
565f175
get_managed_identity_source() for Azure Identity
rayluo May 1, 2024
828b4b8
Merge branch 'mi' into dev
rayluo Jun 13, 2024
34fa99f
MSAL Python 1.29.0
rayluo Jun 18, 2024
c0bd219
The old test app was somehow disabled
rayluo Jun 25, 2024
a86a6c8
Update ROPC broker related tests (#714)
fengga Jun 27, 2024
8267056
Add the missing token query check
rayluo Jun 27, 2024
2663232
Merge branch 'dev' into bugfix-token-cache-search
rayluo Jul 3, 2024
539c913
Support SNI via PFX
rayluo Jul 4, 2024
b3659d7
Re-optimize the O(1) code path
rayluo Jul 5, 2024
6f2308c
Merge branch 'dev' into perf-retune
rayluo Jul 9, 2024
77c72fb
Merge remote-tracking branch 'oauth2cli/dev' into sha256-pss
rayluo Jul 10, 2024
af3b887
Using SHA256 and PSS padding
rayluo Jul 11, 2024
bd0c820
Expose refresh_on (if any) to fresh or cached response
rayluo Jul 12, 2024
3a4ece6
MSAL 1.30.0
rayluo Jul 17, 2024
8159461
Bumping cryptography upper bound
rayluo Jul 23, 2024
1a6398a
Delay getfqdn() from import time to runtime. Fix #715
rayluo Jul 22, 2024
f642acd
Refine inline comment
rayluo Jul 26, 2024
33b1b26
Mentions MSAL-Extensions
rayluo Jul 29, 2024
14ef644
Change arc mi's detection algorithm
rayluo Jul 30, 2024
fab54d4
CAE for MIv1
rayluo Aug 5, 2024
68003af
Merge branch 'dev' into mac
rayluo Jul 24, 2024
49ea5f8
Anticipate a PyMsalRuntime 0.17.0 release soon
rayluo Jul 25, 2024
e7e7669
Add an unofficial doc for mac broker integration (#732)
fengga Aug 23, 2024
3995ad0
Switch to the future-proof ciamcud tenant
rayluo Aug 6, 2024
c64e50f
Refactor to reuse CIAM test cases for CIAM CUD
rayluo Aug 7, 2024
3630410
Fix typos in doc
rayluo Jul 19, 2024
7f6d5a6
parent_window_handle is also needed on Mac
rayluo Aug 9, 2024
b94d7e6
Explicitly test current broker fallback behaviors
rayluo Aug 14, 2024
06c5215
ADFS and B2C shall not invoke broker
rayluo Aug 15, 2024
aa788db
Merge branch 'broker-refactor' into dev
rayluo Aug 15, 2024
70c8ff4
Error out on invalid ManagedIdentity dict
rayluo Aug 19, 2024
a246e7a
Resource id adjustments
rayluo Aug 30, 2024
5aac0c4
Release MSAL Python 1.31.0
rayluo Aug 16, 2024
a421b70
Test PyMsalRuntime ImportError and RuntimeError
rayluo Aug 22, 2024
8a136b2
Expose and document AutoRefresher, deprecate client_assertion as a st…
rayluo Aug 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 63 additions & 15 deletions msal/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from .oauth2cli import Client, JwtAssertionCreator
from .oauth2cli.oidc import decode_part
from .authority import Authority
from .authority import Authority, WORLD_WIDE
from .mex import send_request as mex_send_request
from .wstrust_request import send_request as wst_send_request
from .wstrust_response import *
Expand Down Expand Up @@ -146,7 +146,6 @@ def obtain_token_by_username_password(self, username, password, **kwargs):


class ClientApplication(object):

ACQUIRE_TOKEN_SILENT_ID = "84"
ACQUIRE_TOKEN_BY_REFRESH_TOKEN = "85"
ACQUIRE_TOKEN_BY_USERNAME_PASSWORD_ID = "301"
Expand Down Expand Up @@ -174,6 +173,7 @@ def __init__(
# when we would eventually want to add this feature to PCA in future.
exclude_scopes=None,
http_cache=None,
instance_discovery=None,
):
"""Create an instance of application.

Expand Down Expand Up @@ -409,11 +409,40 @@ def __init__(
Personally Identifiable Information (PII). Encryption is unnecessary.

New in version 1.16.0.

:param boolean instance_discovery:
Historically, MSAL would connect to a central endpoint located at
``https://login.microsoftonline.com`` to acquire some metadata,
especially when using an unfamiliar authority.
This behavior is known as Instance Discovery.

This parameter defaults to None, which enables the Instance Discovery.

If you know some authorities which you allow MSAL to operate with as-is,
without involving any Instance Discovery, the recommended pattern is::

known_authorities = frozenset([ # Treat your known authorities as const
"https://contoso.com/adfs", "https://login.azs/foo"])
...
authority = "https://contoso.com/adfs" # Assuming your app will use this
app1 = PublicClientApplication(
"client_id",
authority=authority,
# Conditionally disable Instance Discovery for known authorities
instance_discovery=authority not in known_authorities,
)

If you do not know some authorities beforehand,
yet still want MSAL to accept any authority that you will provide,
you can use a ``False`` to unconditionally disable Instance Discovery.

New in version 1.19.0.
"""
self.client_id = client_id
self.client_credential = client_credential
self.client_claims = client_claims
self._client_capabilities = client_capabilities
self._instance_discovery = instance_discovery

if exclude_scopes and not isinstance(exclude_scopes, list):
raise ValueError(
Expand Down Expand Up @@ -453,18 +482,24 @@ def __init__(

# Here the self.authority will not be the same type as authority in input
try:
authority_to_use = authority or "https://{}/common/".format(WORLD_WIDE)
self.authority = Authority(
authority or "https://login.microsoftonline.com/common/",
self.http_client, validate_authority=validate_authority)
authority_to_use,
self.http_client,
validate_authority=validate_authority,
instance_discovery=self._instance_discovery,
)
except ValueError: # Those are explicit authority validation errors
raise
except Exception: # The rest are typically connection errors
if validate_authority and azure_region:
# Since caller opts in to use region, here we tolerate connection
# errors happened during authority validation at non-region endpoint
self.authority = Authority(
authority or "https://login.microsoftonline.com/common/",
self.http_client, validate_authority=False)
authority_to_use,
self.http_client,
instance_discovery=False,
)
else:
raise

Expand Down Expand Up @@ -534,10 +569,11 @@ def _get_regional_authority(self, central_authority):
"sts.windows.net",
)
else "{}.{}".format(region_to_use, central_authority.instance))
return Authority(
return Authority( # The central_authority has already been validated
"https://{}/{}".format(regional_host, central_authority.tenant),
self.http_client,
validate_authority=False) # The central_authority has already been validated
instance_discovery=False,
)
return None

def _build_client(self, client_credential, authority, skip_regional_client=False):
Expand Down Expand Up @@ -789,7 +825,8 @@ def get_authorization_request_url(
# Multi-tenant app can use new authority on demand
the_authority = Authority(
authority,
self.http_client
self.http_client,
instance_discovery=self._instance_discovery,
) if authority else self.authority

client = _ClientWithCcsRoutingInfo(
Expand Down Expand Up @@ -1012,14 +1049,23 @@ def _find_msal_accounts(self, environment):
}
return list(grouped_accounts.values())

def _get_instance_metadata(self): # This exists so it can be mocked in unit test
resp = self.http_client.get(
"https://login.microsoftonline.com/common/discovery/instance?api-version=1.1&authorization_endpoint=https://login.microsoftonline.com/common/oauth2/authorize", # TBD: We may extend this to use self._instance_discovery endpoint
headers={'Accept': 'application/json'})
resp.raise_for_status()
return json.loads(resp.text)['metadata']

def _get_authority_aliases(self, instance):
if self._instance_discovery is False:
return []
if self.authority._is_known_to_developer:
# Then it is an ADFS/B2C/known_authority_hosts situation
# which may not reach the central endpoint, so we skip it.
return []
if not self.authority_groups:
resp = self.http_client.get(
"https://login.microsoftonline.com/common/discovery/instance?api-version=1.1&authorization_endpoint=https://login.microsoftonline.com/common/oauth2/authorize",
headers={'Accept': 'application/json'})
resp.raise_for_status()
self.authority_groups = [
set(group['aliases']) for group in json.loads(resp.text)['metadata']]
set(group['aliases']) for group in self._get_instance_metadata()]
for group in self.authority_groups:
if instance in group:
return [alias for alias in group if alias != instance]
Expand Down Expand Up @@ -1168,6 +1214,7 @@ def acquire_token_silent_with_error(
# the_authority = Authority(
# authority,
# self.http_client,
# instance_discovery=self._instance_discovery,
# ) if authority else self.authority
result = self._acquire_token_silent_from_cache_and_possibly_refresh_it(
scopes, account, self.authority, force_refresh=force_refresh,
Expand All @@ -1189,7 +1236,8 @@ def acquire_token_silent_with_error(
the_authority = Authority(
"https://" + alias + "/" + self.authority.tenant,
self.http_client,
validate_authority=False)
instance_discovery=False,
)
result = self._acquire_token_silent_from_cache_and_possibly_refresh_it(
scopes, account, the_authority, force_refresh=force_refresh,
claims_challenge=claims_challenge,
Expand Down
44 changes: 29 additions & 15 deletions msal/authority.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ def http_client(self): # Obsolete. We will remove this eventually
"authority.http_client might be removed in MSAL Python 1.21+", DeprecationWarning)
return self._http_client

def __init__(self, authority_url, http_client, validate_authority=True):
def __init__(
self, authority_url, http_client,
validate_authority=True,
instance_discovery=None,
):
"""Creates an authority instance, and also validates it.

:param validate_authority:
Expand All @@ -67,19 +71,34 @@ def __init__(self, authority_url, http_client, validate_authority=True):
This parameter only controls whether an instance discovery will be
performed.
"""
# :param instance_discovery:
# By default, the known-to-Microsoft validation will use an
# instance discovery endpoint located at ``login.microsoftonline.com``.
# You can customize the endpoint by providing a url as a string.
# Or you can turn this behavior off by passing in a False here.
self._http_client = http_client
if isinstance(authority_url, AuthorityBuilder):
authority_url = str(authority_url)
authority, self.instance, tenant = canonicalize(authority_url)
self.is_adfs = tenant.lower() == 'adfs'
parts = authority.path.split('/')
is_b2c = any(self.instance.endswith("." + d) for d in WELL_KNOWN_B2C_HOSTS) or (
len(parts) == 3 and parts[2].lower().startswith("b2c_"))
if (tenant != "adfs" and (not is_b2c) and validate_authority
and self.instance not in WELL_KNOWN_AUTHORITY_HOSTS):
payload = instance_discovery(
is_b2c = any(
self.instance.endswith("." + d) for d in WELL_KNOWN_B2C_HOSTS
) or (len(parts) == 3 and parts[2].lower().startswith("b2c_"))
self._is_known_to_developer = self.is_adfs or is_b2c or not validate_authority
is_known_to_microsoft = self.instance in WELL_KNOWN_AUTHORITY_HOSTS
instance_discovery_endpoint = 'https://{}/common/discovery/instance'.format( # Note: This URL seemingly returns V1 endpoint only
WORLD_WIDE # Historically using WORLD_WIDE. Could use self.instance too
# See https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/4.0.0/src/Microsoft.Identity.Client/Instance/AadInstanceDiscovery.cs#L101-L103
# and https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/4.0.0/src/Microsoft.Identity.Client/Instance/AadAuthority.cs#L19-L33
) if instance_discovery in (None, True) else instance_discovery
if instance_discovery_endpoint and not (
is_known_to_microsoft or self._is_known_to_developer):
payload = _instance_discovery(
"https://{}{}/oauth2/v2.0/authorize".format(
self.instance, authority.path),
self._http_client)
self._http_client,
instance_discovery_endpoint)
if payload.get("error") == "invalid_instance":
raise ValueError(
"invalid_instance: "
Expand Down Expand Up @@ -113,7 +132,6 @@ def __init__(self, authority_url, http_client, validate_authority=True):
self.token_endpoint = openid_config['token_endpoint']
self.device_authorization_endpoint = openid_config.get('device_authorization_endpoint')
_, _, self.tenant = canonicalize(self.token_endpoint) # Usually a GUID
self.is_adfs = self.tenant.lower() == 'adfs'

def user_realm_discovery(self, username, correlation_id=None, response=None):
# It will typically return a dict containing "ver", "account_type",
Expand Down Expand Up @@ -145,13 +163,9 @@ def canonicalize(authority_url):
% authority_url)
return authority, authority.hostname, parts[1]

def instance_discovery(url, http_client, **kwargs):
resp = http_client.get( # Note: This URL seemingly returns V1 endpoint only
'https://{}/common/discovery/instance'.format(
WORLD_WIDE # Historically using WORLD_WIDE. Could use self.instance too
# See https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/4.0.0/src/Microsoft.Identity.Client/Instance/AadInstanceDiscovery.cs#L101-L103
# and https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/4.0.0/src/Microsoft.Identity.Client/Instance/AadAuthority.cs#L19-L33
),
def _instance_discovery(url, http_client, instance_discovery_endpoint, **kwargs):
resp = http_client.get(
instance_discovery_endpoint,
params={'authorization_endpoint': url, 'api-version': '1.0'},
**kwargs)
return json.loads(resp.text)
Expand Down
66 changes: 66 additions & 0 deletions tests/test_authority.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import os
try:
from unittest.mock import patch
except:
from mock import patch

import msal
from msal.authority import *
from tests import unittest
from tests.http_client import MinimalHttpClient
Expand Down Expand Up @@ -123,3 +128,64 @@ class MockResponse(object):
finally: # MUST NOT let the previous test changes affect other test cases
Authority._domains_without_user_realm_discovery = set([])


@patch("msal.authority.tenant_discovery", return_value={
"authorization_endpoint": "https://contoso.com/placeholder",
"token_endpoint": "https://contoso.com/placeholder",
})
@patch("msal.authority._instance_discovery")
@patch.object(msal.ClientApplication, "_get_instance_metadata", return_value=[])
class TestMsalBehaviorsWithoutAndWithInstanceDiscoveryBoolean(unittest.TestCase):
"""Test cases use ClientApplication, which is a base class of both PCA and CCA"""

def test_by_default_a_known_to_microsoft_authority_should_skip_validation_but_still_use_instance_metadata(
self, instance_metadata, known_to_microsoft_validation, _):
app = msal.ClientApplication("id", authority="https://login.microsoftonline.com/common")
known_to_microsoft_validation.assert_not_called()
app.get_accounts() # This could make an instance metadata call for authority aliases
instance_metadata.assert_called_once_with()

def test_validate_authority_boolean_should_skip_validation_and_instance_metadata(
self, instance_metadata, known_to_microsoft_validation, _):
"""Pending deprecation, but kept for backward compatibility, for now"""
app = msal.ClientApplication(
"id", authority="https://contoso.com/common", validate_authority=False)
known_to_microsoft_validation.assert_not_called()
app.get_accounts() # This could make an instance metadata call for authority aliases
instance_metadata.assert_not_called()

def test_by_default_adfs_should_skip_validation_and_instance_metadata(
self, instance_metadata, known_to_microsoft_validation, _):
"""Not strictly required, but when/if we already supported it, we better keep it"""
app = msal.ClientApplication("id", authority="https://contoso.com/adfs")
known_to_microsoft_validation.assert_not_called()
app.get_accounts() # This could make an instance metadata call for authority aliases
instance_metadata.assert_not_called()

def test_by_default_b2c_should_skip_validation_and_instance_metadata(
self, instance_metadata, known_to_microsoft_validation, _):
"""Not strictly required, but when/if we already supported it, we better keep it"""
app = msal.ClientApplication(
"id", authority="https://login.b2clogin.com/contoso/b2c_policy")
known_to_microsoft_validation.assert_not_called()
app.get_accounts() # This could make an instance metadata call for authority aliases
instance_metadata.assert_not_called()

def test_turning_off_instance_discovery_should_work_for_all_kinds_of_clouds(
self, instance_metadata, known_to_microsoft_validation, _):
for authority in [
"https://login.microsoftonline.com/common", # Known to Microsoft
"https://contoso.com/adfs", # ADFS
"https://login.b2clogin.com/contoso/b2c_policy", # B2C
"https://private.cloud/foo", # Private Cloud
]:
self._test_turning_off_instance_discovery_should_skip_authority_validation_and_instance_metadata(
authority, instance_metadata, known_to_microsoft_validation)

def _test_turning_off_instance_discovery_should_skip_authority_validation_and_instance_metadata(
self, authority, instance_metadata, known_to_microsoft_validation):
app = msal.ClientApplication("id", authority=authority, instance_discovery=False)
known_to_microsoft_validation.assert_not_called()
app.get_accounts() # This could make an instance metadata call for authority aliases
instance_metadata.assert_not_called()