Skip to content

Commit a78be2e

Browse files
authored
[Communication] Use CommunicationRefreshOptions in CommunicaitonTokenCredential (#16228)
* Add user_token_refresh_options.py * Change to use user_token_refresh_options * Add user_token_refresh_options.py in chat and sms sdk * Fix CommunicationTokenRefreshOptions * Fix CommunicationTokenRefreshOptions * Fix lint error * Fix Doc string * Change order of import six * Remove unnecessary import * Address PR review feedback
1 parent 90fbe72 commit a78be2e

File tree

20 files changed

+288
-155
lines changed

20 files changed

+288
-155
lines changed

sdk/communication/azure-communication-administration/azure/communication/administration/_shared/user_credential.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,22 @@
99
cast,
1010
Tuple,
1111
)
12-
import six
12+
1313
from msrest.serialization import TZ_UTC
14-
from .utils import create_access_token
1514

1615
class CommunicationTokenCredential(object):
1716
"""Credential type used for authenticating to an Azure Communication service.
18-
:param str token: The token used to authenticate to an Azure Communication service
19-
:raises: TypeError
17+
:param communication_token_refresh_options: The token used to authenticate to an Azure Communication service
18+
:type communication_token_refresh_options: ~azure.communication.chat.CommunicationTokenRefreshOptions
2019
"""
2120

2221
ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2
2322

2423
def __init__(self,
25-
token, # type: str
26-
token_refresher=None
24+
communication_token_refresh_options
2725
):
28-
# type: (str) -> None
29-
if not isinstance(token, six.string_types):
30-
raise TypeError("token must be a string.")
31-
self._token = create_access_token(token)
32-
self._token_refresher = token_refresher
26+
self._token = communication_token_refresh_options.get_token()
27+
self._token_refresher = communication_token_refresh_options.get_token_refresher()
3328
self._lock = Condition(Lock())
3429
self._some_thread_refreshing = False
3530

@@ -70,7 +65,6 @@ def get_token(self):
7065
self._lock.notify_all()
7166

7267
raise
73-
7468
return self._token
7569

7670
def _wait_till_inprogress_thread_finish_refreshing(self):

sdk/communication/azure-communication-administration/azure/communication/administration/_shared/user_credential_async.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,23 @@
99
cast,
1010
Tuple,
1111
)
12-
import six
12+
1313
from msrest.serialization import TZ_UTC
14-
from .utils import create_access_token
1514

1615
class CommunicationTokenCredential(object):
1716
"""Credential type used for authenticating to an Azure Communication service.
18-
:param str token: The token used to authenticate to an Azure Communication service
17+
:param communication_token_refresh_options: The token used to authenticate to an Azure Communication service
18+
:type communication_token_refresh_options: ~azure.communication.chat.aio.CommunicationTokenRefreshOptions
1919
:raises: TypeError
2020
"""
2121

2222
ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2
2323

2424
def __init__(self,
25-
token, # type: str
26-
token_refresher=None
25+
communication_token_refresh_options
2726
):
28-
# type: (str) -> None
29-
if not isinstance(token, six.string_types):
30-
raise TypeError("token must be a string.")
31-
self._token = create_access_token(token)
32-
self._token_refresher = token_refresher
27+
self._token = communication_token_refresh_options.get_token()
28+
self._token_refresher = communication_token_refresh_options.get_token_refresher()
3329
self._lock = Condition(Lock())
3430
self._some_thread_refreshing = False
3531

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
from typing import ( # pylint: disable=unused-import
7+
cast,
8+
Tuple,
9+
)
10+
import six
11+
from .utils import create_access_token
12+
13+
class CommunicationTokenRefreshOptions(object):
14+
"""Options for refreshing CommunicationTokenCredential.
15+
:param str token: The token used to authenticate to an Azure Communication service
16+
:param token_refresher: The token refresher to provide capacity to fetch fresh token
17+
:raises: TypeError
18+
"""
19+
20+
def __init__(self,
21+
token, # type: str
22+
token_refresher=None
23+
):
24+
# type: (str) -> None
25+
if not isinstance(token, six.string_types):
26+
raise TypeError("token must be a string.")
27+
self._token = token
28+
self._token_refresher = token_refresher
29+
30+
def get_token(self):
31+
"""Return the the serialized JWT token."""
32+
return create_access_token(self._token)
33+
34+
def get_token_refresher(self):
35+
"""Return the token refresher to provide capacity to fetch fresh token."""
36+
return self._token_refresher

sdk/communication/azure-communication-administration/tests/user_credential_tests.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from unittest import TestCase
77
from unittest.mock import MagicMock
88
from azure.communication.administration._shared.user_credential import CommunicationTokenCredential
9+
from azure.communication.administration._shared.user_token_refresh_options import CommunicationTokenRefreshOptions
910
from azure.communication.administration._shared.utils import create_access_token
1011

1112

@@ -18,24 +19,29 @@ class TestCommunicationTokenCredential(TestCase):
1819

1920

2021
def test_communicationtokencredential_decodes_token(self):
21-
credential = CommunicationTokenCredential(self.sample_token)
22+
refresh_options = CommunicationTokenRefreshOptions(self.sample_token)
23+
credential = CommunicationTokenCredential(refresh_options)
2224
access_token = credential.get_token()
2325

2426
self.assertEqual(access_token.token, self.sample_token)
2527

2628
def test_communicationtokencredential_throws_if_invalid_token(self):
27-
self.assertRaises(ValueError, lambda: CommunicationTokenCredential("foo.bar.tar"))
29+
refresh_options = CommunicationTokenRefreshOptions("foo.bar.tar")
30+
self.assertRaises(ValueError, lambda: CommunicationTokenCredential(refresh_options))
2831

2932
def test_communicationtokencredential_throws_if_nonstring_token(self):
30-
self.assertRaises(TypeError, lambda: CommunicationTokenCredential(454))
33+
refresh_options = CommunicationTokenRefreshOptions(454):
34+
self.assertRaises(TypeError, lambda: CommunicationTokenCredential(refresh_options)
3135

3236
def test_communicationtokencredential_static_token_returns_expired_token(self):
33-
credential = CommunicationTokenCredential(self.expired_token)
37+
refresh_options = CommunicationTokenRefreshOptions(self.expired_token)
38+
credential = CommunicationTokenCredential(refresh_options)
3439

3540
self.assertEqual(credential.get_token().token, self.expired_token)
3641

3742
def test_communicationtokencredential_token_expired_refresh_called(self):
3843
refresher = MagicMock(return_value=self.sample_token)
44+
refresh_options = CommunicationTokenRefreshOptions(self.sample_token, refresher)
3945
access_token = CommunicationTokenCredential(
4046
self.expired_token,
4147
token_refresher=refresher).get_token()
@@ -45,9 +51,8 @@ def test_communicationtokencredential_token_expired_refresh_called(self):
4551

4652
def test_communicationtokencredential_token_expired_refresh_called_asnecessary(self):
4753
refresher = MagicMock(return_value=create_access_token(self.expired_token))
48-
credential = CommunicationTokenCredential(
49-
self.expired_token,
50-
token_refresher=refresher)
54+
refresh_options = CommunicationTokenRefreshOptions(self.expired_token, refresher)
55+
credential = CommunicationTokenCredential(refresh_options)
5156

5257
credential.get_token()
5358
access_token = credential.get_token()

sdk/communication/azure-communication-chat/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ from azure.communication.chat import ChatClient, CommunicationTokenCredential
4848
# Your unique Azure Communication service endpoint
4949
endpoint = "https://<RESOURCE_NAME>.communcationservices.azure.com"
5050
token = "<token>"
51-
chat_client = ChatClient(endpoint, CommunicationTokenCredential(token))
51+
refresh_options = CommunicationTokenRefreshOptions(token)
52+
chat_client = ChatClient(endpoint, CommunicationTokenCredential(refresh_options))
5253
```
5354

5455
## Create Chat Thread Client

sdk/communication/azure-communication-chat/azure/communication/chat/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
ChatThreadInfo,
88
)
99
from ._shared.user_credential import CommunicationTokenCredential
10+
from ._shared.user_token_refresh_options import CommunicationTokenRefreshOptions
1011
from ._models import (
1112
ChatThreadMember,
1213
ChatMessage,
@@ -25,6 +26,7 @@
2526
'ChatThread',
2627
'ChatThreadInfo',
2728
'CommunicationTokenCredential',
29+
'CommunicationTokenRefreshOptions',
2830
'ChatThreadMember',
2931
'CommunicationUserIdentifier',
3032
]

sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,22 @@
99
cast,
1010
Tuple,
1111
)
12-
import six
1312

1413
from msrest.serialization import TZ_UTC
15-
from .utils import create_access_token
1614

1715
class CommunicationTokenCredential(object):
1816
"""Credential type used for authenticating to an Azure Communication service.
19-
:param str token: The token used to authenticate to an Azure Communication service
20-
:raises: TypeError
17+
:param communication_token_refresh_options: The token used to authenticate to an Azure Communication service
18+
:type communication_token_refresh_options: ~azure.communication.chat.CommunicationTokenRefreshOptions
2119
"""
2220

2321
ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2
2422

2523
def __init__(self,
26-
token, # type: str
27-
token_refresher=None
24+
communication_token_refresh_options
2825
):
29-
# type: (str) -> None
30-
if not isinstance(token, six.string_types):
31-
raise TypeError("token must be a string.")
32-
self._token = create_access_token(token)
33-
self._token_refresher = token_refresher
26+
self._token = communication_token_refresh_options.get_token()
27+
self._token_refresher = communication_token_refresh_options.get_token_refresher()
3428
self._lock = Condition(Lock())
3529
self._some_thread_refreshing = False
3630

sdk/communication/azure-communication-chat/azure/communication/chat/_shared/user_credential_async.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,22 @@
1010
Tuple,
1111
)
1212

13-
import six
14-
1513
from msrest.serialization import TZ_UTC
16-
from .utils import create_access_token
1714

1815
class CommunicationTokenCredential(object):
1916
"""Credential type used for authenticating to an Azure Communication service.
20-
:param str token: The token used to authenticate to an Azure Communication service
17+
:param communication_token_refresh_options: The token used to authenticate to an Azure Communication service
18+
:type communication_token_refresh_options: ~azure.communication.chat.aio.CommunicationTokenRefreshOptions
2119
:raises: TypeError
2220
"""
2321

2422
ON_DEMAND_REFRESHING_INTERVAL_MINUTES = 2
2523

2624
def __init__(self,
27-
token, # type: str
28-
token_refresher=None
25+
communication_token_refresh_options
2926
):
30-
# type: (str) -> None
31-
if not isinstance(token, six.string_types):
32-
raise TypeError("token must be a string.")
33-
self._token = create_access_token(token)
34-
self._token_refresher = token_refresher
27+
self._token = communication_token_refresh_options.get_token()
28+
self._token_refresher = communication_token_refresh_options.get_token_refresher()
3529
self._lock = Condition(Lock())
3630
self._some_thread_refreshing = False
3731

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for
4+
# license information.
5+
# --------------------------------------------------------------------------
6+
from typing import ( # pylint: disable=unused-import
7+
cast,
8+
Tuple,
9+
)
10+
import six
11+
from .utils import create_access_token
12+
13+
class CommunicationTokenRefreshOptions(object):
14+
"""Options for refreshing CommunicationTokenCredential.
15+
:param str token: The token used to authenticate to an Azure Communication service
16+
:param token_refresher: The token refresher to provide capacity to fetch fresh token
17+
:raises: TypeError
18+
"""
19+
20+
def __init__(self,
21+
token, # type: str
22+
token_refresher=None
23+
):
24+
# type: (str) -> None
25+
if not isinstance(token, six.string_types):
26+
raise TypeError("token must be a string.")
27+
self._token = token
28+
self._token_refresher = token_refresher
29+
30+
def get_token(self):
31+
"""Return the the serialized JWT token."""
32+
return create_access_token(self._token)
33+
34+
def get_token_refresher(self):
35+
"""Return the token refresher to provide capacity to fetch fresh token."""
36+
return self._token_refresher

sdk/communication/azure-communication-chat/azure/communication/chat/aio/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
from ._chat_client_async import ChatClient
66
from ._chat_thread_client_async import ChatThreadClient
77
from .._shared.user_credential_async import CommunicationTokenCredential
8+
from .._shared.user_token_refresh_options import CommunicationTokenRefreshOptions
89

910
__all__ = [
1011
"ChatClient",
1112
"ChatThreadClient",
12-
"CommunicationTokenCredential"
13+
"CommunicationTokenCredential",
14+
"CommunicationTokenRefreshOptions"
1315
]

0 commit comments

Comments
 (0)