Skip to content

Commit ec92c63

Browse files
MikeyMCZMichal Maternaannatisch
authored
[Text Translation] Change scopes to audiences (Azure#35732)
* [Text Translation] Change scopes to audiences * Fixing PR comments * Fix review feedback * Fix PR comment * Update sdk/translation/azure-ai-translation-text/azure/ai/translation/text/_patch.py Co-authored-by: Anna Tisch <[email protected]> * Fixing PR comments --------- Co-authored-by: Michal Materna <[email protected]> Co-authored-by: Anna Tisch <[email protected]>
1 parent f02f1e0 commit ec92c63

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

sdk/translation/azure-ai-translation-text/azure/ai/translation/text/_patch.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from ._client import TextTranslationClient as ServiceClientGenerated
1313

1414
DEFAULT_TOKEN_SCOPE = "https://api.microsofttranslator.com/"
15-
DEFAULT_ENTRA_ID_SCOPE = "https://cognitiveservices.azure.com/.default"
15+
DEFAULT_ENTRA_ID_SCOPE = "https://cognitiveservices.azure.com"
16+
DEFAULT_SCOPE = "/.default"
1617

1718

1819
def patch_sdk():
@@ -47,10 +48,20 @@ class TranslatorEntraIdAuthenticationPolicy(BearerTokenCredentialPolicy):
4748
4849
:param credential: Translator Entra Id Credentials used to access Translator Resource for global endpoint.
4950
:type credential: ~azure.core.credentials.TokenCredential
51+
:keyword str region: Used for National Clouds.
52+
:keyword str resource_id: Used with both a TokenCredential combined with a region.
53+
:keyword str audience: Scopes of the credentials.
5054
"""
5155

52-
def __init__(self, credential: TokenCredential, resource_id: str, region: str, scopes: str, **kwargs: Any) -> None:
53-
super(TranslatorEntraIdAuthenticationPolicy, self).__init__(credential, scopes, **kwargs)
56+
def __init__(
57+
self,
58+
credential: TokenCredential,
59+
resource_id: str,
60+
region: str,
61+
audience: str,
62+
**kwargs: Any
63+
) -> None:
64+
super(TranslatorEntraIdAuthenticationPolicy, self).__init__(credential, audience, **kwargs)
5465
self.resource_id = resource_id
5566
self.region = region
5667
self.translator_credential = credential
@@ -86,19 +97,21 @@ def set_authentication_policy(credential, kwargs):
8697
elif hasattr(credential, "get_token"):
8798
if not kwargs.get("authentication_policy"):
8899
if kwargs.get("region") and kwargs.get("resource_id"):
100+
scope = kwargs.pop("audience", DEFAULT_ENTRA_ID_SCOPE).rstrip("/") + DEFAULT_SCOPE
89101
kwargs["authentication_policy"] = TranslatorEntraIdAuthenticationPolicy(
90102
credential,
91103
kwargs["resource_id"],
92104
kwargs["region"],
93-
kwargs.pop("scopes", DEFAULT_ENTRA_ID_SCOPE),
105+
scope,
94106
)
95107
else:
96108
if kwargs.get("resource_id") or kwargs.get("region"):
97109
raise ValueError(
98-
"Both 'resource_id' and 'region' must be provided with a TokenCredential for authentication."
110+
"""Both 'resource_id' and 'region' must be provided with a TokenCredential for
111+
regional resource authentication."""
99112
)
100113
kwargs["authentication_policy"] = BearerTokenCredentialPolicy(
101-
credential, *kwargs.pop("scopes", [DEFAULT_TOKEN_SCOPE]), kwargs
114+
credential, *[kwargs.pop("audience", DEFAULT_TOKEN_SCOPE)], kwargs
102115
)
103116

104117

@@ -134,15 +147,16 @@ class TextTranslationClient(ServiceClientGenerated):
134147
None + AzureKeyCredential - used for global translator endpoint with global Translator resource
135148
None + TokenCredential - general translator endpoint with token authentication
136149
None + TokenCredential + Region - general translator endpoint with regional Translator resource
150+
137151
:keyword str endpoint: Supported Text Translation endpoints (protocol and hostname, for example:
138-
https://api.cognitive.microsofttranslator.com). If not provided, global translator endpoint will be used.
152+
https://api.cognitive.microsofttranslator.com). If not provided, global translator endpoint will be used.
139153
:keyword credential: Credential used to authenticate with the Translator service
140154
:paramtype credential: Union[AzureKeyCredential, TokenCredential]
141155
:keyword str region: Used for National Clouds.
142156
:keyword str resource_id: Used with both a TokenCredential combined with a region.
143-
:keyword str scopes: Scopes of the credentials.
157+
:keyword str audience: Scopes of the credentials.
144158
:keyword str api_version: Default value is "3.0". Note that overriding this default value may
145-
result in unsupported behavior.
159+
result in unsupported behavior.
146160
"""
147161

148162
@overload
@@ -153,8 +167,8 @@ def __init__(
153167
region: Optional[str] = None,
154168
endpoint: Optional[str] = None,
155169
resource_id: Optional[str] = None,
156-
scopes: Optional[str] = None,
157-
api_version="3.0",
170+
audience: Optional[str] = None,
171+
api_version: str = "3.0",
158172
**kwargs
159173
): ...
160174

@@ -165,7 +179,7 @@ def __init__(
165179
credential: AzureKeyCredential,
166180
region: Optional[str] = None,
167181
endpoint: Optional[str] = None,
168-
api_version="3.0",
182+
api_version: str = "3.0",
169183
**kwargs
170184
): ...
171185

@@ -174,7 +188,7 @@ def __init__(
174188
self,
175189
*,
176190
endpoint: str,
177-
api_version="3.0",
191+
api_version: str ="3.0",
178192
**kwargs
179193
): ...
180194

sdk/translation/azure-ai-translation-text/azure/ai/translation/text/aio/_patch.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .._patch import (
1717
DEFAULT_TOKEN_SCOPE,
1818
DEFAULT_ENTRA_ID_SCOPE,
19+
DEFAULT_SCOPE,
1920
get_translation_endpoint,
2021
TranslatorAuthenticationPolicy,
2122
)
@@ -40,12 +41,15 @@ class AsyncTranslatorEntraIdAuthenticationPolicy(AsyncBearerTokenCredentialPolic
4041
4142
:param credential: Translator Entra Id Credentials used to access Translator Resource for global endpoint.
4243
:type credential: ~azure.core.credentials_async.AsyncTokenCredential
44+
:keyword str region: Used for National Clouds.
45+
:keyword str resource_id: Used with both a TokenCredential combined with a region.
46+
:keyword str audience: Scopes of the credentials.
4347
"""
4448

4549
def __init__(
46-
self, credential: AsyncTokenCredential, resource_id: str, region: str, scopes: str, **kwargs: Any
50+
self, credential: AsyncTokenCredential, resource_id: str, region: str, audience: str, **kwargs: Any
4751
) -> None:
48-
super(AsyncTranslatorEntraIdAuthenticationPolicy, self).__init__(credential, scopes, **kwargs)
52+
super(AsyncTranslatorEntraIdAuthenticationPolicy, self).__init__(credential, audience, **kwargs)
4953
self.resource_id = resource_id
5054
self.region = region
5155
self.translator_credential = credential
@@ -68,19 +72,21 @@ def set_authentication_policy(credential, kwargs):
6872
elif hasattr(credential, "get_token"):
6973
if not kwargs.get("authentication_policy"):
7074
if kwargs.get("region") and kwargs.get("resource_id"):
75+
scope = kwargs.pop("audience", DEFAULT_ENTRA_ID_SCOPE).rstrip("/") + DEFAULT_SCOPE
7176
kwargs["authentication_policy"] = AsyncTranslatorEntraIdAuthenticationPolicy(
7277
credential,
7378
kwargs["resource_id"],
7479
kwargs["region"],
75-
kwargs.pop("scopes", DEFAULT_ENTRA_ID_SCOPE),
80+
scope,
7681
)
7782
else:
7883
if kwargs.get("resource_id") or kwargs.get("region"):
7984
raise ValueError(
80-
"Both 'resource_id' and 'region' must be provided with a TokenCredential for authentication."
85+
"""Both 'resource_id' and 'region' must be provided with a TokenCredential
86+
for regional resource authentication."""
8187
)
8288
kwargs["authentication_policy"] = AsyncBearerTokenCredentialPolicy(
83-
credential, *kwargs.pop("scopes", [DEFAULT_TOKEN_SCOPE]), kwargs
89+
credential, *[kwargs.pop("audience", DEFAULT_TOKEN_SCOPE)], kwargs
8490
)
8591

8692

@@ -116,15 +122,16 @@ class TextTranslationClient(ServiceClientGenerated):
116122
None + AzureKeyCredential - used for global translator endpoint with global Translator resource
117123
None + AsyncTokenCredential - general translator endpoint with token authentication
118124
None + AsyncTokenCredential + Region - general translator endpoint with regional Translator resource
125+
119126
:keyword str endpoint: Supported Text Translation endpoints (protocol and hostname, for example:
120-
https://api.cognitive.microsofttranslator.com). If not provided, global translator endpoint will be used.
127+
https://api.cognitive.microsofttranslator.com). If not provided, global translator endpoint will be used.
121128
:keyword credential: Credential used to authenticate with the Translator service
122129
:paramtype credential: Union[AzureKeyCredential, AsyncTokenCredential]
123130
:keyword str region: Used for National Clouds.
124131
:keyword str resource_id: Used with both a TokenCredential combined with a region.
125-
:keyword str scopes: Scopes of the credentials.
132+
:keyword str audience: Scopes of the credentials.
126133
:keyword str api_version: Default value is "3.0". Note that overriding this default value may
127-
result in unsupported behavior.
134+
result in unsupported behavior.
128135
"""
129136

130137
@overload
@@ -135,8 +142,8 @@ def __init__(
135142
region: Optional[str] = None,
136143
endpoint: Optional[str] = None,
137144
resource_id: Optional[str] = None,
138-
scopes: Optional[str] = None,
139-
api_version="3.0",
145+
audience: Optional[str] = None,
146+
api_version: str = "3.0",
140147
**kwargs
141148
): ...
142149

@@ -147,7 +154,7 @@ def __init__(
147154
credential: AzureKeyCredential,
148155
region: Optional[str] = None,
149156
endpoint: Optional[str] = None,
150-
api_version="3.0",
157+
api_version: str = "3.0",
151158
**kwargs
152159
): ...
153160

@@ -156,7 +163,7 @@ def __init__(
156163
self,
157164
*,
158165
endpoint: str,
159-
api_version="3.0",
166+
api_version: str = "3.0",
160167
**kwargs
161168
): ...
162169

sdk/translation/azure-ai-translation-text/tests/static_access_token_credential.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, apikey, region):
1616
region, apikey
1717
)
1818

19-
def get_token(self, *scopes, **kwargs):
19+
def get_token(self, *audience, **kwargs):
2020
response = requests.post(self.request_url)
2121
access_token = response.content.decode("UTF-8")
2222
expires_on = datetime.datetime.now() + datetime.timedelta(days=1)

0 commit comments

Comments
 (0)