Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1578a2c
update deprected api
msyyc Mar 18, 2025
81f931a
update
msyyc Mar 18, 2025
656cc26
add polling_interval fro lro test
msyyc Mar 18, 2025
c8cb143
update dependency
msyyc Mar 19, 2025
cac26e7
Merge branch 'main' of https://github.com/microsoft/typespec into pub…
msyyc Mar 19, 2025
7402e01
format
msyyc Mar 19, 2025
60a0244
skip failed test case
msyyc Mar 19, 2025
28942c4
update version
msyyc Mar 19, 2025
20a401d
replace some deprecated api
msyyc Mar 19, 2025
9ea40c3
fix
msyyc Mar 19, 2025
bb653a4
fix
msyyc Mar 19, 2025
a974879
Merge branch 'main' of https://github.com/microsoft/typespec into pub…
msyyc Mar 20, 2025
4753b3e
update
msyyc Mar 20, 2025
c6cd1db
update
msyyc Mar 20, 2025
c7848fd
fix test
msyyc Mar 20, 2025
f3e84e4
fix test
msyyc Mar 20, 2025
d0a76a3
add auth_flows
msyyc Mar 20, 2025
b7ca09e
Merge branch 'main' into python-auth-flow
msyyc Mar 26, 2025
9d51b9b
Remove unused import SdkApiVersionParameter
msyyc Mar 26, 2025
aced27c
Remove extra newline in code-model.ts
msyyc Mar 26, 2025
d7bdc14
Merge branch 'main' of https://github.com/microsoft/typespec into pyt…
Mar 26, 2025
f53f78f
pass auth_flows by keyword arg
Mar 26, 2025
030bedc
remove erroneous settings.json commit
Mar 26, 2025
ac66bd9
move azure flavor code into emitter
Mar 26, 2025
a76ef30
Merge branch 'main' into python-auth-flow
msyyc Mar 27, 2025
d19eb85
update format
msyyc Mar 27, 2025
0b77fcb
Merge branch 'main' of https://github.com/microsoft/typespec into pyt…
msyyc Mar 31, 2025
e1d26a8
add test case
msyyc Mar 31, 2025
9063462
update test
msyyc Mar 31, 2025
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
8 changes: 6 additions & 2 deletions packages/http-client-python/emitter/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function getType(
case "enumvalue":
return emitEnumMember(type, emitEnum(context, type.enumType));
case "credential":
return emitCredential(type);
return emitCredential(context, type);
case "bytes":
case "boolean":
case "plainDate":
Expand Down Expand Up @@ -143,7 +143,10 @@ function emitMultiPartFile(
});
}

function emitCredential(credential: SdkCredentialType): Record<string, any> {
function emitCredential(
context: PythonSdkContext,
credential: SdkCredentialType,
): Record<string, any> {
let credential_type: Record<string, any> = {};
const scheme = credential.scheme;
if (scheme.type === "oauth2") {
Expand All @@ -152,6 +155,7 @@ function emitCredential(credential: SdkCredentialType): Record<string, any> {
policy: {
type: "BearerTokenCredentialPolicy",
credentialScopes: [],
flows: (context.emitContext.options as any).flavor === "azure" ? [] : scheme.flows,
},
};
for (const flow of scheme.flows) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ def __init__(
yaml_data: Dict[str, Any],
code_model: "CodeModel",
credential_scopes: List[str],
flows: Optional[Dict[str, Any]] = None,
) -> None:
super().__init__(yaml_data, code_model)
self.credential_scopes = credential_scopes
self.flows = flows

def call(self, async_mode: bool) -> str:
policy_name = f"{'Async' if async_mode else ''}BearerTokenCredentialPolicy"
return f"policies.{policy_name}(self.credential, *self.credential_scopes, **kwargs)"
auth_flows = f"auth_flows={self.flows}, " if self.flows else ""
return f"policies.{policy_name}(self.credential, *self.credential_scopes, {auth_flows}**kwargs)"

@classmethod
def from_yaml(cls, yaml_data: Dict[str, Any], code_model: "CodeModel") -> "BearerTokenCredentialPolicyType":
return cls(yaml_data, code_model, yaml_data["credentialScopes"])
return cls(yaml_data, code_model, yaml_data["credentialScopes"], yaml_data.get("flows"))


class ARMChallengeAuthenticationPolicyType(BearerTokenCredentialPolicyType):
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
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,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",
}
]
Loading