Skip to content

Commit 30b917b

Browse files
Communication identity api redesign (Azure#16420)
* copy identity client files from admin to new pkg * add back version.py for wheel generation * update _version.py * remove redundant files * fixed error with init file * update identity sdk with swagger changes * update swagger + regenerate * fix async impl + tests * Updated sync identity client and tests * Use Azure.Core.AccessToken instead of CommunicationUserToken * Updated formatting in CHANGELOG * Updated CHANGELOG * Updated CHANGELOG * fix typo in samples * update samples to include new method * Update Readme and Samples * docstring + formatting fixes * more docstring fixes * whitespace fixes * formatting + doc fixes * fix sync client test * bodyreplacer modified to handle nested json * generate sanitized recordings * Updated formatting for method documentation * Fix trailing whitespace Co-authored-by: Lakshman Sundaralingam <[email protected]>
1 parent 74d5b49 commit 30b917b

File tree

39 files changed

+1134
-574
lines changed

39 files changed

+1134
-574
lines changed

sdk/communication/azure-communication-identity/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
### Added
66
- Added CommunicationIdentityClient (originally was part of the azure.communication.administration package).
7+
- Added ability to create a user and issue token for it at the same time.
8+
9+
### Breaking
10+
- CommunicationIdentityClient.revoke_tokens now revoke all the currently issued tokens instead of revoking tokens issued prior to a given time.
11+
- CommunicationIdentityClient.issue_tokens returns an instance of `azure.core.credentials.AccessToken` instead of `CommunicationUserToken`.
712

813
<!-- LINKS -->
914
[read_me]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/communication/azure-communication-identity/README.md

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

+43-5
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,58 @@ endpoint = os.getenv('AZURE_COMMUNICATION_SERVICE_ENDPOINT')
3636

3737
# To use Azure Active Directory Authentication (DefaultAzureCredential) make sure to have
3838
# AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET as env variables.
39-
identity_client_managed_identity = CommunicationIdentityClient.(endpoint, DefaultAzureCredential())
39+
identity_client_managed_identity = CommunicationIdentityClient(endpoint, DefaultAzureCredential())
4040

4141
#You can also authenticate using your connection string
4242
identity_client = CommunicationIdentityClient.from_connection_string(connection_str)
4343

4444
```
4545

46-
# Examples
46+
## Examples
4747
The following section provides several code snippets covering some of the most common Azure Communication Services tasks, including:
4848

49-
[Create/delete Azure Communication Service identities][identitysamples]
49+
### Creating a new user
5050

51-
[Create/revoke scoped user access tokens][identitysamples]
51+
Use the `create_user` method to create a new user.
52+
```python
53+
user = identity_client.create_user()
54+
print("User created with id:" + user.identifier)
55+
```
56+
57+
Alternatively, use the `create_user_with_token` method to create a new user and issue a token for it.\
58+
For this option, a list of `CommunicationTokenScope` must be defined (see "Issuing an access token" for more information)
59+
60+
```python
61+
user, tokenresponse = identity_client.create_user_with_token(scopes=[CommunicationTokenScope.CHAT])
62+
print("User id:" + user.identifier)
63+
print("Token issued with value: " + tokenresponse.token)
64+
```
65+
66+
### Issuing or Refreshing an access token for a user
67+
68+
Use the `issue_token` method to issue or refresh a scoped access token for the user. \
69+
Pass in the user object as a parameter, and a list of `CommunicationTokenScope`. Scope options are:
70+
- `CHAT` (Chat)
71+
- `VOIP` (VoIP)
72+
73+
```python
74+
tokenresponse = identity_client.issue_token(user, scopes=[CommunicationTokenScope.CHAT])
75+
print("Token issued with value: " + tokenresponse.token)
76+
```
77+
78+
### Revoking a user's access tokens
79+
80+
Use `revoke_tokens` to revoke all access tokens for a user. Pass in the user object as a parameter
81+
```python
82+
identity_client.revoke_tokens(user)
83+
```
84+
85+
### Deleting a user
86+
87+
Use the `delete_user` method to delete a user. Pass in the user object as a parameter
88+
```python
89+
identity_client.delete_user(user)
90+
```
5291

5392
# Troubleshooting
5493
The Azure Communication Service Identity client will raise exceptions defined in [Azure Core][azure_core].
@@ -73,5 +112,4 @@ This project has adopted the [Microsoft Open Source Code of Conduct](https://ope
73112
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
74113

75114
<!-- LINKS -->
76-
[identitysamples]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/communication/azure-communication-identity/samples/identity_samples.py
77115
[azure_core]: https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/README.md

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@
77
from ._communication_identity_client import CommunicationIdentityClient
88

99
from ._generated.models import (
10-
CommunicationTokenRequest,
11-
CommunicationIdentityToken
10+
CommunicationTokenScope
1211
)
1312

1413
from ._shared.models import CommunicationUserIdentifier
1514

16-
1715
__all__ = [
1816
'CommunicationIdentityClient',
1917

2018
# from _identity
21-
'CommunicationTokenRequest',
22-
'CommunicationIdentityToken',
19+
'CommunicationTokenScope',
2320

2421
# from _shared
2522
'CommunicationUserIdentifier'

sdk/communication/azure-communication-identity/azure/communication/identity/_communication_identity_client.py

+34-16
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
# ------------------------------------
66

77
from azure.core.tracing.decorator import distributed_trace
8+
from azure.core.credentials import AccessToken
89

910
from ._generated._communication_identity_client\
1011
import CommunicationIdentityClient as CommunicationIdentityClientGen
11-
from ._generated.models import CommunicationIdentityToken
1212
from ._shared.utils import parse_connection_str, get_authentication_policy
1313
from ._shared.models import CommunicationUserIdentifier
1414
from ._version import SDK_MONIKER
@@ -62,7 +62,7 @@ def from_connection_string(
6262
:param str conn_str:
6363
A connection string to an Azure Communication Service resource.
6464
:returns: Instance of CommunicationIdentityClient.
65-
:rtype: ~azure.communication.CommunicationIdentityClient
65+
:rtype: ~azure.communication.identity.CommunicationIdentityClient
6666
6767
.. admonition:: Example:
6868
@@ -82,11 +82,32 @@ def create_user(self, **kwargs):
8282
# type: (...) -> CommunicationUserIdentifier
8383
"""create a single Communication user
8484
85-
return: CommunicationUserIdentifier
86-
rtype: ~azure.communication.identity.CommunicationUserIdentifier
85+
:return: CommunicationUserIdentifier
86+
:rtype: ~azure.communication.identity.CommunicationUserIdentifier
8787
"""
8888
return self._identity_service_client.communication_identity.create(
89-
cls=lambda pr, u, e: CommunicationUserIdentifier(u.id),
89+
cls=lambda pr, u, e: CommunicationUserIdentifier(u.identity.id),
90+
**kwargs)
91+
92+
@distributed_trace
93+
def create_user_with_token(
94+
self,
95+
scopes, # type: List[Union[str, "_model.CommunicationTokenScope"]]
96+
**kwargs # type: Any
97+
):
98+
# type: (...) -> Tuple[CommunicationUserIdentifier, AccessToken]
99+
"""create a single Communication user with an identity token.
100+
:param scopes:
101+
List of scopes to be added to the token.
102+
:type scopes: list[str or ~azure.communication.identity.models.CommunicationTokenScope]
103+
:return: A tuple of a CommunicationUserIdentifier and a AccessToken.
104+
:rtype:
105+
tuple of (~azure.communication.identity.CommunicationUserIdentifier, ~azure.core.credentials.AccessToken)
106+
"""
107+
return self._identity_service_client.communication_identity.create(
108+
cls=lambda pr, u, e: (CommunicationUserIdentifier(u.identity.id),
109+
AccessToken(u.access_token.token, u.access_token.expires_on)),
110+
create_token_with_scopes=scopes,
90111
**kwargs)
91112

92113
@distributed_trace
@@ -111,43 +132,40 @@ def delete_user(
111132
def issue_token(
112133
self,
113134
user, # type: CommunicationUserIdentifier
114-
scopes, # type: List[str]
135+
scopes, # List[Union[str, "_model.CommunicationTokenScope"]]
115136
**kwargs # type: Any
116137
):
117-
# type: (...) -> CommunicationIdentityToken
138+
# type: (...) -> AccessToken
118139
"""Generates a new token for an identity.
119140
120141
:param user: Azure Communication User
121142
:type user: ~azure.communication.identity.CommunicationUserIdentifier
122143
:param scopes:
123144
List of scopes to be added to the token.
124-
:type scopes: list[str]
125-
:return: CommunicationIdentityToken
126-
:rtype: ~azure.communication.identity.CommunicationIdentityToken
145+
:type scopes: list[str or ~azure.communication.identity.models.CommunicationTokenScope]
146+
:return: AccessToken
147+
:rtype: ~azure.core.credentials.AccessToken
127148
"""
128-
return self._identity_service_client.communication_identity.issue_token(
149+
return self._identity_service_client.communication_identity.issue_access_token(
129150
user.identifier,
130151
scopes,
152+
cls=lambda pr, u, e: AccessToken(u.token, u.expires_on),
131153
**kwargs)
132154

133155
@distributed_trace
134156
def revoke_tokens(
135157
self,
136158
user, # type: CommunicationUserIdentifier
137-
issued_before=None, # type: Optional[datetime.datetime]
138159
**kwargs # type: Any
139160
):
140161
# type: (...) -> None
141162
"""Schedule revocation of all tokens of an identity.
142163
143164
:param user: Azure Communication User.
144165
:type user: ~azure.communication.identity.CommunicationUserIdentifier.
145-
:param issued_before: All tokens that are issued prior to this time should get revoked.
146-
:type issued_before: ~datetime.datetime.
147166
:return: None
148167
:rtype: None
149168
"""
150-
return self._identity_service_client.communication_identity.update(
169+
return self._identity_service_client.communication_identity.revoke_access_tokens(
151170
user.identifier if user else None,
152-
tokens_valid_from=issued_before,
153171
**kwargs)

sdk/communication/azure-communication-identity/azure/communication/identity/_generated/_communication_identity_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CommunicationIdentityClient(object):
2525
2626
:ivar communication_identity: CommunicationIdentityOperations operations
2727
:vartype communication_identity: azure.communication.identity.operations.CommunicationIdentityOperations
28-
:param endpoint: Auth and Identity endpoint.
28+
:param endpoint: The communication resource, for example https://my-resource.communication.azure.com.
2929
:type endpoint: str
3030
"""
3131

@@ -41,6 +41,7 @@ def __init__(
4141

4242
client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)}
4343
self._serialize = Serializer(client_models)
44+
self._serialize.client_side_validation = False
4445
self._deserialize = Deserializer(client_models)
4546

4647
self.communication_identity = CommunicationIdentityOperations(

sdk/communication/azure-communication-identity/azure/communication/identity/_generated/_configuration.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CommunicationIdentityClientConfiguration(Configuration):
2323
Note that all parameters used to create this instance are saved as instance
2424
attributes.
2525
26-
:param endpoint: Auth and Identity endpoint.
26+
:param endpoint: The communication resource, for example https://my-resource.communication.azure.com.
2727
:type endpoint: str
2828
"""
2929

@@ -38,7 +38,7 @@ def __init__(
3838
super(CommunicationIdentityClientConfiguration, self).__init__(**kwargs)
3939

4040
self.endpoint = endpoint
41-
self.api_version = "2020-07-20-preview2"
41+
self.api_version = "2021-03-07"
4242
kwargs.setdefault('sdk_moniker', 'communicationidentityclient/{}'.format(VERSION))
4343
self._configure(**kwargs)
4444

sdk/communication/azure-communication-identity/azure/communication/identity/_generated/aio/_communication_identity_client.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CommunicationIdentityClient(object):
2121
2222
:ivar communication_identity: CommunicationIdentityOperations operations
2323
:vartype communication_identity: azure.communication.identity.aio.operations.CommunicationIdentityOperations
24-
:param endpoint: Auth and Identity endpoint.
24+
:param endpoint: The communication resource, for example https://my-resource.communication.azure.com.
2525
:type endpoint: str
2626
"""
2727

@@ -36,6 +36,7 @@ def __init__(
3636

3737
client_models = {k: v for k, v in models.__dict__.items() if isinstance(v, type)}
3838
self._serialize = Serializer(client_models)
39+
self._serialize.client_side_validation = False
3940
self._deserialize = Deserializer(client_models)
4041

4142
self.communication_identity = CommunicationIdentityOperations(

sdk/communication/azure-communication-identity/azure/communication/identity/_generated/aio/_configuration.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CommunicationIdentityClientConfiguration(Configuration):
1919
Note that all parameters used to create this instance are saved as instance
2020
attributes.
2121
22-
:param endpoint: Auth and Identity endpoint.
22+
:param endpoint: The communication resource, for example https://my-resource.communication.azure.com.
2323
:type endpoint: str
2424
"""
2525

@@ -33,7 +33,7 @@ def __init__(
3333
super(CommunicationIdentityClientConfiguration, self).__init__(**kwargs)
3434

3535
self.endpoint = endpoint
36-
self.api_version = "2020-07-20-preview2"
36+
self.api_version = "2021-03-07"
3737
kwargs.setdefault('sdk_moniker', 'communicationidentityclient/{}'.format(VERSION))
3838
self._configure(**kwargs)
3939

0 commit comments

Comments
 (0)