| 1 |
# Copyright (c) Microsoft Corporation. |
| 2 |
# Licensed under the MIT License |
| 3 |
from typing import TYPE_CHECKING |
| 4 |
from urllib.parse import urlparse |
| 5 |
|
| 6 |
from ._token_providers import ( |
| 7 |
TokenProviderBase, |
| 8 |
BasicTokenProvider, |
| 9 |
CallbackTokenProvider, |
| 10 |
MsiTokenProvider, |
| 11 |
AzCliTokenProvider, |
| 12 |
UserPassTokenProvider, |
| 13 |
DeviceLoginTokenProvider, |
| 14 |
InteractiveLoginTokenProvider, |
| 15 |
ApplicationKeyTokenProvider, |
| 16 |
ApplicationCertificateTokenProvider, |
| 17 |
TokenConstants, |
| 18 |
AzureIdentityTokenCredentialProvider, |
| 19 |
) |
| 20 |
from .exceptions import KustoAuthenticationError, KustoClientError |
| 21 |
|
| 22 |
if TYPE_CHECKING: |
| 23 |
from . import KustoConnectionStringBuilder |
| 24 |
|
| 25 |
|
| 26 |
class _AadHelper: |
| 27 |
kusto_uri = None # type: str |
| 28 |
authority_uri = None # type: str |
| 29 |
token_provider = None # type: TokenProviderBase |
| 30 |
|
| 31 |
def __init__(self, kcsb: "KustoConnectionStringBuilder", is_async: bool): |
| 32 |
parsed_url = urlparse(kcsb.data_source) |
| 33 |
self.kusto_uri = f"{parsed_url.scheme}://{parsed_url.hostname}" |
| 34 |
if parsed_url.port is not None: |
| 35 |
self.kusto_uri += f":{parsed_url.port}" |
| 36 |
|
| 37 |
self.username = None |
| 38 |
|
| 39 |
if kcsb.interactive_login: |
| 40 |
self.token_provider = InteractiveLoginTokenProvider(self.kusto_uri, kcsb.authority_id, kcsb.login_hint, kcsb.domain_hint, is_async=is_async) |
| 41 |
elif all([kcsb.aad_user_id, kcsb.password]): |
| 42 |
self.token_provider = UserPassTokenProvider(self.kusto_uri, kcsb.authority_id, kcsb.aad_user_id, kcsb.password, is_async=is_async) |
| 43 |
elif all([kcsb.application_client_id, kcsb.application_key]): |
| 44 |
self.token_provider = ApplicationKeyTokenProvider( |
| 45 |
self.kusto_uri, kcsb.authority_id, kcsb.application_client_id, kcsb.application_key, is_async=is_async |
| 46 |
) |
| 47 |
elif all([kcsb.application_client_id, kcsb.application_certificate, kcsb.application_certificate_thumbprint]): |
| 48 |
# kcsb.application_public_certificate can be None if SNI is not used |
| 49 |
self.token_provider = ApplicationCertificateTokenProvider( |
| 50 |
self.kusto_uri, |
| 51 |
kcsb.application_client_id, |
| 52 |
kcsb.authority_id, |
| 53 |
kcsb.application_certificate, |
| 54 |
kcsb.application_certificate_thumbprint, |
| 55 |
kcsb.application_public_certificate, |
| 56 |
is_async=is_async, |
| 57 |
) |
| 58 |
elif kcsb.msi_authentication: |
| 59 |
self.token_provider = MsiTokenProvider(self.kusto_uri, kcsb.msi_parameters, is_async=is_async) |
| 60 |
elif kcsb.user_token: |
| 61 |
self.token_provider = BasicTokenProvider(kcsb.user_token, is_async=is_async) |
| 62 |
elif kcsb.application_token: |
| 63 |
self.token_provider = BasicTokenProvider(kcsb.application_token, is_async=is_async) |
| 64 |
elif kcsb.az_cli_login: |
| 65 |
self.token_provider = AzCliTokenProvider(self.kusto_uri, is_async=is_async) |
| 66 |
elif kcsb.token_provider or kcsb.async_token_provider: |
| 67 |
self.token_provider = CallbackTokenProvider(token_callback=kcsb.token_provider, async_token_callback=kcsb.async_token_provider, is_async=is_async) |
| 68 |
elif kcsb.token_credential_login: |
| 69 |
self.token_provider = AzureIdentityTokenCredentialProvider( |
| 70 |
self.kusto_uri, |
| 71 |
is_async=is_async, |
| 72 |
credential=kcsb.azure_credential, |
| 73 |
credential_from_login_endpoint=kcsb.azure_credential_from_login_endpoint, |
| 74 |
) |
| 75 |
elif kcsb.device_login: |
| 76 |
self.token_provider = DeviceLoginTokenProvider(self.kusto_uri, kcsb.authority_id, kcsb.device_callback, is_async=is_async) |
| 77 |
else: |
| 78 |
self.token_provider = InteractiveLoginTokenProvider(self.kusto_uri, kcsb.authority_id, kcsb.login_hint, kcsb.domain_hint, is_async=is_async) |
| 79 |
|
| 80 |
def acquire_authorization_header(self): |
| 81 |
try: |
| 82 |
return _get_header_from_dict(self.token_provider.get_token()) |
| 83 |
except Exception as error: |
| 84 |
kwargs = self.token_provider.context() |
| 85 |
kwargs["kusto_uri"] = self.kusto_uri |
| 86 |
raise KustoAuthenticationError(self.token_provider.name(), error, **kwargs) |
| 87 |
|
| 88 |
async def acquire_authorization_header_async(self): |
| 89 |
try: |
| 90 |
return _get_header_from_dict(await self.token_provider.get_token_async()) |
| 91 |
except Exception as error: |
| 92 |
kwargs = await self.token_provider.context_async() |
| 93 |
kwargs["resource"] = self.kusto_uri |
| 94 |
raise KustoAuthenticationError(self.token_provider.name(), error, **kwargs) |
| 95 |
|
| 96 |
def close(self): |
| 97 |
self.token_provider.close() |
| 98 |
|
| 99 |
async def close_async(self): |
| 100 |
await self.token_provider.close_async() |
| 101 |
|
| 102 |
|
| 103 |
def _get_header_from_dict(token: dict): |
| 104 |
if TokenConstants.MSAL_ACCESS_TOKEN in token: |
| 105 |
return _get_header(token[TokenConstants.MSAL_TOKEN_TYPE], token[TokenConstants.MSAL_ACCESS_TOKEN]) |
| 106 |
elif TokenConstants.AZ_ACCESS_TOKEN in token: |
| 107 |
return _get_header(token[TokenConstants.AZ_TOKEN_TYPE], token[TokenConstants.AZ_ACCESS_TOKEN]) |
| 108 |
else: |
| 109 |
raise KustoClientError("Unable to determine the token type. Neither 'tokenType' nor 'token_type' property is present.") |
| 110 |
|
| 111 |
|
| 112 |
def _get_header(token_type: str, access_token: str) -> str: |
| 113 |
return "{0} {1}".format(token_type, access_token) |
| 114 |
|
| 115 |
|
| 116 |
def _is_local_address(host): |
| 117 |
if host == "localhost" or host == "127.0.0.1" or host == "::1" or host == "[::1]": |
| 118 |
return True |
| 119 |
|
| 120 |
if host.startswith("127.") and 15 >= len(host) >= 9: |
| 121 |
for i in range(len(host)): |
| 122 |
c = host[i] |
| 123 |
if c != "." and (c < "0" or c > "9"): |
| 124 |
return False |
| 125 |
i += 1 |
| 126 |
return True |
| 127 |
|
| 128 |
return False |