20
20
21
21
from uamqp import AMQPClient , Message , authentication , constants , errors , compat , utils
22
22
import six
23
+ from azure .core .credentials import AccessToken , AzureSasCredential , AzureNamedKeyCredential
23
24
from azure .core .utils import parse_connection_string as core_parse_connection_string
24
- from azure . core . credentials import AccessToken , AzureSasCredential
25
+
25
26
26
27
from .exceptions import _handle_exception , ClientClosedError , ConnectError
27
28
from ._configuration import Configuration
@@ -173,6 +174,25 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
173
174
raise ValueError ("No token scope provided." )
174
175
return _generate_sas_token (scopes [0 ], self .policy , self .key )
175
176
177
+ class EventhubAzureNamedKeyTokenCredential (object ):
178
+ """The named key credential used for authentication.
179
+
180
+ :param credential: The AzureNamedKeyCredential that should be used.
181
+ :type credential: ~azure.core.credentials.AzureNamedKeyCredential
182
+ """
183
+
184
+ def __init__ (self , azure_named_key_credential ):
185
+ # type: (AzureNamedKeyCredential) -> None
186
+ self ._credential = azure_named_key_credential
187
+ self .token_type = b"servicebus.windows.net:sastoken"
188
+
189
+ def get_token (self , * scopes , ** kwargs ): # pylint:disable=unused-argument
190
+ # type: (str, Any) -> _AccessToken
191
+ if not scopes :
192
+ raise ValueError ("No token scope provided." )
193
+ name , key = self ._credential .named_key
194
+ return _generate_sas_token (scopes [0 ], name , key )
195
+
176
196
177
197
class EventHubSASTokenCredential (object ):
178
198
"""The shared access token credential used for authentication.
@@ -197,7 +217,7 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
197
217
"""
198
218
return AccessToken (self .token , self .expiry )
199
219
200
- class AzureSasTokenCredential (object ):
220
+ class EventhubAzureSasTokenCredential (object ):
201
221
"""The shared access token credential used for authentication
202
222
when AzureSasCredential is provided.
203
223
@@ -226,15 +246,17 @@ def get_token(self, *scopes, **kwargs): # pylint:disable=unused-argument
226
246
227
247
class ClientBase (object ): # pylint:disable=too-many-instance-attributes
228
248
def __init__ (self , fully_qualified_namespace , eventhub_name , credential , ** kwargs ):
229
- # type: (str, str, Union[AzureSasCredential, TokenCredential], Any) -> None
249
+ # type: (str, str, Union[AzureSasCredential, TokenCredential, AzureNamedKeyCredential ], Any) -> None
230
250
self .eventhub_name = eventhub_name
231
251
if not eventhub_name :
232
252
raise ValueError ("The eventhub name can not be None or empty." )
233
253
path = "/" + eventhub_name if eventhub_name else ""
234
254
self ._address = _Address (hostname = fully_qualified_namespace , path = path )
235
255
self ._container_id = CONTAINER_PREFIX + str (uuid .uuid4 ())[:8 ]
236
256
if isinstance (credential , AzureSasCredential ):
237
- self ._credential = AzureSasTokenCredential (credential )
257
+ self ._credential = EventhubAzureSasTokenCredential (credential )
258
+ elif isinstance (credential , AzureNamedKeyCredential ):
259
+ self ._credential = EventhubAzureNamedKeyTokenCredential (credential ) # type: ignore
238
260
else :
239
261
self ._credential = credential #type: ignore
240
262
self ._keep_alive = kwargs .get ("keep_alive" , 30 )
0 commit comments