Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: feature
packages:
- "@autorest/python"
- "@azure-tools/typespec-python"
---

Pass authentication flows info into credential policy for unbranded
2 changes: 1 addition & 1 deletion packages/autorest.python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"homepage": "https://github.com/Azure/autorest.python/blob/main/README.md",
"dependencies": {
"@typespec/http-client-python": "~0.8.3-dev.1",
"@typespec/http-client-python": "~0.8.3-dev.2",
"@autorest/system-requirements": "~1.0.2",
"fs-extra": "~11.2.0",
"tsx": "~4.19.1"
Expand Down
8 changes: 7 additions & 1 deletion packages/typespec-python/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"@typespec/rest": ">=0.67.0 <1.0.0",
"@typespec/versioning": ">=0.67.0 <1.0.0",
"@typespec/openapi": ">=0.67.0 <1.0.0",
"@typespec/events": ">=0.67.0 <1.0.0",
"@typespec/sse": ">=0.67.0 <1.0.0",
"@typespec/streams": ">=0.67.0 <1.0.0",
"@azure-tools/typespec-azure-core": ">=0.53.0 <1.0.0",
"@azure-tools/typespec-azure-resource-manager": ">=0.53.0 <1.0.0",
"@azure-tools/typespec-autorest": ">=0.53.0 <1.0.0",
Expand All @@ -60,7 +63,7 @@
"js-yaml": "~4.1.0",
"semver": "~7.6.2",
"tsx": "~4.19.1",
"@typespec/http-client-python": "~0.8.3-dev.1",
"@typespec/http-client-python": "~0.8.3-dev.2",
"fs-extra": "~11.2.0"
},
"devDependencies": {
Expand All @@ -69,6 +72,9 @@
"@typespec/rest": "~0.67.0",
"@typespec/versioning": "~0.67.0",
"@typespec/openapi": "~0.67.0",
"@typespec/events": "~0.67.0",
"@typespec/sse": "~0.67.0",
"@typespec/streams": "~0.67.0",
"@azure-tools/typespec-azure-resource-manager": "~0.53.0",
"@azure-tools/typespec-azure-core": "~0.53.0",
"@azure-tools/typespec-azure-rulesets": "~0.53.0",
Expand Down
1 change: 1 addition & 0 deletions packages/typespec-python/test/azure/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ azure-mgmt-core==1.3.2
-e ./generated/server-versions-versioned
-e ./generated/server-versions-not-versioned
-e ./generated/special-words
-e ./generated/streaming-jsonl
-e ./generated/typetest-array
-e ./generated/typetest-dictionary
-e ./generated/typetest-enum-extensible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class FakeCredential:
async def get_token(*scopes):
return core_library.credentials.AccessToken(token="".join(scopes), expires_on=1800)

@staticmethod
async def get_token_info(*scopes, **kwargs):
return core_library.credentials.AccessTokenInfo(token="".join(scopes), expires_on=1800)

return FakeCredential()


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest

from streaming.jsonl.aio import JsonlClient


@pytest.fixture
async def client():
async with JsonlClient(endpoint="http://localhost:3000") as client:
yield client


JSONL = b'{"desc": "one"}\n{"desc": "two"}\n{"desc": "three"}'


@pytest.mark.asyncio
async def test_basic_send(client: JsonlClient):
await client.basic.send(JSONL)


@pytest.mark.asyncio
async def test_basic_recv(client: JsonlClient):
assert b"".join([d async for d in (await client.basic.receive())]) == JSONL
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class FakeCredential:
def get_token(*scopes):
return core_library.credentials.AccessToken(token="".join(scopes), expires_on=1800)

@staticmethod
def get_token_info(*scopes, **kwargs):
return core_library.credentials.AccessTokenInfo(token="".join(scopes), expires_on=1800)

return FakeCredential()


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest

from streaming.jsonl import JsonlClient


@pytest.fixture
def client():
with JsonlClient(endpoint="http://localhost:3000") as client:
yield client


JSONL = b'{"desc": "one"}\n{"desc": "two"}\n{"desc": "three"}'


def test_basic_send(client: JsonlClient):
client.basic.send(JSONL)


def test_basic_recv(client: JsonlClient):
assert b"".join(client.basic.receive()) == JSONL
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,14 @@ def _configure(self, **kwargs: Any) -> None:
self.authentication_policy = kwargs.get("authentication_policy")
if self.credential and not self.authentication_policy:
self.authentication_policy = policies.BearerTokenCredentialPolicy(
self.credential, *self.credential_scopes, **kwargs
self.credential,
*self.credential_scopes,
auth_flows=[
{
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
"scopes": [{"value": "https://security.microsoft.com/.default"}],
"type": "implicit",
}
],
**kwargs
)
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,14 @@ def _configure(self, **kwargs: Any) -> None:
self.authentication_policy = kwargs.get("authentication_policy")
if self.credential and not self.authentication_policy:
self.authentication_policy = policies.AsyncBearerTokenCredentialPolicy(
self.credential, *self.credential_scopes, **kwargs
self.credential,
*self.credential_scopes,
auth_flows=[
{
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
"scopes": [{"value": "https://security.microsoft.com/.default"}],
"type": "implicit",
}
],
**kwargs
)
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ def _infer_policy(self, **kwargs):
if isinstance(self.credential, ServiceKeyCredential):
return policies.ServiceKeyCredentialPolicy(self.credential, "x-ms-api-key", **kwargs)
if hasattr(self.credential, "get_token"):
return policies.BearerTokenCredentialPolicy(self.credential, *self.credential_scopes, **kwargs)
return policies.BearerTokenCredentialPolicy(
self.credential,
*self.credential_scopes,
auth_flows=[
{
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
"scopes": [{"value": "https://security.microsoft.com/.default"}],
"type": "implicit",
}
],
**kwargs,
)
raise TypeError(f"Unsupported credential: {self.credential}")

def _configure(self, **kwargs: Any) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@ def _infer_policy(self, **kwargs):
if isinstance(self.credential, ServiceKeyCredential):
return policies.ServiceKeyCredentialPolicy(self.credential, "x-ms-api-key", **kwargs)
if hasattr(self.credential, "get_token"):
return policies.AsyncBearerTokenCredentialPolicy(self.credential, *self.credential_scopes, **kwargs)
return policies.AsyncBearerTokenCredentialPolicy(
self.credential,
*self.credential_scopes,
auth_flows=[
{
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
"scopes": [{"value": "https://security.microsoft.com/.default"}],
"type": "implicit",
}
],
**kwargs,
)
raise TypeError(f"Unsupported credential: {self.credential}")

def _configure(self, **kwargs: Any) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
import pytest
from authentication.oauth2.aio import OAuth2Client


@pytest.mark.asyncio
async def test_oauth2_auth_flows():
oauth2_client = OAuth2Client("fake_credential")
assert oauth2_client._config.authentication_policy._auth_flows == [
{
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
"scopes": [{"value": "https://security.microsoft.com/.default"}],
"type": "implicit",
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from authentication.oauth2 import OAuth2Client


def test_oauth2_auth_flows():
oauth2_client = OAuth2Client("fake_credential")
assert oauth2_client._config.authentication_policy._auth_flows == [
{
"authorizationUrl": "https://login.microsoftonline.com/common/oauth2/authorize",
"scopes": [{"value": "https://security.microsoft.com/.default"}],
"type": "implicit",
}
]
2 changes: 1 addition & 1 deletion packages/typespec-python/test/unbranded/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ aiohttp;python_full_version>="3.5.2"
requests==2.32.2
pytest
pytest-asyncio==0.14.0;python_full_version>="3.5.2"
corehttp==1.0.0b3

# common test case
-e ./generated/authentication-api-key
Expand All @@ -21,6 +20,7 @@ corehttp==1.0.0b3
-e ./generated/server-versions-versioned
-e ./generated/server-versions-not-versioned
-e ./generated/special-words
-e ./generated/streaming-jsonl
-e ./generated/typetest-array
-e ./generated/typetest-dictionary
-e ./generated/typetest-enum-extensible
Expand Down
25 changes: 17 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading