-
Notifications
You must be signed in to change notification settings - Fork 3.2k
identity_vscode_credential #10840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
identity_vscode_credential #10840
Changes from 8 commits
Commits
Show all changes
82 commits
Select commit
Hold shift + click to select a range
0002805
identity_win_vscode_credential
xiangyan99 b1e79de
remove used import
xiangyan99 13fd676
update
xiangyan99 d356ba6
update
xiangyan99 41b0394
update
xiangyan99 526470d
update
xiangyan99 dc671f2
update
xiangyan99 5a47894
update shared requirements
xiangyan99 c797482
add async credential and tests
xiangyan99 25de404
update dev_requirements
xiangyan99 f48fdd4
add try in import
xiangyan99 a77e336
not raise
xiangyan99 9f5ad5d
update
xiangyan99 d155f80
mac os support
xiangyan99 015f1f7
update
xiangyan99 bfa2ea1
update msal version
xiangyan99 16873bd
roll back msal change
xiangyan99 58844b9
remove dependency on pywin32
xiangyan99 05694a9
update
xiangyan99 671d5ea
update
xiangyan99 921b7a2
update
xiangyan99 3911b88
add tests
xiangyan99 f313ba4
add pygobject dependency
xiangyan99 283a055
linux support
xiangyan99 9974361
update
xiangyan99 4e7b9c4
updates
xiangyan99 bbe7a20
updates
xiangyan99 94b152b
pylint fix
xiangyan99 657617c
updates
xiangyan99 426dea7
updates
xiangyan99 f9a2abf
updates
xiangyan99 593a62f
update tests
xiangyan99 0805e74
format
xiangyan99 eaf10cb
add type checking
xiangyan99 e1dab12
refactor code
xiangyan99 9c33cfd
remove unused import
xiangyan99 d072e20
updates
xiangyan99 6024a9a
remove pygobject dependency
xiangyan99 a63e157
updates
xiangyan99 a8d45c0
typo
xiangyan99 2fca38a
update tests
xiangyan99 3866a93
update mac tests
xiangyan99 ebb5df2
add linux tests
xiangyan99 595cdea
updates
xiangyan99 246ab2d
updates
xiangyan99 af482e6
clean up
xiangyan99 57953ba
mock patch not work well on async 3.8+
xiangyan99 022f376
refactor code
xiangyan99 7e15ddd
pylint
xiangyan99 975f6ed
pylint
xiangyan99 767906f
add tests for win apis
xiangyan99 1e10649
use __module__
xiangyan99 cf54345
update
xiangyan99 e43a74f
update
xiangyan99 4fe1be3
add tests
xiangyan99 0fd89e6
typo
xiangyan99 5524ea9
update
xiangyan99 ff9d532
update
xiangyan99 e6b83ca
update
xiangyan99 9232a3b
Update sdk/identity/azure-identity/tests/test_vscode_credential.py
xiangyan99 9ea845e
updates
xiangyan99 77f8838
update
xiangyan99 0f69230
update
xiangyan99 ec1f0a3
update
xiangyan99 4fadaf3
update
xiangyan99 435fdce
update
xiangyan99 cb74ce2
updates
xiangyan99 a402f3b
update
xiangyan99 853dd3e
update
xiangyan99 54d1b48
update
xiangyan99 75624b8
disable on 2.7
xiangyan99 ed1db0c
raise on Python 2.7 Linux
xiangyan99 6fc8708
fix typo
xiangyan99 7d36ad5
update
xiangyan99 6f8ae61
update
xiangyan99 42270b8
check error
xiangyan99 5c8bcec
update
xiangyan99 77cc4d8
add vs code credential to default
xiangyan99 10a9e43
Merge branch 'master' into identity_vcode_credential
xiangyan99 ff8e1c7
update
xiangyan99 8efee5c
update
xiangyan99 a5dc253
updates
xiangyan99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
sdk/identity/azure-identity/azure/identity/_credentials/win_vscode_credential.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| import os | ||
| import json | ||
| from azure.core.exceptions import ClientAuthenticationError | ||
| from .._constants import ( | ||
| VSCODE_CREDENTIALS_SECTION, | ||
| AZURE_VSCODE_CLIENT_ID, | ||
| AZURE_VSCODE_TENANT_ID, | ||
| ) | ||
| from .._internal.aad_client import AadClient | ||
| try: | ||
| from win32cred import CredRead | ||
| except ImportError: # pylint:disable=try-except-raise | ||
| raise | ||
|
|
||
| def _read_credential(service_name, account_name): | ||
| target = u"{}/{}".format(service_name, account_name) | ||
| res = CredRead(TargetName=target, Type=0x1) | ||
| cred = res["CredentialBlob"].decode('utf-16') | ||
| return cred | ||
|
|
||
| def _get_user_settings_path(): | ||
| app_data_folder = os.environ['APPDATA'] | ||
| return os.path.join(app_data_folder, "Code", "User", "settings.json") | ||
|
|
||
| def _get_user_settings(): | ||
| path = _get_user_settings_path() | ||
| try: | ||
| with open(path) as file: | ||
| data = json.load(file) | ||
| environment_name = data.get("azure.cloud", "Azure") | ||
| return environment_name | ||
| except IOError: | ||
| return "Azure" | ||
|
|
||
| class WinVSCodeCredential(object): | ||
| """Authenticates by redeeming a refresh token previously saved by VS Code | ||
| :keyword str tenant_id: ID of the application's Azure Active Directory tenant. Also called its 'directory' ID. | ||
| :keyword str client_id: the application's client ID | ||
| """ | ||
| def __init__(self, **kwargs): | ||
| client_id = kwargs.pop("client_id", AZURE_VSCODE_CLIENT_ID) | ||
xiangyan99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tenant_id = kwargs.pop("tenant_id", AZURE_VSCODE_TENANT_ID) | ||
| self._client = AadClient(tenant_id, client_id, **kwargs) | ||
|
|
||
| def get_token(self, *scopes, **kwargs): | ||
| # type: (*str, **Any) -> AccessToken | ||
| """Request an access token for `scopes`. | ||
| .. note:: This method is called by Azure SDK clients. It isn't intended for use in application code. | ||
| When this method is called, the credential will try to get the refresh token saved by VS Code. If a refresh | ||
| token can be found, it will redeem the refresh token for an access token and return the access token. | ||
| :param str scopes: desired scopes for the access token. This method requires at least one scope. | ||
| :rtype: :class:`azure.core.credentials.AccessToken` | ||
| :raises ~azure.core.exceptions.ClientAuthenticationError: authentication failed. The error's ``message`` | ||
| attribute gives a reason. Any error response from Azure Active Directory is available as the error's | ||
| ``response`` attribute. | ||
| """ | ||
| environment_name = _get_user_settings() | ||
xiangyan99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| refresh_token = _read_credential(VSCODE_CREDENTIALS_SECTION, environment_name) | ||
| if not refresh_token: | ||
| raise ClientAuthenticationError( | ||
xiangyan99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| message="No token available." | ||
| ) | ||
| token = self._client.obtain_token_by_refresh_token(refresh_token, scopes, **kwargs) | ||
| return token | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
sdk/identity/azure-identity/tests/test_win_vscode_credential.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # ------------------------------------ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
| # ------------------------------------ | ||
| import sys | ||
| import pytest | ||
| if sys.platform.startswith('win'): | ||
| from azure.identity._credentials.win_vscode_credential import _read_credential | ||
| import win32cred | ||
|
|
||
| @pytest.mark.skipif(not sys.platform.startswith('win'), reason="This test only runs on Windows") | ||
| def test_win_vscode_credential(): | ||
| service_name = u"VS Code Azure" | ||
xiangyan99 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| account_name = u"Azure" | ||
| target = u"{}/{}".format(service_name, account_name) | ||
| comment = u"comment" | ||
| token_written = u"test_refresh_token" | ||
| user_name = u"Azure" | ||
| credential = {"Type": 0x1, | ||
| "TargetName": target, | ||
| "UserName": user_name, | ||
| "CredentialBlob": token_written, | ||
| "Comment": comment, | ||
| "Persist": 0x2} | ||
| win32cred.CredWrite(credential) | ||
| token_read = _read_credential(service_name, account_name) | ||
chlowell marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| assert token_read == token_written | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.