Document tenant-specific authority selection for on-behalf-of token redemption
Maintainer thường phản hồi trong vòng 7 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 1/5
- Thời gian dự kiến
- Dưới một giờ
- Mức phù hợp với người mới
- 88/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- python
- Lĩnh vực
- documentation
Hướng nghiên cứu
Mở msal/application.py và đọc docstring của acquire_token_on_behalf_of() quanh các dòng 2721-2758, sau đó so sánh nó với gợi ý runtime hiện có trong acquire_token_for_client() tại các dòng 2680-2684. Thêm khối .. note:: được đề xuất sau đoạn văn về sự đồng ý, giữ thụt lề reStructuredText nhất quán với docstring xung quanh. Hoàn thành nghĩa là docstring được render mà không có cảnh báo docutils và nêu hướng dẫn về authority dành riêng cho tenant với ví dụ tid; không có test nào bao phủ thay đổi này.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
MSAL client type
Confidential
Problem Statement
Location (AzureAD/microsoft-authentication-library-for-python @ default branch c53d5ea):
msal/application.py:2721-2758—acquire_token_on_behalf_of()and its docstringmsal/application.py:2680-2684— the existing runtime hint inacquire_token_for_client()
def acquire_token_on_behalf_of(self, user_assertion, scopes, claims_challenge=None, forwarded_client_claims=None, **kwargs):
"""Acquires token using on-behalf-of (OBO) flow.
The current app is a middle-tier service which was called with a token
representing an end user.
The current app can use such token (a.k.a. a user assertion) to request
another token to access downstream web API, on behalf of that user.
See `detail docs here <https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow>`_ .
...
if self.authority.tenant.lower() in ["common", "organizations"]:
warnings.warn(
"Using /common or /organizations authority "
"in acquire_token_for_client() is unreliable. "
"Please use a specific tenant instead.", DeprecationWarning)
Problem
A middle-tier web API commonly reuses a ConfidentialClientApplication that was configured with https://login.microsoftonline.com/common or /organizations (for example, copied from a multi-tenant sign-in setup) and then calls acquire_token_on_behalf_of(). Nothing at the Python entry point tells the developer which tenant the OBO redemption should target, which is where guest-user and Conditional Access scenarios become hard to diagnose.
The acquire_token_on_behalf_of() docstring links to the generic protocol page and the consent section, but says nothing about which authority/tenant to use. By contrast, acquire_token_for_client() already warns at runtime that /common or /organizations "is unreliable. Please use a specific tenant instead." There is no similar hint, in code or docs, for OBO.
The MSAL.NET documentation has an explicit section, "Important Note on On-Behalf-Of (OBO) Flow with Guest Users":
When performing the On-Behalf-Of (OBO) flow, especially with guest users, it is important to target the specific tenant, denoted by the tid claim from the client token. Do not use /common or /organizations in OBO, because the token will be for the user's home tenant.
MSAL Python currently has no equivalent guidance.
This is a documentation/usability request. I am not reporting a failing request or a security problem in MSAL Python itself.
Reproduction
There is no failure to reproduce; the gap is in the documentation. Reading help(msal.ConfidentialClientApplication.acquire_token_on_behalf_of) on c53d5ea shows no guidance on authority or tenant selection, and calling it on an application configured with /common or /organizations emits no hint, unlike acquire_token_for_client().
Environment: checked on dev at c53d5ea47f77.
Proposed solution
Suggested fix
Add guidance to the acquire_token_on_behalf_of() docstring (and, if appropriate, the MSAL Python docs on learn.microsoft.com or a sample) that:
- Recommends a tenant-specific authority for OBO redemption rather than
/commonor/organizations, mirroring the MSAL.NET note. - Explains that, for guest users, the tenant to target is the one in the incoming token's
tidclaim (the resource tenant), not the user's home tenant. - Shows a short Python example that takes
tidfrom the already validated incoming access token (that is, after the web API's own JWT validation, not from an unverified decode) and builds a per-tenantConfidentialClientApplication, e.g.:
# `claims` = claims of the incoming access token, AFTER your API has validated it
tenant_id = claims["tid"]
cca = msal.ConfidentialClientApplication(
client_id, client_credential=secret,
authority=f"https://login.microsoftonline.com/{tenant_id}",
token_cache=cache,
)
result = cca.acquire_token_on_behalf_of(incoming_token, scopes=["https://graph.microsoft.com/.default"])
Out of scope: MSAL automatically trusting or decoding tid from an unvalidated assertion. A runtime DeprecationWarning, like the one in acquire_token_for_client(), could be considered separately, but documentation is the main request here, since a warning alone would not explain which tenant to use.
This docstring change covers points 1–3 above, adding a note with the MSAL.NET guidance and the example to acquire_token_on_behalf_of():
diff --git a/msal/application.py b/msal/application.py
--- a/msal/application.py
+++ b/msal/application.py
@@ -2731,6 +2731,24 @@ class ConfidentialClientApplication(ClientApplication): # server-side web app
See how to gain consent upfront for your middle-tier app from this article.
https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow#gaining-consent-for-the-middle-tier-application
+ .. note::
+ Use a tenant-specific authority for OBO rather than ``/common`` or
+ ``/organizations``, which would get a token for the user's home tenant.
+ This matters especially for guest users: target the tenant in the
+ ``tid`` claim of the incoming token (the resource tenant).
+ Read ``tid`` only after your web API has validated the incoming token,
+ for example::
+
+ # `claims` = claims of the incoming access token, AFTER your API has validated it
+ tenant_id = claims["tid"]
+ cca = msal.ConfidentialClientApplication(
+ client_id, client_credential=secret,
+ authority=f"https://login.microsoftonline.com/{tenant_id}",
+ token_cache=cache,
+ )
+ result = cca.acquire_token_on_behalf_of(
+ incoming_token, scopes=["https://graph.microsoft.com/.default"])
+
:param str user_assertion: The incoming token already received by this app
:param list[str] scopes: Scopes required by downstream API (a resource).
:param claims_challenge:
This is a documentation-only change, so there is no test for it; on d73407a the updated acquire_token_on_behalf_of() docstring parses as reStructuredText without warnings (docutils 0.23, Python 3.13.18).
I can submit a PR for this.
Related: checked #53 (original OBO support request, implemented; does not cover tenant selection), #909 (closed unmerged; switched two OBO integration tests from /organizations to a tenant-specific authority because of Conditional Access failures in CI, a practical example of the same pitfall), and #147 (web API token-validation example, not OBO authority selection).
- Ngôn ngữ chính
- Python
- Star
- 971
- Fork
- 223
- Merge trung bình
- 4 ngày 2 giờ
- Pull request đã merge (30 ngày)
- 3
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của AzureAD/microsoft-authentication-library-for-python
-
needs attention untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
AzureAD/microsoft-authentication-library-for-python#997 ·
Maintainer thường phản hồi trong vòng 7 ngày
-
[Bug] User Realm Discovery drops the authority's port, so username/password sign-in fails on non-443 authoritiesCó thể đã có người làm @THEtheChad đã nhận 20 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 25/100
AzureAD/microsoft-authentication-library-for-python#989 ·
Maintainer thường phản hồi trong vòng 7 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 55/100
AzureAD/microsoft-authentication-library-for-python#984 ·
Maintainer thường phản hồi trong vòng 7 ngày
-
[Bug] Support library-agnostic HTTP clients for Service Fabric managed identityCó thể đã có người làm @4gust đã nhận 20 ngày trước. Đang mởAzureSDK bug P1
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
AzureAD/microsoft-authentication-library-for-python#965 · 1 bình luận · 1 người được giao ·
Maintainer thường phản hồi trong vòng 7 ngày
-
http_client docstring does not say that timeout, verify and proxies only configure the default sessionCó thể đã có người làm @HardMax71 đã nhận 84 ngày trước. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 25/100
AzureAD/microsoft-authentication-library-for-python#960 ·
Maintainer thường phản hồi trong vòng 7 ngày
Tất cả issue của AzureAD/microsoft-authentication-library-for-python
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
bad-antics/lateralus-lang#1623 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
fossguild/thelifesnake#82 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Unfitted DialectClassifier crashes with a confusing ValueError: raise a clear "not fitted" errorĐang mởgood first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
omarelshamy25x/shamy-arabic-nlp#111 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
pietbarber/Manage2Soar#1189 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
BUG: write_netcdf fails on Tutorial 3 BEM output with pandas CategoricalDtype DOF coordinatesCó thể đã có người làm @dtgaebe đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
sandialabs/WecOptTool#467 ·