|
5 | 5 | import sys |
6 | 6 | import warnings |
7 | 7 | from threading import Lock |
| 8 | +from typing import Optional # Needed in Python 3.7 & 3.8 |
8 | 9 | import os |
9 | 10 |
|
10 | 11 | from .oauth2cli import Client, JwtAssertionCreator |
@@ -448,11 +449,14 @@ def __init__( |
448 | 449 | Instructs MSAL to use the Entra regional token service. This legacy feature is only available to |
449 | 450 | first-party applications. Only ``acquire_token_for_client()`` is supported. |
450 | 451 |
|
451 | | - Supports 3 values: |
| 452 | + Supports 4 values: |
452 | 453 |
|
453 | | - ``azure_region=None`` - meaning no region is used. This is the default value. |
454 | | - ``azure_region="some_region"`` - meaning the specified region is used. |
455 | | - ``azure_region=True`` - meaning MSAL will try to auto-detect the region. This is not recommended. |
| 454 | + 1. ``azure_region=None`` - This default value means no region is configured. |
| 455 | + MSAL will use the region defined in env var ``MSAL_FORCE_REGION``. |
| 456 | + 2. ``azure_region="some_region"`` - meaning the specified region is used. |
| 457 | + 3. ``azure_region=True`` - meaning |
| 458 | + MSAL will try to auto-detect the region. This is not recommended. |
| 459 | + 4. ``azure_region=False`` - meaning MSAL will use no region. |
456 | 460 |
|
457 | 461 | .. note:: |
458 | 462 | Region auto-discovery has been tested on VMs and on Azure Functions. It is unreliable. |
@@ -630,7 +634,10 @@ def __init__( |
630 | 634 | except ValueError: # Those are explicit authority validation errors |
631 | 635 | raise |
632 | 636 | except Exception: # The rest are typically connection errors |
633 | | - if validate_authority and azure_region and not oidc_authority: |
| 637 | + if validate_authority and not oidc_authority and ( |
| 638 | + azure_region # Opted in to use region |
| 639 | + or (azure_region is None and os.getenv("MSAL_FORCE_REGION")) # Will use region |
| 640 | + ): |
634 | 641 | # Since caller opts in to use region, here we tolerate connection |
635 | 642 | # errors happened during authority validation at non-region endpoint |
636 | 643 | self.authority = Authority( |
@@ -724,9 +731,11 @@ def _build_telemetry_context( |
724 | 731 | self._telemetry_buffer, self._telemetry_lock, api_id, |
725 | 732 | correlation_id=correlation_id, refresh_reason=refresh_reason) |
726 | 733 |
|
727 | | - def _get_regional_authority(self, central_authority): |
728 | | - if not self._region_configured: # User did not opt-in to ESTS-R |
| 734 | + def _get_regional_authority(self, central_authority) -> Optional[Authority]: |
| 735 | + if self._region_configured is False: # User opts out of ESTS-R |
729 | 736 | return None # Short circuit to completely bypass region detection |
| 737 | + if self._region_configured is None: # User did not make an ESTS-R choice |
| 738 | + self._region_configured = os.getenv("MSAL_FORCE_REGION") or None |
730 | 739 | self._region_detected = self._region_detected or _detect_region( |
731 | 740 | self.http_client if self._region_configured is not None else None) |
732 | 741 | if (self._region_configured != self.ATTEMPT_REGION_DISCOVERY |
|
0 commit comments