-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathmsal_credentials.py
More file actions
138 lines (115 loc) · 5.35 KB
/
msal_credentials.py
File metadata and controls
138 lines (115 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import os
from typing import Any, List, Union, Dict, Optional
from typing_extensions import Self
import msal
from .msal_client import MsalClient
from .utils import get_default_authority, normalize_authority, resolve_tenant, validate_tenant_id
from .._constants import EnvironmentVariables
from .._persistent_cache import _load_persistent_cache
class MsalCredential: # pylint: disable=too-many-instance-attributes
"""Base class for credentials wrapping MSAL applications.
:param str client_id: the principal's client ID
:param client_credential: client credential data for the application
:type client_credential: dict
"""
def __init__(
self,
client_id: str,
client_credential: Optional[Union[str, Dict[str, str]]] = None,
*,
additionally_allowed_tenants: Optional[List[str]] = None,
authority: Optional[str] = None,
disable_instance_discovery: Optional[bool] = None,
tenant_id: Optional[str] = None,
enable_support_logging: Optional[bool] = None,
**kwargs: Any
) -> None:
self._instance_discovery = None if disable_instance_discovery is None else not disable_instance_discovery
self._authority = normalize_authority(authority) if authority else get_default_authority()
self._regional_authority = os.environ.get(EnvironmentVariables.AZURE_REGIONAL_AUTHORITY_NAME)
if self._regional_authority and self._regional_authority.lower() in ["tryautodetect", "true"]:
self._regional_authority = msal.ConfidentialClientApplication.ATTEMPT_REGION_DISCOVERY
self._tenant_id = tenant_id or "organizations"
validate_tenant_id(self._tenant_id)
self._client = MsalClient(**kwargs)
self._client_credential = client_credential
self._client_id = client_id
self._enable_support_logging = enable_support_logging
self._additionally_allowed_tenants = additionally_allowed_tenants or []
self._client_applications: Dict[str, msal.ClientApplication] = {}
self._cae_client_applications: Dict[str, msal.ClientApplication] = {}
self._cache = kwargs.pop("_cache", None)
self._cae_cache = kwargs.pop("_cae_cache", None)
if self._cache or self._cae_cache:
self._custom_cache = True
else:
self._custom_cache = False
self._cache_options = kwargs.pop("cache_persistence_options", None)
super(MsalCredential, self).__init__()
def __enter__(self) -> "Self":
self._client.__enter__()
return self
def __exit__(self, *args: Any) -> None:
self._client.__exit__(*args)
def close(self) -> None:
self.__exit__()
def _initialize_cache(self, is_cae: bool = False) -> msal.TokenCache:
if self._cache_options:
if is_cae:
self._cae_cache = _load_persistent_cache(self._cache_options, is_cae)
else:
self._cache = _load_persistent_cache(self._cache_options, is_cae)
else:
if is_cae:
self._cae_cache = msal.TokenCache()
else:
self._cache = msal.TokenCache()
return self._cae_cache if is_cae else self._cache
def _get_app(self, **kwargs: Any) -> msal.ClientApplication:
tenant_id = resolve_tenant(
self._tenant_id, additionally_allowed_tenants=self._additionally_allowed_tenants, **kwargs
)
client_applications_map = self._client_applications
capabilities = None
token_cache = self._cache
app_class = msal.ConfidentialClientApplication if self._client_credential else msal.PublicClientApplication
if kwargs.get("enable_cae"):
client_applications_map = self._cae_client_applications
capabilities = ["CP1"]
token_cache = self._cae_cache
if not token_cache:
token_cache = self._initialize_cache(is_cae=bool(kwargs.get("enable_cae")))
if tenant_id not in client_applications_map:
client_applications_map[tenant_id] = app_class(
client_id=self._client_id,
client_credential=self._client_credential,
client_capabilities=capabilities,
authority="{}/{}".format(self._authority, tenant_id),
azure_region=self._regional_authority,
token_cache=token_cache,
http_client=self._client,
instance_discovery=self._instance_discovery,
enable_pii_log=self._enable_support_logging,
)
return client_applications_map[tenant_id]
def __getstate__(self) -> Dict[str, Any]:
state = self.__dict__.copy()
# Remove the non-picklable entries
del state["_client_applications"]
del state["_cae_client_applications"]
if not self._custom_cache:
del state["_cache"]
del state["_cae_cache"]
return state
def __setstate__(self, state: Dict[str, Any]) -> None:
self.__dict__.update(state)
# Re-create the unpickable entries
self._client_applications = {}
self._cae_client_applications = {}
if not self._custom_cache:
self._cache = None
self._cae_cache = None